Skip to content

Commit 4daa3d8

Browse files
authored
Create Zookeeper(五).md
1 parent 4e17ef0 commit 4daa3d8

File tree

1 file changed

+211
-0
lines changed

1 file changed

+211
-0
lines changed

Zookeeper/Zookeeper(五).md

+211
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,211 @@
1+
Zookeeper实战
2+
---
3+
### 1、IDEA环境搭建
4+
1)创建一个Maven Module
5+
2)添加pom文件
6+
```xml
7+
<dependencies>
8+
<dependency>
9+
<groupId>junit</groupId>
10+
<artifactId>junit</artifactId>
11+
<version>RELEASE</version>
12+
</dependency>
13+
<dependency>
14+
<groupId>org.apache.logging.log4j</groupId>
15+
<artifactId>log4j-core</artifactId>
16+
<version>2.8.2</version>
17+
</dependency>
18+
<dependency>
19+
<groupId>org.apache.zookeeper</groupId>
20+
<artifactId>zookeeper</artifactId>
21+
<version>3.5.7</version>
22+
</dependency>
23+
</dependencies>
24+
```
25+
3)拷贝log4j.properties文件到项目根目录
26+
&emsp; 需要在项目的src/main/resources目录下,新建一个文件,命名为“log4j.properties”,在文件中填入。
27+
```xml
28+
log4j.rootLogger=INFO, stdout
29+
log4j.appender.stdout=org.apache.log4j.ConsoleAppender
30+
log4j.appender.stdout.layout=org.apache.log4j.PatternLayout
31+
log4j.appender.stdout.layout.ConversionPattern=%d %p [%c] - %m%n
32+
log4j.appender.logfile=org.apache.log4j.FileAppender
33+
log4j.appender.logfile.File=target/spring.log
34+
log4j.appender.logfile.layout=org.apache.log4j.PatternLayout
35+
log4j.appender.logfile.layout.ConversionPattern=%d %p [%c] - %m%n
36+
```
37+
### 2、初始化ZooKeeper客户端
38+
```java
39+
public class Zookeeper {
40+
41+
private String connectString;
42+
private int sessionTimeout;
43+
private ZooKeeper zkClient;
44+
45+
@Before //获取客户端对象
46+
public void init() throws IOException {
47+
48+
connectString = "hadoop102:2181,hadoop103:2181,hadoop104:2181";
49+
int sessionTimeout = 10000;
50+
51+
//参数解读 1集群连接字符串 2连接超时时间 单位:毫秒 3当前客户端默认的监控器
52+
zkClient = new ZooKeeper(connectString, sessionTimeout, new Watcher() {
53+
@Override
54+
public void process(WatchedEvent event) {
55+
}
56+
});
57+
}
58+
59+
@After //关闭客户端对象
60+
public void close() throws InterruptedException {
61+
zkClient.close();
62+
}
63+
}
64+
```
65+
### 3、获取子节点列表,不监听
66+
```java
67+
@Test
68+
public void ls() throws IOException, KeeperException, InterruptedException {
69+
//用客户端对象做各种操作
70+
List<String> children = zkClient.getChildren("/", false);
71+
System.out.println(children);
72+
}
73+
```
74+
### 4、获取子节点列表,并监听
75+
```java
76+
@Test
77+
public void lsAndWatch() throws KeeperException, InterruptedException {
78+
List<String> children = zkClient.getChildren("/atguigu", new Watcher() {
79+
@Override
80+
public void process(WatchedEvent event) {
81+
System.out.println(event);
82+
}
83+
});
84+
System.out.println(children);
85+
86+
//因为设置了监听,所以当前线程不能结束
87+
Thread.sleep(Long.MAX_VALUE);
88+
}
89+
```
90+
### 5、创建子节点
91+
```java
92+
@Test
93+
public void create() throws KeeperException, InterruptedException {
94+
//参数解读 1节点路径 2节点存储的数据
95+
//3节点的权限(使用Ids选个OPEN即可) 4节点类型 短暂 持久 短暂带序号 持久带序号
96+
String path = zkClient.create("/atguigu", "shanguigu".getBytes(), ZooDefs.Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT);
97+
98+
//创建临时节点
99+
//String path = zkClient.create("/atguigu2", "shanguigu".getBytes(), ZooDefs.Ids.OPEN_ACL_UNSAFE, CreateMode.EPHEMERAL);
100+
101+
System.out.println(path);
102+
103+
//创建临时节点的话,需要线程阻塞
104+
//Thread.sleep(10000);
105+
}
106+
```
107+
### 6、判断Znode是否存在
108+
```java
109+
@Test
110+
public void exist() throws Exception {
111+
112+
Stat stat = zkClient.exists("/atguigu", false);
113+
114+
System.out.println(stat == null ? "not exist" : "exist");
115+
}
116+
```
117+
### 7、获取子节点存储的数据,不监听
118+
```java
119+
@Test
120+
public void get() throws KeeperException, InterruptedException {
121+
//判断节点是否存在
122+
Stat stat = zkClient.exists("/atguigu", false);
123+
if (stat == null) {
124+
System.out.println("节点不存在...");
125+
return;
126+
}
127+
128+
byte[] data = zkClient.getData("/atguigu", false, stat);
129+
System.out.println(new String(data));
130+
}
131+
```
132+
### 8、获取子节点存储的数据,并监听
133+
```java
134+
@Test
135+
public void getAndWatch() throws KeeperException, InterruptedException {
136+
//判断节点是否存在
137+
Stat stat = zkClient.exists("/atguigu", false);
138+
if (stat == null) {
139+
System.out.println("节点不存在...");
140+
return;
141+
}
142+
143+
byte[] data = zkClient.getData("/atguigu", new Watcher() {
144+
@Override
145+
public void process(WatchedEvent event) {
146+
System.out.println(event);
147+
}
148+
}, stat);
149+
System.out.println(new String(data));
150+
//线程阻塞
151+
Thread.sleep(Long.MAX_VALUE);
152+
}
153+
```
154+
### 9、设置节点的值
155+
```java
156+
@Test
157+
public void set() throws KeeperException, InterruptedException {
158+
//判断节点是否存在
159+
Stat stat = zkClient.exists("/atguigu", false);
160+
if (stat == null) {
161+
System.out.println("节点不存在...");
162+
return;
163+
}
164+
//参数解读 1节点路径 2节点的值 3版本号
165+
zkClient.setData("/atguigu", "sgg".getBytes(), stat.getVersion());
166+
}
167+
```
168+
### 10、删除空节点
169+
```java
170+
@Test
171+
public void delete() throws KeeperException, InterruptedException {
172+
//判断节点是否存在
173+
Stat stat = zkClient.exists("/aaa", false);
174+
if (stat == null) {
175+
System.out.println("节点不存在...");
176+
return;
177+
}
178+
zkClient.delete("/aaa", stat.getVersion());
179+
}
180+
```
181+
### 11、删除非空节点,递归实现
182+
```java
183+
//封装一个方法,方便递归调用
184+
public void deleteAll(String path, ZooKeeper zk) throws KeeperException, InterruptedException {
185+
//判断节点是否存在
186+
Stat stat = zkClient.exists(path, false);
187+
if (stat == null) {
188+
System.out.println("节点不存在...");
189+
return;
190+
}
191+
//先获取当前传入节点下的所有子节点
192+
List<String> children = zk.getChildren(path, false);
193+
if (children.isEmpty()) {
194+
//说明传入的节点没有子节点,可以直接删除
195+
zk.delete(path, stat.getVersion());
196+
} else {
197+
//如果传入的节点有子节点,循环所有子节点
198+
for (String child : children) {
199+
//删除子节点,但是不知道子节点下面还有没有子节点,所以递归调用
200+
deleteAll(path + "/" + child, zk);
201+
}
202+
//删除完所有子节点以后,记得删除传入的节点
203+
zk.delete(path, stat.getVersion());
204+
}
205+
}
206+
//测试deleteAll
207+
@Test
208+
public void testDeleteAll() throws KeeperException, InterruptedException {
209+
deleteAll("/atguigu",zkClient);
210+
}
211+
```

0 commit comments

Comments
 (0)