Zookeeper客户端Curator5.1节点事件监听CuratorCache用法

一、简介

在低版本的curator(4.0.1)中,使用NodeCachePathChildrenCacheTreeCache进行节点事件的监听。

在高版本的curator(5.1.0)中,弃用了原来的三种方式:NodeCachePathChildrenCacheTreeCache 。使用新的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中,NodeCachePathChildrenCacheTreeCache 均被弃用,使用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();

三、总结

  1. NodeCache:只能监听自己节点的
  2. PathChildrenCache: 只能监听直接子节点的
  3. TreeCache:可以监听自己节点及其所有子节点的事件
  4. CuratorCache:可以监听自己节点及其所有子节点的事件

文章参考

https://www.jianshu.com/p/2a3bfc606488
https://blog.csdn.net/hosaos/article/details/88658676

源码

githubhttps://github.com/Liu-Shihao/springboot-curator

  • 2
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Liu_Shihao

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值