zookeeper原生API做java客户端

简介

本文是使用apache提供的原生api做zookeeper客户端

  • jar包

      zookeeper-3.4.5.jar

  •   Demo
  • package bjsxt.zookeeper.base;
    
    import java.util.concurrent.CountDownLatch;
    
    import org.apache.zookeeper.AsyncCallback;
    import org.apache.zookeeper.WatchedEvent;
    import org.apache.zookeeper.Watcher;
    import org.apache.zookeeper.Watcher.Event.EventType;
    import org.apache.zookeeper.ZooKeeper;
    import org.apache.zookeeper.Watcher.Event.KeeperState;
    
    /**
     * Zookeeper 原生api实践
     * @since 2017-6-13
     */
    public class ZookeeperBase {
    
        /** zookeeper地址 */
        static final String CONNECT_ADDR = "192.168.0.4:2181,192.168.0.5:2181,192.168.0.6:2181";
        /** session超时时间 */
        static final int SESSION_OUTTIME = 2000;//ms 
        /** 信号量,阻塞程序执行,用于等待zookeeper连接成功,发送成功信号 */
        static final CountDownLatch connectedSemaphore = new CountDownLatch(1);
        
        public static void main(String[] args) throws Exception{
            
            ZooKeeper zk = new ZooKeeper(CONNECT_ADDR, SESSION_OUTTIME, new Watcher(){
                @Override
                public void process(WatchedEvent event) {
                    //获取事件的状态
                    KeeperState keeperState = event.getState();
                    EventType eventType = event.getType();
                    //如果是建立连接
                    if(KeeperState.SyncConnected == keeperState){
                        if(EventType.None == eventType){
                            //如果建立连接成功,则发送信号量,让后续阻塞程序向下执行
                            connectedSemaphore.countDown();
                            System.out.println("zk 建立连接");
                        }
                    }
                }
            });
    
            //进行阻塞
            connectedSemaphore.await();
            
            System.out.println("..");
            //创建父节点
            //zk.create("/testRoot", "testRoot".getBytes(), Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT);
            
            //创建子节点
    //        String ret = zk.create("/testRoot/children", "children data".getBytes(), Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT);
    //        System.out.println("创建子节点"+ret);
            //获取节点洗信息
            /*byte[] data = zk.getData("/testRoot", false, null);
            System.out.println(new String(data));
            System.out.println(zk.getChildren("/testRoot", false));*/
            
            //修改节点的值
    /*        zk.setData("/testRoot", "modify data root".getBytes(), -1);
            byte[] data = zk.getData("/testRoot", false, null);
            System.out.println(new String(data));    */    
            
            //判断节点是否存在
    //        System.out.println(zk.exists("/testRoot/children", false));
            //同步删除节点
            zk.delete("/testRoot/children", -1);   
            //异步删除节点
            zk.delete("/testRoot/children", -1, new AsyncCallback.VoidCallback() {
                @Override
                public void processResult(int rc, String path, Object ctx) {
                    System.out.println("rc====="+rc);
                    System.out.println("path======"+path);
                    System.out.println("ctc======"+path);
                }
            } , "回调值");
    //        System.out.println(zk.exists("/testRoot/children", false));
            
            zk.close();
            
            
            
        }
        
    }

    总结:

  • 在这里api对zookeeper节点进行增删改查,有同步和异步的方式
  • zookeeper不支持递归创建子节点(也就是说在父节点不存在的情况下,不允许创建子节点)
  • zookeeper不支持递归删除(也就是说在父节点有子节点的情况下,不允许直接删除父节点)
posted @ 2017-02-09 23:15  坏~牧羊人  阅读(1311)  评论(0编辑  收藏  举报