TreeCahce被替换CuratorCahc后的基本使用

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 也可以找到。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值