源码:https://github.com/shawntime/shawn-zookeeper-utils
wiki地址:https://github.com/shawntime/shawn-zookeeper-utils/wiki
zkcli.sh连接Zookeeper集群
客户端连接命名
./zkcli.sh -timeout 0 -r -server host:port
- timeout:连接超时时间,单位:ms
- r:当超过半数服务器失去连接时,zk集群无法写入,但可以提供只读操作
- server:服务器ip和端口
./zkcli.sh -timeout 5000 -server 192.168.1.102:2181
命令集合
ZooKeeper -server host:port cmd args
stat path [watch]:查询节点状态
set path data [version]:修改节点数据
ls path [watch]:查询子节点列表
delquota [-n|-b] path:删除额定设置,-n为子节点个数,-b为节点数据长度
ls2 path [watch]:查询子节点列表+获取节点stat
setAcl path acl:设置节点权限
setquota -n|-b val path:节点额定设置,-n为子节点个数,-b为节点数据长度
history:显示之前操作列表
redo cmdno:配合history使用,重新执行某一步骤的cmdno
printwatches on|off
delete path [version]:删除节点
sync path
listquota path:显示节点额定列表
rmr path:递归删除节点
get path [watch]:获取节点数据
create [-s] [-e] path data acl:创建节点
addauth scheme auth
quit:退出
getAcl path:获取节点权限
close:关闭当前会话
connect host:port:连接另一台服务器
查询
ls path [watch]:查询出path的子节点列表
ls2 path [watch]:查询出path的子节点列表和当前节点stat信息
节点信息
stat path [watcher]:查询节点信息
[zk: localhost:2181(CONNECTED) 2] stat /node_2
// cZxid:创建节点事务id
cZxid = 0x100000002e
// ctime:创建时间
ctime = Tue Mar 20 23:37:02 CST 2018
// mZxid:最新修改节点事务id
mZxid = 0x100000002e
// mtime:修改时间
mtime = Tue Mar 20 23:37:02 CST 2018
// 子节点事务id
pZxid = 0x100000003a
//
cversion = 4
// 更新数据版本号,update/delete命名根据该版本号进行乐观锁操作
dataVersion = 0
// 权限版本号
aclVersion = 0
ephemeralOwner = 0x0
// 节点上数据长度
dataLength = 3
// 子节点个数
numChildren = 2
创建节点
create [-s] [-e] path data acl
-s:顺序节点
-e:临时节点
[zk: localhost:2181(CONNECTED) 4] create -e -s /node_2/node_2_10 shawntime
Created /node_2/node_2_100000000003
修改节点
set path data [version]
version:不设置默认为-1,设置了必须在与stat中dataVersion相等的时才会修改成功,乐观锁的实现
[zk: localhost:2181(CONNECTED) 9] set /node_2/node_2_100000000003 dfsfsdk 1
version No is not valid : /node_2/node_2_100000000003
[zk: localhost:2181(CONNECTED) 10] set /node_2/node_2_100000000003 dfsfsdk 0
cZxid = 0x1200000005
ctime = Fri Mar 23 22:20:42 CST 2018
mZxid = 0x1200000009
mtime = Fri Mar 23 22:23:49 CST 2018
pZxid = 0x1200000005
cversion = 0
dataVersion = 1
aclVersion = 0
ephemeralOwner = 0x16253274de40001
dataLength = 7
numChildren = 0
删除节点
delete path [version]:如果删除节点有子节点则无法删除,version用法与update相同
rmr path:递归删除
[zk: localhost:2181(CONNECTED) 13] ls /node_2
[node_2_100000000003, node_2_1, node_2_2]
[zk: localhost:2181(CONNECTED) 14] delete /node_2
Node not empty: /node_2
[zk: localhost:2181(CONNECTED) 15] delete /node_2/node_2_100000000003 2
version No is not valid : /node_2/node_2_100000000003
[zk: localhost:2181(CONNECTED) 16] delete /node_2/node_2_100000000003 0
version No is not valid : /node_2/node_2_100000000003
[zk: localhost:2181(CONNECTED) 17] delete /node_2/node_2_100000000003 1
[zk: localhost:2181(CONNECTED) 18] rmr /node_2
获取节点数据
get path [watch]
[zk: localhost:2181(CONNECTED) 22] get /node_3
123
cZxid = 0x1000000035
ctime = Tue Mar 20 23:41:12 CST 2018
mZxid = 0x1000000035
mtime = Tue Mar 20 23:41:12 CST 2018
pZxid = 0x1000000035
cversion = 0
dataVersion = 0
aclVersion = 0
ephemeralOwner = 0x0
dataLength = 3
numChildren = 0