Curator实现zookeeper的节点监听

2024-02-08 08:48

本文主要是介绍Curator实现zookeeper的节点监听,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

Curator实现zookeeper的节点监听

Curtor框架中一共有三个实现监听的方式
一种是NodeCache监听指定节点
一种是pathChildrenCache监听子节点
一种是TreeCache可以监控所有节点 相当于以上两种的合集

引入依赖

        <dependency><groupId>org.apache.curator</groupId><artifactId>curator-framework</artifactId><version>4.3.0</version></dependency><dependency><groupId>org.apache.curator</groupId><artifactId>curator-client</artifactId><version>4.3.0</version></dependency><dependency><groupId>org.apache.curator</groupId><artifactId>curator-recipes</artifactId><version>4.3.0</version></dependency>

创建一个测试类 连接好客户端

public class CuratorTest {private CuratorFramework curatorFramework;@Beforepublic void testCreate() {ExponentialBackoffRetry retry = new ExponentialBackoffRetry(2, 10);curatorFramework = CuratorFrameworkFactory.builder().connectString("127.0.0.1:2181").connectionTimeoutMs(60 * 1000).sessionTimeoutMs(15 * 10000).retryPolicy(retry).build();curatorFramework.start();}@Afterpublic void close() {if (curatorFramework != null) {curatorFramework.close();}}
}

Watch监听之NodeCache

