Zookeeper客户端zkCli使用说明
连接Zookeeper服务端
命令:bin/zkCli.sh -server localhost:2181
如下所示,即表示连接成功!
[root@node04 bin]# zkCli.sh
Connecting to localhost:2181
...
[zk: localhost:2181(CONNECTED) 0]
[root@node04 bin]# zkCli.sh -server localhost:2181
Connecting to localhost:2181
...
[zk: localhost:2181(CONNECTED) 0]
help
查看zkCli.sh命令的帮助信息
如下所示:
[zk: localhost:2181(CONNECTED) 1] 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|-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|-N|-B val path
stat [-w] path
sync path
version
whoami
Command not found: Command not found help
[zk: localhost:2181(CONNECTED) 2]
ls
默认显示指定路径下的子节点;
命令:ls [-s] [-w] [-R] path
-s 显示节点的元数据信息
-R 递归显示所有子节点
-w 设置直接子节点变更监听
[zk: localhost:2181(CONNECTED) 0] ls /
[zookeeper]
[zk: localhost:2181(CONNECTED) 1] ls -w /test
[node1, node2]
[zk: localhost:2181(CONNECTED) 2] create /test/node5
WATCHER::
WatchedEvent state:SyncConnected type:NodeChildrenChanged path:/test
Created /test/node5
[zk: localhost:2181(CONNECTED) 3] create /test/node5/node51
Created /test/node5/node51
[zk: localhost:2181(CONNECTED) 4] ls -R /test
/test
/test/node1
/test/node2
/test/node5
/test/node1/node11
/test/node5/node51
[zk: localhost:2181(CONNECTED) 5]
getEphemerals
查询指定路径下的临时节点
命令:getEphemerals path
[zk: localhost:2181(CONNECTED) 37] getEphemerals /test
[]
[zk: localhost:2181(CONNECTED) 38] ls /test
[node1, node2]
[zk: localhost:2181(CONNECTED) 44]
[zk: localhost:2181(CONNECTED) 44] create -e /test/node3_e
Created /test/node3_e
[zk: localhost:2181(CONNECTED) 45] create -e /test/node4_e
Created /test/node4_e
[zk: localhost:2181(CONNECTED) 46] ls /test
[node1, node2, node3_e, node4_e]
[zk: localhost:2181(CONNECTED) 47] getEphemerals /test
[/test/node4_e, /test/node3_e]
[zk: localhost:2181(CONNECTED) 48]
create
创建指定路径的节点;
命令:create [-s] [-e] [-c] [-t ttl] path [data] [acl]
-s 创建有序节点
-e 创建临时节点
-c 创建container节点
-t ttl 有效时间,单位毫秒
[zk: localhost:2181(CONNECTED) 3] ls /
[zookeeper]
[zk: localhost:2181(CONNECTED) 4] create /test
Created /test
[zk: localhost:2181(CONNECTED) 5] ls /
[test, zookeeper]
[zk: localhost:2181(CONNECTED) 6] create /test/node1
Created /test/node1
[zk: localhost:2181(CONNECTED) 7] create /test/node2
Created /test/node2
[zk: localhost:2181(CONNECTED) 8] create /test/node3
Created /test/node3
[zk: localhost:2181(CONNECTED) 9] ls /test
[node1, node2, node3]
[zk: localhost:2181(CONNECTED) 10]
[zk: localhost:2181(CONNECTED) 16] create -t 3000 /test/node6_ttl
Created /test/node6_ttl
[zk: localhost:2181(CONNECTED) 17] get /test/node6_ttl
# after 3s later
[zk: localhost:2181(CONNECTED) 24] get /test/node6_ttl
Node does not exist: /test/node6_ttl
注意:
如果要使ttl生效,需修改zookeeper服务端的启动参数:
打开zookeeper bin/zkServer.sh(win是zkService.cmd),修改启动zookeeper的命令,加上以下参数:
-Dzookeeper.extendedTypesEnabled=true
delete
删除指定路径节点
命令:delete [-v version] path
[zk: localhost:2181(CONNECTED) 10] delete /test
Node not empty: /test
[zk: localhost:2181(CONNECTED) 11] delete /test/node3
[zk: localhost:2181(CONNECTED) 12] ls /test
[node1, node2]
[zk: localhost:2181(CONNECTED) 13]
deleteall
删除指定路径下所有节点(递归)
命令:deleteall path [-b batch size]
[zk: localhost:2181(CONNECTED) 12] ls /test
[node1, node2]
[zk: localhost:2181(CONNECTED) 13] delete /test
Node not empty: /test
[zk: localhost:2181(CONNECTED) 14] deleteall /test
[zk: localhost:2181(CONNECTED) 15] ls /test
Node does not exist: /test
[zk: localhost:2181(CONNECTED) 16]
set
设置或更新指定路径的数据
命令:set [-s] [-v version] path data
-s 显示节点元数据信息
-v 更新version No为stat对应dataVersion版本对应的值
[zk: localhost:2181(CONNECTED) 16] ls /
[zookeeper]
[zk: localhost:2181(CONNECTED) 17] create /test/node1
Node does not exist: /test/node1
[zk: localhost:2181(CONNECTED) 18] create /test
Created /test
[zk: localhost:2181(CONNECTED) 19] create /test/node1
Created /test/node1
[zk: localhost:2181(CONNECTED) 20] create /test/node2
Created /test/node2
[zk: localhost:2181(CONNECTED) 21] set /test 1
[zk: localhost:2181(CONNECTED) 22] get /test
1
[zk: localhost:2181(CONNECTED) 23]
[zk: localhost:2181(CONNECTED) 0] set -v 3 /test 1
version No is not valid : /test
[zk: localhost:2181(CONNECTED) 1] stat /test
cZxid = 0x90000000b
ctime = Fri Dec 02 13:41:04 EST 2022
mZxid = 0x90000001b
mtime = Fri Dec 02 14:14:46 EST 2022
pZxid = 0x900000029
cversion = 17
dataVersion = 4
aclVersion = 0
ephemeralOwner = 0x0
dataLength = 1
numChildren = 3
[zk: localhost:2181(CONNECTED) 2] set -v 4 /test 2
[zk: localhost:2181(CONNECTED) 3] get /test
2
[zk: localhost:2181(CONNECTED) 4] stat /test
cZxid = 0x90000000b
ctime = Fri Dec 02 13:41:04 EST 2022
mZxid = 0x90000002f
mtime = Fri Dec 02 19:59:36 EST 2022
pZxid = 0x900000029
cversion = 17
dataVersion = 5
aclVersion = 0
ephemeralOwner = 0x0
dataLength = 1
numChildren = 3
[zk: localhost:2181(CONNECTED) 5]
get
获取指定路径的数据;
命令:get [-s] [-w] path
-s 显示stat信息
-w 添加数据变更监听事件
[zk: localhost:2181(CONNECTED) 27] get /test
1
[zk: localhost:2181(CONNECTED) 28] get -s /test
1
cZxid = 0x90000000b
ctime = Fri Dec 02 13:41:04 EST 2022
mZxid = 0x90000000e
mtime = Fri Dec 02 13:43:07 EST 2022
pZxid = 0x90000000d
cversion = 2
dataVersion = 1
aclVersion = 0
ephemeralOwner = 0x0
dataLength = 1
numChildren = 2
[zk: localhost:2181(CONNECTED) 29] get -w /test
1
[zk: localhost:2181(CONNECTED) 30]
[zk: localhost:2181(CONNECTED) 33] set /test 10
WATCHER::
WatchedEvent state:SyncConnected type:NodeDataChanged path:/test
[zk: localhost:2181(CONNECTED) 34]
[zk: localhost:2181(CONNECTED) 34] get -w /test
10
[zk: localhost:2181(CONNECTED) 35]
stat
显示节点元数据信息
命令:stat [-w] path
[zk: localhost:2181(CONNECTED) 4] stat /test
cZxid = 0x90000000b
ctime = Fri Dec 02 13:41:04 EST 2022
mZxid = 0x90000002f
mtime = Fri Dec 02 19:59:36 EST 2022
pZxid = 0x900000029
cversion = 17
dataVersion = 5
aclVersion = 0
ephemeralOwner = 0x0
dataLength = 1
numChildren = 3
getAllChildrenNumber
获取指定路径下的子节点数(递归总数)
命令:getAllChildrenNumber path
[zk: localhost:2181(CONNECTED) 6] ls -R /test
/test
/test/node1
/test/node2
/test/node5
/test/node1/node11
/test/node5/node51
[zk: localhost:2181(CONNECTED) 7]
[zk: localhost:2181(CONNECTED) 7] getAllChildrenNumber /test
5
[zk: localhost:2181(CONNECTED) 8]
[zk: localhost:2181(CONNECTED) 8] getAllChildrenNumber /test/node1
1
[zk: localhost:2181(CONNECTED) 9]
printwatches
是否打印监听信息的开关;
命令:printwatches on|off
[zk: localhost:2181(CONNECTED) 19] printwatches
printwatches is on
[zk: localhost:2181(CONNECTED) 20] printwatches on
[zk: localhost:2181(CONNECTED) 21] printwatches off
removewatches
删除指定路径的监听
命令:removewatches path [-c|-d|-a] [-l]
[zk: localhost:2181(CONNECTED) 34] get -w /test/node1
11
[zk: localhost:2181(CONNECTED) 35] removewatches /test/node1
WATCHER::
WatchedEvent state:SyncConnected type:DataWatchRemoved path:/test/node1
[zk: localhost:2181(CONNECTED) 36]
setAcl
设置路径访问权限信息
命令:setAcl [-s] [-v version] [-R] path acl
-R 递归设置访问权限
ACL全称为Access Control List(访问控制列表),用于控制资源的访问权限,可以控制节点的读写操作,保证数据的安全性。
[zk: localhost:2181(CONNECTED) 37] addauth digest user1:12345
[zk: localhost:2181(CONNECTED) 38]
[zk: localhost:2181(CONNECTED) 38] setAcl /test auth:user1:12345:crwad
[zk: localhost:2181(CONNECTED) 40] getAcl /test
'digest,'user1:+owfoSBn/am19roBPzR1/MfCblE=
: cdrwa
[zk: localhost:2181(CONNECTED) 42] getAcl /test/node1
'world,'anyone
: cdrwa
[zk: localhost:2181(CONNECTED) 43] setAcl -R /test auth:user1:12345:crwad
[zk: localhost:2181(CONNECTED) 44] getAcl /test/node1
'digest,'user1:+owfoSBn/am19roBPzR1/MfCblE=
: cdrwa
[zk: localhost:2181(CONNECTED) 45]
getAcl
查询指定路径访问权限
命令:getAcl [-s] path
[zk: localhost:2181(CONNECTED) 44] getAcl /test/node1
'digest,'user1:+owfoSBn/am19roBPzR1/MfCblE=
: cdrwa
[zk: localhost:2181(CONNECTED) 45]
访问权限类型说明:
类型 | 描述 |
---|---|
world | 开放模式,全世界都可以访问 |
ip | IP模式,限制指定客户端IP访问 |
auth | 用户密码认证模式(明文),只有在会话中添加了认证才可以访问 |
digest | 用户密码认证模式(密文),最常见模式 |
权限信息cdrwa说明:
c:create,创建权限
d:delete,删除权限
r:read,读取权限
a:admin,管理者权限
w:write,更新权限
listquota
显示配额信息
命令:listquota path
[zk: localhost:2181(CONNECTED) 46] listquota /test/node1
absolute path is /zookeeper/quota/test/node1/zookeeper_limits
quota for /test/node1 does not exist.
[zk: localhost:2181(CONNECTED) 47]
setquota
设置配额信息
命令:setquota -n|-b|-N|-B val path
-n 软限制-限制子节点数(包含自己本身);若超出只打印警告日志
-b 软限制-限制路径下数据长度,单位字节;若超出只打印警告日志
-N
-B
[zk: localhost:2181(CONNECTED) 48] setquota -n 2 /test/node1
[zk: localhost:2181(CONNECTED) 49] create /test/node1/node12
Created /test/node1/node12
[zk: localhost:2181(CONNECTED) 50] create /test/node1/node13
Created /test/node1/node13
[zk: localhost:2181(CONNECTED) 51] ls /test/node1
[node11, node12, node13]
[zk: localhost:2181(CONNECTED) 52] listquota /test/node1
absolute path is /zookeeper/quota/test/node1/zookeeper_limits
Output quota for /test/node1 count=2,bytes=-1=;byteHardLimit=-1;countHardLimit=-1
Output stat for /test/node1 count=4,bytes=2
[zk: localhost:2181(CONNECTED) 53]
delquota
删除配额信息
命令:delquota [-n|-b|-N|-B] path
[zk: localhost:2181(CONNECTED) 84] listquota /test/node1
absolute path is /zookeeper/quota/test/node1/zookeeper_limits
Output quota for /test/node1 count=2,bytes=-1=;byteHardLimit=-1;countHardLimit=2
Output stat for /test/node1 count=4,bytes=2
[zk: localhost:2181(CONNECTED) 85] delquota /test/node1
[zk: localhost:2181(CONNECTED) 86] listquota /test/node1
absolute path is /zookeeper/quota/test/node1/zookeeper_limits
quota for /test/node1 does not exist.
[zk: localhost:2181(CONNECTED) 87]
connect
连接指定服务端节点
命令:connect host:port
sync
同步leader和follower节点的数据(异步同步)
命令:sync path
[zk: localhost:2181(CONNECTED) 90] sync /
Sync is OK
[zk: localhost:2181(CONNECTED) 91]
close
关闭当前客户端会话连接
命令:close
quit
退出当前客户端连接
命令:quit
[zk: localhost:2181(CLOSED) 13] quit
2022-12-02 22:00:45,078 [myid:] - ERROR [main:o.a.z.u.ServiceUtils@42] - Exiting JVM with code 0
[root@node04 bin]#
history
查看历史执行命令(最近11条)
命令:history
[zk: localhost:2181(CONNECTED) 91] history
81 - ls /test/node1
82 - create /test/node1/node13
83 - ls /test/node1
84 - listquota /test/node1
85 - delquota /test/node1
86 - listquota /test/node1
87 - connect
88 - close
89 - connect
90 - sync /
91 - history
[zk: localhost:2181(CONNECTED) 92]
redo
重新执行history结果中指定序号的命令
命令:redo cmdno
[zk: localhost:2181(CONNECTED) 7] history
0 - get /test/node1
1 - addauth digest +owfoSBn/am19roBPzR1/MfCblE=
2 - get /test/node1
3 - addauth digest user1:+owfoSBn/am19roBPzR1/MfCblE=
4 - get /test/node1
5 - addauth digest user1:12345
6 - get /test/node1
7 - history
[zk: localhost:2181(CONNECTED) 8] redo 6
11
[zk: localhost:2181(CONNECTED) 9]
version
显示zk客户端版本
命令:version
[zk: localhost:2181(CONNECTED) 9] version
ZooKeeper CLI version: 3.8.0-5a02a05eddb59aee6ac762f7ea82e92a68eb9c0f, built on 2022-02-25 08:49 UTC
[zk: localhost:2181(CONNECTED) 10]
whoami
显示客户端连接授权信息
命令:whoami
[zk: localhost:2181(CONNECTED) 10] whoami
Auth scheme: User
digest: +owfoSBn/am19roBPzR1/MfCblE=
digest: user1
digest: user1
ip: 0:0:0:0:0:0:0:1
[zk: localhost:2181(CONNECTED) 11]
addauth
添加访问权限信息
命令:addauth scheme auth
[zk: localhost:2181(CONNECTED) 37] addauth digest user1:12345
[zk: localhost:2181(CONNECTED) 38]
config
查看zk集群配置信息
命令:config [-c] [-w] [-s]
[zk: localhost:2181(CONNECTED) 11] config
server.1=node01:2888:3888:participant
server.2=node02:2888:3888:participant
server.3=node03:2888:3888:participant
server.4=node04:2888:3888:participant
version=0
reconfig
集群运行时重新配置节点
命令:reconfig [-s] [-v version] [[-file path] | [-members serverID=host:port1:port2;port3[,...]]] | [-add serverId=host:port1:port2;port3[,...]] [-remove serverId[,...]*]
例如:更新2,新增5,删除4节点
reconfig -add 2=node2:2781:2786:observer;12182 -add 5=node5:2781:2786:observer;2185 -remove 4
参考文档:
https://zookeeper.apache.org/doc/r3.8.0/zookeeperCLI.html
(完)