四 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 /itheima/tmp itheima-data Created /itheima/tmp [zk: localhost:2181(CONNECTED) 13] get /itheima 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 /itheima [] [zk: localhost:2181(CONNECTED) 2] create -s /itheima/sec seq Created /itheima/sec0000000001 [zk: localhost:2181(CONNECTED) 3] create -s /itheima/sec seq Created /itheima/sec0000000002 [zk: localhost:2181(CONNECTED) 4] ls /itheima [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 /itheima itheima-data3 1 version No is not valid : /itheima #版本不匹配 修改失敗 當前版本2 [zk: localhost:2181(CONNECTED) 23] set /itheima itheima-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 /itheima 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 /itheima [sec0000000003, sec0000000001, sec0000000002] [zk: localhost:2181(CONNECTED) 27] delete /itheima/sec000000000 sec0000000003 sec0000000001 sec0000000002 [zk: localhost:2181(CONNECTED) 27] delete /itheima/sec0000000001 [zk: localhost:2181(CONNECTED) 28] ls /itheima [sec0000000003, sec0000000002] [zk: localhost:2181(CONNECTED) 29] set /itheima/sec000000000 sec0000000003 sec0000000002 [zk: localhost:2181(CONNECTED) 29] set /itheima/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 /itheima/sec000000000 sec0000000003 sec0000000002 [zk: localhost:2181(CONNECTED) 30] delete /itheima/sec0000000002 0 version No is not valid : /itheima/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 /itcast watch Node does not exist: /itcast [zk: localhost:2181(CONNECTED) 35] create /itcast 123 WATCHER::Created /itcast WatchedEvent state:SyncConnected type:NodeCreated path:/itcast
- get path [watch]
[zk: localhost:2181(CONNECTED) 36] get /itcast 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:/itcast 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 /itcast watch [] [zk: localhost:2181(CONNECTED) 39] delete /itcast WATCHER:: WatchedEvent state:SyncConnected type:NodeDeleted path:/itcast
ls為父節點設定watcher, 建立子節點觸發:NodeChildrenChanged
ls為父節點設定watcher, 刪除子節點觸發:NodeChildrenChanged
ls為父節點設定watcher, 修改子節點不觸發事件
[zk: localhost:2181(CONNECTED) 49] ls /itheima watch [sec0000000003, dir1, sec0000000002] [zk: localhost:2181(CONNECTED) 51] create /itheima/dir2 123 WATCHER::Created /itheima/dir2 WatchedEvent state:SyncConnected type:NodeChildrenChanged path:/itheima [zk: localhost:2181(CONNECTED) 52] ls /itheima watch [sec0000000003, dir2, dir1, sec0000000002] [zk: localhost:2181(CONNECTED) 53] delete /itheima/dir2 WATCHER:: WatchedEvent state:SyncConnected type:NodeChildrenChanged path:/itheima [zk: localhost:2181(CONNECTED) 54]
watcher 使用場景