CuratorCahc基本使用
最近在学习zookeeper的时候使用的是curator版本是5.1.0。发现原来TreeCache被替换了。更换为了CuratorChache下面介绍该方法的基本使用。
由于是初学所以有错的地方希望大家指正。
RetryPolicy retryPolicy = new ExponentialBackoffRetry(1000, 1);
CuratorFramework client = CuratorFrameworkFactory.newClient("192.168.38.10:2181,192.168.38.11:2181,192.168.38.12:2181"
, 1000, 1000, retryPolicy);
client.start();
CuratorCache cache = CuratorCache.build(client, "/hello3");
CuratorCacheListener listener = CuratorCacheListener.builder()
.forChanges((oldNode, node) -> {
System.out.println("old data: " + new String(node.getData()) + " new data:" + new String(node.getData()));
})
.forCreates((node) -> {
System.out.println("节点被创建:" + node.getPath());
})
.forDeletes((node) -> {
System.out.println("节点被删除:" + node.getPath());
})
.forInitialized(() -> System.out.println("Cache initialized"))
.build();
cache.listenable().addListener(listener);
cache.start();
client.close();
区别就是监听方法编程了现在的链式风格。不过在调用Client方法的时候回报下面这个错误,我也没有解决。但是这个错误目前没有发现有任何影响。如果有人知道希望可以告知我。
org.apache.zookeeper.ClientCnxn - An exception was thrown while closing send thread for session 0x2000760fc450005.
EndOfStreamException: Unable to read additional data from server sessionid 0x2000760fc450005, likely server has closed socket
以上代码在 Github 也可以找到。