前面几篇博文,我们简单的介绍了一下zookeeper,如何安装zookeeper集群,以及如何使用命令行等。这篇博文我们重点来看下Zookeeper的java客户端API使用方式。

创建会话

客户端可以通过创建一个Zookeeper(org.apache.zookeeper.ZooKeeper)实例来连接ZooKeeper服务器。给大家推荐一篇博文,构造方法和参数都介绍的非常的详细,看一下就好。

我们看一下建立会话的代码就好了。

public class CreateSession implements Watcher{

    private static ZooKeeper zookeeper;
    //创建一个与服务器的连接 需要(服务端的 ip+端口号)(session过期时间)(Watcher监听注册)  
    public static void main(String[] args) throws IOException, InterruptedException {

        zookeeper = new ZooKeeper("127.0.0.1:2181", 5000, new CreateSession());
        System.out.println(zookeeper.getState());
        Thread.sleep(Integer.MAX_VALUE);
    }

    private void doSomething(){
        System.out.println("do something");
    }

    @Override
    public void process(WatchedEvent event) {
        System.out.println("Receive watched event:" + event);
        if(event.getState() == KeeperState.SyncConnected){
            System.out.println("ZooKeeper session established.");  
            doSomething();
        }
    }
}

需要我们注意就一点:ZooKeeper 允许客户端向服务端注册一个 Watcher 监听,当服务端的一些指定事件触发了这个 Watcher,那么就会向指定客户端发送一个事件通知来实现分布式的通知功能。

zookeeper的所有的API,都有同步和异步两种方式,使用异步API时,client可为每个operation设置callback,在operation被执行后,zookeeper会执行对应的callback。

下面给大家展示一下两个方式创建节点的不同。

同步创建节点

public class CreateNodeSync implements Watcher{

    private static ZooKeeper zookeeper;
     //创建一个与服务器的连接 需要(服务端的 ip+端口号)(session过期时间)(Watcher监听注册) 
    public static void main(String[] args) throws IOException, InterruptedException {

        zookeeper = new ZooKeeper("127.0.0.1:2181", 5000, new CreateNodeSync());
        System.out.println(zookeeper.getState());
        Thread.sleep(Integer.MAX_VALUE);
    }

    /**
     * 创建Znode
      * CreateMode:
      *     PERSISTENT (持续的,相对于EPHEMERAL,不会随着client的断开而消失)
      *     PERSISTENT_SEQUENTIAL(持久的且带顺序的)
      *     EPHEMERAL (短暂的,生命周期依赖于client session)
      *     EPHEMERAL_SEQUENTIAL  (短暂的,带顺序的)
      */
    private void doSomething(){
        try {
            String path = zookeeper.create("/node_2", "123".getBytes(), Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT);
            System.out.println("return path:" + path);

        } catch (KeeperException e) {
            e.printStackTrace();
        } catch (InterruptedException e) {
            e.printStackTrace();
        }

        System.out.println("do something");
    }

    @Override
    public void process(WatchedEvent event) {
        System.out.println("Receive watched event:" + event);
        if(event.getState() == KeeperState.SyncConnected){
            doSomething();
        }
    }
}

异步创建节点

public class CreateNodeASync implements Watcher {

    private static ZooKeeper zookeeper;
     //创建一个与服务器的连接 需要(服务端的 ip+端口号)(session过期时间)(Watcher监听注册) 
    public static void main(String[] args) throws IOException, InterruptedException {

        zookeeper = new ZooKeeper("127.0.0.1:2181", 5000, new CreateNodeASync());
        System.out.println(zookeeper.getState());
        Thread.sleep(Integer.MAX_VALUE);
    }
    private void doSomething() {
        zookeeper.create("/node_1", "123".getBytes(), Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT,
                new IStringCallback(), "this is content");

        System.out.println("do something");
    }

    @Override
    public void process(WatchedEvent event) {
        System.out.println("Receive watched event:" + event);
        if (event.getState() == KeeperState.SyncConnected) {
            doSomething();
        }
    }

    static class IStringCallback implements AsyncCallback.StringCallback {

        @Override
        public void processResult(int rc, String path, Object ctx, String name) {

            System.out.println("rc:" + rc);
            System.out.println("path:" + path);
            System.out.println("ctx:" + ctx);
            System.out.println("name:" + name);

        }
    }
}

他们的运行结果就不再给大家展示了,篇幅会比较大,我对API对zookeeper节点增删改查,都做了同步和异步的编写,大家可以在下面的链接地址中下载。

下载

java 桌面客户端 java客户端程序_zookeeper

下载地址:

总结:

  • 在这里api对zookeeper节点进行增删改查,有同步和异步的方
  • zookeeper不支持递归创建子节点(也就是说在父节点不存在的情况下,不允许创建子节点)
  • zookeeper不支持递归删除(也就是说在父节点有子节点的情况下,不允许直接删除父节点)

下篇博文,我们进行zookeeper的实战部分。