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.hupengcool.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; /**
* Created by hupeng on 2014/9/19.
*/
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);
}
}

不足之处,请指正。(*^__^*) ,Thanks

使用Apache Curator监控Zookeeper的Node和Path的状态的更多相关文章

  1. zk 09之:Curator之二:Path Cache监控zookeeper的node和path的状态

    在实际应用开发中,当某个ZNode发生变化后我们需要得到通知并做一些后续处理,Curator Recipes提供了Path Cache 来帮助我们轻松实现watch ZNode. Path Cache ...

  2. 15. 使用Apache Curator管理ZooKeeper

    Apache ZooKeeper是为了帮助解决复杂问题的软件工具,它可以帮助用户从复杂的实现中解救出来. 然而,ZooKeeper只暴露了原语,这取决于用户如何使用这些原语来解决应用程序中的协调问题. ...

  3. 15. 使用Apache Curator装饰ZooKeeper

    Apache ZooKeeper是为了帮助解决复杂问题的软件工具,它可以帮助用户从复杂的实现中解救出来. 然而,ZooKeeper只暴露了原语,这取决于用户如何使用这些原语来解决应用程序中的协调问题. ...

  4. 使用Apache Curator管理ZooKeeper(转)

    Apache ZooKeeper是为了帮助解决复杂问题的软件工具,它可以帮助用户从复杂的实现中解救出来. 然而,ZooKeeper只暴露了原语,这取决于用户如何使用这些原语来解决应用程序中的协调问题. ...

  5. 基于Apache Curator框架的ZooKeeper使用详解

    一 简介 Apache Curator是一个比较完善的ZooKeeper客户端框架,通过封装的一套高级API 简化了ZooKeeper的操作.通过查看官方文档,可以发现Curator主要解决了三类问题 ...

  6. Apache Curator: Zookeeper客户端

    Apache Curator Framework url: http://curator.apache.org/curator-framework/ The Curator Framework is ...

  7. Apache Curator is a Java/JVM client library for Apache ZooKeeper

    http://curator.apache.org/index.html Welcome to Apache Curator What is Curator? Curator n ˈkyoor͝ˌāt ...

  8. 【zookeeper】Apache curator的使用及zk分布式锁实现

    上篇,本篇主要讲Apache开源的curator的使用,有了curator,利用Java对zookeeper的操作变得极度便捷. 其实在学之前我也有个疑虑,我为啥要学curator,撇开涨薪这些外在的 ...

  9. zookeeper连接 org.apache.curator.framework.imps.CuratorFrameworkImpl Background exception was not retry-able or retry gave up [main-EventThread]

    ERROR org.apache.curator.framework.imps.CuratorFrameworkImpl Background exception was not retry-able ...

随机推荐

  1. 深入了解View的绘制流程

    1.  ViewRoot ViewRoot是连接WindowManager与DecorView的纽带,View的整个绘制流程的三大步(measure.layout.draw)都是通过ViewRoot完 ...

  2. HDU 2068 Choose the best route

    http://acm.hdu.edu.cn/showproblem.php?pid=2680 Problem Description One day , Kiki wants to visit one ...

  3. Stax解析XML示例代码

    package org.itat.stax; import java.io.IOException; import java.io.InputStream; import javax.xml.pars ...

  4. BZOJ 2333 棘手的操作(离线+线段树+带权并查集)

    这题搞了我一天啊...拍不出错原来是因为极限数据就RE了啊,竟然返回WA啊.我的线段树要开8倍才能过啊... 首先可以发现除了那个加边操作,其他的操作有点像线段树啊.如果我们把每次询问的联通块都放在一 ...

  5. 【bzoj4196】[Noi2015]软件包管理器 树链剖分+线段树

    题目描述 Linux用户和OSX用户一定对软件包管理器不会陌生.通过软件包管理器,你可以通过一行命令安装某一个软件包,然后软件包管理器会帮助你从软件源下载软件包,同时自动解决所有的依赖(即下载安装这个 ...

  6. Python 源码剖析(五)【DICT对象】

    五.DICT对象 1.散列表概述 2.PyDictObject 3.PyDictObject的创建与维护 4.PyDictObject 对象缓冲池 5.Hack PyDictObject 这篇篇幅较长 ...

  7. P3385 【模板】负环

    题目描述 暴力枚举/SPFA/Bellman-ford/奇怪的贪心/超神搜索 输入输出格式 输入格式: 第一行一个正整数T表示数据组数,对于每组数据: 第一行两个正整数N M,表示图有N个顶点,M条边 ...

  8. 获取接口参数名带有“abc”的参数的值

    public IMethodReturn Invoke(IMethodInvocation input, GetNextInterceptionBehaviorDelegate getNext) va ...

  9. POJ3907:Build Your Home——题解

    http://poj.org/problem?id=3907 题目大意:求多边形面积,结果四舍五入. ———————————————————— 多边形面积公式板子题. #include<cstd ...

  10. POJ2891:Strange Way to Express Integers——题解

    http://poj.org/problem?id=2891 题目大意: k个不同的正整数a1,a2,...,ak.对于一些非负m,满足除以每个ai(1≤i≤k)得到余数ri.求出最小的m. 输入和输 ...