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实现可回滚方案的示例代码》很多项目翻车不是因为不会做,而是走错了方向却没法回头,技术选型失败的风险我们都清楚,但真正能提前规划“回滚方案”的人不多,本文从实际项目出发,教你如何用Py... 目录描述题解答案(核心思路)题解代码分析第一步:抽象缓存接口第二步:实现两个版本第三步:根据 Fea

Go语言使用slices包轻松实现排序功能

《Go语言使用slices包轻松实现排序功能》在Go语言开发中,对数据进行排序是常见的需求,Go1.18版本引入的slices包提供了简洁高效的排序解决方案,支持内置类型和用户自定义类型的排序操作,本... 目录一、内置类型排序:字符串与整数的应用1. 字符串切片排序2. 整数切片排序二、检查切片排序状态:

python利用backoff实现异常自动重试详解

《python利用backoff实现异常自动重试详解》backoff是一个用于实现重试机制的Python库,通过指数退避或其他策略自动重试失败的操作,下面小编就来和大家详细讲讲如何利用backoff实... 目录1. backoff 库简介2. on_exception 装饰器的原理2.1 核心逻辑2.2

Java实现视频格式转换的完整指南

《Java实现视频格式转换的完整指南》在Java中实现视频格式的转换,通常需要借助第三方工具或库,因为视频的编解码操作复杂且性能需求较高,以下是实现视频格式转换的常用方法和步骤,需要的朋友可以参考下... 目录核心思路方法一:通过调用 FFmpeg 命令步骤示例代码说明优点方法二:使用 Jaffree(FF

基于C#实现MQTT通信实战

《基于C#实现MQTT通信实战》MQTT消息队列遥测传输,在物联网领域应用的很广泛,它是基于Publish/Subscribe模式,具有简单易用,支持QoS,传输效率高的特点,下面我们就来看看C#实现... 目录1、连接主机2、订阅消息3、发布消息MQTT(Message Queueing Telemetr

Java实现图片淡入淡出效果

《Java实现图片淡入淡出效果》在现代图形用户界面和游戏开发中,**图片淡入淡出(FadeIn/Out)**是一种常见且实用的视觉过渡效果,它可以用于启动画面、场景切换、轮播图、提示框弹出等场景,通过... 目录1. 项目背景详细介绍2. 项目需求详细介绍2.1 功能需求2.2 非功能需求3. 相关技术详细

Python实现获取带合并单元格的表格数据

《Python实现获取带合并单元格的表格数据》由于在日常运维中经常出现一些合并单元格的表格,如果要获取数据比较麻烦,所以本文我们就来聊聊如何使用Python实现获取带合并单元格的表格数据吧... 由于在日常运维中经常出现一些合并单元格的表格,如果要获取数据比较麻烦,现将将封装成类,并通过调用list_exc

使用animation.css库快速实现CSS3旋转动画效果

《使用animation.css库快速实现CSS3旋转动画效果》随着Web技术的不断发展,动画效果已经成为了网页设计中不可或缺的一部分,本文将深入探讨animation.css的工作原理,如何使用以及... 目录1. css3动画技术简介2. animation.css库介绍2.1 animation.cs

Java进行日期解析与格式化的实现代码

《Java进行日期解析与格式化的实现代码》使用Java搭配ApacheCommonsLang3和Natty库,可以实现灵活高效的日期解析与格式化,本文将通过相关示例为大家讲讲具体的实践操作,需要的可以... 目录一、背景二、依赖介绍1. Apache Commons Lang32. Natty三、核心实现代

SpringBoot实现接口数据加解密的三种实战方案

《SpringBoot实现接口数据加解密的三种实战方案》在金融支付、用户隐私信息传输等场景中,接口数据若以明文传输,极易被中间人攻击窃取,SpringBoot提供了多种优雅的加解密实现方案,本文将从原... 目录一、为什么需要接口数据加解密?二、核心加解密算法选择1. 对称加密(AES)2. 非对称加密(R