watch 机制,客户端使用NIO 通讯模式监听服务端的调用 zkCli 客户端示例
get -w /testNode
ls -w /testNode
ls -R -w /testNode
curator 客户端监听示例
@Test
public void addNodeListener() throws Exception{
NodeCache nodeCache = new NodeCache(curatorFramework, "/test-node1");
nodeCache.getListenable().addListener(new NodeCacheListener() {
@Override
public void nodeChanged() throws Exception {
printZk();
}
});
nodeCache.start();
System.in.read();
}
public void printZk() throws Exception{
byte[] bytes = curatorFramework.getData().forPath("/test-node1");
System.out.println(new String(bytes));
}
修改test-node1 节点,java控制台输出节点变更的值
[zk: localhost:2181(CONNECTED) 10] set /test-node1 123
[zk: localhost:2181(CONNECTED) 11] set /test-node1 333333
|