[关闭]
@coldxiangyu 2018-08-23T03:29:05.000000Z 字数 13268 阅读 640

Spring boot集成hazelcast

Spring boot


Hazelcast介绍

参考[https://blog.csdn.net/educast/article/details/78315656 ]

application.properties

  1. spring.hazelcast.config=classpath:config/hazelcast.xml

config/hazelcast.xml

  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <!--
  3. ~ Copyright (c) 2008-2016, Hazelcast, Inc. All Rights Reserved.
  4. ~
  5. ~ Licensed under the Apache License, Version 2.0 (the "License");
  6. ~ you may not use this file except in compliance with the License.
  7. ~ You may obtain a copy of the License at
  8. ~
  9. ~ http://www.apache.org/licenses/LICENSE-2.0
  10. ~
  11. ~ Unless required by applicable law or agreed to in writing, software
  12. ~ distributed under the License is distributed on an "AS IS" BASIS,
  13. ~ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  14. ~ See the License for the specific language governing permissions and
  15. ~ limitations under the License.
  16. -->
  17. <!--
  18. The default Hazelcast configuration. This is used when no hazelcast.xml is present.
  19. Please see the schema for how to configure Hazelcast at https://hazelcast.com/schema/config/hazelcast-config-3.7.xsd
  20. or the documentation at https://hazelcast.org/documentation/
  21. -->
  22. <hazelcast xsi:schemaLocation="http://www.hazelcast.com/schema/config hazelcast-config-3.7.xsd"
  23. xmlns="http://www.hazelcast.com/schema/config"
  24. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
  25. <group>
  26. <name>Coldxiangyu</name>
  27. <password>passwd</password>
  28. </group>
  29. <network>
  30. <port auto-increment="true" port-count="100">5701</port>
  31. <outbound-ports>
  32. <!--
  33. Allowed port range when connecting to other nodes.
  34. 0 or * means use system provided port.
  35. -->
  36. <ports>0</ports>
  37. </outbound-ports>
  38. <join>
  39. <tcp-ip enabled="false">
  40. <interface>127.0.0.1</interface>
  41. <member-list>
  42. <member>127.0.0.1</member>
  43. </member-list>
  44. </tcp-ip>
  45. </join>
  46. <ssl enabled="false"/>
  47. <socket-interceptor enabled="false"/>
  48. <symmetric-encryption enabled="false">
  49. <!--
  50. encryption algorithm such as
  51. DES/ECB/PKCS5Padding,
  52. PBEWithMD5AndDES,
  53. AES/CBC/PKCS5Padding,
  54. Blowfish,
  55. DESede
  56. -->
  57. <algorithm>PBEWithMD5AndDES</algorithm>
  58. <!-- salt value to use when generating the secret key -->
  59. <salt>thesalt</salt>
  60. <!-- pass phrase to use when generating the secret key -->
  61. <password>thepass</password>
  62. <!-- iteration count to use when generating the secret key -->
  63. <iteration-count>19</iteration-count>
  64. </symmetric-encryption>
  65. </network>
  66. <partition-group enabled="false"/>
  67. <executor-service name="default">
  68. <pool-size>16</pool-size>
  69. <!--Queue capacity. 0 means Integer.MAX_VALUE.-->
  70. <queue-capacity>0</queue-capacity>
  71. </executor-service>
  72. <queue name="default">
  73. <!--
  74. Maximum size of the queue. When a JVM's local queue size reaches the maximum,
  75. all put/offer operations will get blocked until the queue size
  76. of the JVM goes down below the maximum.
  77. Any integer between 0 and Integer.MAX_VALUE. 0 means
  78. Integer.MAX_VALUE. Default is 0.
  79. -->
  80. <max-size>0</max-size>
  81. <!--
  82. Number of backups. If 1 is set as the backup-count for example,
  83. then all entries of the map will be copied to another JVM for
  84. fail-safety. 0 means no backup.
  85. -->
  86. <backup-count>1</backup-count>
  87. <!--
  88. Number of async backups. 0 means no backup.
  89. -->
  90. <async-backup-count>0</async-backup-count>
  91. <empty-queue-ttl>-1</empty-queue-ttl>
  92. </queue>
  93. <map name="default">
  94. <!--
  95. Data type that will be used for storing recordMap.
  96. Possible values:
  97. BINARY (default): keys and values will be stored as binary data
  98. OBJECT : values will be stored in their object forms
  99. NATIVE : values will be stored in non-heap region of JVM
  100. -->
  101. <in-memory-format>BINARY</in-memory-format>
  102. <!--
  103. Number of backups. If 1 is set as the backup-count for example,
  104. then all entries of the map will be copied to another JVM for
  105. fail-safety. 0 means no backup.
  106. -->
  107. <backup-count>1</backup-count>
  108. <!--
  109. Number of async backups. 0 means no backup.
  110. -->
  111. <async-backup-count>0</async-backup-count>
  112. <!--
  113. Maximum number of seconds for each entry to stay in the map. Entries that are
  114. older than <time-to-live-seconds> and not updated for <time-to-live-seconds>
  115. will get automatically evicted from the map.
  116. Any integer between 0 and Integer.MAX_VALUE. 0 means infinite. Default is 0.
  117. -->
  118. <time-to-live-seconds>0</time-to-live-seconds>
  119. <!--
  120. Maximum number of seconds for each entry to stay idle in the map. Entries that are
  121. idle(not touched) for more than <max-idle-seconds> will get
  122. automatically evicted from the map. Entry is touched if get, put or containsKey is called.
  123. Any integer between 0 and Integer.MAX_VALUE. 0 means infinite. Default is 0.
  124. -->
  125. <max-idle-seconds>0</max-idle-seconds>
  126. <!--
  127. Valid values are:
  128. NONE (no eviction),
  129. LRU (Least Recently Used),
  130. LFU (Least Frequently Used).
  131. NONE is the default.
  132. -->
  133. <eviction-policy>NONE</eviction-policy>
  134. <!--
  135. Maximum size of the map. When max size is reached,
  136. map is evicted based on the policy defined.
  137. Any integer between 0 and Integer.MAX_VALUE. 0 means
  138. Integer.MAX_VALUE. Default is 0.
  139. -->
  140. <max-size policy="PER_NODE">0</max-size>
  141. <!--
  142. `eviction-percentage` property is deprecated and will be ignored when it is set.
  143. As of version 3.7, eviction mechanism changed.
  144. It uses a probabilistic algorithm based on sampling. Please see documentation for further details
  145. -->
  146. <eviction-percentage>25</eviction-percentage>
  147. <!--
  148. `min-eviction-check-millis` property is deprecated and will be ignored when it is set.
  149. As of version 3.7, eviction mechanism changed.
  150. It uses a probabilistic algorithm based on sampling. Please see documentation for further details
  151. -->
  152. <min-eviction-check-millis>100</min-eviction-check-millis>
  153. <!--
  154. While recovering from split-brain (network partitioning),
  155. map entries in the small cluster will merge into the bigger cluster
  156. based on the policy set here. When an entry merge into the
  157. cluster, there might an existing entry with the same key already.
  158. Values of these entries might be different for that same key.
  159. Which value should be set for the key? Conflict is resolved by
  160. the policy set here. Default policy is PutIfAbsentMapMergePolicy
  161. There are built-in merge policies such as
  162. com.hazelcast.map.merge.PassThroughMergePolicy; entry will be overwritten if merging entry exists for the key.
  163. com.hazelcast.map.merge.PutIfAbsentMapMergePolicy ; entry will be added if the merging entry doesn't exist in the cluster.
  164. com.hazelcast.map.merge.HigherHitsMapMergePolicy ; entry with the higher hits wins.
  165. com.hazelcast.map.merge.LatestUpdateMapMergePolicy ; entry with the latest update wins.
  166. -->
  167. <merge-policy>com.hazelcast.map.merge.PutIfAbsentMapMergePolicy</merge-policy>
  168. <!--
  169. Control caching of de-serialized values. Caching makes query evaluation faster, but it cost memory.
  170. Possible Values:
  171. NEVER: Never cache deserialized object
  172. INDEX-ONLY: Caches values only when they are inserted into an index.
  173. ALWAYS: Always cache deserialized values.
  174. -->
  175. <cache-deserialized-values>INDEX-ONLY</cache-deserialized-values>
  176. </map>
  177. <map name="hazlcast_online_cache">
  178. <in-memory-format>BINARY</in-memory-format>
  179. <backup-count>1</backup-count>
  180. <async-backup-count>0</async-backup-count>
  181. <!-- 最大过期时间,30分钟 -->
  182. <time-to-live-seconds>1800</time-to-live-seconds>
  183. <!-- 最长空闲时间,10分钟 -->
  184. <max-idle-seconds>600</max-idle-seconds>
  185. <eviction-policy>LRU</eviction-policy>
  186. <max-size policy="PER_NODE">100000</max-size>
  187. <eviction-percentage>25</eviction-percentage>
  188. <min-eviction-check-millis>100</min-eviction-check-millis>
  189. <merge-policy>com.hazelcast.map.merge.PutIfAbsentMapMergePolicy</merge-policy>
  190. <cache-deserialized-values>INDEX-ONLY</cache-deserialized-values>
  191. </map>
  192. <map name="hazlcast_callout_cache">
  193. <in-memory-format>BINARY</in-memory-format>
  194. <backup-count>1</backup-count>
  195. <async-backup-count>0</async-backup-count>
  196. <!-- 最大过期时间,30分钟 -->
  197. <time-to-live-seconds>1800</time-to-live-seconds>
  198. <!-- 最长空闲时间,10分钟 -->
  199. <max-idle-seconds>600</max-idle-seconds>
  200. <eviction-policy>LRU</eviction-policy>
  201. <max-size policy="PER_NODE">100000</max-size>
  202. <eviction-percentage>25</eviction-percentage>
  203. <min-eviction-check-millis>100</min-eviction-check-millis>
  204. <merge-policy>com.hazelcast.map.merge.PutIfAbsentMapMergePolicy</merge-policy>
  205. <cache-deserialized-values>INDEX-ONLY</cache-deserialized-values>
  206. </map>
  207. <map name="api_user_cache">
  208. <in-memory-format>BINARY</in-memory-format>
  209. <backup-count>1</backup-count>
  210. <async-backup-count>0</async-backup-count>
  211. <!-- 最大过期时间,7天 -->
  212. <time-to-live-seconds>604800</time-to-live-seconds>
  213. <!-- 最长空闲时间,8小时 -->
  214. <max-idle-seconds>28800</max-idle-seconds>
  215. <eviction-policy>NONE</eviction-policy>
  216. <max-size policy="PER_NODE">0</max-size>
  217. <eviction-percentage>25</eviction-percentage>
  218. <min-eviction-check-millis>100</min-eviction-check-millis>
  219. <merge-policy>com.hazelcast.map.merge.PutIfAbsentMapMergePolicy</merge-policy>
  220. <cache-deserialized-values>INDEX-ONLY</cache-deserialized-values>
  221. </map>
  222. <map name="callcenter_current_call">
  223. <in-memory-format>BINARY</in-memory-format>
  224. <backup-count>1</backup-count>
  225. <async-backup-count>0</async-backup-count>
  226. <!-- 最大过期时间,7天 -->
  227. <time-to-live-seconds>14400</time-to-live-seconds>
  228. <!-- 最长空闲时间,8小时 -->
  229. <max-idle-seconds>7200</max-idle-seconds>
  230. <eviction-policy>NONE</eviction-policy>
  231. <max-size policy="PER_NODE">0</max-size>
  232. <eviction-percentage>25</eviction-percentage>
  233. <min-eviction-check-millis>100</min-eviction-check-millis>
  234. <merge-policy>com.hazelcast.map.merge.PutIfAbsentMapMergePolicy</merge-policy>
  235. <cache-deserialized-values>INDEX-ONLY</cache-deserialized-values>
  236. </map>
  237. <multimap name="default">
  238. <backup-count>1</backup-count>
  239. <value-collection-type>SET</value-collection-type>
  240. </multimap>
  241. <list name="default">
  242. <backup-count>1</backup-count>
  243. </list>
  244. <set name="default">
  245. <backup-count>1</backup-count>
  246. </set>
  247. <jobtracker name="default">
  248. <max-thread-size>10000</max-thread-size>
  249. <!-- Queue size 0 means number of partitions * 2 -->
  250. <queue-size>10000</queue-size>
  251. <retry-count>0</retry-count>
  252. <chunk-size>1000</chunk-size>
  253. <communicate-stats>true</communicate-stats>
  254. <topology-changed-strategy>CANCEL_RUNNING_OPERATION</topology-changed-strategy>
  255. </jobtracker>
  256. <semaphore name="default">
  257. <initial-permits>0</initial-permits>
  258. <backup-count>1</backup-count>
  259. <async-backup-count>0</async-backup-count>
  260. </semaphore>
  261. <reliable-topic name="default">
  262. <read-batch-size>10</read-batch-size>
  263. <topic-overload-policy>BLOCK</topic-overload-policy>
  264. <statistics-enabled>true</statistics-enabled>
  265. </reliable-topic>
  266. <ringbuffer name="default">
  267. <capacity>10000</capacity>
  268. <backup-count>1</backup-count>
  269. <async-backup-count>0</async-backup-count>
  270. <time-to-live-seconds>30</time-to-live-seconds>
  271. <in-memory-format>BINARY</in-memory-format>
  272. </ringbuffer>
  273. <serialization>
  274. <portable-version>0</portable-version>
  275. </serialization>
  276. <services enable-defaults="true"/>
  277. <lite-member enabled="false"/>
  278. </hazelcast>

CachBean.java

  1. public interface CacheBean {
  2. /**
  3. *
  4. */
  5. public void put(String key , Object value , String orgi) ;
  6. /**
  7. *
  8. */
  9. public void clear(String orgi);
  10. public Object delete(String key , String orgi) ;
  11. public void update(String key , String orgi , Object object) ;
  12. /**
  13. *
  14. * @param key
  15. * @param orgi
  16. * @return
  17. */
  18. public Object getCacheObject(String key, String orgi) ;
  19. /**
  20. *
  21. * @param key
  22. * @param orgi
  23. * @return
  24. */
  25. public Object getCacheObject(String key, String orgi,Object defaultValue) ;
  26. /**
  27. * 获取所有缓存对象
  28. * @param orgi
  29. * @return
  30. */
  31. public Collection<?> getAllCacheObject(String orgi) ;
  32. public CacheBean getCacheInstance(String cacheName);
  33. public Object getCache();
  34. public JsonObject getStatics();
  35. public Lock getLock(String lock, String orgi);
  36. public long getSize();
  37. public long getAtomicLong(String cacheName) ;
  38. public void setAtomicLong(String cacheName , long start) ;
  39. }

CachInstance.java

  1. public interface CacheInstance {
  2. /**
  3. * 系统缓存
  4. * @return
  5. */
  6. public CacheBean getSystemCacheBean();
  7. }

CacheHelper.java

  1. public class CacheHelper {
  2. private static CacheHelper instance = new CacheHelper();
  3. /**
  4. * 获取缓存实例
  5. */
  6. public static CacheHelper getInstance(){
  7. return instance ;
  8. }
  9. private static CacheInstance cacheInstance = new HazlcastCacheHelper();
  10. public static CacheBean getSystemCacheBean() {
  11. return cacheInstance!=null ? cacheInstance.getSystemCacheBean() : null ;
  12. }
  13. }

HazlcastCacheHelper.java

  1. /**
  2. * Hazlcast缓存处理实例类
  3. * @author coldxiangyu
  4. *
  5. */
  6. public class HazlcastCacheHelper implements CacheInstance{
  7. /**
  8. * 服务类型枚举
  9. * @author admin
  10. *
  11. */
  12. public enum CacheServiceEnum{
  13. HAZLCAST_CULUSTER_SYSTEM;
  14. public String toString(){
  15. return super.toString().toLowerCase();
  16. }
  17. }
  18. @Override
  19. public CacheBean getSystemCacheBean() {
  20. return UKDataContext.getContext().getBean(SystemCache.class).getCacheInstance(CacheServiceEnum.HAZLCAST_CULUSTER_SYSTEM.toString()) ;
  21. }
  22. }

SystemCache.java

  1. @Service("system_cache")
  2. public class SystemCache implements CacheBean{
  3. @Autowired
  4. public HazelcastInstance hazelcastInstance;
  5. private String cacheName ;
  6. public HazelcastInstance getInstance(){
  7. return hazelcastInstance ;
  8. }
  9. public CacheBean getCacheInstance(String cacheName){
  10. this.cacheName = cacheName ;
  11. return this ;
  12. }
  13. @Override
  14. public void put(String key, Object value, String orgi) {
  15. getInstance().getMap(getName()).put(key, value) ;
  16. }
  17. @Override
  18. public void clear(String orgi) {
  19. getInstance().getMap(getName()).clear();
  20. }
  21. @Override
  22. public Object delete(String key, String orgi) {
  23. return getInstance().getMap(getName()).remove(key) ;
  24. }
  25. @Override
  26. public void update(String key, String orgi, Object value) {
  27. getInstance().getMap(getName()).put(key, value);
  28. }
  29. @Override
  30. public Object getCacheObject(String key, String orgi) {
  31. return getInstance().getMap(getName()).get(key);
  32. }
  33. public String getName() {
  34. return cacheName ;
  35. }
  36. // @Override
  37. public void service() throws Exception {
  38. // TODO Auto-generated method stub
  39. }
  40. @Override
  41. public Collection<?> getAllCacheObject(String orgi) {
  42. return getInstance().getMap(getName()).keySet();
  43. }
  44. @Override
  45. public Object getCacheObject(String key, String orgi, Object defaultValue) {
  46. return getCacheObject(key, orgi);
  47. }
  48. @Override
  49. public Object getCache() {
  50. return getInstance().getMap(cacheName);
  51. }
  52. @Override
  53. public Lock getLock(String lock , String orgi) {
  54. // TODO Auto-generated method stub
  55. return getInstance().getLock(lock);
  56. }
  57. @Override
  58. public long getSize() {
  59. return getInstance().getMap(getName()).size();
  60. }
  61. @Override
  62. public long getAtomicLong(String cacheName) {
  63. return getInstance().getAtomicLong(getName()).incrementAndGet();
  64. }
  65. @Override
  66. public void setAtomicLong(String cacheName, long start) {
  67. getInstance().getAtomicLong(getName()).set(start);
  68. }
  69. @Override
  70. public JsonObject getStatics() {
  71. // TODO Auto-generated method stub
  72. return getInstance().getMap(getName()).getLocalMapStats().toJson();
  73. }
  74. }

调用方法:

  1. /**
  2. * 获取系统地区配置
  3. * @return
  4. */
  5. public static void initSystemArea(){
  6. CacheHelper.getSystemCacheBean().delete("system", "test") ;
  7. AreaTypeRepository areaTypeRes = UKDataContext.getContext().getBean(AreaTypeRepository.class) ;
  8. CacheHelper.getSystemCacheBean().put("system", areaTypeRes.findAll(), "test");
  9. }
添加新批注
在作者公开此批注前,只有你和作者可见。
回复批注