如何使用Curator监听zookeeper事件变化

摘要:
掌握zookeeper事件监听机制,非常重要,可以说是跨入了进阶的门槛,只有掌握了如何监听某个节点或路径,我们才能在节点变化后,做一些我们想做的事,包括:1,配置文件同步2,主从切换3,分布式队列4,分布式锁5,其他本篇我们就来看下,如何使用curator来完成监听,代码如下:1packagecom.qin.curator.zk;23importjavax.sound.midi.Patch;45i

掌握zookeeper事件监听机制,非常重要,可以说是跨入了进阶的门槛,只有掌握了如何监听某个节点或路径,我们才能在节点变化后,做一些我们想做的事,包括:
1,配置文件同步
2,主从切换
3,分布式队列
4,分布式锁
5,其他
本篇我们就来看下,如何使用curator来完成监听,代码如下:

1 packagecom.qin.curator.zk;
2 
3 importjavax.sound.midi.Patch;
4 
5 importorg.apache.curator.RetryPolicy;
6 importorg.apache.curator.framework.CuratorFramework;
7 importorg.apache.curator.framework.CuratorFrameworkFactory;
8 importorg.apache.curator.framework.CuratorFrameworkFactory.Builder;
9 importorg.apache.curator.framework.api.CuratorWatcher;
10 importorg.apache.curator.framework.recipes.cache.NodeCache;
11 importorg.apache.curator.framework.recipes.cache.NodeCacheListener;
12 importorg.apache.curator.framework.recipes.cache.PathChildrenCache;
13 importorg.apache.curator.framework.recipes.cache.PathChildrenCacheEvent;
14 importorg.apache.curator.framework.recipes.cache.PathChildrenCacheListener;
15 importorg.apache.curator.retry.ExponentialBackoffRetry;
16 importorg.apache.curator.utils.ZKPaths;
17 importorg.apache.zookeeper.WatchedEvent;
18 /**
19 * 
20 * 使用curator监听zookeeper节点
21 * @authorqindongliang
22 * **/
23 public classCuratorWatch {
24     
25     static CuratorFramework zkclient=null;
26     static String nameSpace="php";
27     static{
28         
29           String zkhost="192.168.46.22:2181";//zk的host
30           RetryPolicy rp=new ExponentialBackoffRetry(1000, 3);//重试机制
31           Builder builder =CuratorFrameworkFactory.builder().connectString(zkhost)
32                   .connectionTimeoutMs(5000)
33                   .sessionTimeoutMs(5000)
34 .retryPolicy(rp);
35 builder.namespace(nameSpace);
36           CuratorFramework zclient =builder.build();
37           zkclient=zclient;
38           zkclient.start();//放在这前面执行
39 zkclient.newNamespaceAwareEnsurePath(nameSpace);
40           
41 }
42     
43     
44     public static void main(String[] args) throwsException{
45         
46         
47 watch();
48 Thread.sleep(Long.MAX_VALUE);
49         
50 }
51     
52     
53     /**
54 * 
55 * 监听节点变化
56 * 
57 * */
58     public static void watch()throwsException{
59         PathChildrenCache cache = new PathChildrenCache(zkclient, "/zk", false);
60 cache.start();
61         
62         System.out.println("监听开始/zk........");
63         PathChildrenCacheListener plis=newPathChildrenCacheListener() {
64             
65 @Override
66             public voidchildEvent(CuratorFramework client, PathChildrenCacheEvent event)
67                     throwsException {
68                   switch( event.getType() )
69 {
70                         caseCHILD_ADDED:
71 {
72                             System.out.println("Node added: " +ZKPaths.getNodeFromPath(event.getData().getPath()));
73                             break;
74 }
75                         
76                         
77                         caseCHILD_UPDATED:
78 {
79                             System.out.println("Node changed: " +ZKPaths.getNodeFromPath(event.getData().getPath()));
80                             break;
81 }
82 
83                         caseCHILD_REMOVED:
84 {
85                             System.out.println("Node removed: " +ZKPaths.getNodeFromPath(event.getData().getPath()));
86                             break;
87 }
88 }
89                 
90                 
91 }
92 };
93         //注册监听
94 cache.getListenable().addListener(plis);
95        
96 }
97     
98     
99     
100 
101 }

