ZkClient

ZkClient是由Datameer的工程师开发的开源客户端,对Zookeeper的原生API进行了包装,实现了超时重连、Watcher反复注册等功能。
github源代码地址:https://github.com/sgroschupf/zkclient

依赖


     org.apache.zookeeper
     zookeeper
     3.4.9
 

    com.101tec
    zkclient
    0.10

使用

public ZkClient(String serverstring)
public ZkClient(String zkServers, int connectionTimeout)
public ZkClient(String zkServers, int sessionTimeout, int connectionTimeout)
public ZkClient(String zkServers, int sessionTimeout, int connectionTimeout, ZkSerializer zkSerializer)
public ZkClient(final String zkServers, final int sessionTimeout, final int connectionTimeout, final ZkSerializer zkSerializer, final long operationRetryTimeout)
public ZkClient(IZkConnection connection)
public ZkClient(IZkConnection connection, int connectionTimeout)
public ZkClient(IZkConnection zkConnection, int connectionTimeout, ZkSerializer zkSerializer)
public ZkClient(final IZkConnection zkConnection, final int connectionTimeout, final ZkSerializer zkSerializer, final long operationRetryTimeout)

创建节点

public void createPersistent(String path)
public void createPersistent(String path, boolean createParents)
public void createPersistent(String path, boolean createParents, List acl)
public void createPersistent(String path, Object data)
public void createPersistent(String path, Object data, List acl)
public String createPersistentSequential(String path, Object data)
public String createPersistentSequential(String path, Object data, List acl) 
public void createEphemeral(final String path)
public void createEphemeral(final String path, final List acl)
public String create(final String path, Object data, final CreateMode mode)
public String create(final String path, Object data, final List acl, final CreateMode mode) 
public void createEphemeral(final String path, final Object data)
public void createEphemeral(final String path, final Object data, final List acl)
public String createEphemeralSequential(final String path, final Object data)
public String createEphemeralSequential(final String path, final Object data, final List acl)

删除节点:

public boolean delete(final String path)
public boolean delete(final String path, final int version)
public boolean deleteRecursive(String path)

读取列表

public List getChildren(String path)
public  T readData(String path)
public  T readData(String path, boolean returnNullIfPathNotExists)
public  T readData(String path, Stat stat)

更新内容:

public void writeData(String path, Object object)
public void writeData(final String path, Object datat, final int expectedVersion)
public Stat writeDataReturnStat(final String path, Object datat, final int expectedVersion)

监测节点是否存在

protected boolean exists(final String path, final boolean watch)

注册监听

image.png

其中ZkClient还提供了一个unsubscribeAll方法,来解除所有监听。

你可能感兴趣的:(ZkClient)