Zookeeper--(三)基于Linux的ZK客户端命令行学习

文章目录

    • zk常用命令行操作
    • zk特性-watcher机制
    • Watcher事件类型

zk常用命令行操作

  • 通过./zkCli.sh 打开zk的客户端进行命令行后台
  • ls 与 ls2(会显示状态信息)
[zk: localhost:2181(CONNECTED) 0] ls /
[zookeeper, test]
  • get / 获取节点的状态信息
[zk: localhost:2181(CONNECTED) 2] get /test
123
cZxid = 0x300000002
ctime = Wed Jan 16 00:19:36 CST 2019
mZxid = 0x300000002
mtime = Wed Jan 16 00:19:36 CST 2019
pZxid = 0x300000002
cversion = 0
dataVersion = 0
aclVersion = 0
ephemeralOwner = 0x0
dataLength = 3
numChildren = 0
  • stat + ls = ls2命令
  • 一些字段含义
cZxid = 0x0 创建id
ctime = Thu Jan 01 08:00:00 CST 1970 创建时间
mZxid = 0x0 修改Id
mtime = Thu Jan 01 08:00:00 CST 1970 修改时间
pZxid = 0x0 子节点id
cversion = -1 子节点版本
dataVersion = 0 当前节点数据版本号,修改后加1
aclVersion = 0 权限
ephemeralOwner = 0x0 判断是持久节点还是临时节点
dataLength = 0 数据长度
numChildren = 1 子节点数量
  • create [-s] [-e] path data acl(s-持久,e-临时)
创建临时节点
create -e /chandler/tep chandler-data
创建递增节点
create -s /chandler/sec seq
  • set path data [version]
修改值
set /chandler 123
乐观锁使用
set /chandler 123 1
再次设置必须要最新版本号才能更新
set /chandler 123 2
  • delete path [version]
delete /chandler/sec0001 1(该节点版本之前设置为1,必须加上最新版本号才能删除)

zk特性-watcher机制

  • 针对每个节点的操作,都会有一个监督者 ->wathcer

  • 当监控的某个对象(znode)发生了变化,则出发watcher事件

  • zk中的watcher是一次性的,触发后立即销毁

  • 父节点,子节点增删改都能触发器watcher

  • 针对不同类型的操作,触发的watcher事件也不同:

    • (子)节点创建事件
    • (子)节点删除事件
    • (子)节点数据变化事件
  • get path [watch]设置watcher

  • 父节点增删改操作触发watcher

  • 子节点增删改操作触发watcher

Watcher事件类型

  • 创建父节点触发:NodeCreated
//增加
[zk: localhost:2181(CONNECTED) 1] create /chandler 123
WATCHER::Created /chandler
WatchedEvent state:SyncConnected type:NodeCreated path:/chandler
  • 修改父节点数据触发:NodeDataChanged
//修改
//先设置get /chandler watch
[zk: localhost:2181(CONNECTED) 4] set /chandler 789
WATCHER::cZxid = 0x4
ctime = Sun Jan 13 22:11:03 CST 2019
WatchedEvent state:SyncConnected type:NodeDataChanged path:/chandler
mZxid = 0x6
mtime = Sun Jan 13 22:13:42 CST 2019
pZxid = 0x4
cversion = 0
dataVersion = 2
aclVersion = 0
ephemeralOwner = 0x0
dataLength = 3
numChildren = 0
  • 删除父节点触发:NodeDeleted
//删除
//先设置get /chandler watch
[zk: localhost:2181(CONNECTED) 6] delete /chandler
WATCHER::
WatchedEvent state:SyncConnected type:NodeDeleted path:/chandler
  • ls为父节点设置watcher,创建子节点触发:NodeChildrenChanged
//创建子节点
[zk: localhost:2181(CONNECTED) 13] ls /
[zookeeper, chandler]
[zk: localhost:2181(CONNECTED) 14] ls /chandler watch
[]
[zk: localhost:2181(CONNECTED) 15] create /chandler/abc 88
WATCHER::
Created /chandler/abc

WatchedEvent state:SyncConnected type:NodeChildrenChanged path:/chandler
  • ls为父节点设置watcher,删除子节点触发:NodeChildrenChanged
//删除子节点
[zk: localhost:2181(CONNECTED) 16] ls /chandler watch
[abc]
[zk: localhost:2181(CONNECTED) 17] delete /chandler/abc
WATCHER::

WatchedEvent state:SyncConnected type:NodeChildrenChanged path:/chandler
  • ls为父节点设置watcher,修改子节点不触发事件
//修改子节点没反应,需要通过父节点watch才行
[zk: localhost:2181(CONNECTED) 20] ls /chandler watch
[xyz]
[zk: localhost:2181(CONNECTED) 21] set /chandler/xyz 9090
cZxid = 0xb
ctime = Sun Jan 13 22:23:52 CST 2019
mZxid = 0xc
mtime = Sun Jan 13 22:24:22 CST 2019
pZxid = 0xb
cversion = 0
dataVersion = 1
aclVersion = 0
ephemeralOwner = 0x0
dataLength = 4
numChildren = 0
//通过get /chandler/xyz watch
[zk: localhost:2181(CONNECTED) 23] set /chandler/xyz 8080
WATCHER::cZxid = 0xb

WatchedEvent state:SyncConnected type:NodeDataChanged path:/chandler/xyzctime = Sun Jan 13 22:23:52 CST 2019

mZxid = 0xd
mtime = Sun Jan 13 22:26:16 CST 2019
pZxid = 0xb
cversion = 0
dataVersion = 2
aclVersion = 0
ephemeralOwner = 0x0
dataLength = 4
numChildren = 0

你可能感兴趣的:(Zookeeper)