运行后的控制台打印:

1 18:33:07.722 [main] INFO  o.a.c.f.imps.CuratorFrameworkImpl -Starting
2 18:33:07.727 [main] DEBUG o.a.curator.CuratorZookeeperClient -Starting
3 18:33:07.727 [main] DEBUG org.apache.curator.ConnectionState -Starting
4 18:33:07.727 [main] DEBUG org.apache.curator.ConnectionState -reset
5 18:33:07.734 [main] INFO  org.apache.zookeeper.ZooKeeper - Client environment:zookeeper.version=3.4.6-1569965, built on 02/20/2014 09:09GMT
6 18:33:07.734 [main] INFO  org.apache.zookeeper.ZooKeeper - Client environment:host.name=QINDONGLIANG.dhgatecn.msf
7 18:33:07.734 [main] INFO  org.apache.zookeeper.ZooKeeper - Client environment:java.version=1.7.0_04
8 18:33:07.734 [main] INFO  org.apache.zookeeper.ZooKeeper - Client environment:java.vendor=Oracle Corporation
9 18:33:07.734 [main] INFO  org.apache.zookeeper.ZooKeeper - Client environment:java.home=D:Javajdk1.7.0_04jre
10 18:33:07.734 [main] INFO  org.apache.zookeeper.ZooKeeper - Client environment:java.class.path=D:eclipseworkspace2ywopzkin;D:eclipseworkspace2ywopzklibcurator-client-2.6.0.jar;D:eclipseworkspace2ywopzklibcurator-examples-2.6.0.jar;D:eclipseworkspace2ywopzklibcurator-framework-2.6.0.jar;D:eclipseworkspace2ywopzklibcurator-recipes-2.6.0.jar;D:eclipseworkspace2ywopzklibcurator-test-2.6.0.jar;D:eclipseworkspace2ywopzklibcurator-x-discovery-2.6.0.jar;D:eclipseworkspace2ywopzklibcurator-x-discovery-server-2.6.0.jar;D:eclipseworkspace2ywopzklibcurator-x-rpc-2.6.0.jar;D:eclipseworkspace2ywopzkliblog4j-1.2.15.jar;D:eclipseworkspace2ywopzklibzookeeper-3.4.5.jar;D:eclipseworkspace2ywopzklibcommons-io-2.1.jar
11 18:33:07.734 [main] INFO  org.apache.zookeeper.ZooKeeper - Client environment:java.library.path=D:Javajdk1.7.0_04in;C:WindowsSunJavain;C:Windowssystem32;C:Windows;D:/Java/jdk1.7.0_04/bin/../jre/bin/server;D:/Java/jdk1.7.0_04/bin/../jre/bin;D:/Java/jdk1.7.0_04/bin/../jre/lib/amd64;C:Program FilesCommon FilesMicrosoft SharedWindows Live;C:Program Files (x86)Common FilesMicrosoft SharedWindows Live;D:Javajdk1.7.0_04in;D:Javajdk1.7.0_04jrein;D:apache-ant-1.9.3in;C:Windowssystem32;C:Windows;C:WindowsSystem32Wbem;C:WindowsSystem32WindowsPowerShellv1.0;C:Program Files (x86)LenovoLenovo Home;C:Program Files (x86)Windows LiveShared;C:Program FilesTortoiseSVNin;D:hadoop-2.2.0/bin;C:MyProgramFilesapache-maven-3.0.5in;D:python;E:eclipse;;.
12 18:33:07.734 [main] INFO  org.apache.zookeeper.ZooKeeper - Client environment:java.io.tmpdir=C:UsersQINDON~1AppDataLocalTemp
13 18:33:07.734 [main] INFO  org.apache.zookeeper.ZooKeeper - Client environment:java.compiler=<NA>
14 18:33:07.734 [main] INFO  org.apache.zookeeper.ZooKeeper - Client environment:os.name=Windows 7
15 18:33:07.734 [main] INFO  org.apache.zookeeper.ZooKeeper - Client environment:os.arch=amd64
16 18:33:07.735 [main] INFO  org.apache.zookeeper.ZooKeeper - Client environment:os.version=6.1
17 18:33:07.735 [main] INFO  org.apache.zookeeper.ZooKeeper - Client environment:user.name=qindongliang
18 18:33:07.735 [main] INFO  org.apache.zookeeper.ZooKeeper - Client environment:user.home=C:Usersqindongliang
19 18:33:07.735 [main] INFO  org.apache.zookeeper.ZooKeeper - Client environment:user.dir=D:eclipseworkspace2ywopzk
20 18:33:07.735 [main] INFO  org.apache.zookeeper.ZooKeeper - Initiating client connection, connectString=192.168.46.22:2181 sessionTimeout=5000 watcher=org.apache.curator.ConnectionState@2c351b05
21 18:33:07.738 [main] DEBUG org.apache.zookeeper.ClientCnxn - zookeeper.disableAutoWatchReset is false
22 18:33:07.760 [main-SendThread(192.168.46.22:2181)] INFO  org.apache.zookeeper.ClientCnxn - Opening socket connection to server 192.168.46.22/192.168.46.22:2181. Will not attempt to authenticate using SASL (unknown error)
23 18:33:07.761 [main-SendThread(192.168.46.22:2181)] INFO  org.apache.zookeeper.ClientCnxn - Socket connection established to 192.168.46.22/192.168.46.22:2181, initiating session
24 18:33:07.762 [main-SendThread(192.168.46.22:2181)] DEBUG org.apache.zookeeper.ClientCnxn - Session establishment request sent on 192.168.46.22/192.168.46.22:2181
25 18:33:07.766 [main-SendThread(192.168.46.22:2181)] INFO  org.apache.zookeeper.ClientCnxn - Session establishment complete on server 192.168.46.22/192.168.46.22:2181, sessionid = 0x148ac15236f0046, negotiated timeout = 5000
26 18:33:07.771 [main-EventThread] INFO  o.a.c.f.state.ConnectionStateManager -State change: CONNECTED
27 18:33:08.784 [main-SendThread(192.168.46.22:2181)] DEBUG org.apache.zookeeper.ClientCnxn - Reading reply sessionid:0x148ac15236f0046, packet:: clientPath:null serverPath:null finished:false header:: 1,3  replyHeader:: 1,233,0  request:: '/php,F  response:: s{117,117,1411712541330,1411712541330,0,5,0,0,0,3,204} 
28 18:33:08.784 [main-SendThread(192.168.46.22:2181)] DEBUG org.apache.zookeeper.ClientCnxn - Got ping response for sessionid: 0x148ac15236f0046after 4ms
29 监听开始/zk........
30 18:33:08.795 [main-SendThread(192.168.46.22:2181)] DEBUG org.apache.zookeeper.ClientCnxn - Reading reply sessionid:0x148ac15236f0046, packet:: clientPath:null serverPath:null finished:false header:: 2,3  replyHeader:: 2,233,0  request:: '/php,F  response:: s{117,117,1411712541330,1411712541330,0,5,0,0,0,3,204} 
31 18:33:08.797 [main-SendThread(192.168.46.22:2181)] DEBUG org.apache.zookeeper.ClientCnxn - Reading reply sessionid:0x148ac15236f0046, packet:: clientPath:null serverPath:null finished:false header:: 3,3  replyHeader:: 3,233,0  request:: '/php/zk,F  response:: s{151,182,1411714954448,1411716422668,2,5,0,0,9,3,228} 
32 18:33:08.804 [main-SendThread(192.168.46.22:2181)] DEBUG org.apache.zookeeper.ClientCnxn - Reading reply sessionid:0x148ac15236f0046, packet:: clientPath:/php/zk serverPath:/php/zk finished:false header:: 4,12  replyHeader:: 4,233,0  request:: '/php/zk,T  response:: v{'bb,'cc,'cc334},s{151,182,1411714954448,1411716422668,2,5,0,0,9,3,228} 
33 18:33:08.815 [main-SendThread(192.168.46.22:2181)] DEBUG org.apache.zookeeper.ClientCnxn - Reading reply sessionid:0x148ac15236f0046, packet:: clientPath:/php/zk/bb serverPath:/php/zk/bb finished:false header:: 5,4  replyHeader:: 5,233,0  request:: '/php/zk/bb,T  response:: #6920616d2061207a6b20636f6e74656e742031,s{152,156,1411714954452,1411714982893,2,0,0,0,19,0,152} 
34 18:33:08.816 [main-SendThread(192.168.46.22:2181)] DEBUG org.apache.zookeeper.ClientCnxn - Reading reply sessionid:0x148ac15236f0046, packet:: clientPath:/php/zk/cc serverPath:/php/zk/cc finished:false header:: 6,4  replyHeader:: 6,233,0  request:: '/php/zk/cc,T  response:: #63313163,s{185,222,1411716737805,1411725726750,3,0,0,0,4,0,185} 
35 18:33:08.817 [main-SendThread(192.168.46.22:2181)] DEBUG org.apache.zookeeper.ClientCnxn - Reading reply sessionid:0x148ac15236f0046, packet:: clientPath:/php/zk/cc334 serverPath:/php/zk/cc334 finished:false header:: 7,4  replyHeader:: 7,233,0  request:: '/php/zk/cc334,T  response:: #636363,s{189,226,1411716744253,1411725775109,2,3,0,0,3,3,220} 
36 Node added: bb
37 Node added: cc
38 Node added: cc334
39 18:33:10.482 [main-SendThread(192.168.46.22:2181)] DEBUG org.apache.zookeeper.ClientCnxn - Got ping response for sessionid: 0x148ac15236f0046after 1ms
40 18:33:12.148 [main-SendThread(192.168.46.22:2181)] DEBUG org.apache.zookeeper.ClientCnxn - Got ping response for sessionid: 0x148ac15236f0046 after 1ms

