zookeeper native api operation

Keywords: Zookeeper Session Apache

/**
 * TODO:
 * @package: com.zzhijian.zookeeperdemo.zk
 * @date: 2019-08-15 10:57
 **/
@Slf4j
public class ZookeeperDemo {
    private static String ZK_ADDRESS = "zkServer:2181,zkServer:2182,zkServer:2183";

    public static void main(String[] args) {
        try {
            ZooKeeper zooKeeper = getZkConnect(ZK_ADDRESS);
            //getData(zooKeeper);
           // createNode(zooKeeper);
            getChildren(zooKeeper);
            deleteNode(zooKeeper);
            Thread.sleep(1000*10*60);
        }catch (Exception e){
        }
    }
    /**
     * TODO:  Getting zk cluster connections
     * * @param address
     * @return org.apache.zookeeper.ZooKeeper
     * @version  1.0
     * @date 2019/8/15  9:51 a.m.
     */
    public static ZooKeeper getZkConnect(String address) throws Exception{
        String connectString = address;
        // Session timeout
        int sessionTimeout = 3000;
        log.error("zookeeper start connecting");
        ZooKeeper zooKeeper = new ZooKeeper(connectString, sessionTimeout, new Watcher() {
            @Override
            public void process(WatchedEvent event) {
                log.error("The event was triggered. --- Connection status:{},Event:{} ---, Here we can do something!!!",event.getState(),event.getType());
            }
        });
        log.error("zookeeper connection success!");
        return zooKeeper;
    }
    /**
     * TODO:  Create node
     * @param zooKeeper
     * @return void
     * @version  1.0
     * @date 2019/8/15  1:59 p.m.
     */
    public static void createNode(ZooKeeper zooKeeper) {
        try {
            if (zooKeeper != null) {
                // 1.1. Create znode of zookeeper
                System.out.println(zooKeeper.create("/test", "test node".getBytes(), ZooDefs.Ids.READ_ACL_UNSAFE, CreateMode.EPHEMERAL));
                // 1.2. Create persistent nodes
                System.out.println(zooKeeper.create("/test01", "test node".getBytes(), ZooDefs.Ids.READ_ACL_UNSAFE, CreateMode.PERSISTENT));
                System.out.println(zooKeeper.create("/testseq", "test node".getBytes(), ZooDefs.Ids.READ_ACL_UNSAFE, CreateMode.PERSISTENT_SEQUENTIAL));
                System.out.println(zooKeeper.create("/testseq", "test node".getBytes(), ZooDefs.Ids.READ_ACL_UNSAFE, CreateMode.PERSISTENT_SEQUENTIAL));
            }
        } catch (Exception e) {

        }
    }
    /**
     * TODO:  Get node information (trigger listen when node data changes, and it is one-time listen)
     * @param zooKeeper
     * @return void
     * @version  1.0
     * @date 2019/8/15  11:10 a.m.
     */
    public static void getData(ZooKeeper zooKeeper){
        try {
            log.error(new String(zooKeeper.getData("/zk02", new Watcher() {
                // One-time monitoring
                @Override
                public void process(WatchedEvent event) {
                    log.error("Obtain data listening-state:{},type:{}",event.getState(),event.getType());
                }
            }, new Stat())));
        }catch (Exception e){
        }
    }
    /**
     * TODO: Getting node information
     * @param zooKeeper
     * @return void
     * @version  1.0
     * @date 2019/8/15  2:30 p.m.
     */
    public static void getChildren(ZooKeeper zooKeeper){
        try{
            List<String> list = zooKeeper.getChildren("/zk02", new Watcher() {
                @Override
                public void process(WatchedEvent event) {
                    log.error("Obtaining Node Information Monitor-state:{},type:{}",event.getState(),event.getType());
                }
            },new Stat());
            log.error(list.toString());
        }catch (Exception e){

        }

    }
    /**
     * TODO: Delete node information
     * @param zooKeeper
     * @return void
     * @version  1.0
     * @date 2019/8/15  2:33 p.m.
     */
    public static void deleteNode(ZooKeeper zooKeeper){
        try{
           zooKeeper.delete("/zk04",-1);
        }catch (Exception e){

        }

    }

}

Deficiencies:

  1. Zookeeper's Watcher is one-off and needs to be re-registered after each trigger.
  2. There is no reconnection mechanism after Session timeout.
  3. Exception handling is tedious, Zookeeper provides many exceptions, and developers may not know how to handle these exceptions at all.
  4. Only a simple byte [] array interface is provided, and no object-level serialization is provided.
  5. When creating a node, if there is an exception thrown by the node, it is necessary to check whether the node exists or not.
  6. Deleting nodes can not achieve cascade deletion.

Posted by boardy on Wed, 02 Oct 2019 00:20:06 -0700