Skip to content

Commit e7dcaf9

Browse files
committed
zookeeper test
1 parent 4fdfa2d commit e7dcaf9

File tree

9 files changed

+615
-0
lines changed

9 files changed

+615
-0
lines changed

java-zookeeper-demo/README.md

Lines changed: 245 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,245 @@
1+
# Zookeeper
2+
3+
## Install
4+
5+
> Download
6+
7+
http://zookeeper.apache.org/releases.html
8+
9+
> Start
10+
11+
```
12+
$ tar -zxf zookeeper-3.4.6.tar.gz
13+
$ cd zookeeper-3.4.6
14+
$ vi conf/zoo.cfg
15+
```
16+
17+
> default conf
18+
19+
```
20+
tickTime = 2000
21+
dataDir = /path/to/zookeeper/data
22+
clientPort = 2181
23+
initLimit = 5
24+
syncLimit = 2
25+
```
26+
27+
> Start
28+
29+
```
30+
$ bin/zkServer.sh start
31+
```
32+
33+
> Start CLI
34+
35+
```
36+
$ bin/zkCli.sh
37+
```
38+
39+
> Stop
40+
41+
```
42+
$ bin/zkServer.sh stop
43+
```
44+
45+
46+
## CLI
47+
48+
- <a href="#start-znodes">Start znodes</a>
49+
- <a href="#get-data">Get data</a>
50+
- <a href="#watch">Watch</a>
51+
- <a href="#set-data">Set data</a>
52+
- <a href="#children-sub-znode">Create Children / Sub-znode</a>
53+
54+
55+
<div id="start-znodes"></div>
56+
57+
### Start znodes
58+
59+
> Create
60+
create /path /data
61+
62+
```
63+
[zk: localhost:2181(CONNECTED) 0] create /FirstZnod "Myfirstz4zookeeper-app"
64+
Created /FirstZnod
65+
```
66+
67+
> Create Sequential znode
68+
create -s /path /data
69+
70+
```
71+
[zk: localhost:2181(CONNECTED) 1] create -s /FirstZnode second-data
72+
Created /FirstZnode0000000001
73+
```
74+
75+
> Create Ephemeral znode
76+
create -e /path /data
77+
78+
```
79+
[zk: localhost:2181(CONNECTED) 2] create -e /SecondZnode “Ephemeral-data”
80+
Created /SecondZnode
81+
```
82+
83+
=> client connection is lost => ephemeral znode will be deleted
84+
85+
<div id="get-data"></div>
86+
87+
### GetData
88+
89+
> Get data
90+
get /path
91+
92+
93+
```
94+
[zk: localhost:2181(CONNECTED) 13] get /FirstZnod
95+
Myfirstz4zookeeper-app
96+
cZxid = 0x4
97+
ctime = Tue Aug 21 20:42:59 KST 2018
98+
mZxid = 0x4
99+
mtime = Tue Aug 21 20:42:59 KST 2018
100+
pZxid = 0x4
101+
cversion = 0
102+
dataVersion = 0
103+
aclVersion = 0
104+
ephemeralOwner = 0x0
105+
dataLength = 22
106+
numChildren = 0
107+
[zk: localhost:2181(CONNECTED) 9] get /FirstZnode0000000001
108+
second-data
109+
cZxid = 0x5
110+
ctime = Tue Aug 21 20:43:25 KST 2018
111+
mZxid = 0x5
112+
mtime = Tue Aug 21 20:43:25 KST 2018
113+
pZxid = 0x5
114+
cversion = 0
115+
dataVersion = 0
116+
aclVersion = 0
117+
ephemeralOwner = 0x0
118+
dataLength = 11
119+
numChildren = 0
120+
[zk: localhost:2181(CONNECTED) 10]
121+
[zk: localhost:2181(CONNECTED) 11] get /SecondZnode
122+
“Ephemeral-data”
123+
cZxid = 0x6
124+
ctime = Tue Aug 21 20:46:14 KST 2018
125+
mZxid = 0x6
126+
mtime = Tue Aug 21 20:46:14 KST 2018
127+
pZxid = 0x6
128+
cversion = 0
129+
dataVersion = 0
130+
aclVersion = 0
131+
ephemeralOwner = 0x100068ccaa10000
132+
dataLength = 20
133+
numChildren = 0
134+
```
135+
136+
<div id="watch"></div>
137+
138+
### Watch
139+
140+
> watch
141+
get /path [watch] 1
142+
143+
```
144+
[zk: localhost:2181(CONNECTED) 14] get /FirstZnod 1
145+
Myfirstz4zookeeper-app
146+
cZxid = 0x4
147+
ctime = Tue Aug 21 20:42:59 KST 2018
148+
mZxid = 0x4
149+
mtime = Tue Aug 21 20:42:59 KST 2018
150+
pZxid = 0x4
151+
cversion = 0
152+
dataVersion = 0
153+
aclVersion = 0
154+
ephemeralOwner = 0x0
155+
dataLength = 22
156+
numChildren = 0
157+
```
158+
159+
=> it will wait for znode changes in the background
160+
161+
<div id="set-data"></div>
162+
163+
### Set data
164+
165+
> Set data & get
166+
set /path /data
167+
168+
```
169+
[zk: localhost:2181(CONNECTED) 15] set /SecondZnode Data-updated
170+
cZxid = 0x6
171+
ctime = Tue Aug 21 20:46:14 KST 2018
172+
mZxid = 0x7
173+
mtime = Tue Aug 21 20:55:22 KST 2018
174+
pZxid = 0x6
175+
cversion = 0
176+
dataVersion = 1
177+
aclVersion = 0
178+
ephemeralOwner = 0x100068ccaa10000
179+
dataLength = 12
180+
numChildren = 0
181+
```
182+
183+
184+
<div id="children-sub-znode"></div>
185+
186+
### Create Children / Sub-znode
187+
188+
> Create children
189+
create /parent/path/subnode/path /data
190+
191+
```
192+
[zk: localhost:2181(CONNECTED) 18] create /FirstZnod/Child1 firstchildren
193+
Created /FirstZnod/Child1
194+
[zk: localhost:2181(CONNECTED) 20] create /FirstZnod/Child2 secondchildren
195+
Created /FirstZnod/Child2
196+
```
197+
198+
> List children
199+
ls /path
200+
201+
```
202+
[zk: localhost:2181(CONNECTED) 24] ls /FirstZnod
203+
[Child2, Child1]
204+
```
205+
206+
> Stat
207+
stat /path
208+
209+
```
210+
[zk: localhost:2181(CONNECTED) 25] stat /FirstZnod
211+
cZxid = 0x4
212+
ctime = Tue Aug 21 20:42:59 KST 2018
213+
mZxid = 0x8
214+
mtime = Tue Aug 21 20:56:16 KST 2018
215+
pZxid = 0xc
216+
cversion = 2
217+
dataVersion = 1
218+
aclVersion = 0
219+
ephemeralOwner = 0x0
220+
dataLength = 12
221+
numChildren = 2
222+
```
223+
224+
> Remove
225+
rmr /path
226+
227+
```
228+
[zk: localhost:2181(CONNECTED) 26] rmr /FirstZnod
229+
[zk: localhost:2181(CONNECTED) 27] get /FirstZnod
230+
Node does not exist: /FirstZnod
231+
```
232+
233+
234+
---
235+
236+
## How to use zookeeper in java
237+
238+
1. Connect to the Zookeeper ensemble.
239+
=> assign a Session ID for the client
240+
241+
2. Send heartbeats to the server periodically
242+
243+
3. Get / Set the znodes as long as a session ID is active
244+
245+
4. Disconnect from the ZooKeeper ensemble, once all the tasks are completed

