zk 09之:Curator之二:Path Cache监控zookeeper的node和path的状态
在实际应用开发中,当某个ZNode发生变化后我们需要得到通知并做一些后续处理,Curator Recipes提供了Path Cache 来帮助我们轻松实现watch ZNode。
Path Cache
Path Cache可以监控ZNode子结点的变化,例如:add,update,delete。
A Path Cache is used to watch a ZNode. Whenever a child is added, updated or removed, the Path Cache will change its state to contain the current set of children, the children’s data and the children’s state.
The cache must be started by calling start(). Call close() when you are through with the cache.
Maven依赖
<properties>
<curator.version>2.11.1</curator.version>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>
<dependencies>
<dependency>
<groupId>org.apache.zookeeper</groupId>
<artifactId>zookeeper</artifactId>
<version>3.4.8</version>
<exclusions>
<exclusion>
<artifactId>slf4j-log4j12</artifactId>
<groupId>org.slf4j</groupId>
</exclusion>
<exclusion>
<artifactId>log4j</artifactId>
<groupId>log4j</groupId>
</exclusion>
</exclusions>
</dependency> <dependency>
<groupId>org.apache.curator</groupId>
<artifactId>curator-framework</artifactId>
<version>${curator.version}</version>
</dependency>
<dependency>
<groupId>org.apache.curator</groupId>
<artifactId>curator-client</artifactId>
<version>${curator.version}</version>
<exclusions>
<exclusion>
<groupId>com.google.guava</groupId>
<artifactId>guava</artifactId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>org.apache.curator</groupId>
<artifactId>curator-recipes</artifactId>
<version>${curator.version}</version>
</dependency>
<dependency>
<groupId>com.google.guava</groupId>
<artifactId>guava</artifactId>
<version>18.0</version>
</dependency>
</dependencies>
示例代码如下:
package com.sf.zkclient; import org.apache.curator.framework.CuratorFramework;
import org.apache.curator.framework.CuratorFrameworkFactory;
import org.apache.curator.framework.recipes.cache.ChildData;
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 java.nio.charset.Charset;
import java.util.List;
import java.util.concurrent.TimeUnit; public class CuratorPathCacheDemo { private String path = "/abcde"; public static final Charset CHARSET = Charset.forName("UTF-8"); public static void main(String[] args) { try{
new CuratorPathCacheDemo().start();
} catch (Exception e){
e.printStackTrace();
} } private void start() throws Exception {
String address = "localhost:2181";
CuratorFramework client = CuratorFrameworkFactory.newClient(address, new ExponentialBackoffRetry(1000, 3));
try{
client.start(); final PathChildrenCache pathChildrenCache = new PathChildrenCache(client, path, true); pathChildrenCache.getListenable().addListener(new PathChildrenCacheListener() {
@Override
public void childEvent(CuratorFramework curatorFramework,
PathChildrenCacheEvent event) throws Exception { System.out.println("======== catch children change =======");
System.out.println("update event type:" + event.getType() +
",path:" + event.getData().getPath() + ",data:" + new String(event.getData().getData(), CHARSET)); List<ChildData> childDataList = pathChildrenCache.getCurrentData();
if (childDataList != null && childDataList.size() > 0) {
System.out.println("path all children list:");
for (ChildData childData : childDataList) {
System.out.println("path:" + childData.getPath() + "," + new String(childData.getData(), CHARSET));
}
}
}
}); pathChildrenCache.start(); //must call start(); TimeUnit.MINUTES.sleep(5); pathChildrenCache.close(); }finally {
if(client!=null)
client.close();
}
}
}
用zk客户端工具去add、update、delete znode信息时,上面的代码可以监听到相应事件:
如update /abcde/12345678 的值时,如下图:

上面的程序会监听结果:

