Hazelcast是一个内存分布式计算平台,用于管理数据并并行执行执行应用程序。
1. 它是用Java编写的。
2. 与其他一些内存数据库(如redis)不同,Hazelcast是多线程的,这意味着可从所有可用的CPU内核中受益。
3. 与其他内存数据网格不同 - 它设计用于分布式环境。它支持每个群集无限数量的map和缓存。
根据基准测试,Hazelcast在获取数据方面比Redis快56%,在设置数据方面比Redis快44%。
Hazelcast是一个高度可扩展的数据分发和集群平台。特性包括:
提供java.util.{Queue, Set, List, Map}分布式实现。
提供java.util.concurrency.locks.Lock分布式实现。
提供java.util.concurrent.ExecutorService分布式实现。
提供用于一对多关系的分布式MultiMap。
提供用于发布/订阅的分布式Topic(主题)。
通过JCA与J2EE容器集成和事务支持。
提供用于安全集群的Socket层加密。
支持同步和异步持久化。
通过JMX监控和管理集群。
支持动态HTTP Session集群。
利用备份实现动态分割。
支持动态故障恢复。
我们来看一下如何使用Hazelcast
pom
com.hazelcasthazelcast-all3.10.1
在SpringBoot主程序开启缓存@EnableCaching
@EnableCaching@EnableFeignClients@EnableDiscoveryClient@EnableTransactionManagement@SpringBootApplicationpublic classProductproviderApplication {
public static voidmain(String[] args) {
ApplicationContext app = SpringApplication.run(ProductproviderApplication.class,args);SpringBootUtil.setApplicationContext(app);}
}
配置类
@Configurationpublic classHazelcastConfiguration {
@BeanpublicConfig hazelCastConfig(){
Config config = newConfig();config.setInstanceName("hazelcast-instance").addMapConfig(
newMapConfig().setName("configuration").setMaxSizeConfig(newMaxSizeConfig(200,MaxSizeConfig.MaxSizePolicy.FREE_HEAP_SIZE)).setEvictionPolicy(EvictionPolicy.LFU)
.setTimeToLiveSeconds(-1));returnconfig;}
}
还是以昨天的汽车配件分类实体类来说明,此处注意一定要实现序列化接口Serializable
@AllArgsConstructor@NoArgsConstructorpublic classProviderProductLevel implementsProvider,Serializable {
@Getter@SetterprivateLong id;@Getter@SetterprivateString code;@Getter@SetterprivateString name;@Getter@SetterprivateInteger sort;@Getter@SetterprivateInteger level;@Getter@SetterprivateProductType productType;@Getter@SetterprivateString pictureUrl;@Getter@SetterprivateList otherProperties;@GetterprivateList productListProviders= newCopyOnWriteArrayList<>();
publicProviderProductLevel(Long id,String code,String name,Integer sort,ProductType productType) {
this.id= id;this.code= code;this.name= name;this.sort= sort;this.level= 1;this.productType= productType;}
publicProviderProductLevel(Long id,String code,String name,Integer sort,String pictureUrl) {
this.id= id;this.code= code;this.name= name;this.sort= sort;this.level= 2;this.pictureUrl= pictureUrl;}
@Overridepublic booleanaddProvider(Provider provider) {
LevelDao levelDao = SpringBootUtil.getBean(LevelDao.class);if(provider instanceofProviderProductLevel) {
levelDao.addLevelToLevel(newParamLevel(this.id,((ProviderProductLevel)provider).getId()));}
return this.productListProviders.add(provider);}
}
写一个Controller,其中建立分布式缓存Map,不相关的内容不做解释,只说明分布式缓存内容
@Slf4j@RestControllerpublic classLevelController {
//创建分布式缓存节点privateHazelcastInstance instance= Hazelcast.newHazelcastInstance();@AutowiredprivateLevelDao levelDao;//一级分类缓存// private Map cachelevelMap1 = new ConcurrentHashMap<>();privateMap cachelevelMap1= instance.getMap("level1");
/*** 保存一级汽车配件分类*@paramlevel*@return*/@ProductAnnotation@SuppressWarnings("unchecked")
@Transactional@PostMapping("/productprovider-anon/savelevel1")
publicResult saveLevel1(@RequestBodyProviderProductLevel level) {
ProviderFactory.createProviderProductLevel1(level,true);if(!cachelevelMap1.containsKey(level.getId())) {
cachelevelMap1.put(level.getId(),level);}
returnResult.success("保存成功");}
/*** 保存二级汽车配件分类,并将二级产品分类添加到一级产品分类的子分类*@paramid*@paramlevel*@return*/@SuppressWarnings("unchecked")
@Transactional@PostMapping("/productprovider-anon/savelevel2")
publicResult saveLevel2(@RequestParam("id") Long id,@RequestBodyProviderProductLevel level) {
ProviderFactory.createProviderProductLevel2(level,true);if(cachelevelMap1.containsKey(id)) {
//此处必须获取一级分类对象,再添加二级分类,然后重新放入缓存中//直接使用cachelevelMap1.get(id).addProvider(level),将无法将//level添加到CopyOnRightArrayList中Provider level1 = cachelevelMap1.get(id);level1.addProvider(level);cachelevelMap1.put(id,level1);log.info("缓存获取"+ JSONObject.toJSONString(cachelevelMap1.get(id)));}else{
Provider level1 = levelDao.findLevel1(id);log.info(JSONObject.toJSONString(level1));level1.addProvider(level);cachelevelMap1.put(id,level1);}
returnResult.success("保存成功");}
@GetMapping("/productprovider-anon/getprovider")
publicProvider getProvider(@RequestParam("id") Long id) {
returncachelevelMap1.get(id);}
}
我们将端口号设为8106,并启动该端口的项目,我们可以看到这样一些日志
2019-07-10 10:19:38.381 INFO [productprovider,,,] 10216 --- [ main] com.hazelcast.instance.AddressPicker : [LOCAL] [dev] [3.10.1] Prefer IPv4 stack is true.
2019-07-10 10:19:38.764 INFO [productprovider,,,] 10216 --- [ main] com.hazelcast.instance.AddressPicker : [LOCAL] [dev] [3.10.1] Picked [192.168.192.15]:5702, using socket ServerSocket[addr=/0:0:0:0:0:0:0:0,localport=5702], bind any local is true
2019-07-10 10:19:38.765 INFO [productprovider,,,] 10216 --- [ main] com.hazelcast.system : [192.168.192.15]:5702 [dev] [3.10.1] Hazelcast 3.10.1 (20180521 - 66f881d) starting at [192.168.192.15]:5702
2019-07-10 10:19:38.765 INFO [productprovider,,,] 10216 --- [ main] com.hazelcast.system : [192.168.192.15]:5702 [dev] [3.10.1] Copyright (c) 2008-2018, Hazelcast, Inc. All Rights Reserved.
2019-07-10 10:19:38.766 INFO [productprovider,,,] 10216 --- [ main] com.hazelcast.system : [192.168.192.15]:5702 [dev] [3.10.1] Configured Hazelcast Serialization version: 1
2019-07-10 10:19:38.766 INFO [productprovider,,,] 10216 --- [ main] com.hazelcast.instance.Node : [192.168.192.15]:5702 [dev] [3.10.1] A non-empty group password is configured for the Hazelcast member. Starting with Hazelcast version 3.8.2, members with the same group name, but with different group passwords (that do not use authentication) form a cluster. The group password configuration will be removed completely in a future release.
2019-07-10 10:19:38.770 INFO [productprovider,,,] 10216 --- [ main] c.h.s.i.o.impl.BackpressureRegulator : [192.168.192.15]:5702 [dev] [3.10.1] Backpressure is disabled
2019-07-10 10:19:38.770 INFO [productprovider,,,] 10216 --- [ main] h.s.i.o.i.InboundResponseHandlerSupplier : [192.168.192.15]:5702 [dev] [3.10.1] Running with 2 response threads
2019-07-10 10:19:39.422 INFO [productprovider,,,] 10216 --- [ main] com.hazelcast.instance.Node : [192.168.192.15]:5702 [dev] [3.10.1] Creating MulticastJoiner
2019-07-10 10:19:39.428 INFO [productprovider,,,] 10216 --- [ main] c.h.s.i.o.impl.OperationExecutorImpl : [192.168.192.15]:5702 [dev] [3.10.1] Starting 8 partition threads and 5 generic threads (1 dedicated for priority tasks)
2019-07-10 10:19:39.430 INFO [productprovider,,,] 10216 --- [ main] c.h.internal.diagnostics.Diagnostics : [192.168.192.15]:5702 [dev] [3.10.1] Diagnostics disabled. To enable add -Dhazelcast.diagnostics.enabled=true to the JVM arguments.
2019-07-10 10:19:39.430 INFO [productprovider,,,] 10216 --- [ main] com.hazelcast.core.LifecycleService : [192.168.192.15]:5702 [dev] [3.10.1] [192.168.192.15]:5702 is STARTING
2019-07-10 10:19:39.538 INFO [productprovider,,,] 10216 --- [ main] c.h.i.cluster.impl.MulticastJoiner : [192.168.192.15]:5702 [dev] [3.10.1] Trying to join to discovered node: [192.168.192.15]:5701
2019-07-10 10:19:39.549 INFO [productprovider,,,] 10216 --- [cached.thread-3] com.hazelcast.nio.tcp.TcpIpConnector : [192.168.192.15]:5702 [dev] [3.10.1] Connecting to /192.168.192.15:5701, timeout: 0, bind-any: true
2019-07-10 10:19:39.558 INFO [productprovider,,,] 10216 --- [thread-Acceptor] com.hazelcast.nio.tcp.TcpIpAcceptor : [192.168.192.15]:5701 [dev] [3.10.1] Accepting socket connection from /192.168.192.15:4508
2019-07-10 10:19:39.561 INFO [productprovider,,,] 10216 --- [thread-Acceptor] c.h.nio.tcp.TcpIpConnectionManager : [192.168.192.15]:5701 [dev] [3.10.1] Established socket connection between /192.168.192.15:5701 and /192.168.192.15:4508
2019-07-10 10:19:39.561 INFO [productprovider,,,] 10216 --- [cached.thread-3] c.h.nio.tcp.TcpIpConnectionManager : [192.168.192.15]:5702 [dev] [3.10.1] Established socket connection between /192.168.192.15:4508 and /192.168.192.15:5701
2019-07-10 10:19:46.571 INFO [productprovider,,,] 10216 --- [ration.thread-0] c.h.internal.cluster.ClusterService : [192.168.192.15]:5701 [dev] [3.10.1]
Members {size:2, ver:2} [
Member [192.168.192.15]:5701 - 34da2d53-d8d6-4959-aaf2-bd6cda24f0fa this
Member [192.168.192.15]:5702 - 70b959c7-ebb5-4226-b343-76e7354cbd94
]
2019-07-10 10:19:46.584 INFO [productprovider,,,] 10216 --- [ration.thread-0] com.hazelcast.system : [192.168.192.15]:5702 [dev] [3.10.1] Cluster version set to 3.10
2019-07-10 10:19:46.589 INFO [productprovider,,,] 10216 --- [ration.thread-0] c.h.internal.cluster.ClusterService : [192.168.192.15]:5702 [dev] [3.10.1]
Members {size:2, ver:2} [
Member [192.168.192.15]:5701 - 34da2d53-d8d6-4959-aaf2-bd6cda24f0fa
Member [192.168.192.15]:5702 - 70b959c7-ebb5-4226-b343-76e7354cbd94 this
]
2019-07-10 10:19:47.568 INFO [productprovider,,,] 10216 --- [ main] com.hazelcast.core.LifecycleService : [192.168.192.15]:5702 [dev] [3.10.1] [192.168.192.15]:5702 is STARTED
说明它打开了一个5702端口的分布式缓存系统
我们将Springboot的端口改成8006,并启动该端口的相同项目,我们可以在启动日志中看到
2019-07-10 10:23:16.328 INFO [productprovider,,,] 11764 --- [ main] com.hazelcast.instance.AddressPicker : [LOCAL] [dev] [3.10.1] Prefer IPv4 stack is true.
2019-07-10 10:23:16.687 INFO [productprovider,,,] 11764 --- [ main] com.hazelcast.instance.AddressPicker : [LOCAL] [dev] [3.10.1] Picked [192.168.192.15]:5704, using socket ServerSocket[addr=/0:0:0:0:0:0:0:0,localport=5704], bind any local is true
2019-07-10 10:23:16.688 INFO [productprovider,,,] 11764 --- [ main] com.hazelcast.system : [192.168.192.15]:5704 [dev] [3.10.1] Hazelcast 3.10.1 (20180521 - 66f881d) starting at [192.168.192.15]:5704
2019-07-10 10:23:16.689 INFO [productprovider,,,] 11764 --- [ main] com.hazelcast.system : [192.168.192.15]:5704 [dev] [3.10.1] Copyright (c) 2008-2018, Hazelcast, Inc. All Rights Reserved.
2019-07-10 10:23:16.689 INFO [productprovider,,,] 11764 --- [ main] com.hazelcast.system : [192.168.192.15]:5704 [dev] [3.10.1] Configured Hazelcast Serialization version: 1
2019-07-10 10:23:16.689 INFO [productprovider,,,] 11764 --- [ main] com.hazelcast.instance.Node : [192.168.192.15]:5704 [dev] [3.10.1] A non-empty group password is configured for the Hazelcast member. Starting with Hazelcast version 3.8.2, members with the same group name, but with different group passwords (that do not use authentication) form a cluster. The group password configuration will be removed completely in a future release.
2019-07-10 10:23:16.692 INFO [productprovider,,,] 11764 --- [ main] c.h.s.i.o.impl.BackpressureRegulator : [192.168.192.15]:5704 [dev] [3.10.1] Backpressure is disabled
2019-07-10 10:23:16.692 INFO [productprovider,,,] 11764 --- [ main] h.s.i.o.i.InboundResponseHandlerSupplier : [192.168.192.15]:5704 [dev] [3.10.1] Running with 2 response threads
2019-07-10 10:23:17.310 INFO [productprovider,,,] 11764 --- [ main] com.hazelcast.instance.Node : [192.168.192.15]:5704 [dev] [3.10.1] Creating MulticastJoiner
2019-07-10 10:23:17.315 INFO [productprovider,,,] 11764 --- [ main] c.h.s.i.o.impl.OperationExecutorImpl : [192.168.192.15]:5704 [dev] [3.10.1] Starting 8 partition threads and 5 generic threads (1 dedicated for priority tasks)
2019-07-10 10:23:17.317 INFO [productprovider,,,] 11764 --- [ main] c.h.internal.diagnostics.Diagnostics : [192.168.192.15]:5704 [dev] [3.10.1] Diagnostics disabled. To enable add -Dhazelcast.diagnostics.enabled=true to the JVM arguments.
2019-07-10 10:23:17.317 INFO [productprovider,,,] 11764 --- [ main] com.hazelcast.core.LifecycleService : [192.168.192.15]:5704 [dev] [3.10.1] [192.168.192.15]:5704 is STARTING
2019-07-10 10:23:17.419 INFO [productprovider,,,] 11764 --- [ main] c.h.i.cluster.impl.MulticastJoiner : [192.168.192.15]:5704 [dev] [3.10.1] Trying to join to discovered node: [192.168.192.15]:5701
2019-07-10 10:23:17.420 INFO [productprovider,,,] 11764 --- [cached.thread-3] com.hazelcast.nio.tcp.TcpIpConnector : [192.168.192.15]:5704 [dev] [3.10.1] Connecting to /192.168.192.15:5701, timeout: 0, bind-any: true
2019-07-10 10:23:17.420 INFO [productprovider,,,] 11764 --- [cached.thread-3] c.h.nio.tcp.TcpIpConnectionManager : [192.168.192.15]:5704 [dev] [3.10.1] Established socket connection between /192.168.192.15:4670 and /192.168.192.15:5701
2019-07-10 10:23:23.427 INFO [productprovider,,,] 11764 --- [ration.thread-2] com.hazelcast.system : [192.168.192.15]:5704 [dev] [3.10.1] Cluster version set to 3.10
2019-07-10 10:23:23.429 INFO [productprovider,,,] 11764 --- [ration.thread-2] c.h.internal.cluster.ClusterService : [192.168.192.15]:5704 [dev] [3.10.1]
Members {size:4, ver:4} [
Member [192.168.192.15]:5701 - 34da2d53-d8d6-4959-aaf2-bd6cda24f0fa
Member [192.168.192.15]:5702 - 70b959c7-ebb5-4226-b343-76e7354cbd94
Member [192.168.192.15]:5703 - 02829ed9-0ddf-4675-ba9e-15c1c03b4a50
Member [192.168.192.15]:5704 - 6ecd17c1-425c-4be0-8914-827f51fdbf3e this
]
2019-07-10 10:23:23.431 INFO [productprovider,,,] 11764 --- [thread-Acceptor] com.hazelcast.nio.tcp.TcpIpAcceptor : [192.168.192.15]:5704 [dev] [3.10.1] Accepting socket connection from /192.168.192.15:4674
2019-07-10 10:23:23.432 INFO [productprovider,,,] 11764 --- [thread-Acceptor] c.h.nio.tcp.TcpIpConnectionManager : [192.168.192.15]:5704 [dev] [3.10.1] Established socket connection between /192.168.192.15:5704 and /192.168.192.15:4674
2019-07-10 10:23:23.436 INFO [productprovider,,,] 11764 --- [ration.thread-0] c.h.internal.cluster.ClusterService : [192.168.192.15]:5703 [dev] [3.10.1]
Members {size:4, ver:4} [
Member [192.168.192.15]:5701 - 34da2d53-d8d6-4959-aaf2-bd6cda24f0fa
Member [192.168.192.15]:5702 - 70b959c7-ebb5-4226-b343-76e7354cbd94
Member [192.168.192.15]:5703 - 02829ed9-0ddf-4675-ba9e-15c1c03b4a50 this
Member [192.168.192.15]:5704 - 6ecd17c1-425c-4be0-8914-827f51fdbf3e
]
2019-07-10 10:23:23.437 INFO [productprovider,,,] 11764 --- [cached.thread-1] com.hazelcast.nio.tcp.TcpIpConnector : [192.168.192.15]:5703 [dev] [3.10.1] Connecting to /192.168.192.15:5704, timeout: 0, bind-any: true
2019-07-10 10:23:23.438 INFO [productprovider,,,] 11764 --- [cached.thread-1] c.h.nio.tcp.TcpIpConnectionManager : [192.168.192.15]:5703 [dev] [3.10.1] Established socket connection between /192.168.192.15:4675 and /192.168.192.15:5704
2019-07-10 10:23:23.438 INFO [productprovider,,,] 11764 --- [thread-Acceptor] com.hazelcast.nio.tcp.TcpIpAcceptor : [192.168.192.15]:5704 [dev] [3.10.1] Accepting socket connection from /192.168.192.15:4675
2019-07-10 10:23:23.439 INFO [productprovider,,,] 11764 --- [thread-Acceptor] c.h.nio.tcp.TcpIpConnectionManager : [192.168.192.15]:5704 [dev] [3.10.1] Established socket connection between /192.168.192.15:5704 and /192.168.192.15:4675
2019-07-10 10:23:23.440 INFO [productprovider,,,] 11764 --- [cached.thread-5] com.hazelcast.nio.tcp.TcpIpConnector : [192.168.192.15]:5704 [dev] [3.10.1] Connecting to /192.168.192.15:5702, timeout: 0, bind-any: true
2019-07-10 10:23:23.444 INFO [productprovider,,,] 11764 --- [cached.thread-5] c.h.nio.tcp.TcpIpConnectionManager : [192.168.192.15]:5704 [dev] [3.10.1] Established socket connection between /192.168.192.15:4676 and /192.168.192.15:5702
2019-07-10 10:23:23.444 INFO [productprovider,,,] 11764 --- [cached.thread-2] com.hazelcast.nio.tcp.TcpIpConnector : [192.168.192.15]:5704 [dev] [3.10.1] Connecting to /192.168.192.15:5703, timeout: 0, bind-any: true
2019-07-10 10:23:23.447 INFO [productprovider,,,] 11764 --- [thread-Acceptor] com.hazelcast.nio.tcp.TcpIpAcceptor : [192.168.192.15]:5703 [dev] [3.10.1] Accepting socket connection from /192.168.192.15:4677
2019-07-10 10:23:23.447 INFO [productprovider,,,] 11764 --- [cached.thread-2] c.h.nio.tcp.TcpIpConnectionManager : [192.168.192.15]:5704 [dev] [3.10.1] Established socket connection between /192.168.192.15:4677 and /192.168.192.15:5703
2019-07-10 10:23:23.447 INFO [productprovider,,,] 11764 --- [thread-Acceptor] c.h.nio.tcp.TcpIpConnectionManager : [192.168.192.15]:5703 [dev] [3.10.1] Established socket connection between /192.168.192.15:5703 and /192.168.192.15:4677
2019-07-10 10:23:24.422 INFO [productprovider,,,] 11764 --- [ main] com.hazelcast.core.LifecycleService : [192.168.192.15]:5704 [dev] [3.10.1] [192.168.192.15]:5704 is STARTED
我们先在8006端口的进程中添加一个一级配件
2019-07-10 10:23:43.408 DEBUG [productprovider,7c69f637b05303a0,7c69f637b05303a0,false] 11764 --- [nio-8006-exec-1] c.c.p.d.L.saveProviderProductLevel1 : ==> Preparing: insert into product_level (id,code,name,sort,level,product_type) values (?,?,?,?,?,?)
2019-07-10 10:23:43.431 DEBUG [productprovider,7c69f637b05303a0,7c69f637b05303a0,false] 11764 --- [nio-8006-exec-1] c.c.p.d.L.saveProviderProductLevel1 : ==> Parameters: 2459048597577007931(Long), 10003(String), 汽车项链(String), 3(Integer), 1(Integer), 车品(String)
2019-07-10 10:23:43.454 DEBUG [productprovider,7c69f637b05303a0,7c69f637b05303a0,false] 11764 --- [nio-8006-exec-1] c.c.p.d.L.saveProviderProductLevel1 : <== Updates: 1
然后我们在8106端口进程中添加一个二级汽车配件分类,我们来看一看是否可以获取一级配件分类的缓存
根据日志打印
2019-07-10 10:26:02.587 DEBUG [productprovider,deca9b0c23fc065f,deca9b0c23fc065f,false] 10216 --- [nio-8106-exec-1] c.c.p.d.L.saveProviderProductLevel2 : ==> Preparing: insert into product_level (id,code,name,sort,level,picture_url) values (?,?,?,?,?,?)
2019-07-10 10:26:02.613 DEBUG [productprovider,deca9b0c23fc065f,deca9b0c23fc065f,false] 10216 --- [nio-8106-exec-1] c.c.p.d.L.saveProviderProductLevel2 : ==> Parameters: 2459048746827121467(Long), 100201(String), 项链(String), 1(Integer), 2(Integer), sdfasgfd23dg(String)
2019-07-10 10:26:02.635 DEBUG [productprovider,deca9b0c23fc065f,deca9b0c23fc065f,false] 10216 --- [nio-8106-exec-1] c.c.p.d.L.saveProviderProductLevel2 : <== Updates: 1
2019-07-10 10:26:02.664 INFO [productprovider,deca9b0c23fc065f,deca9b0c23fc065f,false] 10216 --- [nio-8106-exec-1] c.c.p.controller.LevelController : 缓存获取{"code":"10003","id":2459069885582410555,"level":1,"name":"汽车项链","productListProviders":[{"code":"100201","id":2459069910278472507,"level":2,"name":"项链","pictureUrl":"sdfasgfd23dg","productListProviders":[],"sort":1}],"productType":"TYPE2","sort":3}
2019-07-10 10:26:02.666 DEBUG [productprovider,deca9b0c23fc065f,deca9b0c23fc065f,false] 10216 --- [nio-8106-exec-1] c.c.p.dao.LevelDao.addLevelToLevel : ==> Preparing: update product_level set parent_id=? where id=?
2019-07-10 10:26:02.666 DEBUG [productprovider,deca9b0c23fc065f,deca9b0c23fc065f,false] 10216 --- [nio-8106-exec-1] c.c.p.dao.LevelDao.addLevelToLevel : ==> Parameters: 2459048597577007931(Long), 2459048746827121467(Long)
2019-07-10 10:26:02.689 DEBUG [productprovider,deca9b0c23fc065f,deca9b0c23fc065f,false] 10216 --- [nio-8106-exec-1] c.c.p.dao.LevelDao.addLevelToLevel : <== Updates: 1
如果此处一级配件是从数据库中查出来的,那么此处的日志将不会有缓存获取4个字,说明该缓存已经同步到了不同进程中。
————————————————
版权声明:本文为CSDN博主「weixin_39997253」的原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接及本声明。
原文链接:https://blog.csdn.net/weixin_39997253/article/details/114173522