ZooKeeper常用命令行操作
ZooKeeper常用命令行操作
-
通过./zkCli.sh 打开zk的客户端进入命令行后台
- ls/ls2 列出当前节点下的子节点 ls2还会列出当前节点的状态
[zk: localhost:2181(CONNECTED) 5] ls2 / [cluster, controller_epoch, brokers, zookeeper, admin, isr_change_notification, consumers, log_dir_event_notification, latest_producer_id_block, config] cZxid = 0x0 ctime = Thu Jan 01 08:00:00 CST 1970 mZxid = 0x0 mtime = Thu Jan 01 08:00:00 CST 1970 pZxid = 0x4e cversion = 14 dataVersion = 0 aclVersion = 0 ephemeralOwner = 0x0 dataLength = 0 numChildren = 10
- get 与 stat命令
- stat 列出节点状态 ls2相当于 ls + stat
[zk: localhost:2181(CONNECTED) 6] stat / cZxid = 0x0 ctime = Thu Jan 01 08:00:00 CST 1970 mZxid = 0x0 mtime = Thu Jan 01 08:00:00 CST 1970 pZxid = 0x4e cversion = 14 dataVersion = 0 aclVersion = 0 ephemeralOwner = 0x0 dataLength = 0 numChildren = 10
- get命令 获取节点内容和节点状态
[zk: localhost:2181(CONNECTED) 8] get /zookeeper cZxid = 0x0 #该节点创建(Create)时的id ctime = Thu Jan 01 08:00:00 CST 1970 mZxid = 0x0 #该节点修改(Modify)后的id mtime = Thu Jan 01 08:00:00 CST 1970 pZxid = 0x0 #子节点列表最后一次被修改的事务id cversion = -1 #子节点的版本号 dataVersion = 0 #当前节点数据的版本号 节点数据改变时这个值会变化 aclVersion = 0 # 权限版本, 权限发生变化时版本号回发生变化 ephemeralOwner = 0x0 dataLength = 0 #数据长度 numChildren = 1 #子节点数量
4.2 zk特性 session的基本原理与create命令使用
-
客户端与服务端之间的链接存在Session(会话)
-
每个会话可以设置一个超时时间
-
心跳机制: 客户端向服务端的ping包请求
-
心跳结束, session则过期
-
Session过期, 则临时节点znode会被抛弃
-
create命令
[zk: localhost:2181(CONNECTED) 10] create /czk czk-data #创建永久节点 Created /czk [zk: localhost:2181(CONNECTED) 11] get /czk czk-data cZxid = 0x50 ctime = Sat Dec 29 17:17:53 CST 2018 mZxid = 0x50 mtime = Sat Dec 29 17:17:53 CST 2018 pZxid = 0x50 cversion = 0 dataVersion = 0 aclVersion = 0 ephemeralOwner = 0x0 dataLength = 12 numChildren = 0
- 创建临时节点 &session过期临时节点删除
[zk: localhost:2181(CONNECTED) 12] create -e /czk/tmp czk-data Created /itheima/tmp [zk: localhost:2181(CONNECTED) 13] get /czk itheima-data cZxid = 0x50 ctime = Sat Dec 29 17:17:53 CST 2018 mZxid = 0x50 mtime = Sat Dec 29 17:17:53 CST 2018 pZxid = 0x51 cversion = 1 dataVersion = 0 aclVersion = 0 ephemeralOwner = 0x0 dataLength = 12 numChildren = 1
- 创建顺序节点
[zk: localhost:2181(CONNECTED) 1] ls /czk [] [zk: localhost:2181(CONNECTED) 2] create -s /czk/sec seq Created /itheima/sec0000000001 [zk: localhost:2181(CONNECTED) 3] create -s /czk/sec seq Created /itheima/sec0000000002 [zk: localhost:2181(CONNECTED) 4] ls /czk [sec0000000001, sec0000000002]
4.3 set/delete 命令
-
set命令:
set path data [version] # set 路径 数据 [版本号] []内为可选参数
- 不传入版本号
[zk: localhost:2181(CONNECTED) 17] get /czk czk-data1 cZxid = 0x5b ctime = Sat Dec 29 17:45:26 CST 2018 mZxid = 0x66 mtime = Sat Dec 29 18:00:12 CST 2018 pZxid = 0x65 cversion = 5 dataVersion = 1 aclVersion = 0 ephemeralOwner = 0x0 dataLength = 13 numChildren = 3 [zk: localhost:2181(CONNECTED) 18] set /czk czk-data2 cZxid = 0x5b ctime = Sat Dec 29 17:45:26 CST 2018 mZxid = 0x67 mtime = Sat Dec 29 18:02:15 CST 2018 pZxid = 0x65 cversion = 5 dataVersion = 2 aclVersion = 0 ephemeralOwner = 0x0 dataLength = 13 numChildren = 3 [zk: localhost:2181(CONNECTED) 19] get /czk czk-data2 cZxid = 0x5b ctime = Sat Dec 29 17:45:26 CST 2018 mZxid = 0x67 mtime = Sat Dec 29 18:02:15 CST 2018 pZxid = 0x65 cversion = 5 dataVersion = 2 aclVersion = 0 ephemeralOwner = 0x0 dataLength = 13 numChildren = 3
- 不传入版本号可能会有并发修改问题 可以通过指定版本号类似加锁功能
[zk: localhost:2181(CONNECTED) 22] set /czk czk-data3 1 version No is not valid : /czk #版本不匹配 修改失败 当前版本2 [zk: localhost:2181(CONNECTED) 23] set /czk czk-data3 2 cZxid = 0x5b ctime = Sat Dec 29 17:45:26 CST 2018 mZxid = 0x69 mtime = Sat Dec 29 18:29:21 CST 2018 pZxid = 0x65 cversion = 5 dataVersion = 3 aclVersion = 0 ephemeralOwner = 0x0 dataLength = 13 numChildren = 3 [zk: localhost:2181(CONNECTED) 24] get /czk itheima-data3 cZxid = 0x5b ctime = Sat Dec 29 17:45:26 CST 2018 mZxid = 0x69 mtime = Sat Dec 29 18:29:21 CST 2018 pZxid = 0x65 cversion = 5 dataVersion = 3 aclVersion = 0 ephemeralOwner = 0x0 dataLength = 13 numChildren = 3
- delete 命令: delete path [version]
[zk: localhost:2181(CONNECTED) 25] ls /czk [sec0000000003, sec0000000001, sec0000000002] [zk: localhost:2181(CONNECTED) 27] delete /czk/sec000000000 sec0000000003 sec0000000001 sec0000000002 [zk: localhost:2181(CONNECTED) 27] delete /czk/sec0000000001 [zk: localhost:2181(CONNECTED) 28] ls /czk [sec0000000003, sec0000000002] [zk: localhost:2181(CONNECTED) 29] set /czk/sec000000000 sec0000000003 sec0000000002 [zk: localhost:2181(CONNECTED) 29] set /czk/sec0000000002 test2 cZxid = 0x64 ctime = Sat Dec 29 17:49:38 CST 2018 mZxid = 0x6b mtime = Sat Dec 29 18:54:33 CST 2018 pZxid = 0x64 cversion = 0 dataVersion = 1 aclVersion = 0 ephemeralOwner = 0x0 dataLength = 5 numChildren = 0 [zk: localhost:2181(CONNECTED) 30] delete /czk/sec000000000 sec0000000003 sec0000000002 [zk: localhost:2181(CONNECTED) 30] delete /czk/sec0000000002 0 version No is not valid : /czk/sec0000000002
-
总结 使用set / delete命令的时候尽量使用版本号 避免误操作
4.4 watcher机制
-
针对每个节点的操作, 都会有一个监督者->watcher
-
当监控的某个对象(znode)发生了变化, 则触发watcher事件
-
zk中的watcher是一次性的, 触发后立即销毁
-
父节点, 子节点增删改都能触发其watcher
-
针对不同类型的操作, 触发的watcher事件也不同:
- (子)节点创建事件
- (子)节点删除事件
- (子)节点数据变化事件
-
watcher命令行
- 通过get path [watch]设置watcher
- 父节点 增 删 改操作触发 watcher
- 子节点 增 删 改操作触发 watcher
-
创建父节点触发: NodeCreated
- stat path [watch]
[zk: localhost:2181(CONNECTED) 33] stat /czk watch Node does not exist: /itcast [zk: localhost:2181(CONNECTED) 35] create /czk 123 WATCHER::Created /czk WatchedEvent state:SyncConnected type:NodeCreated path:/czk
- get path [watch]
[zk: localhost:2181(CONNECTED) 36] get /czk watch 123 cZxid = 0x6d ctime = Sat Dec 29 19:43:13 CST 2018 mZxid = 0x6d mtime = Sat Dec 29 19:43:13 CST 2018 pZxid = 0x6d cversion = 0 dataVersion = 0 aclVersion = 0 ephemeralOwner = 0x0 dataLength = 3 numChildren = 0 [zk: localhost:2181(CONNECTED) 37] set /itcast 789 WATCHER:: WatchedEvent state:SyncConnected type:NodeDataChanged path:/czk cZxid = 0x6d ctime = Sat Dec 29 19:43:13 CST 2018 mZxid = 0x6e mtime = Sat Dec 29 19:47:24 CST 2018 pZxid = 0x6d cversion = 0 dataVersion = 1 aclVersion = 0 ephemeralOwner = 0x0 dataLength = 3 numChildren = 0
- ls/ls2 path [watch]
[zk: localhost:2181(CONNECTED) 38] ls /czk watch [] [zk: localhost:2181(CONNECTED) 39] delete /czk WATCHER:: WatchedEvent state:SyncConnected type:NodeDeleted path:/czk
-
ls为父节点设置watcher, 创建子节点触发:NodeChildrenChanged
-
ls为父节点设置watcher, 删除子节点触发:NodeChildrenChanged
-
ls为父节点设置watcher, 修改子节点不触发事件
[zk: localhost:2181(CONNECTED) 49] ls /czk watch [sec0000000003, dir1, sec0000000002] [zk: localhost:2181(CONNECTED) 51] create /czk/dir2 123 WATCHER::Created /czk/dir2 WatchedEvent state:SyncConnected type:NodeChildrenChanged path:/czk [zk: localhost:2181(CONNECTED) 52] ls /czk watch [sec0000000003, dir2, dir1, sec0000000002] [zk: localhost:2181(CONNECTED) 53] delete /czk/dir2 WATCHER:: WatchedEvent state:SyncConnected type:NodeChildrenChanged path:/czk [zk: localhost:2181(CONNECTED) 54]
莫见乎隐,莫显乎微,故君子慎其独也。