一、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 = 30000
WATCHER::
WatchedEvent state:SyncConnected type:None path:null
————————————————
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。
————————————————
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
————————————————
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
————————————————
如果您发现该资源为电子书等存在侵权的资源或对该资源描述不正确等,可点击“私信”按钮向作者进行反馈;如作者无回复可进行平台仲裁,我们会在第一时间进行处理!
添加我为好友,拉您入交流群!
请使用微信扫一扫!