作者:雨中散步撒哈拉 来源:https://liudongdong.top 公众号:雨中散步撒哈拉 备注:欢迎关注公众号,学习技术,一起成长! 文末福利:上百本电子书,等待你的领取^v^
环境说明:
- 服务器为centos7集群
- zk为zookeeper-3.4.10.tar.gz版本
- jdk为1.8
一、常用命令
image.png
0、启动客户端
代码语言:javascript复制# zkCli.sh -server master:2181
1、help帮助
显示所有命令
代码语言:javascript复制[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 路径 [watch]
查看节点
代码语言:javascript复制[zk: master:2181(CONNECTED) 1] ls /
[zookeeper]
3、ls2 路径[watch]
查看当前节点数据并能看到更新次数等数据
代码语言:javascript复制[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 创建节点
代码语言:javascript复制[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 路径 [watch]
获取节点的值
代码语言:javascript复制[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 路径 设置节点的值
代码语言:javascript复制[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 状态查看
查看节点状态
代码语言:javascript复制[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 路径
删除节点
代码语言:javascript复制[zk: master:2181(CONNECTED) 26] delete /hello2
[zk: master:2181(CONNECTED) 27] ls /
[hello, zookeeper]
9、rmr 路径
递归删除节点
代码语言:javascript复制[zk: master:2181(CONNECTED) 31] ls /hello
[word]
[zk: master:2181(CONNECTED) 32] rmr /hello
[zk: master:2181(CONNECTED) 33] ls /
[zookeeper]
二、监听节点变化
1、监听节点值变化
- 第一台服务器,进行监听节点hello,路径为:/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
- 第二台服务器进行修改节点hello的数据,修改节点路径为:/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]
- 此时发现第一台服务器有打印日志
[zk: slave1:2181(CONNECTED) 2]
WATCHER::
WatchedEvent state:SyncConnected type:NodeDataChanged path:/hello
注:监听只能监听一次,重新监听需再次进行watch
2、监听节点路径变化
- 第一台服务器,进行监听节点hello节点下的路径变化
[zk: slave1:2181(CONNECTED) 0] ls /
[hello, zookeeper]
[zk: slave1:2181(CONNECTED) 1] ls /hello watch
[]
- 第二台服务器,进行创建节点world,父节点路径为:/hello
[zk: master:2181(CONNECTED) 38] create /hello/world "world"
Created /hello/world
- 此时第一台服务器变化为
[zk: slave1:2181(CONNECTED) 2]
WATCHER::
WatchedEvent state:SyncConnected type:NodeChildrenChanged path:/hello
文末福利
包含c、c 、java、python、linux、html、php等上百本电子书!
获取方式: 搜索并关注公众号:雨中散步撒哈拉 回复关键词:001