掌握zookeeper事件监听机制,非常重要,可以说是跨入了进阶的门槛,只有掌握了如何监听某个节点或路径,我们才能在节点变化后,做一些我们想做的事,包括:
1,配置文件同步
2,主从切换
3,分布式队列
4,分布式锁
5,其他
散仙,在以前的文章里面有写过使用zookeeper原生的api,监听zk节点变化,那么本篇我们就来看下,如何使用curator来完成监听,代码如下:
- package com.qin.curator.zk;
- import javax.sound.midi.Patch;
- import org.apache.curator.RetryPolicy;
- import org.apache.curator.framework.CuratorFramework;
- import org.apache.curator.framework.CuratorFrameworkFactory;
- import org.apache.curator.framework.CuratorFrameworkFactory.Builder;
- import org.apache.curator.framework.api.CuratorWatcher;
- import org.apache.curator.framework.recipes.cache.NodeCache;
- import org.apache.curator.framework.recipes.cache.NodeCacheListener;
- import org.apache.curator.framework.recipes.cache.PathChildrenCache;
- import org.apache.curator.framework.recipes.cache.PathChildrenCacheEvent;
- import org.apache.curator.framework.recipes.cache.PathChildrenCacheListener;
- import org.apache.curator.retry.ExponentialBackoffRetry;
- import org.apache.curator.utils.ZKPaths;
- import org.apache.zookeeper.WatchedEvent;
- /**
- *
- * 使用curator监听zookeeper节点
- * @author qindongliang
- * **/
- public class CuratorWatch {
- static CuratorFramework zkclient=null;
- static String nameSpace="php";
- static {
- String zkhost="192.168.46.22:2181";//zk的host
- RetryPolicy rp=new ExponentialBackoffRetry(1000, 3);//重试机制
- Builder builder = CuratorFrameworkFactory.builder().connectString(zkhost)
- .connectionTimeoutMs(5000)
- .sessionTimeoutMs(5000)
- .retryPolicy(rp);
- builder.namespace(nameSpace);
- CuratorFramework zclient = builder.build();
- zkclient=zclient;
- zkclient.start();// 放在这前面执行
- zkclient.newNamespaceAwareEnsurePath(nameSpace);
- }
- public static void main(String[] args) throws Exception{
- watch();
- Thread.sleep(Long.MAX_VALUE);
- }
- /**
- *
- * 监听节点变化
- *
- * */
- public static void watch()throws Exception{
- PathChildrenCache cache = new PathChildrenCache(zkclient, "/zk", false);
- cache.start();
- System.out.println("监听开始/zk........");
- PathChildrenCacheListener plis=new PathChildrenCacheListener() {
- @Override
- public void childEvent(CuratorFramework client, PathChildrenCacheEvent event)
- throws Exception {
- switch ( event.getType() )
- {
- case CHILD_ADDED:
- {
- System.out.println("Node added: " + ZKPaths.getNodeFromPath(event.getData().getPath()));
- break;
- }
- case CHILD_UPDATED:
- {
- System.out.println("Node changed: " + ZKPaths.getNodeFromPath(event.getData().getPath()));
- break;
- }
- case CHILD_REMOVED:
- {
- System.out.println("Node removed: " + ZKPaths.getNodeFromPath(event.getData().getPath()));
- break;
- }
- }
- }
- };
- //注册监听
- cache.getListenable().addListener(plis);
- }
- }
package com.qin.curator.zk; import javax.sound.midi.Patch; import org.apache.curator.RetryPolicy; import org.apache.curator.framework.CuratorFramework; import org.apache.curator.framework.CuratorFrameworkFactory; import org.apache.curator.framework.CuratorFrameworkFactory.Builder; import org.apache.curator.framework.api.CuratorWatcher; import org.apache.curator.framework.recipes.cache.NodeCache; import org.apache.curator.framework.recipes.cache.NodeCacheListener; import org.apache.curator.framework.recipes.cache.PathChildrenCache; import org.apache.curator.framework.recipes.cache.PathChildrenCacheEvent; import org.apache.curator.framework.recipes.cache.PathChildrenCacheListener; import org.apache.curator.retry.ExponentialBackoffRetry; import org.apache.curator.utils.ZKPaths; import org.apache.zookeeper.WatchedEvent; /** * * 使用curator监听zookeeper节点 * @author qindongliang * **/ public class CuratorWatch { static CuratorFramework zkclient=null; static String nameSpace="php"; static { String zkhost="192.168.46.22:2181";//zk的host RetryPolicy rp=new ExponentialBackoffRetry(1000, 3);//重试机制 Builder builder = CuratorFrameworkFactory.builder().connectString(zkhost) .connectionTimeoutMs(5000) .sessionTimeoutMs(5000) .retryPolicy(rp); builder.namespace(nameSpace); CuratorFramework zclient = builder.build(); zkclient=zclient; zkclient.start();// 放在这前面执行 zkclient.newNamespaceAwareEnsurePath(nameSpace); } public static void main(String[] args) throws Exception{ watch(); Thread.sleep(Long.MAX_VALUE); } /** * * 监听节点变化 * * */ public static void watch()throws Exception{ PathChildrenCache cache = new PathChildrenCache(zkclient, "/zk", false); cache.start(); System.out.println("监听开始/zk........"); PathChildrenCacheListener plis=new PathChildrenCacheListener() { @Override public void childEvent(CuratorFramework client, PathChildrenCacheEvent event) throws Exception { switch ( event.getType() ) { case CHILD_ADDED: { System.out.println("Node added: " + ZKPaths.getNodeFromPath(event.getData().getPath())); break; } case CHILD_UPDATED: { System.out.println("Node changed: " + ZKPaths.getNodeFromPath(event.getData().getPath())); break; } case CHILD_REMOVED: { System.out.println("Node removed: " + ZKPaths.getNodeFromPath(event.getData().getPath())); break; } } } }; //注册监听 cache.getListenable().addListener(plis); } }
运行后的控制台打印:
- 18:33:07.722 [main] INFO o.a.c.f.imps.CuratorFrameworkImpl - Starting
- 18:33:07.727 [main] DEBUG o.a.curator.CuratorZookeeperClient - Starting
- 18:33:07.727 [main] DEBUG org.apache.curator.ConnectionState - Starting
- 18:33:07.727 [main] DEBUG org.apache.curator.ConnectionState - reset
- 18:33:07.734 [main] INFO org.apache.zookeeper.ZooKeeper - Client environment:zookeeper.version=3.4.6-1569965, built on 02/20/2014 09:09 GMT
- 18:33:07.734 [main] INFO org.apache.zookeeper.ZooKeeper - Client environment:host.name=QINDONGLIANG.dhgatecn.msf
- 18:33:07.734 [main] INFO org.apache.zookeeper.ZooKeeper - Client environment:java.version=1.7.0_04
- 18:33:07.734 [main] INFO org.apache.zookeeper.ZooKeeper - Client environment:java.vendor=Oracle Corporation
- 18:33:07.734 [main] INFO org.apache.zookeeper.ZooKeeper - Client environment:java.home=D:\Java\jdk1.7.0_04\jre
- 18:33:07.734 [main] INFO org.apache.zookeeper.ZooKeeper - Client environment: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
- 18:33:07.734 [main] INFO org.apache.zookeeper.ZooKeeper - Client environment:java.library.path=D:\Java\jdk1.7.0_04\bin;C:\Windows\Sun\Java\bin;C:\Windows\system32;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 Files\Common Files\Microsoft Shared\Windows Live;C:\Program Files (x86)\Common Files\Microsoft Shared\Windows Live;D:\Java\jdk1.7.0_04\bin;D:\Java\jdk1.7.0_04\jre\bin;D:\apache-ant-1.9.3\bin;C:\Windows\system32;C:\Windows;C:\Windows\System32\Wbem;C:\Windows\System32\WindowsPowerShell\v1.0\;C:\Program Files (x86)\Lenovo\Lenovo Home\;C:\Program Files (x86)\Windows Live\Shared;C:\Program Files\TortoiseSVN\bin;D:\hadoop-2.2.0/bin;C:\MyProgramFiles\apache-maven-3.0.5\bin;D:\python;E:\eclipse;;.
- 18:33:07.734 [main] INFO org.apache.zookeeper.ZooKeeper - Client environment:java.io.tmpdir=C:\Users\QINDON~1\AppData\Local\Temp\
- 18:33:07.734 [main] INFO org.apache.zookeeper.ZooKeeper - Client environment:java.compiler=<NA>
- 18:33:07.734 [main] INFO org.apache.zookeeper.ZooKeeper - Client environment:os.name=Windows 7
- 18:33:07.734 [main] INFO org.apache.zookeeper.ZooKeeper - Client environment:os.arch=amd64
- 18:33:07.735 [main] INFO org.apache.zookeeper.ZooKeeper - Client environment:os.version=6.1
- 18:33:07.735 [main] INFO org.apache.zookeeper.ZooKeeper - Client environment:user.name=qindongliang
- 18:33:07.735 [main] INFO org.apache.zookeeper.ZooKeeper - Client environment:user.home=C:\Users\qindongliang
- 18:33:07.735 [main] INFO org.apache.zookeeper.ZooKeeper - Client environment:user.dir=D:\eclipseworkspace2yw\opzk
- 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
- 18:33:07.738 [main] DEBUG org.apache.zookeeper.ClientCnxn - zookeeper.disableAutoWatchReset is false
- 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)
- 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
- 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
- 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
- 18:33:07.771 [main-EventThread] INFO o.a.c.f.state.ConnectionStateManager - State change: CONNECTED
- 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}
- 18:33:08.784 [main-SendThread(192.168.46.22:2181)] DEBUG org.apache.zookeeper.ClientCnxn - Got ping response for sessionid: 0x148ac15236f0046 after 4ms
- 监听开始/zk........
- 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}
- 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}
- 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}
- 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}
- 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}
- 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}
- Node added: bb
- Node added: cc
- Node added: cc334
- 18:33:10.482 [main-SendThread(192.168.46.22:2181)] DEBUG org.apache.zookeeper.ClientCnxn - Got ping response for sessionid: 0x148ac15236f0046 after 1ms
- 18:33:12.148 [main-SendThread(192.168.46.22:2181)] DEBUG org.apache.zookeeper.ClientCnxn - Got ping response for sessionid: 0x148ac15236f0046 after 1ms
18:33:07.722 [main] INFO o.a.c.f.imps.CuratorFrameworkImpl - Starting 18:33:07.727 [main] DEBUG o.a.curator.CuratorZookeeperClient - Starting 18:33:07.727 [main] DEBUG org.apache.curator.ConnectionState - Starting 18:33:07.727 [main] DEBUG org.apache.curator.ConnectionState - reset 18:33:07.734 [main] INFO org.apache.zookeeper.ZooKeeper - Client environment:zookeeper.version=3.4.6-1569965, built on 02/20/2014 09:09 GMT 18:33:07.734 [main] INFO org.apache.zookeeper.ZooKeeper - Client environment:host.name=QINDONGLIANG.dhgatecn.msf 18:33:07.734 [main] INFO org.apache.zookeeper.ZooKeeper - Client environment:java.version=1.7.0_04 18:33:07.734 [main] INFO org.apache.zookeeper.ZooKeeper - Client environment:java.vendor=Oracle Corporation 18:33:07.734 [main] INFO org.apache.zookeeper.ZooKeeper - Client environment:java.home=D:\Java\jdk1.7.0_04\jre 18:33:07.734 [main] INFO org.apache.zookeeper.ZooKeeper - Client environment: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 18:33:07.734 [main] INFO org.apache.zookeeper.ZooKeeper - Client environment:java.library.path=D:\Java\jdk1.7.0_04\bin;C:\Windows\Sun\Java\bin;C:\Windows\system32;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 Files\Common Files\Microsoft Shared\Windows Live;C:\Program Files (x86)\Common Files\Microsoft Shared\Windows Live;D:\Java\jdk1.7.0_04\bin;D:\Java\jdk1.7.0_04\jre\bin;D:\apache-ant-1.9.3\bin;C:\Windows\system32;C:\Windows;C:\Windows\System32\Wbem;C:\Windows\System32\WindowsPowerShell\v1.0\;C:\Program Files (x86)\Lenovo\Lenovo Home\;C:\Program Files (x86)\Windows Live\Shared;C:\Program Files\TortoiseSVN\bin;D:\hadoop-2.2.0/bin;C:\MyProgramFiles\apache-maven-3.0.5\bin;D:\python;E:\eclipse;;. 18:33:07.734 [main] INFO org.apache.zookeeper.ZooKeeper - Client environment:java.io.tmpdir=C:\Users\QINDON~1\AppData\Local\Temp\ 18:33:07.734 [main] INFO org.apache.zookeeper.ZooKeeper - Client environment:java.compiler=<NA> 18:33:07.734 [main] INFO org.apache.zookeeper.ZooKeeper - Client environment:os.name=Windows 7 18:33:07.734 [main] INFO org.apache.zookeeper.ZooKeeper - Client environment:os.arch=amd64 18:33:07.735 [main] INFO org.apache.zookeeper.ZooKeeper - Client environment:os.version=6.1 18:33:07.735 [main] INFO org.apache.zookeeper.ZooKeeper - Client environment:user.name=qindongliang 18:33:07.735 [main] INFO org.apache.zookeeper.ZooKeeper - Client environment:user.home=C:\Users\qindongliang 18:33:07.735 [main] INFO org.apache.zookeeper.ZooKeeper - Client environment:user.dir=D:\eclipseworkspace2yw\opzk 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 18:33:07.738 [main] DEBUG org.apache.zookeeper.ClientCnxn - zookeeper.disableAutoWatchReset is false 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) 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 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 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 18:33:07.771 [main-EventThread] INFO o.a.c.f.state.ConnectionStateManager - State change: CONNECTED 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} 18:33:08.784 [main-SendThread(192.168.46.22:2181)] DEBUG org.apache.zookeeper.ClientCnxn - Got ping response for sessionid: 0x148ac15236f0046 after 4ms 监听开始/zk........ 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} 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} 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} 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} 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} 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} Node added: bb Node added: cc Node added: cc334 18:33:10.482 [main-SendThread(192.168.46.22:2181)] DEBUG org.apache.zookeeper.ClientCnxn - Got ping response for sessionid: 0x148ac15236f0046 after 1ms 18:33:12.148 [main-SendThread(192.168.46.22:2181)] DEBUG org.apache.zookeeper.ClientCnxn - Got ping response for sessionid: 0x148ac15236f0046 after 1ms
用到的jar包如下所示:
相关推荐
zookeeper 事件监听机制 zookeeper 集群搭建 一致性协议:zab协议 zookeeper的leader选举 observer角色及其配置 zookeeperAPI连接集群 zookeeper 开源客户端curator介绍 zookeeper四字监控命令 zookeeper图形化的...
三、Curator 监听机制 Curator 提供了事件监听器,可以注册到 ZooKeeper 路径上,当该路径下发生创建、删除、数据改变等事件时,监听器会被触发。这在分布式系统中非常有用,例如监控配置变化、节点状态变更等。 ...
### ZooKeeper会话超时及重连机制详解 #### 一、ZooKeeper会话超时机制 在分布式系统中,ZooKeeper作为一款流行的协调服务框架,为开发者提供了高效且可靠的协调服务。其中,会话管理是ZooKeeper的重要组成部分之...
本文将会深入解析Zookeeper监听器的工作原理,以及如何利用监听器来监控数据的变化。 首先,Zookeeper的监听器工作依赖于客户端与服务端之间的一种特定交互模式。当客户端应用程序需要监听某个数据节点的时候,它会...
ZooKeeper 的监听机制是基于事件驱动的,例如当节点发生变化时,Watcher 会触发相应的事件。 在实际开发中,需要根据实际情况选择合适的 Java 客户端 API,例如 ZooKeeper 官方的 Java 客户端 API 或者第三方的 ...
2. 考虑到Zookeeper的特性,如watch机制,以实现配置变更的实时监听。 3. 在实现过程中,应确保与现有Viper功能的兼容性,避免引入新的错误或冲突。 4. 编写单元测试以验证新功能的正确性和稳定性。 5. 最后,如果你...
3. **事件通知**:Zookeeper提供watcher机制,允许客户端设置监听某个节点的变化,当节点状态发生变化时,Zookeeper会立即通知相关客户端,实现快速响应。 4. **会话管理**:Zookeeper维护客户端的会话状态,如果...
2. MySQL binlog监听器:解析binlog事件并将其转换为Zookeeper操作。 3. 数据同步逻辑:处理Zookeeper通知,根据Znode的变化执行相应的binlog应用操作。 4. 故障检测和恢复机制:监控主库状态,异常时触发主备切换。...
3. **Watcher**:ZooKeeper提供了一种事件监听机制,称为Watcher,允许客户端注册对特定ZNode的兴趣,当ZNode状态发生变化时,ZooKeeper会通知相应的客户端。 **ZooKeeper的工作原理** 1. **主备选举(Leader ...
2. **Watcher**: Watcher是Zookeeper的事件监听机制,可以设置对特定Znode的监听,当该节点状态发生变化时,Watcher会接收到通知。 3. **会话(Session)**: 用户与Zookeeper服务器之间的连接称为会话,会话期间,...
第6章 Zookeeper 2 6.1. Zookeeper入门 2 6.1.1. 概述 2 6.1.2. 特点 3 ...6.5.1. 请简述ZooKeeper的选举机制 37 6.5.2. ZooKeeper的监听原理是什么 37 6.5.3. ZooKeeper的部署方式有哪几种?集群中的角
Watcher是一种事件监听机制,允许客户端注册对特定Znode的变更事件感兴趣,一旦发生改变,ZooKeeper会主动通知客户端。 在分布式一致性方面,ZooKeeper采用了类似Paxos的Zab协议(ZooKeeper Atomic Broadcast)。...
此外,Spring Framework的事件驱动模型也能与ZooKeeper的监听机制相结合。通过监听ZooKeeper上的节点变化,我们可以实现对服务状态变化的响应,例如,当新主节点选举成功后,可以触发相应的回调函数,更新服务列表...
- **Watcher 事件监听**:Zookeeper 支持 Watcher 机制,允许客户端注册监听特定节点的变化,当节点发生变化时,Zookeeper 会触发回调通知客户端。 ### 2. ZooKeeper 的功能 - **命名服务**:为分布式组件提供唯一...
# 基于ZooKeeper的分布式服务管理系统 ## 项目简介 ...4. 商品秒杀系统结合分布式锁和ZooKeeper的监听机制,实现高并发的商品秒杀功能,确保库存的一致性和操作的原子性。 ## 安装使用步骤 1. 环境准备
Watcher 是一种事件监听机制,可以注册在 Znode 上,当 Znode 发生变化时,Watcher 将接收到通知。 在 Zookeeper-3.8.0 中,主要包含以下组件和目录: 1. bin:包含了启动和停止 Zookeeper 服务的脚本,如 zk...
3. **Watcher**:Watcher是Zookeeper的一种事件监听机制,允许客户端注册对ZNode的变更或事件的兴趣,一旦发生相应变化,Zookeeper会触发Watcher通知。 4. **原子性**:Zookeeper的所有操作都是原子性的,即一次...
4. **Zookeeper的Watcher机制**:允许客户端注册监听事件,当数据或状态发生变化时,Zookeeper会触发回调通知。 《分布式服务框架 Zookeeper -- 管理分布式环境中的数据 - 给自己一片纯净的天空 - 开源中国社区.htm...
Watch是一种一次性触发的事件监听器,当某个ZNode的数据或者状态发生变化时,所有设置了相应Watch的客户端都会收到通知。这种机制使得分布式系统中的节点能够实时感知到系统的变化,及时做出响应。 再者,Zookeeper...
Watcher是Zookeeper的核心机制之一,它是一种事件触发模型。当Zookeeper服务器端的数据状态发生变化时,会主动通知已注册的Watcher对象。这种通知是一次性的,即触发后Watcher就会被移除,如果需要持续监听,客户端...