zkclient 快速指南

Maven依赖

最新的版本发布在Maven中央库。

<dependency>
<groupId>com.github.adyliu</groupId>
<artifactId>zkclient</artifactId>
<version>2.0</version>
</dependency>
如果不使用Maven依赖,那么需要以下依赖:

org.apache.zookeeper:zookeeper
log4j:log4j
zookeeper 3.4.x以上依赖于

org.slf4j:slf4j-api
org.slf4j:slf4j-log4j12
快速入门

操作的接口在com.github.zkclient.IZkClient中,默认的实现com.github.zkclient.ZkClient。

构造一个ZkClient非常简单,只需要如下代码即可:

IZkClient zkClient = new ZkClient("127.0.0.1:3000,127.0.0.1:3001,127.0.0.1:3002");
读取数据

byte[] readData(String path);
byte[] readData(String path, boolean returnNullIfPathNotExists);
byte[] readData(String path, Stat stat);
写入数据

Stat writeData(String path, byte[] data);
Stat writeData(String path, byte[] data, int expectedVersion);
创建节点

void createPersistent(String path);
void createPersistent(String path, boolean createParents);
void createPersistent(String path, byte[] data);
String createPersistentSequential(String path, byte[] data);
void createEphemeral(final String path);
void createEphemeral(final String path, final byte[] data);
String createEphemeralSequential(final String path, final byte[] data);
String create(final String path, byte[] data, final CreateMode mode);
删除节点

boolean delete(final String path);
boolean deleteRecursive(String path);
查询节点

boolean exists(final String path);
List<String> getChildren(String path);
long getCreationTime(String path);
int countChildren(String path);
订阅事件

zkclient的强大之处不在于基本zookeeper api操作,而在于事件监听机制,也就是zookeeper的watches。

Zookeeper的watcher在使用上存在一次性、session过期等难点,因此zkclient对这些问题进行了封装和屏蔽。 zkclient一共定义了种事件:

接口:com.github.zkclient.IZkStateListener  接口的定义实现了事件发生时,如果进行处理,如进行打印消息。。

    public void handleStateChanged(KeeperState state) throws Exception;
    public void handleNewSession() throws Exception;
接口:com.github.zkclient.IZkDataListener
    public void handleDataChange(String dataPath, byte[] data) throws Exception;
    public void handleDataDeleted(String dataPath) throws Exception;
接口:com.github.zkclient.IZkChildListener
    public void handleChildChange(String parentPath, List currentChildren) throws Exception;

IZkStateListener 定义了两种事件,一种是连接状态的改变,例如由未连接改变成连接上,连接上改为过期等;

另一种创建一个新的session(连接), 通常是由于session失效然后新的session被建立时触发。一般此时需要开发者重新创建临时节点(Ephemeral Nodes)。

IZkDataListener 也定义了两种事件,一种是节点数据的变化,另一种是节点被删除。

IZkChildListener 定义了一种事件,描述子节点变化了,这时候获取到的是新的子节点列表。如果此节点被删除,那么子节点列表是null(非空列表)。

IZkClient能够非常方便的订阅(subscribe)这三种事件:

void subscribeStateChanges(IZkStateListener listener); // listener 进行消息处理
void subscribeDataChanges(String path, IZkDataListener listener);
List subscribeChildChanges(String path, IZkChildListener listener);
而zkclient最强大之处在于,当发送session失效时能够自动重新订阅这些事件,而不需要开发者重新订阅。

例如,订阅一个节点的数据变更:

zkclient.subscribeDataChanges('/xpower/ddd/compass', new IZkDataListener() {
  public void handleDataDeleted(String path) throws Exception {
    destroyDatasource("compass");
  }
  public void handleDataChange(String path, byte[] data) throws Exception {
    rebuildDatasource("compass",data);
  }
});

https://github.com/adyliu/zkclient/wiki/tutorial

