zookeeper快速上手

创建组 加入组 列出组成员 删除组

创建组

创建名为/zoo的znode

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
public class CreateGroup implements Watcher {
private static final int SESSION_TIMEOUT = 5000;
private Zookeeper zk;
private CountDownLatch connectionedSignal = 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) { // 检测zk对象是否就绪
if (event.getState() == keeperState.SyncConnected) {
connectedSignal.countDown; // 计数值减一, 从1变为0, await()返回
}
}
public void create(String groupName) throws KeeperExcption,
InterruptedException {
String parh = "/" + groupName;
String createdPath = zk.create(path, null/*data*/, Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT); //持久连接
System.out.println("Created " + createdPath);
}
public static void main(String[] args) throws Exception {
CreateGroup createGroup = new CreateGroup();
createGroup.connect(args[0]);
createGroup.create(args[1]); //创建znode
createGroup.close();
}
}

main()
方法执行时,创建了一个CreateGroup的实例,然后调用了这个实例的connect()方法.connect方法实例化了一个新的Zookeeper类的对象,这个类是客户端API中的主要类,用于维护客户端和zookeeper服务的连接.zookeeper类的构造函数有三个参数:第一个参数是zookeeper的主机地址(可指定参数,默认是2181);第二个参数是设置会话超时参数,第三个是watcher对象的实例.Watcher对象接受来自于zookeeper的回调,以获得各种事件的通知.在这个例子中,createGroup是一个Watcher,把它传给zookeeper构造函数.

加入组

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
public class JoinGroup extends ConnectionWatcher {
public void join(String groupName, String memberName) throws
KeeperException, InterruptedExcption {
String path = '/' + groupName + "/" + merberName;
String createPath = zk.create(Path, null/*data*/, Ids.OPEN_ACL_UNSAFE,
CreateMode.EPHEMETAL);
System.out.println("Created" + createdPath);
}
public static void main(String[] args) throws Exception {
JoinGroup joinGroup = new JoinGroup;
joinGroup.connect(args[0]);
joinGroup.join(args[1], args[2]);
//stay alive until process is killed or thread is interrupted
//随着进程的终止,这个短暂的znode被zookeeper删除
Thread.sleep(Long.MAX_VALUE);
}
}
//该辅助类等待与zookeeper建立连接
public class ConnectionWatcher implement Watcher {
private static final int SESSION_TIMEOUT = 5000;
protected 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();
}
@overide
public void process(WatcherEvent event) {
if (event.getState() == KeeperState.SyncConnected) {
connectedSignal.countDown();
}
}
public void close() throws InterruptedException {
zk.close();
}
}

列出组成员

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
public class ListGroup ectends ConnectionWatcher {
public void list(String groupName) throws KeeperException,
InterruptedException {
String path = "/" + groupName;
try {
List<String> childern = zk.getChildren(path, false); //false 为不设置观察标志
if (childern.isEmpty()) {
System.out.printf("no member in group %s\n", groupName);
System.exit(1);
}
for (String child : children) {
System.out.println(child);
}
} catch (KeeperExcption.NoNodeException e) {
System.out.println(groupName + "does not exist");
System.exit(1);
}
}
public static void main(String[] args) throws Exception {
ListGroup listGroup = new ListGroup();
listGroup.connect(args[0]);
listGroup.list(args[1]);
listGroup.close();
}
}

删除组

版本检测机制(通过设置为-1绕过此机制)
不支持递归操作

//删除一个组及其所有成员

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
public class DeleteGroup ectends ConnectionWatcher {
public void delete(String groupName) throws KeeperExcetion,
InterruptedException {
String path = "/" + groupName;
try {
List<String> children = zk.getChildren(path, false);
for (String child : children) {
zk.delete(path + "/" + child, -1);
}
zk.delete(path, -1);
} catch (KeeperException.NoNodeException e) {
System.out.println(groupName + "does not exist");
System.exit(1);
}
public static void main(String[] args) throws Exception {
DeleteGroup deleteGroup = new DeleteGroup();
deleteGroup.connect(args[0]);
deleteGroup.delete(args[1]);
deleteGroup.close();
}
}
}