免责声明:文章转载自《如何使用Curator监听zookeeper事件变化》仅用于学习参考。如对内容有疑问,请及时联系本站处理。

上篇LOGON rejected【转】.net学习笔记---xml序列化下篇

宿迁高防,2C2G15M,22元/月;香港BGP,2C5G5M,25元/月 雨云优惠码:MjYwNzM=

相关文章

linux php安装扩展方法 查找配置文件

如何在linux中查看nginx、apache、php、mysql配置文件路径了,如果你接收一个别人配置过的环境,但没留下相关文档。这时该怎么判断找到正确的加载文件路径了。可以通过以下来判断 1、判断apache 首先执行命令找到httpd路径 ps aux | grep httpd 如httpd路径为 /usr/local/apache/bin/http...

htpasswd用法

apache htpasswd命令用法   htpasswd [-cmdpsD] passwordfile username   htpasswd -b[cmdpsD] passwordfile username password   htpasswd -n[mdps] username   htpasswd -nb[mdps] username pass...

将多个jar包重新打包成一个jar包

我介绍的方法是使用java命令来操作的,所以首先的安装jdk,这个就自己搞定吧. 提取jar包 为了将多个jar包打包成一个jar包,首先要将每个jar包的内容提取出来放到一个文件夹下,具体的操作命令就是使用jar xf jar的文件名 比如我的jar放到桌面的一个新建文件夹(2)下,那么就通过cmd进入到这个目录然后输入上面的命令即可 重新打包jar包...

基于kubernetes实现链路监控

介绍 官方文档:https://skywalking.apache.org/docs/main/latest/readme/ chart包地址:https://github.com/apache/skywalking-kubernetes 实践 Install released version using Helm repository 下载cha...

Java 9 揭秘(19. 平台和JVM日志)

Tips做一个终身学习的人。 在这章中,主要介绍以下内容: 新的平台日志(logging)API JVM日志的命令行选项 JDK 9已经对平台类(JDK类)和JVM组件的日志系统进行了大整。 有一个新的API可以指定所选择的日志框架作为从平台类记录消息的日志后端。 还有一个新的命令行选项,可以从所有JVM组件访问消息。 在本章中,详细介绍两个记录工具...

Docker——Tomcat JVM 内存配置

前言 安装再docker中的tomcat,在下载大文件或者某些情况下,会出现tomcat的内存溢出等情况,所以需要配置tomcat的内存大小,docker中的tomcat内存大小配置有四种方式。 一、修改catalina.sh 加入JVM: JAVA_OPTS="-server -Dfile.encoding=UTF-8 -Xms4g -Xmx4g -Xm...