1、命令行语法
命令基本语法 | 功能描述 |
---|
help | 显示所有的操作命令 | ls path | 使用 ls 命令查看当前znode的子节点 -w :监听子节点变化 -s 附加次级信息 | create | 普通创建 -s 含有序列 -e :临时(重启或者超时消失) | get path | 获得节点的值 【可监听】 -w : 监听子节点内容变化 -s :附加次级信息 | set | 设置节点的具体值 | stat | 查看节点状态 | delete | 删除节点 | deleteall | 递归删除节点 |
1.1、客户端启动
root@ubuntu:/usr/local/software/zookeeper/bin# zkCli.sh -server 192.168.44.133:2181
显示连接192.168.44.133:
[zk: 192.168.44.133:2181(CONNECTED) 0]
1.2、显示所有操作命令
[zk: 192.168.44.133:2181(CONNECTED) 4] help
ZooKeeper -server host:port -client-configuration properties-file cmd args
addWatch [-m mode] path
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
2、 znode 节点数据信息
2.1、查看当前znode中所包含的内容
[zk: 192.168.44.133:2181(CONNECTED) 5] ls /
[zookeeper]
2.2、查看当前节点详细数据
[zk: 192.168.44.133:2181(CONNECTED) 7] ls -s /
[zookeeper]
cZxid = 0x0
ctime = Wed Dec 31 16:00:00 PST 1969
mZxid = 0x0
mtime = Wed Dec 31 16:00:00 PST 1969
pZxid = 0x0
cversion = -1
dataVersion = 0
aclVersion = 0
ephemeralOwner = 0x0
dataLength = 0
numChildren = 1
详细信息介绍: 1)cZxid :创建节点的事务zxid 每次修改Zookeeper状态都会产生一个Zookeeper事务id,事务id是Zookeeper中所有修改总的次序,如果zxid1 < zxid2 ,那么zxid1在zxid2之前发生
2)ctime :znode被创建的毫秒数(从1969年开始)
3)mZxid :znode最后更新的事务zxid
4)mtime :znode最后修改的毫秒数(从1969年开始)
5)pZxid :znode最后更新的子节点zxid
6)cversion :znode子节点变化号,znode子节点修改次数
7)dataversion :znode数据变化号
8)dataLength :znode的数据长度
9)numChildren :znode子节点数量
2.3、节点类型以及创建节点
持久 :客户端和服务器端断开连接后,创建的节点不删除 短暂 :客户端和服务器端断开连接后,创建的节点自己删除
说明 :有序,顺序号是一个单调递增的计数器,由父节点维护
1)创建持久无序的
[zk: 192.168.232.1(CONNECTED) 6] ls -s /sanguo
[]
cZxid = 0xb66
ctime = Sun Dec 05 15:59:00 CST 2021
mZxid = 0xb66
mtime = Sun Dec 05 15:59:00 CST 2021
pZxid = 0xb66
cversion = 0
dataVersion = 0
aclVersion = 0
ephemeralOwner = 0x0
dataLength = 6
numChildren = 0
[zk: 192.168.232.1(CONNECTED) 7] create /sanguo/shuguo
Created /sanguo/shuguo
[zk: 192.168.232.1(CONNECTED) 8] ls -s /sanguo
[shuguo]
cZxid = 0xb66
ctime = Sun Dec 05 15:59:00 CST 2021
mZxid = 0xb66
mtime = Sun Dec 05 15:59:00 CST 2021
pZxid = 0xb67
cversion = 1
dataVersion = 0
aclVersion = 0
ephemeralOwner = 0x0
dataLength = 6
numChildren = 1
[zk: 192.168.247.1(CONNECTED) 9]
获得节点的值
[zk: 192.168.232.1(CONNECTED) 9] get -s /sanguo
liubei
cZxid = 0xb66
ctime = Sun Dec 05 15:59:00 CST 2021
mZxid = 0xb66
mtime = Sun Dec 05 15:59:00 CST 2021
pZxid = 0xb67
cversion = 1
dataVersion = 0
aclVersion = 0
ephemeralOwner = 0x0
dataLength = 6
numChildren = 1
2)创建持久有序的
create -s /sanguo/weiguo 增加一个 -s
[zk: 192.168.232.1(CONNECTED) 12] create -s /sanguo/weiguo
Created /sanguo/weiguo0000000001
[zk: 192.168.232.1(CONNECTED) 13] ls /sanguo/
shuguo weiguo0000000001
持久呢?退出连接再次查看是否还存在?
[zk: 192.168.232.1(CONNECTED) 13] quit
:/usr/local/software/zookeeper/bin# ./zkCli.sh -server 192.168.232.1
[zk: 192.168.232.1(CONNECTED) 0] ls /sanguo/
Path must not end with / character
[zk: 192.168.232.1(CONNECTED) 1] ls /sanguo
[shuguo, weiguo0000000001]
3)创建临时无序的
[zk: 192.168.232.1(CONNECTED) 2] create -e /sanguo/wuguo
Created /sanguo/wuguo
[zk: 192.168.232.1(CONNECTED) 3] ls /sanguo
[shuguo, weiguo0000000001, wuguo]
[zk: 192.168.232.1(CONNECTED) 4] quit
WatchedEvent state:SyncConnected type:None path:null
[zk: 192.168.232.1(CONNECTED) 0] ls /sanguo
[shuguo, weiguo0000000001]
4)创建临时有序的
[zk: 192.168.232.1(CONNECTED) 1] create -e -s /sanguo/wuguo "孙权"
Created /sanguo/wuguo0000000003
[zk: 192.168.232.1(CONNECTED) 4] quit
[zk: 192.168.232.1(CONNECTED) 0] ls /sanguo
[shuguo, weiguo0000000001]
5)修改节点数据值
[zk: 192.168.232.1(CONNECTED) 16] get /sanguo
三国
[zk: 192.168.232.1(CONNECTED) 17] set /sanguo "蜀-魏-吴"
[zk: 192.168.232.1(CONNECTED) 18] get /sanguo
蜀-魏-吴
3、删除节点以及查看
3.1、删除节点
[zk: 192.168.232.1(CONNECTED) 20] ls /sanguo
[shuguo, weiguo0000000001]
[zk: 192.168.232.1(CONNECTED) 21] delete /sanguo/shuguo
[zk: 192.168.232.1(CONNECTED) 22] ls /sanguo
[weiguo0000000001]
3.2、递归删除节点
[zk: 192.168.232.1(CONNECTED) 25] delete /sanguo
Node not empty: /sanguo
[zk: 192.168.232.1(CONNECTED) 26] deleteall /sanguo
[zk: 192.168.232.1(CONNECTED) 27] ls /sanguo
Node does not exist: /sanguo
3.3、查看节点状态
[zk: 192.168.232.1(CONNECTED) 6] stat /sanguo
cZxid = 0xb7b
ctime = Sun Dec 05 18:19:03 CST 2021
mZxid = 0xb7b
mtime = Sun Dec 05 18:19:03 CST 2021
pZxid = 0xb7c
cversion = 1
dataVersion = 0
aclVersion = 0
ephemeralOwner = 0x0
dataLength = 0
numChildren = 1
4、监听器原理
监听的详细过程 :
1)首先要有一个main 线程
2)在main()线程中创建Zookeeper客户端,这时就会创建两个线程,一个线程负责网络连接通信 一个负责监听
3)在通信线程将注册监听事件发送给Zookeeper
4)在Zookeeper的注册监听列表中,将注册的监听事件添加到列表 中
5)Zookeeper监听到有数据 或者路径 变化,就会将这个消息发送到监听线程
6)监听线程内部调用了process()方法
4.1、监听节点数据变化
在 192.168.42.133 主机上注册监听/sanguo 节点数据变化
[zk: 192.168.42.133:2181(CONNECTED) 26] get -w /sanguo
在 192.168.42.134 主机上修改/sanguo 节点的数据
[zk: 192.168.42.134:2181(CONNECTED) 1] set /sanguo "wuguo"
在 192.168.42.133 主机上提示信息
WATCHER::
WatchedEvent state:SyncConnected type:NodeDataChanged
path:/sanguo
注意 :在192.168.42.134再次修改 /sanguo 的数据值,192.168.42.133上不会再收到监听信息,因为注册一次,只可以监听一次,在监听需要再次注册
4.2、监听节点路径变化 (节点数变化)
在 192.168.42.133 主机上注册监听/sanguo 节点变化
[zk: 192.168.42.133:2181(CONNECTED) 27] ls -w /sanguo
在 192.168.42.134 主机增加 /sanguo 节点
[zk: 192.168.42.134:2181(CONNECTED) 3] create /sanguo/jin "simayi"
Created /sanguo/jin
在 192.168.42.133 主机上提示信息
WATCHER::
WatchedEvent state:SyncConnected type:NodeChildrenChanged
path:/sanguo
注意 :节点的路径变化,也是注册一次,生效一次,想多次生效,就需要多次注册
|