客户端命令行操作
命令基本语法 | 功能描述 |
---|---|
help | 显示所有操作命令 |
ls path [watch] | 使用ls命令来查看znode中所包含的内容 |
ls2 path [watch] | 查看当前节点数据并能看到更新次数等数据 |
create | 普通创建、-s 含有序列、-e 临时(重启或者超时消失) |
get path [watch] | 获得节点的值 |
set | 设置节点的具体值 |
stat | 查看节点状态 |
delete | 删除节点 |
rmr | 递归删除节点 |
1.启动客户端
[root@bhz196 bin]# zkCli.sh
2.显示所有操作
[zk: localhost:2181(CONNECTED) 0] help
ZooKeeper -server host:port cmd args
stat path [watch]
set path data [version]
ls path [watch]
delquota [-n|-b] path
ls2 path [watch]
setAcl path acl
setquota -n|-b val path
history
redo 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
3.查看当前znode中所包含的内容
[zk: localhost:2181(CONNECTED) 0] ls /
[dubbo, zookeeper]
4.查看当前节点详细数据
[zk: localhost:2181(CONNECTED) 2] ls2 /
[dubbo, zookeeper]
cZxid = 0x0
ctime = Wed Dec 31 19:00:00 EST 1969
mZxid = 0x0
mtime = Wed Dec 31 19:00:00 EST 1969
pZxid = 0x200000002
cversion = 0
dataVersion = 0
aclVersion = 0
ephemeralOwner = 0x0
dataLength = 0
numChildren = 2
5.分别创建2个普通节点
[zk: localhost:2181(CONNECTED) 3] create /sanguo "jinlian"
Created /sanguo
[zk: localhost:2181(CONNECTED) 4] ls /
[dubbo, zookeeper, sanguo]
[zk: localhost:2181(CONNECTED) 5] create /sanguo/shuguo "liubei"
Created /sanguo/shuguo
[zk: localhost:2181(CONNECTED) 6] ls /sanguo
[shuguo]
6.获得节点的值
[zk: localhost:2181(CONNECTED) 8] get /sanguo/shuguo
"liubei"
cZxid = 0x300000005
ctime = Wed Jan 15 15:46:43 EST 2020
mZxid = 0x300000005
mtime = Wed Jan 15 15:46:43 EST 2020
pZxid = 0x300000005
cversion = 0
dataVersion = 0
aclVersion = 0
ephemeralOwner = 0x0
dataLength = 8
numChildren = 0
7.创建短暂节点
[zk: localhost:2181(CONNECTED) 11] create -e /sanguo/wuguo "zhouyu"
Created /sanguo/wuguo
8.创建带序号的节点
[zk: localhost:2181(CONNECTED) 14] create -s /sanguo/weiguo "caocao"
Created /sanguo/weiguo0000000002
[zk: localhost:2181(CONNECTED) 16] create -s /sanguo/weiguo "caocao"
Created /sanguo/weiguo0000000003
如果原来没有序号节点,序号从0开始递增,如果原节点下已有2个节点,则再排序时从2开始,以此类推。
9.修改节点数据值
[zk: localhost:2181(CONNECTED) 17] set /sanguo/shuguo "diaochan"
cZxid = 0x300000005
ctime = Wed Jan 15 15:46:43 EST 2020
mZxid = 0x300000009
mtime = Wed Jan 15 15:59:22 EST 2020
pZxid = 0x300000005
cversion = 0
dataVersion = 1
aclVersion = 0
ephemeralOwner = 0x0
dataLength = 10
numChildren = 0
[zk: localhost:2181(CONNECTED) 18] get /sanguo/shuguo
"diaochan"
cZxid = 0x300000005
ctime = Wed Jan 15 15:46:43 EST 2020
mZxid = 0x300000009
mtime = Wed Jan 15 15:59:22 EST 2020
pZxid = 0x300000005
cversion = 0
dataVersion = 1
aclVersion = 0
ephemeralOwner = 0x0
dataLength = 10
numChildren = 0
10.节点值的变化监听
1.在zk3主机上注册监听/sanguo节点数据变化
[zk: localhost:2181(CONNECTED) 1] get /sanguo watch
"jinlian"
cZxid = 0x300000004
ctime = Wed Jan 15 15:45:54 EST 2020
mZxid = 0x300000004
mtime = Wed Jan 15 15:45:54 EST 2020
pZxid = 0x300000008
cversion = 4
dataVersion = 0
aclVersion = 0
ephemeralOwner = 0x0
dataLength = 9
numChildren = 4
2.在zk2主机上修改节点
[zk: localhost:2181(CONNECTED) 1] set /sanguo "jingjing"
cZxid = 0x300000004
ctime = Wed Jan 15 15:45:54 EST 2020
mZxid = 0x30000000c
mtime = Wed Jan 15 16:04:14 EST 2020
pZxid = 0x300000008
cversion = 4
dataVersion = 1
aclVersion = 0
ephemeralOwner = 0x0
dataLength = 10
numChildren = 4
3.zk3上会弹出以下信息
[zk: localhost:2181(CONNECTED) 2]
WATCHER::
WatchedEvent state:SyncConnected type:NodeDataChanged path:/sanguo
11.节点的子节点变化监听(路径变化)
1.在zk3主机上创建监听
[zk: localhost:2181(CONNECTED) 0] ls /sanguo watch
[wuguo, weiguo0000000003, weiguo0000000002, shuguo]
2.在zk2主机上创建一个节点
[zk: localhost:2181(CONNECTED) 2] create /sanguo/banzhang "banzhang"
Created /sanguo/banzhang
3.zk3主机上会显示如下信息
[zk: localhost:2181(CONNECTED) 1]
WATCHER::
WatchedEvent state:SyncConnected type:NodeChildrenChanged path:/sanguo
12.删除节点
[zk: localhost:2181(CONNECTED) 20] delete /sanguo/banzhang
[zk: localhost:2181(CONNECTED) 21] ls /sanguo
[wuguo, weiguo0000000003, weiguo0000000002, shuguo]
13.递归删除节点
[zk: localhost:2181(CONNECTED) 22] delete /sanguo
Node not empty: /sanguo
[zk: localhost:2181(CONNECTED) 23] rmr /sanguo
[zk: localhost:2181(CONNECTED) 24] ls /
[dubbo, zookeeper]
[zk: localhost:2181(CONNECTED) 25]
14.查看节点状态
[zk: localhost:2181(CONNECTED) 25] stat /zookeeper
cZxid = 0x0
ctime = Wed Dec 31 19:00:00 EST 1969
mZxid = 0x0
mtime = Wed Dec 31 19:00:00 EST 1969
pZxid = 0x0
cversion = -1
dataVersion = 0
aclVersion = 0
ephemeralOwner = 0x0
dataLength = 0
numChildren = 1