ZooKeeper客户端操作命令详解

启动服务端

./zkServer.sh start

启动客户端并连接服务端

 ./zkcli.sh 连接本地的zookeeper服务器 
 ./zkCli.sh -server ip:port 连接指定的服务器

连接成功后,系统会输出zookeeper的相关环境及配置信息,如下所示

Connecting to localhost:2181
2020-08-08 17:53:05,144 [myid:] - INFO  [main:Environment@98] - Client environment:zookeeper.version=3.6.1--104dcb3e3fb464b30c5186d229e00af9f332524b, built on 04/21/2020 15:01 GMT
2020-08-08 17:53:05,148 [myid:] - INFO  [main:Environment@98] - Client environment:host.name=rpp
2020-08-08 17:53:05,148 [myid:] - INFO  [main:Environment@98] - Client environment:java.version=1.8.0_101
2020-08-08 17:53:05,150 [myid:] - INFO  [main:Environment@98] - Client environment:java.vendor=Oracle Corporation
2020-08-08 17:53:05,150 [myid:] - INFO  [main:Environment@98] - Client environment:java.home=/usr/local/jdk1.8.0_101/jre
2020-08-08 17:53:05,150 [myid:] - INFO  [main:Environment@98] - Client environment:java.class.path=/usr/local/apache-zookeeper-3.6.1/bin/../zookeeper-server/target/classes:/usr/local/apache-zookeeper-3.6.1/bin/../build/classes:/usr/local/apache-zookeeper-3.6.1/bin/../zookeeper-server/target/lib/*.jar:/usr/local/apache-zookeeper-3.6.1/bin/../build/lib/*.jar:/usr/local/apache-zookeeper-3.6.1/bin/../lib/zookeeper-prometheus-metrics-3.6.1.jar:/usr/local/apache-zookeeper-3.6.1/bin/../lib/zookeeper-jute-3.6.1.jar:/usr/local/apache-zookeeper-3.6.1/bin/../lib/zookeeper-3.6.1.jar:/usr/local/apache-zookeeper-3.6.1/bin/../lib/snappy-java-1.1.7.jar:/usr/local/apache-zookeeper-3.6.1/bin/../lib/slf4j-log4j12-1.7.25.jar:/usr/local/apache-zookeeper-3.6.1/bin/../lib/slf4j-api-1.7.25.jar:/usr/local/apache-zookeeper-3.6.1/bin/../lib/simpleclient_servlet-0.6.0.jar:/usr/local/apache-zookeeper-3.6.1/bin/../lib/simpleclient_hotspot-0.6.0.jar:/usr/local/apache-zookeeper-3.6.1/bin/../lib/simpleclient_common-0.6.0.jar:/usr/local/apache-zookeeper-3.6.1/bin/../lib/simpleclient-0.6.0.jar:/usr/local/apache-zookeeper-3.6.1/bin/../lib/netty-transport-native-unix-common-4.1.48.Final.jar:/usr/local/apache-zookeeper-3.6.1/bin/../lib/netty-transport-native-epoll-4.1.48.Final.jar:/usr/local/apache-zookeeper-3.6.1/bin/../lib/netty-transport-4.1.48.Final.jar:/usr/local/apache-zookeeper-3.6.1/bin/../lib/netty-resolver-4.1.48.Final.jar:/usr/local/apache-zookeeper-3.6.1/bin/../lib/netty-handler-4.1.48.Final.jar:/usr/local/apache-zookeeper-3.6.1/bin/../lib/netty-common-4.1.48.Final.jar:/usr/local/apache-zookeeper-3.6.1/bin/../lib/netty-codec-4.1.48.Final.jar:/usr/local/apache-zookeeper-3.6.1/bin/../lib/netty-buffer-4.1.48.Final.jar:/usr/local/apache-zookeeper-3.6.1/bin/../lib/metrics-core-3.2.5.jar:/usr/local/apache-zookeeper-3.6.1/bin/../lib/log4j-1.2.17.jar:/usr/local/apache-zookeeper-3.6.1/bin/../lib/json-simple-1.1.1.jar:/usr/local/apache-zookeeper-3.6.1/bin/../lib/jline-2.11.jar:/usr/local/apache-zookeeper-3.6.1/bin/../lib/jetty-util-9.4.24.v20191120.jar:/usr/local/apache-zookeeper-3.6.1/bin/../lib/jetty-servlet-9.4.24.v20191120.jar:/usr/local/apache-zookeeper-3.6.1/bin/../lib/jetty-server-9.4.24.v20191120.jar:/usr/local/apache-zookeeper-3.6.1/bin/../lib/jetty-security-9.4.24.v20191120.jar:/usr/local/apache-zookeeper-3.6.1/bin/../lib/jetty-io-9.4.24.v20191120.jar:/usr/local/apache-zookeeper-3.6.1/bin/../lib/jetty-http-9.4.24.v20191120.jar:/usr/local/apache-zookeeper-3.6.1/bin/../lib/javax.servlet-api-3.1.0.jar:/usr/local/apache-zookeeper-3.6.1/bin/../lib/jackson-databind-2.10.3.jar:/usr/local/apache-zookeeper-3.6.1/bin/../lib/jackson-core-2.10.3.jar:/usr/local/apache-zookeeper-3.6.1/bin/../lib/jackson-annotations-2.10.3.jar:/usr/local/apache-zookeeper-3.6.1/bin/../lib/commons-lang-2.6.jar:/usr/local/apache-zookeeper-3.6.1/bin/../lib/commons-cli-1.2.jar:/usr/local/apache-zookeeper-3.6.1/bin/../lib/audience-annotations-0.5.0.jar:/usr/local/apache-zookeeper-3.6.1/bin/../zookeeper-*.jar:/usr/local/apache-zookeeper-3.6.1/bin/../zookeeper-server/src/main/resources/lib/*.jar:/usr/local/apache-zookeeper-3.6.1/bin/../conf:.:/usr/local/jdk1.8.0_101/lib/dt.jar:/usr/local/jdk1.8.0_101/lib/tools.jar
2020-08-08 17:53:05,150 [myid:] - INFO  [main:Environment@98] - Client environment:java.library.path=/usr/java/packages/lib/amd64:/usr/lib64:/lib64:/lib:/usr/lib
2020-08-08 17:53:05,150 [myid:] - INFO  [main:Environment@98] - Client environment:java.io.tmpdir=/tmp
2020-08-08 17:53:05,150 [myid:] - INFO  [main:Environment@98] - Client environment:java.compiler=
2020-08-08 17:53:05,150 [myid:] - INFO  [main:Environment@98] - Client environment:os.name=Linux
2020-08-08 17:53:05,151 [myid:] - INFO  [main:Environment@98] - Client environment:os.arch=amd64
2020-08-08 17:53:05,151 [myid:] - INFO  [main:Environment@98] - Client environment:os.version=3.10.0-957.21.3.el7.x86_64
2020-08-08 17:53:05,151 [myid:] - INFO  [main:Environment@98] - Client environment:user.name=root
2020-08-08 17:53:05,151 [myid:] - INFO  [main:Environment@98] - Client environment:user.home=/root
2020-08-08 17:53:05,151 [myid:] - INFO  [main:Environment@98] - Client environment:user.dir=/usr/local/apache-zookeeper-3.6.1/bin
2020-08-08 17:53:05,151 [myid:] - INFO  [main:Environment@98] - Client environment:os.memory.free=50MB
2020-08-08 17:53:05,153 [myid:] - INFO  [main:Environment@98] - Client environment:os.memory.max=228MB
2020-08-08 17:53:05,153 [myid:] - INFO  [main:Environment@98] - Client environment:os.memory.total=56MB
2020-08-08 17:53:05,158 [myid:] - INFO  [main:ZooKeeper@1005] - Initiating client connection, connectString=localhost:2181 sessionTimeout=30000 watcher=org.apache.zookeeper.ZooKeeperMain$MyWatcher@762efe5d
2020-08-08 17:53:05,162 [myid:] - INFO  [main:X509Util@77] - Setting -D jdk.tls.rejectClientInitiatedRenegotiation=true to disable client-initiated TLS renegotiation
2020-08-08 17:53:05,169 [myid:] - INFO  [main:ClientCnxnSocket@239] - jute.maxbuffer value is 1048575 Bytes
2020-08-08 17:53:05,177 [myid:] - INFO  [main:ClientCnxn@1703] - zookeeper.request.timeout value is 0. feature enabled=false
2020-08-08 17:53:05,182 [myid:localhost:2181] - INFO  [main-SendThread(localhost:2181):ClientCnxn$SendThread@1154] - Opening socket connection to server localhost/127.0.0.1:2181.
2020-08-08 17:53:05,183 [myid:localhost:2181] - INFO  [main-SendThread(localhost:2181):ClientCnxn$SendThread@1156] - SASL config status: Will not attempt to authenticate using SASL (unknown error)
Welcome to ZooKeeper!
JLine support is enabled
2020-08-08 17:53:05,279 [myid:localhost:2181] - INFO  [main-SendThread(localhost:2181):ClientCnxn$SendThread@986] - Socket connection established, initiating session, client: /127.0.0.1:40650, server: localhost/127.0.0.1:2181
2020-08-08 17:53:05,317 [myid:localhost:2181] - INFO  [main-SendThread(localhost:2181):ClientCnxn$SendThread@1420] - Session establishment complete on server localhost/127.0.0.1:2181, session id = 0x105f5cb2c990000, negotiated timeout = 30000

WATCHER::

WatchedEvent state:SyncConnected type:None path:null

输入help命令后,会输出可用的zookeeper命令,如下所示

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] 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 val path
        stat [-w] path
        sync path
        version 

各个命令简介如下

addauth scheme auth 添加用户,语法在后面权限内会详细去说。

close 断开当前客户端和服务端的连接

config [-c] [-w] [-s] 动态加载配置

connect host:port 连接到客户端 

create [-s] [-e] [-c] [-t ttl] path [data] [acl] 创建节点 -e 临时节点(不允许有子节点) -s 序列节点 -c 默认节点  (path为路径)  【data为数据】 【acl权限】 -t节点存活时间,我这未成功。有弄明白-t的小伙伴帮我一下,留言就行。

delete [-v version] path 删除节点(不能带有子节点) [-v version] 版本号,一般不用

deleteall path 删除节点(包含其子节点)

delquota [-n|-b] path 删除节点限额 -n 子节点数 -b 字节数

get [-s] [-w] path 取得节点的值 -s 取值和状态,-w 添加监听(监听数据)

getAcl [-s] path 取得限权,-s取权限和状态

history 历史操作记录

listquota path 查看节点限额

ls [-s] [-w] [-R] path查看节点  -s查看节点和内容  -w(添加监听是否添加或删除子节点,但不会监听子节点值的变化) -R查到所有节点(包含根节点)

ls2 path [watch] 相当于ls和stat的组合

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 重新操作某命令,与history配合使用

removewatches path [-c|-d|-a] [-l] 移除监听

rmr path 删除和deleteAll完全一致(已被deleteAll替换,不建议继续使用)

set [-s] [-v version] path data 给节点赋值 -s返回节点状态

setAcl [-s] [-v version] [-R] path acl 设置节点权限(后面会详细说一下这个)

setquota -n|-b val path 设置节点限额 -n 子节点数 -b 字节数

stat [-w]path 查看节点状态,-w同get -w用法

sync path 强制同步

创建节点

命令:

create [-s] [-e] [-c] [-t ttl] path [data] [acl]
-e 临时节点(不允许有子节点) -s 序列节点 -c 默认节点  (path为路径)  【data为数据】 【acl权限】 -t节点存活时间
  1. 创建顺序节点
[zk: localhost:2181(CONNECTED) 1] create -s /zk-test 123
Created /zk-test0000000000

如上所示,创建一个内容为123的顺序节点/zk-test,执行完后,在根节点下创建了一个叫做zk-test0000000000的节点,自动在节点名称后面加了一个数字后缀

  1. 创建临时节点
[zk: localhost:2181(CONNECTED) 2] create -e /zk-temp 123
Created /zk-temp

如上所示,临时节点名称没有数字后缀。临时节点在客户端会话结束后,就会自动删除,下面使用quit命令退出客户端

[zk: localhost:2181(CONNECTED) 3] ls /
[zk-temp, zk-test0000000000, zookeeper]
[zk: localhost:2181(CONNECTED) 4] quit

WATCHER::

WatchedEvent state:Closed type:None path:null
2020-08-08 18:11:16,250 [myid:] - INFO  [main:ZooKeeper@1618] - Session: 0x105f5cb2c990000 closed
2020-08-08 18:11:16,250 [myid:] - INFO  [main-EventThread:ClientCnxn$EventThread@566] - EventThread shut down for session: 0x105f5cb2c990000
2020-08-08 18:11:16,251 [myid:] - ERROR [main:ServiceUtils@42] - Exiting JVM with code 0

再次使用客户端连接服务端,使用ls / 命令查看根节点下的子节点

[zk: localhost:2181(CONNECTED) 0] ls /
[zk-test0000000000, zookeeper]

此时可以看到根目录下已经不存在/zk-temp 临时节点了

  1. 创建永久节点
[zk: localhost:2181(CONNECTED) 1] create /zk-permanent 123
Created /zk-permanent
[zk: localhost:2181(CONNECTED) 2] ls /
[zk-permanent, zk-test0000000000, zookeeper]

如上,可以看到永久节点不同于顺序节点,不会自动在节点名称后面加上数字后缀

读取节点

读取节点可以使用ls、get 命令
ls命令可以列出zookeeper指定节点下的所有子节点,但只能查看指定节点下的第一级的所有子节点

[zk: localhost:2181(CONNECTED) 2] ls /
[zk-permanent, zk-test0000000000, zookeeper]

get命令可以获取zookeeper指定节点的数据内容和属性信息
get [-s] [-w] path 取得节点的值 -s 取值和状态,-w 添加监听(监听数据)

[zk: localhost:2181(CONNECTED) 2] get -s /zk-permanent
123
cZxid = 0x6
ctime = Sat Aug 08 18:15:09 CST 2020
mZxid = 0x7
mtime = Sat Aug 08 18:26:41 CST 2020
pZxid = 0x6
cversion = 0
dataVersion = 0
aclVersion = 0
ephemeralOwner = 0x0
dataLength = 3
numChildren = 0

cZxid:节点创建时的zxid
ctime:节点创建时间
mZxid:节点最近一次更新时的zxid
mtime:节点最近一次更新的时间
cversion:子节点的版本号
dataVersion:本节点的版本号
aclVersion:节点ACL(授权信息)的版本号
ephemeralOwner:如果该节点为临时节点,ephemeralOwner值表示与该节点绑定的session id. 如果该节点不是临时节点,ephemeralOwner值为0
dataLength:节点数据长度,本例中为hello world的长度
numChildren:子节点个数

更新节点

set [-s] [-v version] path data 给节点赋值 -s返回节点状态,-v 对指定的版本进行更新

[zk: localhost:2181(CONNECTED) 11] set -s /zk-permanent 234
cZxid = 0x6
ctime = Sat Aug 08 18:15:09 CST 2020
mZxid = 0x8
mtime = Sat Aug 08 18:29:59 CST 2020
pZxid = 0x6
cversion = 0
dataVersion = 1
aclVersion = 0
ephemeralOwner = 0x0
dataLength = 3
numChildren = 0

如上所示,更新以后,dataVersion版本信息变为1,表示进行了更新

删除节点

delete [-v version] path 删除节点(不能带有子节点) [-v version] 版本号,一般不用
deleteall path 删除节点(包含其子节点)

[zk: localhost:2181(CONNECTED) 13] delete /zk-permanent
[zk: localhost:2181(CONNECTED) 14] ls /
[zk-test0000000000, zookeeper]

如上所示,成功删除/zk-permanent 节点。值得注意的是,如果删除节点存在子节点,则无法删除该节点,必须删除子节点再删除父节点。

你可能感兴趣的:(zookeeper,zookeeper)