Author: Walking Sahara in the Rain Source: https://liudongdong.top Official Account: Walking Sahara in the Rain Remarks: Welcome to pay attention to the public account, learn technology, grow up together!

Bonus at the end of the article: hundreds of e-books, waiting for you to receive ^ V ^

Environmental Description:

  1. The server is a Centos7 cluster
  2. ZK is version 3.4.10.tar.gz of ZooKeeper
  3. The JDK 1.8

1. Common commands

image.png

Start the client

# zkCli.sh -server master:2181

1. I’ll help you

Show all commands

[zk: master:2181(CONNECTED) 0] help
ZooKeeper -server host:port cmd args
    stat path [watch]
    set path data [version]
    ls path [watch]
    delquota [-n|-b] path
    ls2 path [watch]
    setAcl path acl
    setquota -n|-b val path
    history 
    redo cmdno
    printwatches on|off
    delete path [version]
    sync path
    listquota path
    rmr path
    get path [watch]
    create [-s] [-e] path data acl
    addauth scheme auth
    quit 
    getAcl path
    close 
    connect host:port

2. LS path [watch]

Look at the node

[zk: master:2181(CONNECTED) 1] ls /
[zookeeper]

3. LS2 path [watch]

View the current node data and see the number of updates, etc

[zk: master:2181(CONNECTED) 2] ls2 / [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

4. Create the node

[zk: master:2181(CONNECTED) 17] create /hello "world"
Created /hello
[zk: master:2181(CONNECTED) 18] ls /
[hello, zookeeper]

[zk: master:2181(CONNECTED) 19] create -e /hello2 "world"
Created /hello2
[zk: master:2181(CONNECTED) 20] ls /                     
[hello2, hello, zookeeper]

5, get path [watch]

Gets the value of the node

[zk: master:2181(CONNECTED) 21] get /hello2
world
cZxid = 0x300000008
ctime = Sun May 09 03:38:15 PDT 2021
mZxid = 0x300000008
mtime = Sun May 09 03:38:15 PDT 2021
pZxid = 0x300000008
cversion = 0
dataVersion = 0
aclVersion = 0
ephemeralOwner = 0x17950947f120001
dataLength = 5
numChildren = 0

6, Set path to set the value of the node

[zk: master:2181(CONNECTED) 22] set /hello2 "112233"
cZxid = 0x300000008
ctime = Sun May 09 03:38:15 PDT 2021
mZxid = 0x300000009
mtime = Sun May 09 03:46:09 PDT 2021
pZxid = 0x300000008
cversion = 0
dataVersion = 1
aclVersion = 0
ephemeralOwner = 0x17950947f120001
dataLength = 6
numChildren = 0
[zk: master:2181(CONNECTED) 23] get /hello2
112233
cZxid = 0x300000008
ctime = Sun May 09 03:38:15 PDT 2021
mZxid = 0x300000009
mtime = Sun May 09 03:46:09 PDT 2021
pZxid = 0x300000008
cversion = 0
dataVersion = 1
aclVersion = 0
ephemeralOwner = 0x17950947f120001
dataLength = 6
numChildren = 0

7, STAT status check

View node status

[zk: master:2181(CONNECTED) 24] stat /hello2
cZxid = 0x300000008
ctime = Sun May 09 03:38:15 PDT 2021
mZxid = 0x300000009
mtime = Sun May 09 03:46:09 PDT 2021
pZxid = 0x300000008
cversion = 0
dataVersion = 1
aclVersion = 0
ephemeralOwner = 0x17950947f120001
dataLength = 6
numChildren = 0

8, DELETE path

Remove nodes

[zk: master:2181(CONNECTED) 26] delete /hello2
[zk: master:2181(CONNECTED) 27] ls /
[hello, zookeeper]

9. RMR path

Recursively delete nodes

[zk: master:2181(CONNECTED) 31] ls /hello                
[word]
[zk: master:2181(CONNECTED) 32] rmr /hello
[zk: master:2181(CONNECTED) 33] ls /
[zookeeper]

Two, monitor node changes

1. Monitor node value changes

  1. The first server listens on the node hello, with the path :/hello
[zk: slave1:2181(CONNECTED) 0] ls /
[hello, zookeeper]
[zk: slave1:2181(CONNECTED) 1] get /hello watch
jjj
cZxid = 0x30000000f
ctime = Sun May 09 03:54:52 PDT 2021
mZxid = 0x30000000f
mtime = Sun May 09 03:54:52 PDT 2021
pZxid = 0x30000000f
cversion = 0
dataVersion = 0
aclVersion = 0
ephemeralOwner = 0x0
dataLength = 3
numChildren = 0
  1. The second server modifies the data of the node hello by changing the node path to :/hello
[zk: master:2181(CONNECTED) 35] ls / [hello, zookeeper] [zk: master:2181(CONNECTED) 36] set /hello "kkk" cZxid = 0x30000000f ctime = Sun May 09 03:54:52 PDT 2021 mZxid = 0x300000011  mtime = Sun May 09 03:55:57 PDT 2021 pZxid = 0x30000000f cversion = 0 dataVersion = 1 aclVersion = 0 ephemeralOwner = 0x0 dataLength = 3 numChildren = 0 [zk: master:2181(CONNECTED) 37]
  1. The first server has printed logs
[zk: slave1:2181(CONNECTED) 2] 
WATCHER::

WatchedEvent state:SyncConnected type:NodeDataChanged path:/hello

Note: The monitoring can only be done once, and the monitoring needs to be done again

2. Monitor node path changes

  1. The first server listens for path changes under the hello node
[zk: slave1:2181(CONNECTED) 0] ls /   
[hello, zookeeper]
[zk: slave1:2181(CONNECTED) 1] ls /hello watch
[]
  1. On the second server, create the node world with a parent path of /hello
[zk: master:2181(CONNECTED) 38] create /hello/world "world"
Created /hello/world
  1. So the first server is going to be 0
[zk: slave1:2181(CONNECTED) 2] 
WATCHER::

WatchedEvent state:SyncConnected type:NodeChildrenChanged path:/hello

At the end of the article’s welfare

Contains C, C ++, Java, Python, Linux, HTML, PHP and other hundreds of e-books!

Obtain method:

Search and follow the official account: Walking the Sahara in the Rain

Reply keyword: 001