Java客户端使用zookeeper api

微笑、不失礼 提交于 2019-12-26 23:54:35

添加zookeeper依赖

<!-- https://mvnrepository.com/artifact/org.apache.zookeeper/zookeeper -->
<dependency>
    <groupId>org.apache.zookeeper</groupId>
    <artifactId>zookeeper</artifactId>
    <version>3.5.6</version>
</dependency>

创建会话

Zookeeper类提供了以下的构造方法,无论使用哪一个构造方法都可以顺利的创建会话。

 	public ZooKeeper(java.lang.String connectString, int sessionTimeout, org.apache.zookeeper.Watcher watcher) throws java.io.IOException { /* compiled code */ }

    public ZooKeeper(java.lang.String connectString, int sessionTimeout, org.apache.zookeeper.Watcher watcher, org.apache.zookeeper.client.ZKClientConfig conf) throws java.io.IOException { /* compiled code */ }

    public ZooKeeper(java.lang.String connectString, int sessionTimeout, org.apache.zookeeper.Watcher watcher, boolean canBeReadOnly, org.apache.zookeeper.client.HostProvider aHostProvider) throws java.io.IOException { /* compiled code */ }

    public ZooKeeper(java.lang.String connectString, int sessionTimeout, org.apache.zookeeper.Watcher watcher, boolean canBeReadOnly, org.apache.zookeeper.client.HostProvider aHostProvider, org.apache.zookeeper.client.ZKClientConfig clientConfig) throws java.io.IOException { /* compiled code */ }

    public ZooKeeper(java.lang.String connectString, int sessionTimeout, org.apache.zookeeper.Watcher watcher, boolean canBeReadOnly) throws java.io.IOException { /* compiled code */ }

    public ZooKeeper(java.lang.String connectString, int sessionTimeout, org.apache.zookeeper.Watcher watcher, boolean canBeReadOnly, org.apache.zookeeper.client.ZKClientConfig conf) throws java.io.IOException { /* compiled code */ }

    public ZooKeeper(java.lang.String connectString, int sessionTimeout, org.apache.zookeeper.Watcher watcher, long sessionId, byte[] sessionPasswd) throws java.io.IOException { /* compiled code */ }

    public ZooKeeper(java.lang.String connectString, int sessionTimeout, org.apache.zookeeper.Watcher watcher, long sessionId, byte[] sessionPasswd, boolean canBeReadOnly, org.apache.zookeeper.client.HostProvider aHostProvider) throws java.io.IOException { /* compiled code */ }

    public ZooKeeper(java.lang.String connectString, int sessionTimeout, org.apache.zookeeper.Watcher watcher, long sessionId, byte[] sessionPasswd, boolean canBeReadOnly, org.apache.zookeeper.client.HostProvider aHostProvider, org.apache.zookeeper.client.ZKClientConfig clientConfig) throws java.io.IOException { /* compiled code */ }

    public ZooKeeper(java.lang.String connectString, int sessionTimeout, org.apache.zookeeper.Watcher watcher, long sessionId, byte[] sessionPasswd, boolean canBeReadOnly) throws java.io.IOException { /* compiled code */ }

  • connectString:指定ZooKeeper服务器列表,有英文状态逗号分开的host:port组成的字符串,例如“192.168.44.130:2181,192.168.44.132:2181,192.168.44.133:2181”。
  • sessionTimout:会话超时时间,以“毫秒”为单位的整数,zookeeper客户端和服务端通过心跳检测的方式保持会话的有效性,指定sessionTimout之后,在这一段时间内,没有进行有效的心跳检测,会话就会失效。
  • watcher:定义事件接受的Watcher,可以设置为null,表示不监听。
  • canBeReadOnly:boolean型的值,默认情况下,zookeeper只要有过半服务器主机失去网络连接,就不会再处理客户端的操作(读或者写),添加该属性则表示,在出现这种情况时,我们依旧可以使用读服务。
  • sessionId和sessionPasswd:代表会话ID和秘钥,这两个参数可以确定唯一会话,可以做到复活会话的效果。

创建节点

    public java.lang.String create(java.lang.String path, byte[] data, java.util.List<org.apache.zookeeper.data.ACL> acl, org.apache.zookeeper.CreateMode createMode) throws org.apache.zookeeper.KeeperException, java.lang.InterruptedException { /* compiled code */ }

    public void create(java.lang.String path, byte[] data, java.util.List<org.apache.zookeeper.data.ACL> acl, org.apache.zookeeper.CreateMode createMode, org.apache.zookeeper.AsyncCallback.StringCallback cb, java.lang.Object ctx) { /* compiled code */ }
  • path:节点路径
  • data[]:节点数据
  • acl:acl策略
  • createMode:节点类型
    • 持久 RERSISTENT
    • 持久序列化 PERSISTENT_SEQUENTIAL
    • 临时 EPHEMERAL
    • 临时序列化 EPHEMERAL_SEQUENTIAL
  • cb:异步回调函数,服务器节点创建完毕之后,自动调用该方法
  • ctx:可传入一个对象,该对象可以在回调方法中使用

删除节点

public void delete(java.lang.String path, int version) throws java.lang.InterruptedException, org.apache.zookeeper.KeeperException { /* compiled code */ }
public void delete(java.lang.String path, int version, org.apache.zookeeper.AsyncCallback.VoidCallback cb, java.lang.Object ctx) { /* compiled code */ }
  • path:节点路径
  • version:数据版本
  • cb:异步回调函数,服务器节点创建完毕之后,自动调用该方法
  • ctx:可传入一个对象,该对象可以在回调方法中使用

