一, Zookeeper 数据模型
具备目录和文件的双重特点
通过路径引用
储存数据有大小限制
三部分: 状态信息, 关联数据, 子节点
二, Znode 节点类型
永久节点: 生命周期不依赖于会话, 并且只能在客户端显式执行删除操作的时候, 才能被删除;
临时节点: 生命周期依赖于会话, 一旦会话结束, 临时节点将会被自动删除; 不能有子节点.
Znode 具有序列化特性, 如果在创建的时候指定的话, Znode 的名字后面会自动追加一个不断增加的序列号. 序列号对于此节点的父节点来说是唯一的, 这样子节点根据创建的先后顺序不同, 在名字上有不同的先后顺序(%10d: 10 位数字, 没有数值的数位用 0 补充)
三, Shell 操作
使用 zkCli.sh 连接到 zookeeper 集群之后, 如果不晓得操作有哪些, 就随便输入一些, 便会显示关于 zookeeper 的操作指令
bin/zkCli.sh -server node1:2181
[zk: node1:2181(CONNECTED) 0] ls /
[zookeeper]
[zk: node1: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: node1:2181(CONNECTED) 2] ls -R /
/
/zookeeper
/zookeeper/config
/zookeeper/quota
[zk: node1:2181(CONNECTED) 3]
复制代码
指令的内容, 无非是对 zookeeper 这个 Znode 的增删改查
创建节点
1, PERSISTENT:永久节点
[zk: node1:2181(CONNECTED) 11] create /2021sunny
Created /2021sunny
[zk: node1:2181(CONNECTED) 13] ls /
[2021sunny, zookeeper]
[zk: node1:2181(CONNECTED) 14] ls / -R
/
/2021sunny
/zookeeper
/zookeeper/config
/zookeeper/quota
复制代码
[zk: node1:2181(CONNECTED) 11] create /2021sunny
Created /2021sunny
[zk: node1:2181(CONNECTED) 13] ls /
[2021sunny, zookeeper]
[zk: node1:2181(CONNECTED) 14] ls / -R
/
/2021sunny
/zookeeper
/zookeeper/config
/zookeeper/quota
复制代码
2, EPHEMERAL:临时节点
[zk: node1:2181(CONNECTED) 20] create -e /2021happy
Created /2021happy
[zk: node1:2181(CONNECTED) 22] ls -R /
/
/2021happy
/2021sunny
/zookeeper
/zookeeper/config
/zookeeper/quota
--------------------退出客户端, 重新登录zookeeper--------------------
[zk: node1:2181(CONNECTED) 23] quit
WATCHER::
WatchedEvent state:Closed type:None path:null
2021-01-12 11:55:37,972 [myid:] - INFO [main:ZooKeeper@1422] - Session: 0x10001ef1be90000 closed
2021-01-12 11:55:37,972 [myid:] - INFO [main-EventThread:ClientCnxn$EventThread@524] - EventThread shut down for session: 0x10001ef1be90000
--------------------退出客户端, 重新登录zookeeper--------------------
[zk: node1:2181(CONNECTED) 0] ls -R /
/
/2021sunny
/zookeeper
/zookeeper/config
/zookeeper/quota
[zk: node1:2181(CONNECTED) 1]
复制代码
临时 Znode 节点原理: 该临时节点只与客户端的会话具有相同生命周期, 当前会话 session 断开, 节点自动删除.
应用场景: 集群中各个节点的存活状态管理.
3, PERSISTENT_SEQUENTIAL:永久节点、序列化
[zk: node1:2181(CONNECTED) 0] ls -R /
/
/2021sunny
/zookeeper
/zookeeper/config
/zookeeper/quota
[zk: node1:2181(CONNECTED) 1] create -s /2021sunny/node
Created /2021sunny/node0000000003
[zk: node1:2181(CONNECTED) 2] create -s /2021sunny/node
Created /2021sunny/node0000000004
[zk: node1:2181(CONNECTED) 3] create -s /2021sunny/node
Created /2021sunny/node0000000005
[zk: node1:2181(CONNECTED) 4] ls -r /
org.apache.commons.cli.UnrecognizedOptionException: Unrecognized option: -r
[zk: node1:2181(CONNECTED) 5] ls -R /
/
/2021sunny
/zookeeper
/2021sunny/node0000000003
/2021sunny/node0000000004
/2021sunny/node0000000005
/zookeeper/config
/zookeeper/quota
[zk: node1:2181(CONNECTED) 6]
复制代码
4, EPHEMERAL_SEQUENTIAL:临时节点、序列化
WatchedEvent state:SyncConnected type:None path:null
[zk: node1:2181(CONNECTED) 0] ls /
[2021sunny, zookeeper]
[zk: node1:2181(CONNECTED) 1] create -e -s /2021sunny/cnode
Created /2021sunny/cnode0000000000
[zk: node1:2181(CONNECTED) 2] create -e -s /2021sunny/cnode
Created /2021sunny/cnode0000000001
[zk: node1:2181(CONNECTED) 3] create -e -s /2021sunny/cnode
Created /2021sunny/cnode0000000002
[zk: node1:2181(CONNECTED) 4] ls -R /
/
/2021sunny
/zookeeper
/2021sunny/cnode0000000000
/2021sunny/cnode0000000001
/2021sunny/cnode0000000002
/zookeeper/config
/zookeeper/quota
[zk: node1:2181(CONNECTED) 5]
--------------------退出客户端, 重新登录zookeeper--------------------
[zk: node1:2181(CONNECTED) 23] quit
--------------------退出客户端, 重新登录zookeeper--------------------
[zk: node1:2181(CONNECTED) 0] ls -R /
/
/2021sunny
/zookeeper
/zookeeper/config
/zookeeper/quota
[zk: node1:2181(CONNECTED) 1]
复制代码
查看节点
上面已经试验了 ls 以及 ls -R 的使用效果
下面查看 ls2 的效果: 使用 ls -s 替代了, 也就是展示详细信息
[zk: node1:2181(CONNECTED) 7] ls2 /
'ls2' has been deprecated. Please use 'ls [-s] path' instead.
[zookeeper, 2021sunny]
cZxid = 0x0
ctime = Thu Jan 01 08:00:00 CST 1970
mZxid = 0x0
mtime = Thu Jan 01 08:00:00 CST 1970
pZxid = 0x100000000c
cversion = 6
dataVersion = 0
aclVersion = 0
ephemeralOwner = 0x0
dataLength = 0
numChildren = 2
[zk: node1:2181(CONNECTED) 8] ls2 /2021sunny
'ls2' has been deprecated. Please use 'ls [-s] path' instead.
[node0000000003, node0000000004, node0000000005]
cZxid = 0x1000000002
ctime = Tue Jan 12 11:49:28 CST 2021
mZxid = 0x1000000005
mtime = Tue Jan 12 11:50:43 CST 2021
pZxid = 0x1000000015
cversion = 9
dataVersion = 2
aclVersion = 0
ephemeralOwner = 0x0
dataLength = 14
numChildren = 3
[zk: node1:2181(CONNECTED) 9]
复制代码
设置 or 更新节点数据
作用: 给 Znode 节点设置值和获取值
[zk: node1:2181(CONNECTED) 16] set /2021sunny/node0000000003 "work hard"
[zk: node1:2181(CONNECTED) 17] get /2021sunny/node0000000003
work hard
[zk: node1:2181(CONNECTED) 18] ls -s /2021sunny/node0000000003
[]cZxid = 0x1000000013
ctime = Tue Jan 12 12:02:39 CST 2021
mZxid = 0x1000000016
mtime = Tue Jan 12 12:07:26 CST 2021
pZxid = 0x1000000013
cversion = 0
dataVersion = 1
aclVersion = 0
ephemeralOwner = 0x0
dataLength = 9
numChildren = 0
复制代码
删除节点
[zk: node1:2181(CONNECTED) 34] ls -R /
/
/2021sunny
/zookeeper
/2021sunny/node0000000003
/2021sunny/node0000000004
/2021sunny/node0000000005
/zookeeper/config
/zookeeper/quota
[zk: node1:2181(CONNECTED) 35] delete /2021sunny/node0000000003
[zk: node1:2181(CONNECTED) 36] ls -R /
/
/2021sunny
/zookeeper
/2021sunny/node0000000004
/2021sunny/node0000000005
/zookeeper/config
/zookeeper/quota
[zk: node1:2181(CONNECTED) 37]
复制代码
删除所有: rmr(已经过时), 提示使用 deleteall
[zk: node1:2181(CONNECTED) 57] rmr /2021sunny
The command 'rmr' has been deprecated. Please use 'deleteall' instead.
[zk: node1:2181(CONNECTED) 58] ls -R /
/
/zookeeper
/zookeeper/config
/zookeeper/quota
[zk: node1:2181(CONNECTED) 59]
复制代码
列出历史操作记录
作用: 罗列出当前 session 中的所有历史操作
[zk: node1:2181(CONNECTED) 6] history
0 - ls -R /
1 - create -s /2021sunny/node
2 - create -s /2021sunny/node
3 - create -s /2021sunny/node
4 - ls -r /
5 - ls -R /
6 - history
复制代码
评论