zookeeper的数据模型和文件系统类似,每一个节点称为:znode. 是zookeeper中的最小数据单元。每一个znode上都可以保存数据和挂载子节点。 从而构成一个层次化的属性结构
zookeeper提供了分布式数据发布/订阅,zookeeper允许客户端向服务器注册一个watcher监听。当服务器端的节点触发指定事件的时候会触发watcher。服务端会向客户端发送一个事件通知
watcher的通知是一次性,一旦触发一次通知后,该watcher就失效
zookeeper提供控制节点访问权限的功能,用于有效的保证zookeeper中数据的安全性。避免误操作而导致系统出现重大事故。
CREATE /READ/WRITE/DELETE/ADMIN
-s 表示节点是否有序
-e 表示是否为临时节点
默认情况下,是持久化节点
获得指定 path的信息
修改节点 path对应的data
乐观锁的概念
数据库里面有一个 version 字段去控制数据行的版本号
删除节点
cversion = 0 子节点的版本号
aclVersion = 0 表示acl的版本号,修改节点权限
dataVersion = 1 表示的是当前节点数据的版本号
czxid 节点被创建时的事务ID
mzxid 节点最后一次被更新的事务ID
pzxid 当前节点下的子节点最后一次被修改时的事务ID
ctime = Sat Aug 05 20:48:26 CST 2017
mtime = Sat Aug 05 20:48:50 CST 2017
cZxid = 0x500000015
ctime = Sat Aug 05 20:48:26 CST 2017
mZxid = 0x500000016
mtime = Sat Aug 05 20:48:50 CST 2017
pZxid = 0x500000015
cversion = 0
dataVersion = 1
aclVersion = 0
ephemeralOwner = 0x0 创建临时节点的时候,会有一个sessionId 。 该值存储的就是这个sessionid
dataLength = 3 数据值长度
numChildren = 0 子节点数
启动一个服务端和客户端参考:zookeeper安装和启动
help命令提示
[zk: localhost:2181(CONNECTED) 1] 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
命令
create [-s] [-e] path data acl
普通新增节点node
zk: localhost:2181(CONNECTED) 2] create /node node123
Created /node
[zk: localhost:2181(CONNECTED) 4] ls /
[node, zookeeper]
新增有序节点snode
[zk: localhost:2181(CONNECTED) 5] create -s /snode s123456
Created /snode0000000004
[zk: localhost:2181(CONNECTED) 6] ls /
[node, zookeeper, snode0000000004]
新增临时节点tempnode
[zk: localhost:2181(CONNECTED) 7] create -e /tempnode "会话有限"
Created /tempnode
[zk: localhost:2181(CONNECTED) 9] get /tempnode
会话有限
cZxid = 0xf
ctime = Wed Jan 16 22:51:33 CST 2019
mZxid = 0xf
mtime = Wed Jan 16 22:51:33 CST 2019
pZxid = 0xf
cversion = 0
dataVersion = 0
aclVersion = 0
ephemeralOwner = 0x1000002a3760000
dataLength = 12
numChildren = 0
[zk: localhost:2181(CONNECTED) 2] get /node
node123
cZxid = 0xd
ctime = Wed Jan 16 22:47:38 CST 2019
mZxid = 0xd
mtime = Wed Jan 16 22:47:38 CST 2019
pZxid = 0xd
cversion = 0
dataVersion = 0
aclVersion = 0
ephemeralOwner = 0x0
dataLength = 7
numChildren = 0
[zk: localhost:2181(CONNECTED) 4] set /node hhelk1234
cZxid = 0xd
ctime = Wed Jan 16 22:47:38 CST 2019
mZxid = 0x12
mtime = Wed Jan 16 22:54:20 CST 2019
pZxid = 0xd
cversion = 0
dataVersion = 1
aclVersion = 0
ephemeralOwner = 0x0
dataLength = 9
numChildren = 0
[zk: localhost:2181(CONNECTED) 5] get /node
hhelk1234
cZxid = 0xd
ctime = Wed Jan 16 22:47:38 CST 2019
mZxid = 0x12
mtime = Wed Jan 16 22:54:20 CST 2019
pZxid = 0xd
cversion = 0
dataVersion = 1
aclVersion = 0
ephemeralOwner = 0x0
dataLength = 9
numChildren = 0
[zk: localhost:2181(CONNECTED) 8] ls /
[node, zookeeper, snode0000000004]
[zk: localhost:2181(CONNECTED) 9] delete /snode0000000004
[zk: localhost:2181(CONNECTED) 11] ls /
[node, zookeeper]