读取数据

读取数据包括对子节点的读取还有节点数据的读取。

getChildren

public java.util.List<java.lang.String> getChildren(java.lang.String path, org.apache.zookeeper.Watcher watcher) throws org.apache.zookeeper.KeeperException, java.lang.InterruptedException { /* compiled code */ }

    public java.util.List<java.lang.String> getChildren(java.lang.String path, boolean watch) throws org.apache.zookeeper.KeeperException, java.lang.InterruptedException { /* compiled code */ }

    public void getChildren(java.lang.String path, org.apache.zookeeper.Watcher watcher, org.apache.zookeeper.AsyncCallback.ChildrenCallback cb, java.lang.Object ctx) { /* compiled code */ }

    public void getChildren(java.lang.String path, boolean watch, org.apache.zookeeper.AsyncCallback.ChildrenCallback cb, java.lang.Object ctx) { /* compiled code */ }

    public java.util.List<java.lang.String> getChildren(java.lang.String path, org.apache.zookeeper.Watcher watcher, org.apache.zookeeper.data.Stat stat) throws org.apache.zookeeper.KeeperException, java.lang.InterruptedException { /* compiled code */ }

    public java.util.List<java.lang.String> getChildren(java.lang.String path, boolean watch, org.apache.zookeeper.data.Stat stat) throws org.apache.zookeeper.KeeperException, java.lang.InterruptedException { /* compiled code */ }

    public void getChildren(java.lang.String path, org.apache.zookeeper.Watcher watcher, org.apache.zookeeper.AsyncCallback.Children2Callback cb, java.lang.Object ctx) { /* compiled code */ }

    public void getChildren(java.lang.String path, boolean watch, org.apache.zookeeper.AsyncCallback.Children2Callback cb, java.lang.Object ctx) { /* compiled code */ }
  • path:节点路径
  • watcher:注册的watcher,子节点列表改变时,会触发
  • watch:表明是否需要注册一个watcher
  • cb:异步回调函数,服务器节点创建完毕之后,自动调用该方法
  • ctx:可传入一个对象,该对象可以在回调方法中使用
  • stat:指定节点的节点状态信息,服务器会响应新的stat对象替换

getData

    public byte[] getData(java.lang.String path, org.apache.zookeeper.Watcher watcher, org.apache.zookeeper.data.Stat stat) throws org.apache.zookeeper.KeeperException, java.lang.InterruptedException { /* compiled code */ }

    public byte[] getData(java.lang.String path, boolean watch, org.apache.zookeeper.data.Stat stat) throws org.apache.zookeeper.KeeperException, java.lang.InterruptedException { /* compiled code */ }

    public void getData(java.lang.String path, org.apache.zookeeper.Watcher watcher, org.apache.zookeeper.AsyncCallback.DataCallback cb, java.lang.Object ctx) { /* compiled code */ }

    public void getData(java.lang.String path, boolean watch, org.apache.zookeeper.AsyncCallback.DataCallback cb, java.lang.Object ctx) { /* compiled code */ }
  • path:节点路径
  • watcher:注册的watcher,子节点列表改变时,会触发
  • watch:表明是否需要注册一个watcher
  • cb:异步回调函数,服务器节点创建完毕之后,自动调用该方法
  • ctx:可传入一个对象,该对象可以在回调方法中使用
  • stat:指定节点的节点状态信息,服务器会响应新的stat对象替换

更新数据

  public org.apache.zookeeper.data.Stat setData(java.lang.String path, byte[] data, int version) throws org.apache.zookeeper.KeeperException, java.lang.InterruptedException { /* compiled code */ }

    public void setData(java.lang.String path, byte[] data, int version, org.apache.zookeeper.AsyncCallback.StatCallback cb, java.lang.Object ctx) { /* compiled code */ }
  • path:节点路径
  • data[]:节点数据
  • version:数据版本
  • cb:异步回调函数,服务器节点创建完毕之后,自动调用该方法
  • ctx:可传入一个对象,该对象可以在回调方法中使用

检测节点是否存在

	public org.apache.zookeeper.data.Stat exists(java.lang.String path, org.apache.zookeeper.Watcher watcher) throws org.apache.zookeeper.KeeperException, java.lang.InterruptedException { /* compiled code */ }

    public org.apache.zookeeper.data.Stat exists(java.lang.String path, boolean watch) throws org.apache.zookeeper.KeeperException, java.lang.InterruptedException { /* compiled code */ }

    public void exists(java.lang.String path, org.apache.zookeeper.Watcher watcher, org.apache.zookeeper.AsyncCallback.StatCallback cb, java.lang.Object ctx) { /* compiled code */ }

    public void exists(java.lang.String path, boolean watch, org.apache.zookeeper.AsyncCallback.StatCallback cb, java.lang.Object ctx) { /* compiled code */ }
  • path:节点路径
  • watcher:注册的watcher,子节点列表改变时,会触发
  • watch:表明是否需要注册一个watcher
  • cb:异步回调函数,服务器节点创建完毕之后,自动调用该方法
  • ctx:可传入一个对象,该对象可以在回调方法中使用
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!