Zookeeper树形节点结构与JavaAPI

本文详细介绍了Zookeeper的三种监听器:NodeCache用于监听单个节点的变化,PathChildrenCache关注指定路径下子节点的增删改,而TreeCache则能监听整个树形结构的更新。通过实例代码展示了如何配置和使用这些监听器,帮助理解Zookeeper的数据同步机制。
摘要由CSDN通过智能技术生成

三个监听器zookeeper的API:NodeCache TreeCache PathChildrenCache

1.NodeChache

private CuratorFramework client;

@Before
public void buildsCurator() {
    RetryPolicy re = new ExponentialBackoffRetry(3000,10,5000);//int baseSleepTimeMs, int maxRetries, int maxSleepMs

    client = CuratorFrameworkFactory.builder().connectString("192.168.214.33:2181").sessionTimeoutMs(60000)
            .connectionTimeoutMs(1500).retryPolicy(re).namespace("com").build();

    client.start();

}

@After
public void close() {
    if (client != null)  client.close();
}
    //NodeCache
    @Test
    public void testNodeCache() throws Exception {
//        1.创建NodeCache对象
        final NodeCache nc = new NodeCache(client,"/it01");  //CuratorFramework client, String path
//        2.注册监听
        nc.getListenable().addListener(new NodeCacheListener() {
            public void nodeChanged() throws Exception {
                System.out.println("单个节点被修改了..........");
                byte[] data = nc.getCurrentData().getData();
                System.out.println(new String(data));
            }
        });
//        3.启动监听
        nc.start(true);

//        4.while true 

        while (true) {

        }
    }

2.PathChildrenCache

//PathChildrenCache
@Test
public void pathChildrenCache() throws Exception {
    //1.创建PathChildrenCache对象
    final PathChildrenCache pcc = new PathChildrenCache(client,"/it01",true);
    //2.获取监听器
    pcc.getListenable().addListener(new PathChildrenCacheListener() {
        public void childEvent(CuratorFramework curatorFramework, PathChildrenCacheEvent pathChildrenCacheEvent) throws Exception {
            System.out.println("节点变化了....");
            System.out.println(pathChildrenCacheEvent);
            PathChildrenCacheEvent.Type type = pathChildrenCacheEvent.getType();

            //update
            if (type.equals(PathChildrenCacheEvent.Type.CHILD_UPDATED)) {
                System.out.println("修改数据为====>" + new String(pathChildrenCacheEvent.getData().getData()));
            }

        }
    });

    //3.启动监听器
    pcc.start();

    while (true) {}
}

3.TreeCache

@Test
public void treeCache() throws Exception {

    TreeCache tc = new TreeCache(client,"/");

    tc.getListenable().addListener(new TreeCacheListener() {
        public void childEvent(CuratorFramework curatorFramework, TreeCacheEvent treeCacheEvent) throws Exception {
            System.out.println("==============>节点变化了");
            System.out.println(treeCacheEvent);

            TreeCacheEvent.Type type = treeCacheEvent.getType();

            if (type.equals(TreeCacheEvent.Type.NODE_UPDATED)) {
                System.out.println("================>" + new String(treeCacheEvent.getData().getData()));
            }
        }
    });
    tc.start();
    while (true){}
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值