目录
一、简介
在低版本的curator(4.0.1)中,使用NodeCache
、PathChildrenCache
、TreeCache
进行节点事件的监听。
在高版本的curator(5.1.0)中,弃用了原来的三种方式:NodeCache 、PathChildrenCache 、TreeCache 。使用新的CuratorCache
类进行监听。
二、用法示例
2.1 环境准备
spring-boot-starter-parent:
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.6.7</version>
<relativePath />
</parent>
curator:
<dependency>
<groupId>org.apache.curator</groupId>
<artifactId>curator-framework</artifactId>
<version>5.1.0</version>
</dependency>
<dependency>
<groupId>org.apache.curator</groupId>
<artifactId>curator-recipes</artifactId>
<version>5.1.0</version>
</dependency>
<!-- 使用TestingServer可以模拟zk环境 -->
<dependency>
<groupId>org.apache.curator</groupId>
<artifactId>curator-test</artifactId>
<version>5.1.0</version>
</dependency>
CuratorFramework客户端:
public static String connectString ="192.168.153.131:2181";//集群用,隔开
CuratorFramework curatorFramework = CuratorFrameworkFactory.builder()
// .connectString(testingServer.getConnectString())//使用TestingServer模拟zk环境(无需部署zk环境)
.connectString(connectString)
.connectionTimeoutMs(15 * 1000)
//会话超时时间
.sessionTimeoutMs(60 * 1000)
//设置重试机制
.retryPolicy(new ExponentialBackoffRetry(10*1000,3))
.build();
curatorFramework.start();
2.2 NodeCache(已弃用)
NodeCache只能监听当前节点的增删改操作,不能监听子节点的事件。
注意:在节点被创删除的时候,getCurrentData()会空指针。
/**
* @curator 4.0.1
* @NodeCache:监听节点对应增、删、改操作,无法监听到子节点事件
*/
NodeCache nodeCache = new NodeCache(curatorFramework, "/nodeCache");
nodeCache.start();
nodeCache.getListenable().addListener(new NodeCacheListener() {
@Override
public void nodeChanged() throws Exception {
log.info("-------------nodeCache-----------------");
log.info("path:{}",nodeCache.getPath());
log.info("currentData:{}",new String(nodeCache.getCurrentData().getData()));
log.info("---------------------------------------");
}
});
2.3 PathChildrenCache(已弃用)
PathChildrenCache可以监听直接子节点的增、删、改事件,不能监听当前节点和孙子节点及下节点的事件。
/**
* @curator 4.0.1
* @PathChildrenCache:监听节点下一级子节点的增、删、改操作
* 1.无法对监听路径所在节点进行监听(即不能监听path对应节点的变化)
* 2.只能监听path对应节点下一级目录的子节点的变化内容(即只能监听/path/node1的变化,而不能监听/path/node1/node2 的变化)
*/
PathChildrenCache pathChildrenCache = new PathChildrenCache(curatorFramework, "/pathChildrenCache",true);
pathChildrenCache.start();
pathChildrenCache.getListenable().addListener(new PathChildrenCacheListener() {
@Override
public void childEvent(CuratorFramework client, PathChildrenCacheEvent event) throws Exception {
log.info("-------------pathChildrenCache-----------------");
String type = event.getType().name();
log.info("event type:{}",type);
if (type.equals("CHILD_ADDED")||type.equals("CHILD_UPDATED")||type.equals("CHILD_REMOVED")){
log.info("path:{}",event.getData().getPath());
log.info("data:{}",new String(event.getData().getData()));
}
log.info("---------------------------------------");
}
});
2.4 TreeCache(已弃用)
TreeCache可以监听当前节点及其所有子节点的事件。
/**
* @curator 4.0.1
* @TreeCache:可以将指定的路径节点作为根节点,对节点及其所有的子节点操作进行监听
*/
TreeCache treeCache = new TreeCache(curatorFramework, "/treeCache");
treeCache.start();
treeCache.getListenable().addListener(new TreeCacheListener() {
@Override
public void childEvent(CuratorFramework client, TreeCacheEvent event) throws Exception {
log.info("-------------treeCache-----------------");
String type = event.getType().name();
log.info("event type:{}",type);
if (type.equals("NODE_ADDED")||type.equals("NODE_UPDATED")||type.equals("NODE_REMOVED")){
log.info("path:{}",event.getData().getPath());
log.info("data:{}",new String(event.getData().getData()));
}
log.info("---------------------------------------");
}
});
2.5 CuratorCache
在curator 5.1.0中,NodeCache 、PathChildrenCache 、TreeCache 均被弃用,使用CuratorCache进行所有节点的事件监听,CuratorCache可以监听当前节点及其所有子节点的事件。
// curator 5.1.0:NODE_CREATED、NODE_CHANGED、NODE_DELETED
CuratorCache curatorCache = CuratorCache.build(curatorFramework, "/curatorCache");
curatorCache.listenable().addListener(new CuratorCacheListener() {
@Override
public void event(Type type, ChildData oldData, ChildData data) {
if (type.name().equals(CuratorCacheListener.Type.NODE_CREATED.name())) {
//(注意:创建节点时,oldData为null)
log.info("A new node was added to the cache :{}",data.getPath());
//TODO...
} else if (type.name().equals(CuratorCacheListener.Type.NODE_CHANGED.name())) {
log.info("A node already in the cache has changed :{}", data.getPath());
//TODO...
} else {
//NODE_DELETED: node already in the cache was deleted.(注意:删除节点时,data为null)
log.info("A node already in the cache was deleted :{}", oldData.getPath());
//TODO...
}
}
});
curatorCache.start();
三、总结
- NodeCache:只能监听自己节点的
- PathChildrenCache: 只能监听直接子节点的
- TreeCache:可以监听自己节点及其所有子节点的事件
- CuratorCache:可以监听自己节点及其所有子节点的事件
文章参考
https://www.jianshu.com/p/2a3bfc606488
https://blog.csdn.net/hosaos/article/details/88658676