java-zookeeper-demo/pom.xml

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<project xmlns="http://maven.apache.org/POM/4.0.0"
3+
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
4+
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
5+
<modelVersion>4.0.0</modelVersion>
6+
7+
<groupId>com.zaccoding</groupId>
8+
<artifactId>demo</artifactId>
9+
<version>1.0-SNAPSHOT</version>
10+
11+
<dependencies>
12+
<dependency>
13+
<groupId>org.apache.zookeeper</groupId>
14+
<artifactId>zookeeper</artifactId>
15+
<version>3.4.13</version>
16+
</dependency>
17+
</dependencies>
18+
19+
<build>
20+
<plugins>
21+
<plugin>
22+
<groupId>org.apache.maven.plugins</groupId>
23+
<artifactId>maven-compiler-plugin</artifactId>
24+
<configuration>
25+
<source>1.8</source>
26+
<target>1.8</target>
27+
</configuration>
28+
</plugin>
29+
</plugins>
30+
</build>
31+
</project>
Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
package demo;
2+
3+
import org.apache.zookeeper.CreateMode;
4+
import org.apache.zookeeper.KeeperException;
5+
import org.apache.zookeeper.ZooDefs.Ids;
6+
import org.apache.zookeeper.ZooKeeper;
7+
8+
/**
9+
* https://www.tutorialspoint.com/zookeeper/zookeeper_api.htm
10+
*/
11+
public class ZKCreate {
12+
13+
private static ZooKeeper zk;
14+
15+
private static ZooKeeperConnection conn;
16+
17+
public static void create(String path, byte[] data) throws KeeperException, InterruptedException {
18+
// path :: Znode path
19+
// data :: data to store in a specified znode path
20+
// acl :: access control list of the node to be created
21+
// createMode :: the type of node
22+
zk.create(path, data, Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT);
23+
}
24+
25+
public static void main(String[] args) {
26+
String path = "/MyFirstZnode";
27+
byte[] data = "My first zookeeper app".getBytes();
28+
29+
try {
30+
conn = new ZooKeeperConnection();
31+
zk = conn.connect("192.168.5.77");
32+
create(path, data); // Create the data to the specified path
33+
conn.close();
34+
} catch (Exception e) {
35+
System.out.println(e.getMessage()); //Catch error message
36+
}
37+
38+
// check
39+
/*
40+
[zk: localhost:2181(CONNECTED) 0] get /MyFirstZnode
41+
My first zookeeper app
42+
cZxid = 0x12
43+
ctime = Tue Aug 21 21:41:31 KST 2018
44+
mZxid = 0x12
45+
mtime = Tue Aug 21 21:41:31 KST 2018
46+
pZxid = 0x12
47+
cversion = 0
48+
dataVersion = 0
49+
aclVersion = 0
50+
ephemeralOwner = 0x0
51+
dataLength = 22
52+
numChildren = 0
53+
*/
54+
55+
}
56+
}
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
package demo;
2+
3+
import org.apache.zookeeper.KeeperException;
4+
import org.apache.zookeeper.ZooKeeper;
5+
6+
/**
7+
* @author zacconding
8+
* @Date 2018-08-21
9+
* @GitHub : https://github.com/zacscoding
10+
*/
11+
public class ZKDelete {
12+
13+
private static ZooKeeper zk;
14+
private static ZooKeeperConnection conn;
15+
16+
public static void delete(String path) throws KeeperException, InterruptedException {
17+
// path :: Znode path
18+
// version :: Current version of the znode
19+
zk.delete(path, zk.exists(path, true).getVersion());
20+
}
21+
22+
public static void main(String[] args) {
23+
String path = "/MyFirstZone";
24+
25+
try {
26+
conn = new ZooKeeperConnection();
27+
zk = conn.connect("192.168.5.77");
28+
delete(path); //delete the node with the specified path
29+
} catch (Exception e) {
30+
e.printStackTrace();
31+
}
32+
}
33+
}

0 commit comments

Comments
 (0)