导入依赖
监听单个节点org.springframework.boot spring-boot-starter-parent 2.1.8.RELEASE org.springframework.boot spring-boot-starter org.springframework.boot spring-boot-starter-test test org.springframework.boot spring-boot-starter-web org.projectlombok lombok org.apache.curator curator-framework 4.0.0 org.apache.curator curator-recipes 4.0.0
public void testNodeCache() throws Exception {
//1. 创建NodeCache对象
final 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){
}
}
application.yml springBoot配置文件
zookeeper:
address: 192.168.23.129:2181 #zookeeper端口以及地址
sessionTimeoutMs: 2000 #会话超时时间,单位毫秒,默认60000ms
connectionTimeoutMs: 2000 #连接创建超时时间,单位毫秒,默认60000ms
config配置文件,加载zookpeeper连接
@Configuration
@Data
public class ZKclientConfig {
@Value("${zookeeper.address}")
private String connect;
@Value("${zookeeper.sessionTimeoutMs}")
private Integer sessionTimeoutMs;
@Value("${zookeeper.connectionTimeoutMs}")
private Integer connectionTimeoutMs;
@Bean
public Curatorframework Client(){
//重试策略
RetryPolicy retryPolicy = new ExponentialBackoffRetry(3000, 10);
Curatorframework ZkClient = CuratorframeworkFactory.builder()
.connectString(connect)
.sessionTimeoutMs(sessionTimeoutMs)
.connectionTimeoutMs(connectionTimeoutMs)
.retryPolicy(retryPolicy)
.namespace("qyx") //根节点
.build();
ZkClient.start();
return ZkClient;
}
}
在虚拟机zookeeper中修改节点数据
控制台打印
再次修改数据
创建节点
两个节点的值设置为
public void testPathChildrenCache() throws Exception {
//1.创建监听对象
PathChildrenCache pathChildrenCache = new PathChildrenCache(Client,"/app2",true);
//2. 绑定监听器
pathChildrenCache.getListenable().addListener(new PathChildrenCacheListener() { @Override
public void childEvent(Curatorframework client, PathChildrenCacheEvent event) throws Exception {
System.out.println("子节点变化了~");
System.out.println(event);
//监听子节点的数据变更,并且拿到变更后的数据
//1.获取类型
PathChildrenCacheEvent.Type type = event.getType();
//2.判断类型是否是update
if(type.equals(PathChildrenCacheEvent.Type.CHILD_UPDATED)){
System.out.println("数据变了!!!");
byte[] data = event.getData().getData();
System.out.println(new String(data));
}
}
});
//3. 开启
pathChildrenCache.start();
while (true){
}
}
修改虚拟机zookeeper中app22节点数据看控制台输出
修改虚拟机zookeeper中app23节点数据看控制台输出
所以,app2的子节点无论那个发生变化,都会被监听到
但是修改app2时
不会被监听到
public void testTreeCache() throws Exception {
//1. 创建监听器
TreeCache treeCache = new TreeCache(Client,"/app2");
//2. 注册监听
treeCache.getListenable().addListener(new TreeCacheListener() {
@Override
public void childEvent(Curatorframework client, TreeCacheEvent event) throws Exception {
System.out.println("节点变化了");
System.out.println(event);
}
});
//3. 开启
treeCache.start();
while (true){
}
}
修改虚拟机zookeeper中app2节点数据看控制台输出
修改虚拟机zookeeper中app22节点数据看控制台输出
修改虚拟机zookeeper中app23节点数据看控制台输出
因此节点app2和其子节点app22和app23的改变都会被监听到



