Zookeeper中组成员的关系
h理解Zookeeper的一种方法就是将其看做一个具有高可用性的文件系统。但这个文件系统中没有文件和目录,而是统一使用"节点(node)的概念,成为znode。znode既可以作为作为保存数据的容器,也可以作为保存其他znode的容器。所有的znode构成一个层次化的命名空间。
创建组
改程序在Zookeeper中新建表示组的znode
public class CreatedGroup implemetns Watcher {
private static final int SESSION_TIMEOUT = 5000;
private Zookeeper zk;
private CountDownLatch connectedSignal = new CountDownLatch(1);
public void connect(String hosts) throws IOException, InterruptedException {
zk = new Zookeeper(hosts, SESSION_TIMEOUT, this);
connectedSignal.await();
}
@Override
public void process(WatchedEvent event) { // Watcher interface
if(event.getState() == KeeperState.SyncConnected) {
connectedSignal.countDown();
}
}
public void create(String groupName)
throws KeeperException, InterruptedException {
String path = "/" + groupName;
String createdPath = zk.create(path, null/*data*/,
Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT);
Systemout.println("Creted " + createdPath);
}
public void close() throws InterruptedException {
zk.close();
}
}
Watch对象接收来自于Zookeeper的回调,以获得各种事件的通知。
This chapter requires login to view full content. You are viewing a preview.
Login to View Full Content