Zookeeper introduction
zookeeper is a distributed service framework, which is mainly used to solve data management problems in distributed applications, such as unified naming service, state synchronization service, cluster management, distributed application configuration item management, etc. zookeeper can be understood as a framework composed of file system + listening notification mechanism.
Detailed introduction can go Zookeeper official website Query,
1. File system
Zookeeper maintains a file system like data structure:
Each subdirectory item, such as NameService, is called as a znode (directory node). Like the file system, we can freely add and delete znode. The only difference is that znode can store data.
Because Zookeeper is used to keep "collaborative data", every znode file is very small, which is generally at the "kilobyte range" level.
There are four types of znode:
-
PERSISTENT - PERSISTENT directory node
After the client is disconnected from zookeeper, the node still exists
-
Persistent - persistent sequential number directory node
After the client is disconnected from zookeeper, the node still exists, but zookeeper numbers the node name in sequence
-
EPHEMERAL - temporary directory node
After the client is disconnected from zookeeper, the node is deleted
-
Ephemeral - temporary sequence number directory node
After the client is disconnected from zookeeper, the node is deleted, only zookeeper numbers the node name in sequence
2. Monitoring notification mechanism
Clients register to listen to the directory nodes they care about. When the directory node changes (data changes, is deleted, subdirectory nodes are added or deleted) or the connection between client and zookeeper server is disconnected, zookeeper will notify clients.
Zookeeper function
zookeeper has powerful functions, such as distributed application configuration management, unified naming service, state synchronization service, cluster management, etc. Let's take a simple distributed application configuration management as an example.
Suppose that our program is distributed and deployed on multiple machines. If we want to change the configuration file of the program, we need to modify it one by one, which is very troublesome. Now we put all these configurations on zookeeper and save them in a directory node of zookeeper. Then all related applications monitor this directory node. Once the configuration information changes, Each application will be notified by zookeeper, and then get new configuration information from zookeeper to apply to the system.
install
- To make sure the JAVA environment is configured, verify the environment: JAVA version
- Download and unzip zookeeper (download address: http://www.apache.org/dyn/closer.cgi/zookeeper)
cfg file interpretation
-
Ticketime: the basic unit of time used in Zookeeper, in milliseconds. This time is used as the interval between Zookeeper servers or between clients and servers to maintain the heartbeat, that is, each tick time will send a heartbeat.
-
dataDir: it can be any directory. It is the directory where Zookeeper saves data. By default, Zookeeper saves the log files that write data in this directory.
-
dataLogDir: log directory, which can also be any directory. If this parameter is not set, the same setting as "dataDir" will be used
-
clientPort: this port is the port where the client connects to the zookeeper server. Zookeeper will listen to this port and accept the client's access request.
-
initLimit: this configuration item is used to configure the Follower server connected to the Leader in the Zookeeper server cluster. The maximum number of heartbeat time intervals can be tolerated during the initial connection. When the value is 10, it means that the Zookeeper server has not received the return information from the Follower server after the time length of more than 10 heartbeats, which indicates that the client fails to connect. The total length of time is 10 * 2000 = 20 seconds
-
syncLimit: this configuration item identifies the time length of sending messages, requests and replies between the Leader and Follower. The maximum time length cannot exceed how many ticktimes. If the value is 5, the total time length is 5 * 2000 = 10 seconds
-
server.A=B: C: D: where a is a number, indicating the number of server; B is the ip address of the server; C is the port where the server exchanges information with the Leader server in the cluster; D is the port where a new Leader needs to be re elected in case the Leader server in the cluster hangs up It is the port used to communicate with each other when the election is executed. If it is a pseudo cluster configuration mode, because B is the same, the communication port numbers of different Zookeeper instances cannot be the same, so they need to be assigned different port numbers.
Single machine installation
1. Modify configuration
In the extracted file, there is a conf folder, in which there is a zoo sample.cfg file. Copy and rename it to zoo.cfg (or in the conf folder), and modify the file to:
tickTime=2000
#Fill in your own data directory
dataDir= /myenv/zookeeper-3.4.14/data
#Fill in your own data directory
dataLogDir=/myenv/zookeeper-3.4.14/logs
clientPort=2181
2. Start zookeeper
Entering bin directory to execute zkServer.sh start is not allowed. Use the following command at the upper level of bin
./bin/zkServer.sh start
The result is:
ZooKeeper JMX enabled by default
Using config: /Users/dingkaige/Documents/ProjectEnvironment/zookeeper/zookeeper-3.4.14/bin/.../conf/zoo.cfg
Starting zookeeper ... already running as process 15625.
3. View status
./bin/zkServer.sh status
The result is:
ZooKeeper JMX enabled by default
Using config: /Users/dingkaige/Documents/ProjectEnvironment/zookeeper/zookeeper-3.4.14/bin/.../conf/zoo.cfg
Mode: standalone
Mode is standalone because it is a stand-alone
4. Test connection
./bin/zkCli.sh
Result:
Connecting to localhost:2181
2020-01-11 14:37:12,287 [myid:] - INFO [main:Environment@100] - Client environment:zookeeper.version=3.4.14-4c25d480e66aadd371de8bd2fd8da255ac140bcf, built on 03/06/2019 16:18 GMT
2020-01-11 14:37:12,291 [myid:] - INFO [main:Environment@100] - Client environment:host.name=192.168.10.14
2020-01-11 14:37:12,291 [myid:] - INFO [main:Environment@100] - Client environment:java.version=1.8.0_201
2020-01-11 14:37:12,294 [myid:] - INFO [main:Environment@100] - Client environment:java.vendor=Oracle Corporation
2020-01-11 14:37:12,294 [myid:] - INFO [main:Environment@100] - Client environment:java.home=/Library/Java/JavaVirtualMachines/jdk1.8.0_201.jdk/Contents/Home/jre
2020-01-11 14:37:12,294 [myid:] - INFO [main:Environment@100] - Client environment:java.class.path=/Users/dingkaige/Documents/ProjectEnvironment/zookeeper/zookeeper-3.4.14/bin/.../zookeeper-server/target/classes:/Users/dingkaige/Documents/ProjectEnvironment/zookeeper/zookeeper-3.4.14/bin/.../build/classes:/Users/dingkaige/Documents/ProjectEnvironment/zookeeper/zookeeper-3.4.14/bin/.../zookeeper-server/target/lib/.jar:/Users/dingkaige/Documents/ProjectEnvironment/zookeeper/zookeeper-3.4.14/bin/.../build/lib/.jar:/Users/dingkaige/Documents/ProjectEnvironment/zookeeper/zookeeper-3.4.14/bin/.../lib/slf4j-log4j12-1.7.25.jar:/Users/dingkaige/Documents/ProjectEnvironment/zookeeper/zookeeper-3.4.14/bin/.../lib/slf4j-api-1.7.25.jar:/Users/dingkaige/Documents/ProjectEnvironment/zookeeper/zookeeper-3.4.14/bin/.../lib/netty-3.10.6.Final.jar:/Users/dingkaige/Documents/ProjectEnvironment/zookeeper/zookeeper-3.4.14/bin/.../lib/log4j-1.2.17.jar:/Users/dingkaige/Documents/ProjectEnvironment/zookeeper/zookeeper-3.4.14/bin/.../lib/jline-0.9.94.jar:/Users/dingkaige/Documents/ProjectEnvironment/zookeeper/zookeeper-3.4.14/bin/.../lib/audience-annotations-0.5.0.jar:/Users/dingkaige/Documents/ProjectEnvironment/zookeeper/zookeeper-3.4.14/bin/.../zookeeper-3.4.14.jar:/Users/dingkaige/Documents/ProjectEnvironment/zookeeper/zookeeper-3.4.14/bin/.../zookeeper-server/src/main/resources/lib/*.jar:/Users/dingkaige/Documents/ProjectEnvironment/zookeeper/zookeeper-3.4.14/bin/.../conf:
2020-01-11 14:37:12,295 [myid:] - INFO [main:Environment@100] - Client environment:java.library.path=/Users/dingkaige/Library/Java/Extensions:/Library/Java/Extensions:/Network/Library/Java/Extensions:/System/Library/Java/Extensions:/usr/lib/java:.
2020-01-11 14:37:12,295 [myid:] - INFO [main:Environment@100] - Client environment:java.io.tmpdir=/var/folders/c4/xktkts4j5d78zrnxt5b0vd4m0000gn/T/
2020-01-11 14:37:12,295 [myid:] - INFO [main:Environment@100] - Client environment:java.compiler=
2020-01-11 14:37:12,295 [myid:] - INFO [main:Environment@100] - Client environment:os.name=Mac OS X
2020-01-11 14:37:12,295 [myid:] - INFO [main:Environment@100] - Client environment:os.arch=x86_64
2020-01-11 14:37:12,295 [myid:] - INFO [main:Environment@100] - Client environment:os.version=10.15.1
2020-01-11 14:37:12,295 [myid:] - INFO [main:Environment@100] - Client environment:user.name=dingkaige
2020-01-11 14:37:12,295 [myid:] - INFO [main:Environment@100] - Client environment:user.home=/Users/dingkaige
2020-01-11 14:37:12,295 [myid:] - INFO [main:Environment@100] - Client environment:user.dir=/Users/dingkaige/Documents/ProjectEnvironment/zookeeper/zookeeper-3.4.14
2020-01-11 14:37:12,296 [myid:] - INFO [main:ZooKeeper@442] - Initiating client connection, connectString=localhost:2181 sessionTimeout=30000 watcher=org.apache.zookeeper.ZooKeeperMainMyWatcher@277050dcWelcometoZooKeeper!2020−01−1114:37:12,325[myid:]−INFO[main−SendThread(localhost:2181):ClientCnxnMyWatcher@277050dc Welcome to ZooKeeper! 2020-01-11 14:37:12,325 [myid:] - INFO [main-SendThread(localhost:2181):ClientCnxnMyWatcher@277050dcWelcometoZooKeeper!2020−01−1114:37:12,325[myid:]−INFO[main−SendThread(localhost:2181):ClientCnxnSendThread@1025] - Opening socket connection to server localhost/127.0.0.1:2181. Will not attempt to authenticate using SASL (unknown error)
JLine support is enabled
2020-01-11 14:37:12,395 [myid:] - INFO [main-SendThread(localhost:2181):ClientCnxnSendThread@879]−Socketconnectionestablishedtolocalhost/127.0.0.1:2181,initiatingsession2020−01−1114:37:12,411[myid:]−INFO[main−SendThread(localhost:2181):ClientCnxnSendThread@879] - Socket connection established to localhost/127.0.0.1:2181, initiating session 2020-01-11 14:37:12,411 [myid:] - INFO [main-SendThread(localhost:2181):ClientCnxnSendThread@879]−Socketconnectionestablishedtolocalhost/127.0.0.1:2181,initiatingsession2020−01−1114:37:12,411[myid:]−INFO[main−SendThread(localhost:2181):ClientCnxnSendThread@1299] - Session establishment complete on server localhost/127.0.0.1:2181, sessionid = 0x10006beaf43000b, negotiated timeout = 30000
WATCHER::
WatchedEvent state:SyncConnected type:None path:null
[zk: localhost:2181(CONNECTED) 0]
It indicates that the connection is successful and has been involved in the file system of Zookeeper.
5. stop
./bin/zkServer.sh stop
The result is:
ZooKeeper JMX enabled by default
Using config: /Users/dingkaige/Documents/ProjectEnvironment/zookeeper/zookeeper-3.4.14/bin/.../conf/zoo.cfg
Stopping zookeeper ... STOPPED
Cluster mode
In the example, the pseudo cluster mode is set up, that is, three zookeeper instances are started on one machine to form a cluster. The real cluster mode is that the IP address of the instance is different, and the building method is not different
1. Modify configuration
Copy and rename the zoo_sample.cfg file to zoo-1.cfg (or in the conf folder) and change the file to:
tickTime=2000
dataDir= /myenv/zookeeper-3.4.14/data1(Fill in your own data Catalog)
dataLogDir=/myenv/zookeeper-3.4.14/logs 1 (Fill in your own data Catalog)
clientPort=2181
initLimit=10
syncLimit=2
server.1=127.0.0.1:2888:3888
server.2=127.0.0.1:2889:3889
server.3=127.0.0.1:2890:3890
Copy two copies of the zoo-1.cfg file and rename it to zoo-2.cfg and zoo-3.cfg (still in the conf folder) and modify the files to:
#zoo-2.cfg
tickTime=2000
dataDir= /myenv/zookeeper-3.4.14/data2(Fill in your own data Catalog)
dataLogDir=/myenv/zookeeper-3.4.14/logs2(Fill in your own data Catalog)
clientPort=2182
initLimit=10
syncLimit=2
server.1=127.0.0.1:2888:3888
server.2=127.0.0.1:2889:3889
server.3=127.0.0.1:2890:3890
#zoo-3.cfg
tickTime=2000
#Fill in your own data directory
dataDir= /myenv/zookeeper-3.4.14/data3
#Fill in your own data directory
dataLogDir=/myenv/zookeeper-3.4.14/logs3
clientPort=2183
initLimit=10
syncLimit=2
server.1=127.0.0.1:2888:3888
server.2=127.0.0.1:2889:3889
server.3=127.0.0.1:2890:3890
Add the myid file in data1, data2 and data3. The values in the file are 1, 2 and 3 respectively (that is, the number corresponding to server.x in the cfg file).
The configuration correspondence in this example is as follows:
myid | Data directory | Client | Server | Leader | Profile name |
---|---|---|---|---|---|
1 | /myenv/zookeeper-3.4.14/data1 | 2181 | 2888 | 3888 | zoo-1.cfg |
2 | /myenv/zookeeper-3.4.14/data2 | 2182 | 2889 | 3889 | zoo-2.cfg |
3 | /myenv/zookeeper-3.4.14/data3 | 2183 | 2890 | 3890 | zoo-3.cfg |
Precautions for parameter configuration:
- In the same ZooKeeper cluster, the identities of different ZooKeeper Server processes need to be different, that is, the values in the myid file need to be different
- In pseudo cluster mode, attention should be paid to the unsharability of "directory, port number" and other resources. If shared, ZooKeeper Server process will not work normally. For example, "data directory, several listening port numbers" cannot be shared
2. Start zookeeper
Execute the following three commands respectively
./bin/zkServer.sh start ./conf/zoo-1.cfg
./bin/zkServer.sh start ./conf/zoo-2.cfg
./bin/zkServer.sh start ./conf/zoo-3.cfg
Be sure to execute three commands and then execute other commands. If you execute one of them and then execute other commands such as status command, zookeeper will report an error, indicating that zookeeper is not started.
3. View status
./bin/zkServer.sh status ./conf/zoo-1.cfg
The result is:
ZooKeeper JMX enabled by default
Using config: ./conf/zoo-1.cfg
Mode: follower
In the same way, you can see zoo-2 and zoo-3. There are two modes: follower and leader
4. Test connection
./bin/zkCli.sh -server 127.0.0.1:2181,127.0.0.1:2182,127.0.0.1:2183
The result is:
Connecting to 127.0.0.1:2181,127.0.0.1:2182,127.0.0.1:2183
2020-01-11 15:57:38,390 [myid:] - INFO [main:Environment@100] - Client environment:zookeeper.version=3.4.14-4c25d480e66aadd371de8bd2fd8da255ac140bcf, built on 03/06/2019 16:18 GMT
2020-01-11 15:57:38,394 [myid:] - INFO [main:Environment@100] - Client environment:host.name=192.168.10.14
2020-01-11 15:57:38,395 [myid:] - INFO [main:Environment@100] - Client environment:java.version=1.8.0_201
2020-01-11 15:57:38,398 [myid:] - INFO [main:Environment@100] - Client environment:java.vendor=Oracle Corporation
2020-01-11 15:57:38,398 [myid:] - INFO [main:Environment@100] - Client environment:java.home=/Library/Java/JavaVirtualMachines/jdk1.8.0_201.jdk/Contents/Home/jre
2020-01-11 15:57:38,398 [myid:] - INFO [main:Environment@100] - Client environment:java.class.path=/Users/dingkaige/Documents/ProjectEnvironment/zookeeper/zookeeper-3.4.14/bin/.../zookeeper-server/target/classes:/Users/dingkaige/Documents/ProjectEnvironment/zookeeper/zookeeper-3.4.14/bin/.../build/classes:/Users/dingkaige/Documents/ProjectEnvironment/zookeeper/zookeeper-3.4.14/bin/.../zookeeper-server/target/lib/.jar:/Users/dingkaige/Documents/ProjectEnvironment/zookeeper/zookeeper-3.4.14/bin/.../build/lib/.jar:/Users/dingkaige/Documents/ProjectEnvironment/zookeeper/zookeeper-3.4.14/bin/.../lib/slf4j-log4j12-1.7.25.jar:/Users/dingkaige/Documents/ProjectEnvironment/zookeeper/zookeeper-3.4.14/bin/.../lib/slf4j-api-1.7.25.jar:/Users/dingkaige/Documents/ProjectEnvironment/zookeeper/zookeeper-3.4.14/bin/.../lib/netty-3.10.6.Final.jar:/Users/dingkaige/Documents/ProjectEnvironment/zookeeper/zookeeper-3.4.14/bin/.../lib/log4j-1.2.17.jar:/Users/dingkaige/Documents/ProjectEnvironment/zookeeper/zookeeper-3.4.14/bin/.../lib/jline-0.9.94.jar:/Users/dingkaige/Documents/ProjectEnvironment/zookeeper/zookeeper-3.4.14/bin/.../lib/audience-annotations-0.5.0.jar:/Users/dingkaige/Documents/ProjectEnvironment/zookeeper/zookeeper-3.4.14/bin/.../zookeeper-3.4.14.jar:/Users/dingkaige/Documents/ProjectEnvironment/zookeeper/zookeeper-3.4.14/bin/.../zookeeper-server/src/main/resources/lib/*.jar:/Users/dingkaige/Documents/ProjectEnvironment/zookeeper/zookeeper-3.4.14/bin/.../conf:
2020-01-11 15:57:38,398 [myid:] - INFO [main:Environment@100] - Client environment:java.library.path=/Users/dingkaige/Library/Java/Extensions:/Library/Java/Extensions:/Network/Library/Java/Extensions:/System/Library/Java/Extensions:/usr/lib/java:.
2020-01-11 15:57:38,398 [myid:] - INFO [main:Environment@100] - Client environment:java.io.tmpdir=/var/folders/c4/xktkts4j5d78zrnxt5b0vd4m0000gn/T/
2020-01-11 15:57:38,398 [myid:] - INFO [main:Environment@100] - Client environment:java.compiler=
2020-01-11 15:57:38,399 [myid:] - INFO [main:Environment@100] - Client environment:os.name=Mac OS X
2020-01-11 15:57:38,399 [myid:] - INFO [main:Environment@100] - Client environment:os.arch=x86_64
2020-01-11 15:57:38,399 [myid:] - INFO [main:Environment@100] - Client environment:os.version=10.15.1
2020-01-11 15:57:38,399 [myid:] - INFO [main:Environment@100] - Client environment:user.name=dingkaige
2020-01-11 15:57:38,399 [myid:] - INFO [main:Environment@100] - Client environment:user.home=/Users/dingkaige
2020-01-11 15:57:38,399 [myid:] - INFO [main:Environment@100] - Client environment:user.dir=/Users/dingkaige/Documents/ProjectEnvironment/zookeeper/zookeeper-3.4.14
2020-01-11 15:57:38,400 [myid:] - INFO [main:ZooKeeper@442] - Initiating client connection, connectString=127.0.0.1:2181,127.0.0.1:2182,127.0.0.1:2183 sessionTimeout=30000 watcher=org.apache.zookeeper.ZooKeeperMainMyWatcher@277050dcWelcometoZooKeeper!2020−01−1115:57:38,478[myid:]−INFO[main−SendThread(localhost:2183):ClientCnxnMyWatcher@277050dc Welcome to ZooKeeper! 2020-01-11 15:57:38,478 [myid:] - INFO [main-SendThread(localhost:2183):ClientCnxnMyWatcher@277050dcWelcometoZooKeeper!2020−01−1115:57:38,478[myid:]−INFO[main−SendThread(localhost:2183):ClientCnxnSendThread@1025] - Opening socket connection to server localhost/127.0.0.1:2183. Will not attempt to authenticate using SASL (unknown error)
JLine support is enabled
2020-01-11 15:57:38,644 [myid:] - INFO [main-SendThread(localhost:2183):ClientCnxnSendThread@879]−Socketconnectionestablishedtolocalhost/127.0.0.1:2183,initiatingsession2020−01−1115:57:38,678[myid:]−INFO[main−SendThread(localhost:2183):ClientCnxnSendThread@879] - Socket connection established to localhost/127.0.0.1:2183, initiating session 2020-01-11 15:57:38,678 [myid:] - INFO [main-SendThread(localhost:2183):ClientCnxnSendThread@879]−Socketconnectionestablishedtolocalhost/127.0.0.1:2183,initiatingsession2020−01−1115:57:38,678[myid:]−INFO[main−SendThread(localhost:2183):ClientCnxnSendThread@1299] - Session establishment complete on server localhost/127.0.0.1:2183, sessionid = 0x300080837100000, negotiated timeout = 30000
WATCHER::
WatchedEvent state:SyncConnected type:None path:null
[zk: 127.0.0.1:2181,127.0.0.1:2182,127.0.0.1:2183(CONNECTED) 0]
5. stop
./bin/zkServer.sh stop ./conf/zoo-1.cfg
./bin/zkServer.sh stop ./conf/zoo-2.cfg
./bin/zkServer.sh stop ./conf/zoo-3.cfg
Frequently used commands
You can operate on the data after executing. / bin/zkCli.sh command
1.ls command to view the contents of the current ZooKeeper
[zk: localhost:2181(CONNECTED) 12] ls /
[zookeeper]
2.create / command, create znode
[zk: localhost:2181(CONNECTED) 13] create /username ding
Created /username
3.get / command to get the znode node
[zk: localhost:2181(CONNECTED) 14] get /username
ding
cZxid = 0x3a
ctime = Mon Jan 13 10:21:09 CST 2020
mZxid = 0x3a
mtime = Mon Jan 13 10:21:09 CST 2020
pZxid = 0x3a
cversion = 0
dataVersion = 0
aclVersion = 0
ephemeralOwner = 0x0
dataLength = 4
numChildren = 0
4.set / command reassigns the value to the znode node
[zk: localhost:2181(CONNECTED) 15] set /username dingding
cZxid = 0x3a
ctime = Mon Jan 13 10:21:09 CST 2020
mZxid = 0x3b
mtime = Mon Jan 13 10:28:28 CST 2020
pZxid = 0x3a
cversion = 0
dataVersion = 1
aclVersion = 0
ephemeralOwner = 0x0
dataLength = 8
numChildren = 0
5.delete / delete the znode node
[zk: localhost:2181(CONNECTED) 16] delete /username
Making a client in Java
1. First create a username with the value ding in zookeeper
[zk: localhost:2181(CONNECTED) 18] create /username ding
Created /username
[zk: localhost:2181(CONNECTED) 19] get /username
ding
cZxid = 0x3d
ctime = Mon Jan 13 10:35:12 CST 2020
mZxid = 0x3d
mtime = Mon Jan 13 10:35:12 CST 2020
pZxid = 0x3d
cversion = 0
dataVersion = 0
aclVersion = 0
ephemeralOwner = 0x0
dataLength = 4
numChildren = 0
2. code
maven
<dependency> <groupId>org.apache.zookeeper</groupId> <artifactId>zookeeper</artifactId> <version>3.4.14</version> <!-- <type>pom</type> --> </dependency>
Code:
package com.edu.springboot.mybatis.testFile; import org.apache.zookeeper.WatchedEvent; import org.apache.zookeeper.Watcher; import org.apache.zookeeper.ZooKeeper; import org.apache.zookeeper.data.Stat; import java.util.concurrent.CountDownLatch; /** * @description: zookeeper test * @program: sbm * @author: dingkaige * @date: 2020-01-10 12:27 **/ public class ZooKeeperProSyncTest implements Watcher { private static CountDownLatch connectedSemaphore = new CountDownLatch(1); private static ZooKeeper zk = null; private static Stat stat = new Stat(); public static void main(String[] args) throws Exception { //Zookeeper configuration data storage path String path = "/username"; //Connect Zookeeper and register a default listener, indicating the IP and port of the server zk = new ZooKeeper("127.0.0.1:2181", 5000, new ZooKeeperProSyncTest()); //Wait for notification of zk connection success connectedSemaphore.await(); //Get the configuration data of the path directory node and register the default listener System.out.println(new String(zk.getData(path, true, stat))); Thread.sleep(Integer.MAX_VALUE); } @Override public void process(WatchedEvent event) { if (Event.KeeperState.SyncConnected == event.getState()) { //zk connection success notification event if ( Event.EventType.None == event.getType() && null == event.getPath() ) { connectedSemaphore.countDown(); } else if (event.getType() == Event.EventType.NodeDataChanged) { //zk directory node data change notification event try { System.out.println("The configuration has been modified and the new value is:" + new String(zk.getData(event.getPath(), true, stat))); } catch (Exception e) { e.printStackTrace(); } } } else { } } }
Operation result:
log4j:WARN No appenders could be found for logger (org.apache.zookeeper.ZooKeeper). log4j:WARN Please initialize the log4j system properly. log4j:WARN See http://logging.apache.org/log4j/1.2/faq.html#noconfig for more info. ding
Use the terminal to connect to Zookeeper's server and change the configuration of znode:
[zk: localhost:2181(CONNECTED) 20] set /username dingding
cZxid = 0x3d
ctime = Mon Jan 13 10:35:12 CST 2020
mZxid = 0x3f
mtime = Mon Jan 13 10:39:39 CST 2020
pZxid = 0x3d
cversion = 0
dataVersion = 1
aclVersion = 0
ephemeralOwner = 0x0
dataLength = 8
numChildren = 0
Console print results:
Configuration modified, new value: dinging
It can be seen that the information has become "dinging", indicating that the test is successful!