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();
}
}
}