Zookeeper Curator 使用以及 5.0版本新 CuratorCache 示例
本篇文章不复述Curator 的监听机制以及其他原理性知识,只做新版本中的代码测试示例。使用过程中也遇到了很多坑。
使用PathChildrenCache进行监听
此种方法适合所有版本监听使用
@Slf4j
@RunWith(SpringRunner.class)
@SpringBootTest
public class ZkClientListenerTest{
public CuratorFramework curatorFramework;
@Before
public void init() {
RetryPolicy retryPolicy = new ExponentialBackoffRetry(5000, 30);
curatorFramework = CuratorFrameworkFactory.builder()
.connectString("192.168.0.10:2181")
.sessionTimeoutMs(5000) // 会话超时时间
.connectionTimeoutMs(5000) // 连接超时时间
.retryPolicy(retryPolicy)
.namespace("base") // 包含隔离名称
.build();
curatorFramework.getConnectionStateListenable().addListener((client, newState) -> {
if (newState == ConnectionState.CONNECTED) {
log.info("连接成功!");
}
});
log.info("连接中......");
curatorFramework.start();
}
@After
public void test(){
try {
TimeUnit.SECONDS.sleep(Integer.MAX_VALUE);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
public void createIfNeed(String path) throws Exception {
Stat stat = curatorFramework.checkExists().forPath(path);
if (stat==null){
String s = curatorFramework.create().forPath(path);
log.info("path {} created! ",s);
}
}
@Test
public void curator() throws Exception{
String path = "/java";
String subPath = "/java/work";
createIfNeed(path);
PathChildrenCache pathChildrenCache = new PathChildrenCache(curatorFramework, path, true);
PathChildrenCacheListener listener = new PathChildrenCacheListener() {
@Override
public void childEvent(CuratorFramework client, PathChildrenCacheEvent event) throws Exception {
log.info("event: {}", event);
}
};
pathChildrenCache.getListenable().addListener(listener);
pathChildrenCache.start(PathChildrenCache.StartMode.BUILD_INITIAL_CACHE);
Thread.sleep(1000);
for (int i = 3; i < 6; i++) {
createIfNeed(subPath + i);
}
}
}
其他 NodeCache、TreeCache 具体使用类似,不再重复演示。
Curator 5.0 CuratorCache使用示例
工程 pom文件依赖
<dependency>
<groupId>org.apache.curator</groupId>
<artifactId>curator-framework</artifactId>
<version>5.1.0</version>
<exclusions>
<exclusion>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-api</artifactId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>org.apache.curator</groupId>
<artifactId>curator-recipes</artifactId>
<version>5.1.0</version>
</dependency>
@Test
/**
* 该版本适合zookeeper 3.6.0 以上版本,服务器 zookeeper 服务也需要为 3.6.0 版本
* Curator 为 5.0 以上使用
*/
public void curator5() throws Exception{
RetryPolicy retryPolicy = new ExponentialBackoffRetry(1000, 3);
CuratorFramework client = CuratorFrameworkFactory.newClient("192.168.0.10:2181", retryPolicy);
client.start();
String path = "/python";
createIfNeed(path);
CuratorCache cache = CuratorCache.build(client, path);
CuratorCacheListener listener = CuratorCacheListener.builder()
.forCreates(node -> System.out.println(String.format("Node created: [%s]", node)))
.forChanges((oldNode, node) -> System.out.println(String.format("Node changed. Old: [%s] New: [%s]", oldNode, node)))
.forDeletes(oldNode -> System.out.println(String.format("Node deleted. Old value: [%s]", oldNode)))
.forInitialized(() -> System.out.println("Cache initialized"))
.build();
// register the listener
cache.listenable().addListener(listener);
// the cache must be started
cache.start();
}
zookeeper 客户端操作演示
监听响应输出: