• 读《分布式一致性原理》JAVA客户端API操作


    创建会话

    客户端可以通过创建一个Zookeeper实例来连接服务器。4种构造方法如下

    ZooKeeper(connectString, sessionTimeout, watcher);
    
    ZooKeeper(String connectString, int sessionTimeout, Watcher watcher, boolean canBeReadOnly)
    
    
     ZooKeeper(String connectString, int sessionTimeout, Watcher watcher,long sessionId, byte[] sessionPasswd);
    
    ZooKeeper(String connectString, int sessionTimeout, Watcher watcher,long sessionId, byte[] sessionPasswd, boolean canBeReadOnly)

     

     

    注意,zookeeper客户端和服务端会话建立是一个异步的过程,也是就是说在程序中构造zookeeper方法初始化方法执行完后

    会立即返回,在大多数情况下此时并没有真正的建立一个可用的会话,此时会话正处于CONnectioning状态。当真正的会话建立后

    服务端会向客户端发送一个通知,客户端在获取这个通知后会话才真正的建立。

    package session;
    import java.io.IOException;
    import java.util.concurrent.CountDownLatch;
    import org.apache.zookeeper.WatchedEvent;
    import org.apache.zookeeper.Watcher;
    import org.apache.zookeeper.ZooKeeper;
    import org.apache.zookeeper.Watcher.Event.KeeperState;
    
    public class CreateZookeeper implements Watcher {
        private static CountDownLatch connectedSemaphore = new CountDownLatch(1);
        
        @Override
        public void process(WatchedEvent event) {
            System.out.println("receive watched event:"+event);
            if (KeeperState.SyncConnected==event.getState()) {
                connectedSemaphore.countDown();
            }
            
        }
        public static void main(String[] args) throws IOException {
            ZooKeeper zooKeeper = new ZooKeeper("192.168.64.60", 5000, new CreateZookeeper());
            
            System.out.println(zooKeeper.getState());
            
            try {
                connectedSemaphore.await();
            } catch (InterruptedException e) {}
                System.out.println("zookeeper session established");
            
            
        }
        
    
    }

    程序运行结果如下:

    CreateZookeeper类实现了Watcher接口重写了process方法,该方法负责处理来自zookeeper服务端的通知。在

    收到来自服务端的synconncted事件后,解除了主程序中的CountDownLatch等待阻塞。至此客户端会话创建完成。

     创建一个复用sessionId和sessionPasswd的实例

    package session;
    
    import java.io.IOException;
    import java.util.concurrent.CountDownLatch;
    
    import org.apache.zookeeper.WatchedEvent;
    import org.apache.zookeeper.Watcher;
    import org.apache.zookeeper.ZooKeeper;
    import org.apache.zookeeper.Watcher.Event.KeeperState;
    
    public class CreateZookeeper2 implements Watcher {
    public static CountDownLatch connectedSemaphore = new CountDownLatch(1);
    
    @Override
    public void process(WatchedEvent event) {
        System.out.println("receive watched event:"+event);
        if (KeeperState.SyncConnected==event.getState()) {
            connectedSemaphore.countDown();
        }
        
    }
    
    public static void main(String[] args) throws IOException, InterruptedException {
        ZooKeeper zooKeeper = new ZooKeeper("192.168.64.60", 5000, new CreateZookeeper());
        connectedSemaphore.await();
        long sessionId = zooKeeper.getSessionId();
        byte[] sessionPasswd = zooKeeper.getSessionPasswd();
        
        //复用sessionId和sessionPasswd
         zooKeeper = new ZooKeeper("192.168.64.60", 5000, new CreateZookeeper(),1l,"test".getBytes());
         
         zooKeeper = new ZooKeeper("192.168.64.60", 5000, new CreateZookeeper(),sessionId,sessionPasswd);
         
         Thread.sleep(Integer.MAX_VALUE);
    
    }
    
    }

    我们可以看出,第一次使用错误的id和pw服务端返回“EXpired”事件,而第二次使用正确的id和pw则重新连接上。

  • 相关阅读:
    词汇小助手V1.0——一款跨平台词频统计和外语学习工具
    穿越狙击V1.0
    词汇小助手V1.4——加入单词测试功能
    词汇小助手V1.2——可以显示英语单词的国际音标
    词汇小助手官方网站发布了
    强大的云存储与应用管理工具DzzOffice1.0 Beta(大桌子办公)发布下载 大桌子
    <!DOCTYPE>前加有<! xxx >注释在IE中引发的bug
    去笔试腾讯的前端实习生,题目太尼玛坑爹了
    IE中的布局BUG和一些可以避开的BUG
    removeClass,addClass的原生JS代码
  • 原文地址:https://www.cnblogs.com/duan2/p/9063006.html
Copyright © 2020-2023  润新知