Java操作Zookeeper API--Watch监听

1.NodeCache:给指定一个节点注册监听器
2.PathChildrenCache : 监听某个节点下的所有子节点
3.TreeCache: 监听某个节点自己和所有的子节点们

package com.test.curator;

import org.apache.curator.RetryPolicy;
import org.apache.curator.framework.CuratorFramework;
import org.apache.curator.framework.CuratorFrameworkFactory;
import org.apache.curator.framework.recipes.cache.*;
import org.apache.curator.retry.ExponentialBackoffRetry;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;

public class WatcherTest {
    private CuratorFramework client;

    @Before
    public void test(){
        // 1.第一种方式
        RetryPolicy retryPolicy = new ExponentialBackoffRetry(3000,10);
        /**
         * Create a new client
         *
         * @param connectString       list of servers to connect to;zk server 地址和端口号
         * @param sessionTimeoutMs    session timeout  会话超时时间
         * @param connectionTimeoutMs connection timeout 连接超时时间
         * @param retryPolicy         retry policy to use 重试策略
         * @return client
         */
        String connectString = "192.168.10.102:2181,192.168.10.103:2181,192.168.10.103:2181";
        /*CuratorFramework client = CuratorFrameworkFactory.newClient(connectString,60*1000,15*1000,retryPolicy);
        client.start();*/

        // 2.第二种方式  推荐
        client = CuratorFrameworkFactory.builder().connectString(connectString)
                .sessionTimeoutMs(60 * 1000).connectionTimeoutMs(15 * 100)
                .retryPolicy(retryPolicy)
                .namespace("duck").build();
        client.start();

    }

    /**
     * NodeCache:给指定一个节点注册监听器
     */
    @Test
    public void testNodeCache() throws Exception {
        // 1.创建NodeCache对象
        NodeCache nodeCache = new NodeCache(client,"/app1");
        // 2.注册监听
        nodeCache.getListenable().addListener(new NodeCacheListener() {
            @Override
            public void nodeChanged() throws Exception {
                System.out.println("节点变化了...");
                // 获取修改节点后的数据
                byte[] data = nodeCache.getCurrentData().getData();
                System.out.println(new String(data));
            }
        });

        // 3.开启监听,如果设置为true,则开启监听是,加载缓冲数据
        nodeCache.start(true);

        while (true){}
    }

    /**
     * PathChildrenCache : 监听某个节点下的所有子节点
     * @throws Exception
     */
    @Test
    public void testPathChildrenCache() throws Exception {
        // 1. 创建监听对象
        PathChildrenCache pathChildrenCache = new PathChildrenCache(client,"/app2",true);
        // 2. 绑定监听器
        pathChildrenCache.getListenable().addListener(new PathChildrenCacheListener() {
            @Override
            public void childEvent(CuratorFramework curatorFramework, PathChildrenCacheEvent pathChildrenCacheEvent) throws Exception {
                System.out.println("节点变化了");
                System.out.println(pathChildrenCacheEvent);

                // 监听子节点的数据变更,并且拿到变更后的数据
                // 1.获取类型
                PathChildrenCacheEvent.Type type = pathChildrenCacheEvent.getType();
                // 2.判断是否更新
                if(type.equals(PathChildrenCacheEvent.Type.CHILD_UPDATED)){
                    System.out.println("数据变化了");
                    byte[] data = pathChildrenCacheEvent.getData().getData();
                    System.out.println(new String(data));
                }
            }
        });

        pathChildrenCache.start();

        while (true){}
    }

    /**
     * TreeCache: 监听某个节点自己和所有的子节点们
     */
    @Test
    public void testTreeCache() throws Exception {
        // 1.创建监听器
        TreeCache treeCache = new TreeCache(client,"/app2");

        // 2.注册监听
        treeCache.getListenable().addListener(new TreeCacheListener() {
            @Override
            public void childEvent(CuratorFramework curatorFramework, TreeCacheEvent treeCacheEvent) throws Exception {
                System.out.println("节点变化了");
                System.out.println(treeCacheEvent);
            }
        });

        // 3.开启
        treeCache.start();

        while (true){}

    }

    @After
    public void close(){
        if(client != null){
            client.close();
        }
    }

}

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值