读《分布式一致性原理》JAVA客户端API操作
Posted duan2
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了读《分布式一致性原理》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则重新连接上。
以上是关于读《分布式一致性原理》JAVA客户端API操作的主要内容,如果未能解决你的问题,请参考以下文章