添加maven依赖

1
2
3
4
5
6
7
<dependencies>
<dependency>
<groupId>org.apache.zookeeper</groupId>
<artifactId>zookeeper</artifactId>
<version>3.4.8</version>
</dependency>
</dependencies>

示例代码

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
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
package com.bai.javaapi;

import org.apache.zookeeper.*;
import org.apache.zookeeper.data.Stat;

import java.io.IOException;
import java.util.List;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.TimeUnit;

/**
* Created by 2bai on 24/10/2017.
* 一般情况下,不需要客户端手动注册监听事件
* 此处只为演示原生API使用
*/
public class ApiOperatorDemo implements Watcher {
private final static String CONNECTSTRING = "123.57.3.144:2184";
private static CountDownLatch countDownLatch = new CountDownLatch(1);
private static ZooKeeper zooKeeper;
private static Stat stat = new Stat();

public static void main(String[] args) throws IOException, InterruptedException, KeeperException {
zooKeeper = new ZooKeeper(CONNECTSTRING, 5000, new ApiOperatorDemo());
countDownLatch.await();
System.out.println(zooKeeper.getState());
// 创建结果
String result = zooKeeper.create("/test", "123".getBytes(), ZooDefs.Ids.OPEN_ACL_UNSAFE, CreateMode.EPHEMERAL);
// 增加一个watch
zooKeeper.getData("/test", new ApiOperatorDemo(), stat);
System.out.println("创建结果:" + result);

TimeUnit.SECONDS.sleep(2);

// 修改数据
// -1表示忽略版本号
zooKeeper.setData("/test", "456".getBytes(), -1);
TimeUnit.SECONDS.sleep(2);

// 再次修改,测试watch事件是一次性的
zooKeeper.setData("/test", "789".getBytes(), -1);
TimeUnit.SECONDS.sleep(2);

// 数据删除
zooKeeper.delete("/test", -1);
TimeUnit.SECONDS.sleep(2);


String re = "";
//创建节点和子节点
String fatherNodePath = "/testChild";
String sonNodePath="/test";

stat = zooKeeper.exists(fatherNodePath+sonNodePath, true);
//节点不存在
System.out.println(stat);
if (stat != null) {
//先删除子节点
zooKeeper.delete(fatherNodePath + sonNodePath, -1);
//再删除父节点
zooKeeper.delete(fatherNodePath, -1);
}
//临时节点下面不能挂载子节点
//要挂载子节点必须创建持久化节点
re = zooKeeper.create(fatherNodePath, "123".getBytes(), ZooDefs.Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT);
System.out.println("创建/testChild节点成功---" + re);
TimeUnit.SECONDS.sleep(1);
re = zooKeeper.create(fatherNodePath + "/test", "456".getBytes(), ZooDefs.Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT);
System.out.println("创建/testChild/test节点成功---" + re);
TimeUnit.SECONDS.sleep(1);

//修改子路径
zooKeeper.setData(fatherNodePath + sonNodePath, "789".getBytes(), -1);
TimeUnit.SECONDS.sleep(1);

//获取指定节点下的子节点
List<String> children=zooKeeper.getChildren(fatherNodePath+sonNodePath,true);
System.out.println(children);
}

public void process(WatchedEvent watchedEvent) {
// 如果当前的连接状态是连接成功,那么就通过计数器去控制
if (watchedEvent.getState() == Event.KeeperState.SyncConnected) {
if (Event.EventType.None == watchedEvent.getType() && null == watchedEvent.getPath()) {
countDownLatch.countDown();
System.out.println(watchedEvent.getState() + "--->" + watchedEvent.getType());
} else {
switch (watchedEvent.getType()) {
case None:
break;
case NodeCreated:// 创建节点
try {
System.out.println("NodeCreated---路径:" + watchedEvent.getPath() + "---值:" + zooKeeper.getData(watchedEvent.getPath(), true, stat));
} catch (KeeperException e) {
e.printStackTrace();
} catch (InterruptedException e) {
e.printStackTrace();
}
break;
case NodeDeleted:// 删除节点
System.out.println("NodeDeleted---路径:" + watchedEvent.getPath());
break;
case NodeDataChanged:// 修改节点
try {
System.out.println("NodeDataChanged---路径:" + watchedEvent.getPath() + "---值:" + zooKeeper.getData(watchedEvent.getPath(), true, stat));
} catch (KeeperException e) {
e.printStackTrace();
} catch (InterruptedException e) {
e.printStackTrace();
}
break;
case NodeChildrenChanged: // 修改子节点
try {
System.out.println("NodeChildrenChanged---路径:" + watchedEvent.getPath() + "---值:" + zooKeeper.getData(watchedEvent.getPath(), true, stat));
} catch (KeeperException e) {
e.printStackTrace();
} catch (InterruptedException e) {
e.printStackTrace();
}
break;
}
}
}
}
}