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. DAY5敏捷冲刺

    站立式会议 工作安排 (1)服务器配置 对单词学习的记录储存 (2)数据库配置 单词学习记录+用户信息 燃尽图 燃尽图有误,已重新修改,先贴卡片的界面,后面补修改后燃尽图 代码提交记录

  2. dpr dproj 扩展名区别,dprdproj

    这段时间用xe6,看了下目录下生成的一些文件,因为隐藏了扩展名,看到两个名字一样的文件,右键属性看了下,同名但扩展名不同,百度了下区别,没有找到答案,问群里的朋友才知道区别,特此记录下来: dpr:D ...

  3. WPF布局间的切换方法

    效果图,两种效果间的切换

  4. 动态include是通过servlet进行页面信息交互的

    动态include是通过servlet进行页面信息交互的

  5. bzoj4502 串

    题意:给你n(n<=10000)个字符串,每个字符串的长度不超过30,可以选择两个非空前缀把它们拼起来得到一个字符串(这两个前缀可以来自同一个字符串,也可以是同一个字符串的同一个非空前缀),问得 ...

  6. 【题解】Atcoder ARC#83 E-Bichrome Tree

    哈哈~自己做出来的E题!(虽然这题被机房大佬强D极水).最开始神经错乱,写了个完全不对的贪心,竟然只错了4个点(。•ˇ‸ˇ•。) 可以发现,一个节点的子树内部和他颜色相同的节点权值和 是固定的,那么不 ...

  7. POJ3630:Phone List——题解

    http://poj.org/problem?id=3630 简单的trie树问题,先添加,然后每个跑一边看中途有没有被打上结束标记即可. #include<cstdio> #includ ...

  8. BZOJ2924 [Poi1998]Flat broken lines 【Dilworth定理 + 树状数组】

    题目链接 BZOJ2924 题解 题面有误..是\(45°\) 如果两个点间连线与\(x\)轴夹角在\(45°\)以内,那么它们之间连边 求最小路径覆盖 = 最长反链 由于\(45°\)比较难搞,我们 ...

  9. 洛谷 P4556 [Vani有约会]雨天的尾巴 解题报告

    P4556 [Vani有约会]雨天的尾巴 题目背景 深绘里一直很讨厌雨天. 灼热的天气穿透了前半个夏天,后来一场大雨和随之而来的洪水,浇灭了一切. 虽然深绘里家乡的小村落对洪水有着顽固的抵抗力,但也倒 ...

  10. UVA.725 Division (暴力)

    UVA.725 Division (暴力) 题意分析 找出abcdefghij分别是0-9(不得有重复),使得式子abcde/fghij = n. 如果分别枚举每个数字,就会有10^10,肯定爆炸,由 ...