示例~配置中心的配置被修改后,通知各个客户端
1.Zookeeper经常被我们用来做配置管理,配置的管理在分布式应用环境中很常见,例如同一个应用系统需要多台 PC Server 运行,但是它们运行的应用系统的某些配置项是相同的,如果要修改这些相同的配置项,那么就必须同时修改每台运行这个应用系统的 PC Server,这样非常麻烦而且容易出错。像这样的配置信息完全可以交给 Zookeeper 来管理,将配置信息保存在 Zookeeper 的某个目录节点中,然后将所有需要修改的应用机器监控配置信息的状态,一旦配置信息发生变化,每台应用机器就会收到 Zookeeper 的通知,然后从 Zookeeper 获取新的配置信息应用到系统中。

我们通过Curator是如何实现的呢? 那就是NodeCache,关于如何实现,后面代码给出说明。
假如我们有多个服务保存在Zookeeper的/services下,例如/services/service1,/services/service2......在service1,servce2下,保存的有服务的ip,端口等信息。如果我们需要增加服务,或者某个服务不可用了,从Zookeeper中删除了,或者我们修改了某个Service下的ip和端口值,我们有必要第一时间内收到通知,已进行相应的处理,这时候可以怎么办呢? Curator提供了一个pathChildrenCache来满足我们的需求。下面我们给出代码来说明两个的用法.
package com.sf.zkclient.cache; import org.apache.curator.framework.CuratorFramework;
import org.apache.curator.framework.CuratorFrameworkFactory;
import org.apache.curator.framework.recipes.cache.ChildData;
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.CloseableUtils;
import org.apache.curator.utils.EnsurePath; import java.util.List; public class Cache { public static PathChildrenCache pathChildrenCache(CuratorFramework client, String path, Boolean cacheData)
throws Exception {
final PathChildrenCache cached = new PathChildrenCache(client, path, cacheData);
cached.getListenable().addListener(new PathChildrenCacheListener() {
@Override
public void childEvent(CuratorFramework client, PathChildrenCacheEvent event) throws Exception {
PathChildrenCacheEvent.Type eventType = event.getType();
switch (eventType) {
case CONNECTION_RECONNECTED:
cached.rebuild();
break;
case CONNECTION_SUSPENDED:
case CONNECTION_LOST:
System.out.println("Connection error,waiting...");
break;
default:
System.out.println("PathChildrenCache changed : {path:" + event.getData().getPath() + " data:"
+ new String(event.getData().getData()) + "}");
}
}
});
return cached;
} public static NodeCache nodeCache(CuratorFramework client, String path) {
final NodeCache cache = new NodeCache(client, path);
cache.getListenable().addListener(new NodeCacheListener() {
@Override
public void nodeChanged() throws Exception {
System.out.println("NodeCache changed, data is: " + new String(cache.getCurrentData().getData()));
}
}); return cache;
} public static void main(String[] args) throws Exception {
ExponentialBackoffRetry retryPolicy = new ExponentialBackoffRetry(1000, 3); CuratorFramework client = CuratorFrameworkFactory.newClient("127.0.0.1", retryPolicy);
client.start(); EnsurePath ensurePath = client.newNamespaceAwareEnsurePath("/create/test");
ensurePath.ensure(client.getZookeeperClient()); /**
* pathChildrenCache
*/
PathChildrenCache cache = pathChildrenCache(client, "/create", true);
cache.start(PathChildrenCache.StartMode.BUILD_INITIAL_CACHE);
List<ChildData> datas = cache.getCurrentData(); for (ChildData data : datas) {
System.out.println("pathcache:{" + data.getPath() + ":" + new String(data.getData()) + "}");
} /**
* NodeCache
*/
NodeCache nodeCache = nodeCache(client, "/create/test");
nodeCache.start(true); client.setData().forPath("/create/test", "1111".getBytes()); System.out.println(new String(nodeCache.getCurrentData().getData())); Thread.sleep(10000);
CloseableUtils.closeQuietly(cache);
CloseableUtils.closeQuietly(client);
}
}
结果:
pathcache:{/create/test:}
PathChildrenCache changed : {path:/create/test data:1111}
NodeCache changed, data is: 1111
zk 09之:Curator之二:Path Cache监控zookeeper的node和path的状态的更多相关文章
- 使用Apache Curator监控Zookeeper的Node和Path的状态
1.Zookeeper经常被我们用来做配置管理,配置的管理在分布式应用环境中很常见,例如同一个应用系统需要多台 PC Server 运行,但是它们运行的应用系统的某些配置项是相同的,如果要修改这些相同 ...
- MATLAB Toolbox Path Cache is out of date and is not being used的解决
作者:朱金灿 来源:http://blog.csdn.net/clever101 使用mcc编译MATLAB\R2009a\extern\examples\compiler目录下的hello.m,编译 ...
- curator教程二——分布式锁
简介 在分布式环境下,为了防止多个服务同时修改同一个值,出现数据同步问题,通常用redis和zookeeper做分布式锁,在这里我们用zookeeper做分布式锁,并和单点环境中ReenTranL ...
- node之path模块
node之path模块 原文链接 //引用该模块 var path = require("path"); 1.路径解析,得到规范化的路径格式 对window系统,目录分隔为'', ...
- Node.js Path 模块
Node.js path 模块提供了一些用于处理文件路径的小工具,我们可以通过以下方式引入该模块: var path = require("path") 方法 序号 方法 & ...
- 安装vmware-tools遇the path "" is not valid path to the gcc binary和the path "" is not a valid path to the 3.10.0-327.e17.x86_64 kernel headers问题解决
#./vmware-install.pl踩点: 1.the path "" is not valid path to the gcc binary 2.the path " ...
- 安装vm tools时出现如下问题 The path "/usr/bin/gcc" is not valid path to the
sudo suapt-get updateapt-get dist-upgradeapt-get install open-vm-tools-desktop fusereboot https://bl ...
- [Node.js]Path模块
摘要 path模块提供了一些处理文件路径问题的工具. path模块 引入模块 var path=require("path"); 方法 1 path.normalize(p)规范化 ...
- os.path.join合并 os.path.dirname返回上一级目录 os.path.exists(path) os.stat('path/filename')获取文件/目录信息
import os str1 = "grsdgfd" str2 = "wddf" str3 = "gddgs" # print(str1 + ...
随机推荐
- 在 CentOS 6.4上安装Erlang
如何在CentOS 6.4上安装erlang,具体的Erlang版本是R15B03-1. 在安装之前,需要先要安装一些其他的软件,否则在安装中间会出现一些由于没有其依赖的软件模块而失败. 一开始,要是 ...
- webpack 样式分离之The root route must render a single element
公司项目使用的是webpack1,使用extract-text-webpack-plugin 插件无法将css分离出来,检查原因,发现有如下代码 <Route path="/home& ...
- 九度OJ 1066:字符串排序 (排序)
时间限制:1 秒 内存限制:32 兆 特殊判题:否 提交:5632 解决:2299 题目描述: 输入一个长度不超过20的字符串,对所输入的字符串,按照ASCII码的大小从小到大进行排序,请输出排序后的 ...
- Greedy Function Approximation:A Gradient Boosting Machine
https://statweb.stanford.edu/~jhf/ftp/trebst.pdf page10 90% to 95% of the observations were often de ...
- cocos2d-js添加广点通插屏(通过jsb反射机制)
1.把广点通的jar包加入libs文件夹 2.修改AndroidManifest.xml文件 添加权限: <uses-permission android:name="android. ...
- 模拟登陆,selenium,线程池
一 . 模拟登陆案例(识别验证码) 1 . 打码平台 - 云打码 : www.yundama.com 使用步骤 : - 注册两个账户,普通用户和开发者用户 : - 登陆 普通用户查看余额 登陆开发 ...
- 学习使用ExpressJS 4.0中的新Router
概述 ExpressJS 4.0中提出了新的路由Router.Router好比是一个"迷你版"的express应用,它没有引入views或者settings,但是提供了路由应有的A ...
- 3D文字特效
在线演示 本地下载
- 《CSS权威指南(第三版)》---第五章 字体
这章主要的内容有: 1.字体:一般使用一种通用的字体. 2.字体加粗:一般从数字100 -900 . 3.字体大小:font-size 4.拉伸和调整字体:font-stretch 5.调整字体大小: ...
- EF异常捕捉
try{// 写数据库}catch (DbEntityValidationException dbEx){ }