zookeeper Actual Warfare 05-The Use of zookeeper Client Curator

There are three main clients of zookeeper. One is to operate zookeeper in a native way.( zookeeper Actual 04-Java Operation zookeeper The second is open source zkclient, the third is apache's official open source Curator.

  • Advantages of Curator
1. Connection Processing between Encapsulating ZooKeeper client and ZooKeeper server
 2. Provide a set of Fluent-style operation API
 3. Provide Abstract encapsulation of ZooKeeper application scenarios (recipe, such as shared lock service, cluster leader election mechanism)
  • Usage mode
         <dependency>
           <groupId>org.apache.curator</groupId>
            <artifactId>curator-framework</artifactId>
            <version>4.0.0</version>
         </dependency>
         <dependency>
            <groupId>org.apache.curator</groupId>
            <artifactId>curator-recipes</artifactId>
            <version>4.0.0</version>
         </dependency>

Basic curd usage

  • Add node
        String nodePath = "/super/kkcl";
        byte [] data = "kkcl".getBytes();
        curatorOperator.client.create().creatingParentsIfNeeded()
                .withMode(CreateMode.PERSISTENT)
                .withACL(ZooDefs.Ids.OPEN_ACL_UNSAFE)
                .forPath(nodePath,data);
  • Modify node
    byte [] newData = "newData".getBytes();
    curatorOperator.client.setData().withVersion(0).forPath(nodePath,newData);
  • Delete node
	curatorOperator.client.delete().guaranteed()
    		.deletingChildrenIfNeeded()
            .withVersion(1)
            .forPath(nodePath);
  • Read node data
	Stat stat = new Stat();
        byte [] da = curatorOperator.client.getData().storingStatIn(stat).forPath(nodePath);
        System.out.println("Node:" + nodePath + "The data are as follows:" + new String(da));
        System.out.println("The version number of the node is:" + stat.getAversion());
  • Query subnodes
 	List<String> childNodes = curatorOperator.client.getChildren().forPath(nodePath);
        System.out.println("Start printing child nodes");
        for(String s : childNodes){
            System.out.println(s);
  • Judging whether a node exists or not
	Stat stat = curatorOperator.client.checkExists().forPath(nodePath);
        System.out.println(stat);

Node-specific monitoring

  • There are two ways to monitor: one is to use one class to implement watcher and process method; the other is to use one class to implement Curator Watcher and process method at the same time.
	curatorOperator.client.getData().usingWatcher(new MyCuratorWatcher()).forPath(nodePath);
	curatorOperator.client.getData().usingWatcher(new MyWatcher()).forPath(nodePath);

Complete test list

public class CuratorOperator {
    public CuratorFramework client = null;
    public static final String path = "192.168.1.108:2182";

    //Instantiate zookeeper client
    public CuratorOperator(){
        RetryPolicy retryPolicy = new RetryNTimes(3, 5000);
        client = CuratorFrameworkFactory.builder()
                .connectString(path)
                .sessionTimeoutMs(50000).retryPolicy(retryPolicy)
                //.build();
                .namespace("workspace").build();
        client.start();
    }

    //Close zk client connection
    public void closeZKClient(){
        if(client != null){
            this.client.close();
        }
    }

    public static void main(String[] args) throws Exception {
        CuratorOperator curatorOperator = new CuratorOperator();
        boolean isZKCuratorStarted = curatorOperator.client.isStarted();
        System.out.println("Current client status:" + (isZKCuratorStarted ? "Connection" : "Closed"));
        String nodePath = "/super/kkcl";
        //Create node
        byte [] data = "kkcl".getBytes();
        /**
        curatorOperator.client.create().creatingParentsIfNeeded()
                .withMode(CreateMode.PERSISTENT)
                .withACL(ZooDefs.Ids.OPEN_ACL_UNSAFE)
                .forPath(nodePath,data);
         */

        //Update node data
        //byte [] newData = "newData".getBytes();
        //curatorOperator.client.setData().withVersion(0).forPath(nodePath,newData);

        //Delete node
        /**
        curatorOperator.client.delete().guaranteed()
                .deletingChildrenIfNeeded()
                .withVersion(1)
                .forPath(nodePath);
         */

        //Read the data of the node
        /**
        Stat stat = new Stat();
        byte [] da = curatorOperator.client.getData().storingStatIn(stat).forPath(nodePath);
        System.out.println("Node: "+nodePath+" data is "+new String (da);"
        System.out.println("The version number of the node is: "+stat.getAversion();"
         */

        //Query subnodes
        /**
        List<String> childNodes = curatorOperator.client.getChildren().forPath(nodePath);
        System.out.println("Start printing child nodes ";
        for(String s : childNodes){
            System.out.println(s);
        }
         */

        //Judging whether a node exists or not
        //Stat stat = curatorOperator.client.checkExists().forPath(nodePath);
        //System.out.println(stat);

        //Watcher event. When using the using watcher, the listener will only trigger once, and the listener will be destroyed when it is finished.
        //curatorOperator.client.getData().usingWatcher(new MyCuratorWatcher()).forPath(nodePath);
        //curatorOperator.client.getData().usingWatcher(new MyWatcher()).forPath(nodePath);
        Thread.sleep(3000);
        curatorOperator.closeZKClient();
        boolean isZKCuratorStarted02 = curatorOperator.client.isStarted();
        System.out.println("Current client status:" + (isZKCuratorStarted02 ? "Connection" : "Closed"));
    }
}

Tags: Zookeeper Apache Java

Posted on Sat, 05 Oct 2019 21:03:53 -0700 by P3t3r