监听一个指定节点

 @Testpublic void testUpdate() throws Exception {//监听一个节点NodeCache nodeCache = new NodeCache(curatorFramework,"/dongwuyuan");//注册监听nodeCache.getListenable().addListener(new NodeCacheListener() {@Overridepublic void nodeChanged() throws Exception {System.out.println("节点变化了!!!!");}});//开启监听 参数 如果设置为true 则开启监听时加载缓存数据nodeCache.start(true);while (true){}}
set /dongwuyuan "laohu"

在这里插入图片描述

Watch监听之PathChildrenCache
监听子节点的变化

 @Testpublic void testPathChildrenCache() throws Exception {// 参数  客户端,路径 ,缓存数据,是否压缩,线程池PathChildrenCache pathChildrenCache = new PathChildrenCache(curatorFramework,"/dongwuyuan",true);//绑定监听器pathChildrenCache.getListenable().addListener(new PathChildrenCacheListener() {@Overridepublic void childEvent(CuratorFramework curatorFramework, PathChildrenCacheEvent pathChildrenCacheEvent) throws Exception {System.out.println("子节点变化了");System.out.println(pathChildrenCacheEvent);//监听子节点的变更,并且拿到变更后的数据PathChildrenCacheEvent.Type type = pathChildrenCacheEvent.getType();//判断类型是否是updateif(type.equals(PathChildrenCacheEvent.Type.CHILD_UPDATED)){//拿到数据byte[] data = pathChildrenCacheEvent.getData().getData();System.out.println(data);}}});//开启监听pathChildrenCache.start();while (true){}}

watch监听之TreeCache

  /*** TreeCache:监听节点自己和所有子节点们*/@Testpublic void testTreeCache(){//1.创建监听器TreeCache treeCache = new TreeCache(curatorFramework, "/dongwuyuan");//2.注册监听treeCache.getListenable().addListener(new TreeCacheListener() {@Overridepublic void childEvent(CuratorFramework client, TreeCacheEvent event) throws Exception {System.out.println("节点变化了");System.out.println(event);}});//开启监听try {treeCache.start();while (true){}} catch (Exception e) {e.printStackTrace();}}
[zk: localhost:2181(CONNECTED) 13] delete /dongwuyuan/node1

在这里插入图片描述

[zk: localhost:2181(CONNECTED) 15] set /dongwuyuan/node2  "shizi"

在这里插入图片描述
完整代码

public class CuratorTest {private CuratorFramework curatorFramework;@Beforepublic void testCreate() {ExponentialBackoffRetry retry = new ExponentialBackoffRetry(2, 10);curatorFramework = CuratorFrameworkFactory.builder().connectString("127.0.0.1:2181").connectionTimeoutMs(60 * 1000).sessionTimeoutMs(15 * 10000).retryPolicy(retry).build();curatorFramework.start();}@Testpublic void testUpdate() throws Exception {//监听一个节点NodeCache nodeCache = new NodeCache(curatorFramework,"/dongwuyuan");//注册监听nodeCache.getListenable().addListener(new NodeCacheListener() {@Overridepublic void nodeChanged() throws Exception {System.out.println("节点变化了!!!!");}});//开启监听 参数 如果设置为true 则开启监听时加载缓存数据nodeCache.start(true);while (true){}}@Testpublic void testPathChildrenCache() throws Exception {// 参数  客户端,路径 ,缓存数据,是否压缩,线程池PathChildrenCache pathChildrenCache = new PathChildrenCache(curatorFramework,"/dongwuyuan",true);//绑定监听器pathChildrenCache.getListenable().addListener(new PathChildrenCacheListener() {@Overridepublic void childEvent(CuratorFramework curatorFramework, PathChildrenCacheEvent pathChildrenCacheEvent) throws Exception {System.out.println("子节点变化了");System.out.println(pathChildrenCacheEvent);//监听子节点的变更,并且拿到变更后的数据PathChildrenCacheEvent.Type type = pathChildrenCacheEvent.getType();//判断类型是否是updateif(type.equals(PathChildrenCacheEvent.Type.CHILD_UPDATED)){//拿到数据byte[] data = pathChildrenCacheEvent.getData().getData();System.out.println(data);}}});//开启监听pathChildrenCache.start();while (true){}}/*** TreeCache:监听节点自己和所有子节点们*/@Testpublic void testTreeCache(){//1.创建监听器TreeCache treeCache = new TreeCache(curatorFramework, "/dongwuyuan");//2.注册监听treeCache.getListenable().addListener(new TreeCacheListener() {@Overridepublic void childEvent(CuratorFramework client, TreeCacheEvent event) throws Exception {System.out.println("节点变化了");System.out.println(event);}});//开启监听try {treeCache.start();while (true){}} catch (Exception e) {e.printStackTrace();}}@Afterpublic void close() {if (curatorFramework != null) {curatorFramework.close();}}
}

这篇关于Curator实现zookeeper的节点监听的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/690506

相关文章

Python实现终端清屏的几种方式详解

《Python实现终端清屏的几种方式详解》在使用Python进行终端交互式编程时,我们经常需要清空当前终端屏幕的内容,本文为大家整理了几种常见的实现方法,有需要的小伙伴可以参考下... 目录方法一:使用 `os` 模块调用系统命令方法二:使用 `subprocess` 模块执行命令方法三:打印多个换行符模拟

SpringBoot+EasyPOI轻松实现Excel和Word导出PDF

《SpringBoot+EasyPOI轻松实现Excel和Word导出PDF》在企业级开发中,将Excel和Word文档导出为PDF是常见需求,本文将结合​​EasyPOI和​​Aspose系列工具实... 目录一、环境准备与依赖配置1.1 方案选型1.2 依赖配置(商业库方案)二、Excel 导出 PDF

Python实现MQTT通信的示例代码

《Python实现MQTT通信的示例代码》本文主要介绍了Python实现MQTT通信的示例代码,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一... 目录1. 安装paho-mqtt库‌2. 搭建MQTT代理服务器(Broker)‌‌3. pytho

使用zip4j实现Java中的ZIP文件加密压缩的操作方法

《使用zip4j实现Java中的ZIP文件加密压缩的操作方法》本文介绍如何通过Maven集成zip4j1.3.2库创建带密码保护的ZIP文件,涵盖依赖配置、代码示例及加密原理,确保数据安全性,感兴趣的... 目录1. zip4j库介绍和版本1.1 zip4j库概述1.2 zip4j的版本演变1.3 zip4

python生成随机唯一id的几种实现方法

《python生成随机唯一id的几种实现方法》在Python中生成随机唯一ID有多种方法,根据不同的需求场景可以选择最适合的方案,文中通过示例代码介绍的非常详细,需要的朋友们下面随着小编来一起学习学习... 目录方法 1:使用 UUID 模块(推荐)方法 2:使用 Secrets 模块(安全敏感场景)方法

Spring StateMachine实现状态机使用示例详解

《SpringStateMachine实现状态机使用示例详解》本文介绍SpringStateMachine实现状态机的步骤,包括依赖导入、枚举定义、状态转移规则配置、上下文管理及服务调用示例,重点解... 目录什么是状态机使用示例什么是状态机状态机是计算机科学中的​​核心建模工具​​,用于描述对象在其生命

Spring Boot 结合 WxJava 实现文章上传微信公众号草稿箱与群发

《SpringBoot结合WxJava实现文章上传微信公众号草稿箱与群发》本文将详细介绍如何使用SpringBoot框架结合WxJava开发工具包,实现文章上传到微信公众号草稿箱以及群发功能,... 目录一、项目环境准备1.1 开发环境1.2 微信公众号准备二、Spring Boot 项目搭建2.1 创建

IntelliJ IDEA2025创建SpringBoot项目的实现步骤

《IntelliJIDEA2025创建SpringBoot项目的实现步骤》本文主要介绍了IntelliJIDEA2025创建SpringBoot项目的实现步骤,文中通过示例代码介绍的非常详细,对大家... 目录一、创建 Spring Boot 项目1. 新建项目2. 基础配置3. 选择依赖4. 生成项目5.

Spring boot整合dubbo+zookeeper的详细过程

《Springboot整合dubbo+zookeeper的详细过程》本文讲解SpringBoot整合Dubbo与Zookeeper实现API、Provider、Consumer模式,包含依赖配置、... 目录Spring boot整合dubbo+zookeeper1.创建父工程2.父工程引入依赖3.创建ap

Linux下删除乱码文件和目录的实现方式

《Linux下删除乱码文件和目录的实现方式》:本文主要介绍Linux下删除乱码文件和目录的实现方式,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教... 目录linux下删除乱码文件和目录方法1方法2总结Linux下删除乱码文件和目录方法1使用ls -i命令找到文件或目录