一、zookeeper客户端简介
ZooKeeper提供了一个非常简单的命令行客户端zkCli,它在ZooKeeper安装目录的bin目录下。输入./zkCli.sh命令默认连接本地127.0.0.1:2181节点,如果我们需要连接远程节点可以使用./zkCli.sh -server ip:2181方式进行连接。连接过程中会输出一大堆信息。当连接成功后,将进入ZooKeeper的交互式模式。博文实验环境:
- 操作系统:centos7.6
- zookeeper版本:3.6.3
二、zookeeper常用命令示例
0、连接zookeeper server
[wuhs@s143 bin]$ ./zkCli.sh -server 192.168.0.142:2181
Connecting to 192.168.0.142:2181
2022-03-02 22:26:26,705 [myid:] - INFO [main:Environment@98] - Client environment:zookeeper.version=
…
mplete on server s142/192.168.0.142:2181, session id = 0x100002542a90003, negotiated timeout = 30000WATCHER::
WatchedEvent state:SyncConnected type:None path:null
1、获取命令帮助
早先版本是通过help命令获取命令帮助,新版本会提示找不到help命令,但是输出结果还是会显示zookeeper客户端下支持的命令。
2、查看软件版本
[zk: 192.168.0.142:2181(CONNECTED) 1] version
ZooKeeper CLI version: 3.6.3–6401e4ad2087061bc6b9f80dec2d69f2e3c8660a, built on 04/08/2021 16:35 GMT
3、连接一个远程节点
[zk: 192.168.0.142:2181(CONNECTED) 2] connect 192.168.0.143:2181
4、关闭连接
[zk: 192.168.0.143:2181(CONNECTED) 3] close
WATCHER::
WatchedEvent state:Closed type:None path:null
2022-03-03 01:29:57,127 [myid:] - INFO [main:ZooKeeper@1619] - Session: 0x20000253ae20003 closed
2022-03-03 01:29:57,127 [myid:] - INFO [main-EventThread:ClientCnxn$EventThread@578] - EventThread shut down for session: 0x20000253ae20003
5、显示仲裁成员的配置
[zk: 192.168.0.143:2181(CONNECTED) 8] config
server.1=192.168.0.142:2888:3888:participant
server.2=192.168.0.143:2888:3888:participant
server.3=192.168.0.144:2888:3888:participant
version=0
6、创建一个znode
命令语法:create [-s] [-e] [-c] [-t ttl] path [data] [acl]
- -s:创建的是带序列号的节点,序列号用0填充节点路径。
- -e:创建的是临时节点。
- -c:创建的是容器节点
- path:znode的路径,ZooKeeper中没有相对路径,所有路径都必须以’/'开头。
- data:znode携带的数据。
- acl:这个节点的ACL。
#创建一个永久节点
[zk: 192.168.0.143:2181(CONNECTED) 19] create /zk_test
Created /zk_test
#创建一个临时节点
[zk: 192.168.0.143:2181(CONNECTED) 25] create -e /ephemeral_node
Created /ephemeral_node
7、删除znode节点
#删除节点前要求节点目录为空,不存在子节点
[zk: 192.168.0.143:2181(CONNECTED) 34] delete /config
Node not empty: /config
[zk: 192.168.0.143:2181(CONNECTED) 35] delete /config/topics/test
[zk: 192.168.0.143:2181(CONNECTED) 27] delete /ephemeral_node
#如果要删除整个节点及子节点可以使用deleteall
[zk: 192.168.0.143:2181(CONNECTED) 36] deleteall /config
8、指定路径下的配额管理
配额管理主要有两个参数
- -n限制子节点的数量(包括自身)
- -b限制一条路径的字节(数据长度)
#查看配额
[zk: localhost:2181(CONNECTED) 21] listquota /config/topics
absolute path is /zookeeper/quota/config/topics/zookeeper_limits
Output quota for /config/topics count=2,bytes=-1
Output stat for /config/topics count=6,bytes=0
#设置配额
[zk: localhost:2181(CONNECTED) 15] setquota -n 2 /config/topics
#删除配额
[zk: localhost:2181(CONNECTED) 24] delquota /config/topics
9、显示一个节点的状态
[zk: localhost:2181(CONNECTED) 25] stat /config/topics
cZxid = 0x100000024
ctime = Thu Mar 03 15:16:38 CST 2022
mZxid = 0x100000024
mtime = Thu Mar 03 15:16:38 CST 2022
pZxid = 0x100000031
cversion = 5
dataVersion = 0
aclVersion = 0
ephemeralOwner = 0x0
dataLength = 0
numChildren = 5
10、查看路径子节点
命令语法:ls [-s] [-w] [-R] path
- -s 同时显示stat信息
- -w 只显示子节点信息,默认选项
- -R 递归显示
[zk: localhost:2181(CONNECTED) 27] ls -R /
/
/config
/zk_test
/zookeeper
/config/topics
/config/topics/test
/config/topics/test2
/config/topics/test3
/config/topics/test4
/config/topics/test5
/zookeeper/config
/zookeeper/quota
11、获取指定路径下的数据
[zk: localhost:2181(CONNECTED) 4] get /zookeeper/config
server.1=192.168.0.142:2888:3888:participant
server.2=192.168.0.143:2888:3888:participant
server.3=192.168.0.144:2888:3888:participant
version=0
12、获取子节点数量
[zk: localhost:2181(CONNECTED) 10] getAllChildrenNumber /zookeeper
2
13、获取会话创建的临时节点
[zk: localhost:2181(CONNECTED) 14] create -e /config/abc
Created /config/abc
[zk: localhost:2181(CONNECTED) 15] getEphemerals /config
[/config/abc]
14、设置或者更新路径数据
[zk: 192.168.0.143:2181(CONNECTED) 45] set /config/topics/test “this is a test”
[zk: 192.168.0.143:2181(CONNECTED) 46] get /config/topics/test
this is a test
15、设置ACL
ACL权限ACL 简写允许的操作CREATEc创建子节点READr获取节点的数据和它的子节点WRITEw设置节点的数据DELETEd删除子节点 (仅下一级节点)ADMINa设置 ACL 权限
ZooKeeper内置了一些权限控制方案,可以用以下方案为每个节点设置权限:
方案描述world只有一个用户:anyone,代表所有人(默认)ip使用IP地址认证auth使用已添加认证的用户认证digest使用“用户名:密码”方式认证
#远程登录可以查看/config权限
[zk: 192.168.0.144:2181(CONNECTED) 0] getAcl /config
'ip,'192.168.0.0/24
: cdrwa
#本地登录查看或者配置config会报错无权限
[zk: localhost:2181(CONNECTED) 37] ls /config
Insufficient permission : /config
#设置auth方案
[zk: 192.168.0.144:2181(CONNECTED) 2] create /mynode1 hello
Created /mynode1
[zk: 192.168.0.144:2181(CONNECTED) 3] addauth digest admin:admin
[zk: 192.168.0.144:2181(CONNECTED) 4] setAcl /mynode1 auth:admin:cdrwa
[zk: 192.168.0.144:2181(CONNECTED) 5] getAcl /mynode1
'digest,'admin:x1nq8J5GOJVPY6zgzhtTtA9izLc=
: cdrwa
16、获取ACL
[zk: localhost:2181(CONNECTED) 17] getAcl /config
'world,'anyone
: cdrwa
17、同步数据集群间数据
[zk: 192.168.0.143:2181(CONNECTED) 48] sync /
Sync is OK
18、查看命令执行历史
[zk: 192.168.0.143:2181(CONNECTED) 49] history
39 - create /config
40 - create /config/topics
41 - create /config/topics/test
42 - ls /
43 - help
44 - help
45 - set /config/topics/test “this is a test”
46 - get /config/topics/test
47 - set -v 2 /config/topics/test “this is a test 2”
48 - sync /
49 - history
19、退出客户端
[zk: 192.168.0.143:2181(CONNECTED) 50] quit
WATCHER::
WatchedEvent state:Closed type:None path:null
2022-03-03 03:58:33,818 [myid:] - INFO [main:ZooKeeper@1619] - Session: 0x20000253ae20004 closed
2022-03-03 03:58:33,818 [myid:] - INFO [main-EventThread:ClientCnxn$EventThread@578] - EventThread shut down for session: 0x20000253ae20004
2022-03-03 03:58:33,820 [myid:] - ERROR [main:ServiceUtils@42] - Exiting JVM with code 0
20、更多信息查看
更多命令帮助信息我们可以查阅官网帮助文档,zookeeper 3.6.3版本命令帮助文件见链接:
https://zookeeper.apache.org/doc/r3.6.3/zookeeperCLI.html
版权归原作者 恒悦sunsite 所有, 如有侵权,请联系我们删除。