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

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了如何使用Cruator监听zookeeper事件变化相关的知识,希望对你有一定的参考价值。

参考技术A 掌握zookeeper事件监听机制,非常重要,可以说是跨入了进阶的门槛,只有掌握了如何监听某个节点或路径,我们才能在节点变化后,做一些我们想做的事,包括:1,配置文件同步2,主从切换3,分布式队列4,分布式锁5,其他散仙,在以前的文章里面有写过使用zookeeper原生的api,监听zk节点变化,那么本篇我们就来看下,如何使用curator来完成监听,代码如下:packagecom.qin.curator.zk;importjavax.sound.midi.Patch;importorg.apache.curator.RetryPolicy;importorg.apache.curator.framework.CuratorFramework;importorg.apache.curator.framework.CuratorFrameworkFactory;importorg.apache.curator.framework.CuratorFrameworkFactory.Builder;importorg.apache.curator.framework.api.CuratorWatcher;importorg.apache.curator.framework.recipes.cache.NodeCache;importorg.apache.curator.framework.recipes.cache.NodeCacheListener;importorg.apache.curator.framework.recipes.cache.PathChildrenCache;importorg.apache.curator.framework.recipes.cache.PathChildrenCacheEvent;importorg.apache.curator.framework.recipes.cache.PathChildrenCacheListener;importorg.apache.curator.retry.ExponentialBackoffRetry;importorg.apache.curator.utils.ZKPaths;importorg.apache.zookeeper.WatchedEvent;/****使用curator监听zookeeper节点*@authorqindongliang***/publicclassCuratorWatchstaticCuratorFrameworkzkclient=null;staticStringnameSpace="php";staticStringzkhost="192.168.46.22:2181";//zk的hostRetryPolicyrp=newExponentialBackoffRetry(1000,3);//重试机制Builderbuilder=CuratorFrameworkFactory.builder().connectString(zkhost).connectionTimeoutMs(5000).sessionTimeoutMs(5000).retryPolicy(rp);builder.namespace(nameSpace);CuratorFrameworkzclient=builder.build();zkclient=zclient;zkclient.start();//放在这前面执行zkclient.newNamespaceAwareEnsurePath(nameSpace);publicstaticvoidmain(String[]args)throwsExceptionwatch();Thread.sleep(Long.MAX_VALUE);/****监听节点变化***/publicstaticvoidwatch()throwsExceptionPathChildrenCachecache=newPathChildrenCache(zkclient,"/zk",false);cache.start();System.out.println("监听开始/zk..");PathChildrenCacheListenerplis=newPathChildrenCacheListener()@OverridepublicvoidchildEvent(CuratorFrameworkclient,PathChildrenCacheEventevent)throwsExceptionswitch(event.getType())caseCHILD_ADDED:System.out.println("Nodeadded:"+ZKPaths.getNodeFromPath(event.getData().getPath()));break;caseCHILD_UPDATED:System.out.println("Nodechanged:"+ZKPaths.getNodeFromPath(event.getData().getPath()));break;caseCHILD_REMOVED:System.out.println("Noderemoved:"+ZKPaths.getNodeFromPath(event.getData().getPath()));break;;//注册监听cache.getListenable().addListener(plis);运行后的控制台打印:18:33:07.722[main]INFOo.a.c.f.imps.CuratorFrameworkImpl-Starting18:33:07.727[main]DEBUGo.a.curator.CuratorZookeeperClient-Starting18:33:07.727[main]DEBUGorg.apache.curator.ConnectionState-Starting18:33:07.727[main]DEBUGorg.apache.curator.ConnectionState-reset18:33:07.734[main]INFOorg.apache.zookeeper.ZooKeeper-Clientenvironment:zookeeper.version=3.4.6-1569965,builton02/20/201409:09GMT18:33:07.734[main]INFOorg.apache.zookeeper.ZooKeeper-Clientenvironment:host.name=QINDONGLIANG.dhgatecn.msf18:33:07.734[main]INFOorg.apache.zookeeper.ZooKeeper-Clientenvironment:java.version=1.7.0_0418:33:07.734[main]INFOorg.apache.zookeeper.ZooKeeper-Clientenvironment:java.vendor=OracleCorporation18:33:07.734[main]INFOorg.apache.zookeeper.ZooKeeper-Clientenvironment:java.home=D:\Java\jdk1.7.0_04\jre18:33:07.734[main]INFOorg.apache.zookeeper.ZooKeeper-Clientenvironment:java.class.path=D:\eclipseworkspace2yw\opzk\bin;D:\eclipseworkspace2yw\opzk\lib\curator-client-2.6.0.jar;D:\eclipseworkspace2yw\opzk\lib\curator-examples-2.6.0.jar;D:\eclipseworkspace2yw\opzk\lib\curator-framework-2.6.0.jar;D:\eclipseworkspace2yw\opzk\lib\curator-recipes-2.6.0.jar;D:\eclipseworkspace2yw\opzk\lib\curator-test-2.6.0.jar;D:\eclipseworkspace2yw\opzk\lib\curator-x-discovery-2.6.0.jar;D:\eclipseworkspace2yw\opzk\lib\curator-x-discovery-server-2.6.0.jar;D:\eclipseworkspace2yw\opzk\lib\curator-x-rpc-2.6.0.jar;D:\eclipseworkspace2yw\opzk\lib\log4j-1.2.15.jar;D:\eclipseworkspace2yw\opzk\lib\zookeeper-3.4.5.jar;D:\eclipseworkspace2yw\opzk\lib\commons-io-2.1.jar

以上是关于如何使用Cruator监听zookeeper事件变化的主要内容,如果未能解决你的问题,请参考以下文章

Zookeeper之Watcher监听事件丢失分析

Zookeeper Watcher(事件监听器)?

初学zookeeper--自定义事件监听

Zookeeper监听器原理

zookeeper源码分析-事件监听Watcher

zookeeper概念 zookeeper常用客户端指令 Curator 事件监听 zookeeper分布式锁 ZooKeeper 集群搭建