zkclient的更多相关文章

  1. ZooKeeper:第三方客户端 ZKClient

    ZKClient ZKClient的设计 ZKClient组件说明 重要的处理流程说明 启动ZKClient 为节点注册Watcher ZooKeeper的变更操作 客户端处理变更 序列化处理 ZKC ...

  2. zookeeper_service 出错 java.lang.NoClassDefFoundError: org/I0Itec/zkclient/exception/ZkNoNodeException

    2016-12-18 08:28:07 ContextLoader:358 ERROR - Context initialization failed java.lang.NoClassDefFoun ...

  3. (原) 2.2 ZkClient使用

    本文为原创文章,转载请注明出处,谢谢 ZkClient使用 1.jar包引入,演示版本为0.8,非maven项目,可以下载jar包导入到项目中 <dependency> <group ...

  4. 为什么dubbo使用ZkClient作为zookeeper的客户端

    本文内容并非原创,使用资料均来自互联网. dubbo使用了zkClient而不是使用zookeeper本身的客户端与zookeeper进行交互,为什么呢? 先看看zookeeper本身自带的客户端的问 ...

  5. 基于库zkclient 的leader选举代码实现

    利用了zookeeper临时节点,在当连接或session断掉时被删除这一特性来做选举.(简单简单互斥锁) 查了下网上的做法. 大致流程: <1>判定是否存在/wzgtest路径 < ...

  6. 面向服务的体系架构 SOA(三) --- Zookeeper API、zkClient API的使用

    zookeeper简单介绍及API使用 1.1 zookeeper简介 zookeeper是一个针对大型分布式系统的可靠的协调系统,提供的功能包括配置维护.名字服务.分布式同步.组服务等.zookee ...

  7. java.lang.ClassNotFoundException: org.I0Itec.zkclient.IZkStateListener异常解决

    在启动Dubbo项目时,出现该异常 java.lang.ClassNotFoundException: org.I0Itec.zkclient.IZkStateListener 解决,引入 <d ...

  8. Exception in thread "main" org.I0Itec.zkclient.exception.ZkAuthFailedException: Authentication failure is thrown while creating kafka topic

    Exception in thread "main" org.I0Itec.zkclient.exception.ZkAuthFailedException: Authentica ...

  9. zkclient中包引用不对,导致NoSuchMethodError

    nidonglin commented on 31 Oct 2014 Exception in thread "main" java.lang.NoSuchMethodError: ...

随机推荐

  1. Windows帐户类型

    摘自:http://blog.csdn.net/shineorrain/article/details/18181707 LocalSystem   账户  LocalSystem是预设的拥有本机所有 ...

  2. pouchdb 安装使用

    1. 安装: If you are on a Debian flavor of Linux (Ubuntu, Mint, etc.), you can install CouchDB with: $ ...

  3. 【转】PHP error_reporting() 错误控制函数功能详解

    定义和用法: error_reporting() 设置 PHP 的报错级别并返回当前级别.   函数语法: error_reporting(report_level)   如果参数 level 未指定 ...

  4. SpringQtz 时间任务调度

    1.配置所需要maven jar包 <!-- 任务调度需要的jar包--> <dependency> <groupId>org.quartz-scheduler&l ...

  5. C#子线程刷新界面并关闭窗体

    目的:要循环刷新界面上的控件,同时不影响用户操作.循环结束后关闭窗体. 步骤:先创建一个窗体,窗体中拖入一个lable控件(label1),一个button控件(button1) 代码窗口输入: // ...

  6. MockMultipartFile

    org.springframework.mock.webClass MockMultipartFile java.lang.Object org.springframework.mock.web.Mo ...

  7. DB2 组内分组排序,游标使用

    CREATE PROCEDURE Sys_Init_tblaccountsuser_sortid () P1: BEGIN '; ; ; ; DECLARE CUR1 CURSOR WITH RETU ...

  8. 解决SQLite3数据库Error: database disk image is malformed

    这种错误的提示一般都是数据库文件出现了问题,具体导致问题的原因不必深究,我们只讨论这种问题的饿解决方法: 比如数据库:test.db 这里还要分两种情况: 情况一: sqlite3 test.db & ...

  9. LintCode "Triangle Count"

    Should be "Medium" or even "Easy".. Just with a little Greedy. class Solution { ...

  10. IntelliJ IDEA修改Output输出缓存区大小【应对:too much output to process】

    IntelliJ IDEA默认的Output输出缓存区大小只有1024KB,超过大小限制的就会被清除,而且还会显示[too much output to process],可通过如下配置界面进行修改O ...