Zookeeper之Curator(1)客户端对节点的一些监控事件的api使用
《一》节点改变事件的监听
public class CauratorClientTest {
//链接地址
private static String zkhost="172.19.27.246:2181";
//sessionTimeoutMs会话超时时间,单位为毫秒。默认是60000ms
private static int sessionTimeoutMs=5000;
//connectionTimeoutMs连接创建超时时间,单位毫秒,默认15000ms
private static int connectionTimeOutMs=3000;
//重连策略
private static int maxRetries;
// zookeeper连接间隔时间基数
private static int baseSleepTimeMs=1000;
//系统域dev,qa,pro
private static String domain="dev";
/**
* 数据节点被改变的事件。能监听节点存储的数据发生变化,和节点被删除的事件。
* 节点被删除后,会调用回调方法。
*/
public static void testNodeChangeEvent() throws Exception{
CuratorFramework client = CuratorFrameworkFactory.newClient(zkhost,sessionTimeoutMs,connectionTimeOutMs, new ExponentialBackoffRetry(baseSleepTimeMs,maxRetries));
client.start();
String path=client.create().creatingParentContainersIfNeeded().withMode(CreateMode.EPHEMERAL).forPath("/dev/sxf/cd","sxf".getBytes());
NodeCache nodeCache=new NodeCache(client,path);
/**
* 注册数据节点中存储的数据被改变的事件
*/
nodeCache.getListenable().addListener(new NodeCacheListener() {
/**
* 数据节点变化事件
*/
@Override
public void nodeChanged() throws Exception {
ChildData data=nodeCache.getCurrentData();
if(data==null){
System.out.println("节点被删除");
return;
}
Stat stat=data.getStat();
int a=stat.getNumChildren();
System.out.println("子节点的个数为==>"+a);
System.out.println("节点数据被改变改变后的数值为==>"+new String(nodeCache.getCurrentData().getData()));
}
});
nodeCache.start();
//当前线程休眠几秒
Thread.sleep(10000L);
client.setData().forPath("/dev/sxf/cd","中国人民解放军".getBytes());
//当前线程休眠几秒
Thread.sleep(10000L);
client.setData().forPath("/dev/sxf/cd","第二次改变".getBytes());
//当前线程休眠几秒
Thread.sleep(10000L);
client.delete().forPath("/dev/sxf/cd");
Thread.sleep(100000L);
}
}
Zookeeper之Curator(1)客户端对节点的一些监控事件的api使用的更多相关文章
- ZooKeeper与Curator注册和监控
Curator提供了对zookeeper客户端的封装,并监控连接状态和会话session,特别是会话session过期后,curator能够重新连接zookeeper,并且创建一个新的session. ...
- ZooKeeper和Curator相关经验总结
一.关于ZooKeeper的watch用法,需要注意 详细说明如下: ZooKeeper Watches All of the read operations in ZooKeeper - getDa ...
- Zookeeper框架Curator使用
本文参考自https://blog.csdn.net/wo541075754/article/details/69138878?utm_source=gold_browser_extension ht ...
- zookeeper系列(三)zookeeper的使用--开源客户端
作者:leesf 掌控之中,才会成功:掌控之外,注定失败, 原创博客地址:http://www.cnblogs.com/leesf456/ 奇文共欣赏,大家共同学习进步. 一.前言 上一篇博客已 ...
- ZooKeeper 之 zkCli.sh客户端的命令使用
zkCli.sh的使用 ZooKeeper服务器简历客户端 ./zkCli.sh -timeout 0 -r -server ip:port ./zkCli.sh -timeout 5000 -ser ...
- zookeeper之二 zkCli客户端命令
ZooKeeper命令行界面(CLI)用于与ZooKeeper集合进行交互以进行开发.它有助于调试和解决不同的选项.要执行ZooKeeper CLI操作,首先打开ZooKeeper服务器(“bin/z ...
- zookeeper图形化的客户端工具
追加一个zookeeper图形化的客户端工具: 1.zookeeper图像化客户端工具的下载地址:https://issues.apache.org/jira/secure/attachment/12 ...
- zookeeper系列(一)zookeeper图形化的客户端工具
追加一个zookeeper图形化的客户端工具: 1.zookeeper图像化客户端工具的下载地址:https://issues.apache.org/jira/secure/attachment/12 ...
- jQuery之防止冒泡事件,冒泡事件就是点击子节点,会向上触发父节点,祖先节点的点击事件。
冒泡事件就是点击子节点,会向上触发父节点,祖先节点的点击事件. 下面是html代码部分: <body> <div id="content"> 外层div元素 ...
随机推荐
- jq的$.each遍历数组
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...
- Apple Tree POJ - 3321 dfs序列构造树状数组(好题)
There is an apple tree outside of kaka's house. Every autumn, a lot of apples will grow in the tree. ...
- Leetcode 380. 常数时间插入、删除和获取随机元素
1.题目描述 设计一个支持在平均 时间复杂度 O(1) 下,执行以下操作的数据结构. insert(val):当元素 val 不存在时,向集合中插入该项. remove(val):元素 val 存在时 ...
- 论C++11 中vector的N种遍历方法
随着C++11标准的出现,C++标准添加了许多有用的特性,C++代码的写法也有比较多的变化. vector是经常要使用到的std组件,对于vector的遍历,本文罗列了若干种写法. (注:本文中代码为 ...
- Debug模式下加载文件,运行程序异常的慢
今天在进行单元测试的时候,debug模式下加载速度很慢,但是run模式下速度很快. 原因:在debug模式下,断点位置不当,解决办法 移除编译器中的所有断点.
- cookie与session的区别与应用
通常我们所说的浏览器自动保存密码,下次不用登陆,提示一次就不再出现的内容,大部分通过cookie或者session来实现的. cookie的概念 cookie是浏览器(User Agent)访问一些网 ...
- 洛谷P2868 [USACO07DEC]观光奶牛 Sightseeing Cows
题目描述 Farmer John has decided to reward his cows for their hard work by taking them on a tour of the ...
- 使用.net core abp framework
abp是一个有用的框架,包含许多功能,可以用来作为脚手架. 直接在官方网站上输入相应的工程名称,选择对应的版本就会下载对应的版本..net core 版本的可以使用后端框架部分来做api,包含了常用框 ...
- 超详细的Java面试题总结(二)之Java基础知识篇
多线程和Java虚拟机 创建线程有几种不同的方式?你喜欢哪一种?为什么? 继承Thread类 实现Runnable接口 应用程序可以使用Executor框架来创建线程池 实现Callable接口. 我 ...
- list互转datatable 支持Nullable转换
/// <summary> /// list转datatable /// </summary> /// <param name="list">& ...