您的位置:首页 > Web前端 > Node.js

curator监听zookeeper目录(TreeNode)

2017-12-01 07:50 232 查看

curator监听zookeeper目录(TreeNode)

本文主要介绍如何通过curator框架进行监听节点的监听。
好,下面上货。
package com.xueyou.zkdemo;

import com.xueyou.zkdemo.zkUtils.CreateClient;
import com.xueyou.zkdemo.zkUtils.CuratorZkClientBridge;
import org.apache.curator.framework.CuratorFramework;
import org.apache.curator.framework.recipes.cache.NodeCache;
import org.apache.curator.framework.recipes.cache.TreeCache;
import org.apache.curator.framework.recipes.cache.TreeCacheEvent;
import org.apache.curator.framework.recipes.cache.TreeCacheListener;

import java.util.ArrayList;
import java.util.List;

/**
* Hello world!
*/
public class AppListenser {
public static String connectionString = "192.168.0.66:62181,192.168.0.66:62182,192.168.0.66:62183";
public static List<String> res = new ArrayList<>();
public static final int ID = 3;

public static void main(String[] args) throws Exception {
System.out.println("Hello World!");
CuratorFramework curatorFramework = CreateClient.createSimple(connectionString);
curatorFramework.start();
//doSomething to zookeeper
CuratorZkClientBridge curatorZkClientBridge = new CuratorZkClientBridge(curatorFramework);

final TreeCache treeCache = new TreeCache(curatorFramework, "/test");
treeCache.getListenable().addListener(new TreeCacheListener() {
@Override
public void childEvent(CuratorFramework curatorFramework, TreeCacheEvent treeCacheEvent) throws Exception {
System.out.println(treeCacheEvent.getType());
System.out.println(treeCacheEvent.getData().getPath());
System.out.println(new String(treeCacheEvent.getData().getData()));
}
});
treeCache.start();
;
Thread.sleep(Integer.MAX_VALUE);
}

}


当修改zk中的内容是,就会对相应的事件进行捕获。
下面是运行结果:

内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: