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

 我来答
笑喘是病得抽
2017-07-23 · TA获得超过925个赞
知道大有可为答主
回答量:2140
采纳率:98%
帮助的人:2211万
展开全部
掌握zookeeper事件监听机制,非常重要,可以说是跨入了进阶的门槛,只有掌握了如何监听某个节点或路径,我们才能在节点变化后,做一些我们想做的事,包括:
1,配置文件同步
2,主从切换
3,分布式队列
4,分布式锁
5,其他

首先,在以前的文章里面有写过使用zookeeper原生的api,监听zk节点变化,那么本篇我们就来看下,如何使用curator来完成监听,代码如下:
<pre name="code" class="java">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);

}

}
</pre>

运行后的控制台打印:
<pre name="code" class="java">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
推荐律师服务: 若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询

为你推荐:

下载百度知道APP,抢鲜体验
使用百度知道APP,立即抢鲜体验。你的手机镜头里或许有别人想知道的答案。
扫描二维码下载
×

类别

我们会通过消息、邮箱等方式尽快将举报结果通知您。

说明

0/200

提交
取消

辅 助

模 式