1. Command line syntax
Basic command syntax | Function description |
---|---|
help | Display all operation commands |
ls path | Use the ls command to view the child nodes of the current znode - w: listen for child node changes - s additional secondary information |
create | Normal creation - s contains sequence - e: temporary (restart or timeout disappears) |
get path | Get the value of the node [listenable] - w: listen for changes in the content of child nodes - s: add secondary information |
set | Set the specific value of the node |
stat | View node status |
delete | Delete node |
deleteall | Recursively delete nodes |
1.1 client startup
root@ubuntu:/usr/local/software/zookeeper/bin# zkCli.sh -server 192.168.44.133:2181
Display connection 192.168.44.133:
[zk: 192.168.44.133:2181(CONNECTED) 0]
1.2. Display all operation commands
[zk: 192.168.44.133:2181(CONNECTED) 4] help ZooKeeper -server host:port -client-configuration properties-file cmd args addWatch [-m mode] path # optional mode is one of [PERSISTENT, PERSISTENT_RECURSIVE] - default is PERSISTENT_RECURSIVE addauth scheme auth close config [-c] [-w] [-s] connect host:port create [-s] [-e] [-c] [-t ttl] path [data] [acl] delete [-v version] path deleteall path [-b batch size] delquota [-n|-b] path get [-s] [-w] path getAcl [-s] path getAllChildrenNumber path getEphemerals path history listquota path ls [-s] [-w] [-R] path printwatches on|off quit reconfig [-s] [-v version] [[-file path] | [-members serverID=host:port1:port2;port3[,...]*]] | [-add serverId=host:port1:port2;port3[,...]]* [-remove serverId[,...]*] redo cmdno removewatches path [-c|-d|-a] [-l] set [-s] [-v version] path data setAcl [-s] [-v version] [-R] path acl setquota -n|-b val path stat [-w] path sync path version
2. znode node data information
2.1. View the contents contained in the current znode
[zk: 192.168.44.133:2181(CONNECTED) 5] ls / [zookeeper]
2.2. View detailed data of current node
[zk: 192.168.44.133:2181(CONNECTED) 7] ls -s / [zookeeper] cZxid = 0x0 ctime = Wed Dec 31 16:00:00 PST 1969 mZxid = 0x0 mtime = Wed Dec 31 16:00:00 PST 1969 pZxid = 0x0 cversion = -1 dataVersion = 0 aclVersion = 0 ephemeralOwner = 0x0 dataLength = 0 numChildren = 1
Details:
1) cZxid: create the transaction zxid of the node
Each modification of Zookeeper status will generate a Zookeeper transaction id, which is the total order of all modifications in Zookeeper. If zxid1 < zxid2, zxid1 occurs before zxid2
2) ctime: the number of milliseconds that znode was created (since 1969)
3) mZxid: transaction zxid last updated by znode
4) mtime: the number of milliseconds the znode was last modified (since 1969)
5) pZxid: the last updated child node zxid of znode
6) cversion: change number of znode child node and modification times of znode child node
7) Data version: znode data change number
8) dataLength: the data length of znode
9) numChildren: number of child nodes of znode
2.3 node type and node creation
Persistent: after the client and server are disconnected, the created node is not deleted
Short: after the client and server are disconnected, the created node is deleted by itself
Note: the sequence number is a monotonically increasing counter maintained by the parent node
1) Create persistent unordered
[zk: 192.168.232.1(CONNECTED) 6] ls -s /sanguo [] cZxid = 0xb66 ctime = Sun Dec 05 15:59:00 CST 2021 mZxid = 0xb66 mtime = Sun Dec 05 15:59:00 CST 2021 pZxid = 0xb66 cversion = 0 dataVersion = 0 aclVersion = 0 ephemeralOwner = 0x0 dataLength = 6 numChildren = 0 [zk: 192.168.232.1(CONNECTED) 7] create /sanguo/shuguo Created /sanguo/shuguo [zk: 192.168.232.1(CONNECTED) 8] ls -s /sanguo [shuguo] cZxid = 0xb66 ctime = Sun Dec 05 15:59:00 CST 2021 mZxid = 0xb66 mtime = Sun Dec 05 15:59:00 CST 2021 pZxid = 0xb67 cversion = 1 dataVersion = 0 aclVersion = 0 ephemeralOwner = 0x0 dataLength = 6 numChildren = 1 [zk: 192.168.247.1(CONNECTED) 9]
Gets the value of the node
[zk: 192.168.232.1(CONNECTED) 9] get -s /sanguo liubei cZxid = 0xb66 ctime = Sun Dec 05 15:59:00 CST 2021 mZxid = 0xb66 mtime = Sun Dec 05 15:59:00 CST 2021 pZxid = 0xb67 cversion = 1 dataVersion = 0 aclVersion = 0 ephemeralOwner = 0x0 dataLength = 6 numChildren = 1
2) Create a persistent and orderly
create -s /sanguo/weiguo add a - s
[zk: 192.168.232.1(CONNECTED) 12] create -s /sanguo/weiguo Created /sanguo/weiguo0000000001 [zk: 192.168.232.1(CONNECTED) 13] ls /sanguo/ shuguo weiguo0000000001
Lasting? Exit the connection and check again if it still exists?
[zk: 192.168.232.1(CONNECTED) 13] quit :/usr/local/software/zookeeper/bin# ./zkCli.sh -server 192.168.232.1 [zk: 192.168.232.1(CONNECTED) 0] ls /sanguo/ Path must not end with / character [zk: 192.168.232.1(CONNECTED) 1] ls /sanguo [shuguo, weiguo0000000001]
3) Create temporary unordered
[zk: 192.168.232.1(CONNECTED) 2] create -e /sanguo/wuguo Created /sanguo/wuguo [zk: 192.168.232.1(CONNECTED) 3] ls /sanguo [shuguo, weiguo0000000001, wuguo] [zk: 192.168.232.1(CONNECTED) 4] quit WatchedEvent state:SyncConnected type:None path:null [zk: 192.168.232.1(CONNECTED) 0] ls /sanguo [shuguo, weiguo0000000001]
4) Create temporary ordered
[zk: 192.168.232.1(CONNECTED) 1] create -e -s /sanguo/wuguo "Sun Quan" Created /sanguo/wuguo0000000003 [zk: 192.168.232.1(CONNECTED) 4] quit [zk: 192.168.232.1(CONNECTED) 0] ls /sanguo [shuguo, weiguo0000000001]
5) Modify node data value
[zk: 192.168.232.1(CONNECTED) 16] get /sanguo three countries [zk: 192.168.232.1(CONNECTED) 17] set /sanguo "Shu-Wei-Wu" [zk: 192.168.232.1(CONNECTED) 18] get /sanguo Shu-Wei-Wu
3. Delete nodes and view
3.1. Delete node
[zk: 192.168.232.1(CONNECTED) 20] ls /sanguo [shuguo, weiguo0000000001] [zk: 192.168.232.1(CONNECTED) 21] delete /sanguo/shuguo [zk: 192.168.232.1(CONNECTED) 22] ls /sanguo [weiguo0000000001]
3.2. Recursively delete nodes
[zk: 192.168.232.1(CONNECTED) 25] delete /sanguo Node not empty: /sanguo [zk: 192.168.232.1(CONNECTED) 26] deleteall /sanguo [zk: 192.168.232.1(CONNECTED) 27] ls /sanguo Node does not exist: /sanguo
3.3. View node status
[zk: 192.168.232.1(CONNECTED) 6] stat /sanguo cZxid = 0xb7b ctime = Sun Dec 05 18:19:03 CST 2021 mZxid = 0xb7b mtime = Sun Dec 05 18:19:03 CST 2021 pZxid = 0xb7c cversion = 1 dataVersion = 0 aclVersion = 0 ephemeralOwner = 0x0 dataLength = 0 numChildren = 1
4. Listener principle
Detailed monitoring process:
1) First, there must be a main thread
2) Create a Zookeeper client in the main () thread. At this time, two threads will be created. One thread is responsible for network connection communication and the other is responsible for listening
3) Send the registered listening event to Zookeeper in the communication thread
4) In the registered listening list of Zookeeper, add the registered listening events to the list
5) Zookeeper will send this message to the listening thread if it detects data or path changes
6) The process () method was called inside the listening thread
4.1. Monitor node data changes
At 192.168.42.133 Register listening on the host/sanguo Node data change [zk: 192.168.42.133:2181(CONNECTED) 26] get -w /sanguo At 192.168.42.134 Modify on host/sanguo Node data [zk: 192.168.42.134:2181(CONNECTED) 1] set /sanguo "wuguo" At 192.168.42.133 Prompt information on host WATCHER:: WatchedEvent state:SyncConnected type:NodeDataChanged path:/sanguo
Note: if you modify the data value of / sanguo again at 192.168.42.134, the monitoring information will not be received on 192.168.42.133, because you can only monitor once after registering, and you need to register again during monitoring
4.2. Monitor node path change (node number change)
At 192.168.42.133 Register listening on the host/sanguo Node change [zk: 192.168.42.133:2181(CONNECTED) 27] ls -w /sanguo At 192.168.42.134 Host increase /sanguo node [zk: 192.168.42.134:2181(CONNECTED) 3] create /sanguo/jin "simayi" Created /sanguo/jin At 192.168.42.133 Prompt information on host WATCHER:: WatchedEvent state:SyncConnected type:NodeChildrenChanged path:/sanguo
Note: a node's path change is registered once and takes effect once. If you want to take effect multiple times, you need to register multiple times