help 查看zookeeper所支持的所有命令
代码语言:javascript复制[zk: localhost:2181(CONNECTED) 1] help
ZooKeeper -server host:port cmd args
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
delquota [-n|-b] path
get [-s] [-w] path
getAcl [-s] path
history
listquota path
ls [-s] [-w] [-R] path
ls2 path [watch]
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]
rmr path
set [-s] [-v version] path data
setAcl [-s] [-v version] [-R] path acl
setquota -n|-b val path
stat [-w] path
sync path
Command not found: Command not found help
[zk: localhost:2181(CONNECTED) 2]
创建zookeeper 节点命令
代码语言:javascript复制 create [-s] [-e] [-c] [-t ttl] path [data] [acl]
中括号为可选项,没有则默认创建持久化节点
- -s: 顺序节点
- -e: 临时节点
- -c: 容器节点
- -t: 可以给节点添加过期时间,默认禁用,需要通过系统参数启用
(-Dzookeeper.extendedTypesEnabled=true, znode.container.checkIntervalMs : (Java system property only) New in 3.5.1: The time interval in milliseconds for each check of candidate container and ttl nodes. Default is “60000”.)
创建持久化节点 create
持久化保存的节点,也是默认创建的
代码语言:javascript复制[zk: localhost:2181(CONNECTED) 1] create /artisan-persist data_xxx
Created /artisan-persist
查看节点 get
代码语言:javascript复制[zk: localhost:2181(CONNECTED) 3] get /artisan-persist
data_xxx
[zk: localhost:2181(CONNECTED) 4]
修改节点 set
代码语言:javascript复制[zk: localhost:2181(CONNECTED) 4] set /artisan-persist data_xxx_new
[zk: localhost:2181(CONNECTED) 5]
[zk: localhost:2181(CONNECTED) 5]
[zk: localhost:2181(CONNECTED) 5]
[zk: localhost:2181(CONNECTED) 5] get /artisan-persist
data_xxx_new
[zk: localhost:2181(CONNECTED) 6]
创建持久序号节点,加参数 -s
创建时zookeeper 会在路径上加上序号作为后缀,。非常适合用于分布式锁、分布式选举等场景。创建时添加 -s 参数即可。
代码语言:javascript复制[zk: localhost:2181(CONNECTED) 11] create -s /artisan_seq
Created /artisan_seq0000000002
[zk: localhost:2181(CONNECTED) 12]
[zk: localhost:2181(CONNECTED) 12] create -s /artisan_seq
Created /artisan_seq0000000003
[zk: localhost:2181(CONNECTED) 13] create -s /artisan_seq
Created /artisan_seq0000000004
[zk: localhost:2181(CONNECTED) 14] create -s /artisan_seq
Created /artisan_seq0000000005
[zk: localhost:2181(CONNECTED) 15] create -s /artisan_seq
Created /artisan_seq0000000006
[zk: localhost:2181(CONNECTED) 16] ls -R /
/
/artisan-persist
/artisan_seq0000000002
/artisan_seq0000000003
/artisan_seq0000000004
/artisan_seq0000000005
/artisan_seq0000000006
/zookeeper
/zookeeper/config
/zookeeper/quota
[zk: localhost:2181(CONNECTED) 17]
创建临时节点,加参数 -e
临时节点会在客户端会话断开后自动删除。适用于心跳,服务发现等场景。创建时添加参数-e 即可。
创建临时节点, 断开会话 在连接将会自动删除
代码语言:javascript复制[zk: localhost:2181(CONNECTED) 17] create -e /artisan-e
Created /artisan-e
代码语言:javascript复制[zk: localhost:2181(CONNECTED) 20] ls -R /artisan-e
/artisan-e
[zk: localhost:2181(CONNECTED) 21]
退出重连
临时节点没啦。。。。
我们结合日志来看一下
创建临时序号节点, 加参数 -s -e
与持久序号节点类似,不同之处在于EPHEMERAL_SEQUENTIAL是临时的会在会话断开后删除。创建时添加 -e -s
语法
代码语言:javascript复制[zk: localhost:2181(CONNECTED) 1] create -s -e /abc
Created /abc0000000008
[zk: localhost:2181(CONNECTED) 2] create -s -e /abc
Created /abc0000000009
[zk: localhost:2181(CONNECTED) 3] create -s -e /abc
Created /abc0000000010
[zk: localhost:2181(CONNECTED) 4] create -s -e /abc
Created /abc0000000011
[zk: localhost:2181(CONNECTED) 5] create -s -e /abc
Created /abc0000000012
[zk: localhost:2181(CONNECTED) 6] ls -R /
/
/abc0000000008
/abc0000000009
/abc0000000010
/abc0000000011
/abc0000000012
/artisan-persist
/artisan_seq0000000002
/artisan_seq0000000003
/artisan_seq0000000004
/artisan_seq0000000005
/artisan_seq0000000006
/zookeeper
/zookeeper/config
/zookeeper/quota
[zk: localhost:2181(CONNECTED) 7]
创建容器节点 -c
容器节点主要用来容纳字节点,如果没有给其创建子节点,容器节点表现和持久化节点一样,如果给容器节点创建了子节点,后续又把子节点清空,容器节点也会被zookeeper删除。
代码语言:javascript复制[zk: localhost:2181(CONNECTED) 14] create -c /artisan-c
Created /artisan-c
查看节点属性信息 stat
代码语言:javascript复制[zk: localhost:2181(CONNECTED) 6] stat /artisan-persist
cZxid = 0xa
ctime = Mon Nov 16 23:07:37 CST 2020
mZxid = 0xb
mtime = Mon Nov 16 23:08:55 CST 2020
pZxid = 0xa
cversion = 0
dataVersion = 1
aclVersion = 0
ephemeralOwner = 0x0
dataLength = 12
numChildren = 0
[zk: localhost:2181(CONNECTED) 7]
- cZxid:创建znode的事务ID(Zxid的值)。
- mZxid:最后修改znode的事务ID。
- pZxid:最后添加或删除子节点的事务ID(子节点列表发生变化才会发生改变)。
- ctime:znode创建时间。
- mtime:znode最近修改时间。
- dataVersion:znode的当前数据版本。
- cversion:znode的子节点结果集版本(一个节点的子节点增加、删除都会影响这个版本)。
- aclVersion:表示对此znode的acl版本。
- ephemeralOwner:znode是临时znode时,表示znode所有者的 session ID。 如果znode不是临时znode,则该字段设置为零。
- dataLength:znode数据字段的长度。
- numChildren:znode的子znode的数量。
查看节点状态信息同时查看数据 get -s
代码语言:javascript复制[zk: localhost:2181(CONNECTED) 9] get -s /artisan-persist
data_xxx_new
cZxid = 0xa
ctime = Mon Nov 16 23:07:37 CST 2020
mZxid = 0xb
mtime = Mon Nov 16 23:08:55 CST 2020
pZxid = 0xa
cversion = 0
dataVersion = 1
aclVersion = 0
ephemeralOwner = 0x0
dataLength = 12
numChildren = 0
[zk: localhost:2181(CONNECTED) 10]