ZooKeeper 初體驗

安裝Zookeeper

Mac OSnode

Mac 用戶能夠使用 Homebrew 安裝和管理 Zookeeper 服務:docker

brew install zookeeper

配置文件地址在: /usr/local/etc/zookeeper。bash

啓動 zookeeper 服務:工具

brew services start zookeeper

進入命令行客戶端zkCli:ui

zkCli

默認鏈接localhost:2181, 手動指定服務地址:命令行

zkCli -server localhost:2181

Dockercode

能夠使用官方提供的Docker鏡像快速啓動Zookeeper。server

啓動服務端:遞歸

docker run --name my_zookeeper -d zookeeper

ZNode 操做

使用終端工具ZkCli鏈接:get

docker run -it --rm --link my_zookeeper:zookeeper zookeeper zkCli.sh -server zookeeper

命令行客戶端 zkCli 能夠交互式操做 Zookeeper, 其命令風格相似於 Unix 終端。

ls

查看某個路徑包含的全部節點:

[zk: localhost:2181(CONNECTED) 1] ls /
[cluster, zookeeper, admin, config]
[zk: localhost:2181(CONNECTED) 2] ls /zookeeper
[quota]

ls2

查看某個路徑包含的全部節點,以及節點元數據:

[zk: localhost:2181(CONNECTED) 7] ls2 /zookeeper
[quota]
cZxid = 0x0
ctime = Thu Jan 01 08:00:00 CST 1970
mZxid = 0x0
mtime = Thu Jan 01 08:00:00 CST 1970
pZxid = 0x0
cversion = -1
dataVersion = 0
aclVersion = 0
ephemeralOwner = 0x0
dataLength = 0
numChildren = 1

create

建立節點:

[zk: localhost:2181(CONNECTED) 0] create /test 1
Created /test

/test爲建立節點的路徑,1爲Znode的數據data

create 命令沒法遞歸建立節點,即/test節點不存在時不能直接建立/test/t1

使用-e 選項建立臨時節點:

[zk: localhost:2181(CONNECTED) 0] create -e /test/t2 t2
Created /test/t2
[zk: localhost:2181(CONNECTED) 1] get /test/t2
t2
[zk: localhost:2181(CONNECTED) 2] quit
Quitting...
[zk: localhost:2181(CONNECTED) 0] get /test/t2
Node does not exist: /test/t2

退出zkCli後從新進入,臨時節點已經消失。

get

獲取節點數據與元數據:

[zk: localhost:2181(CONNECTED) 8] get /test
1
cZxid = 0x11d28
ctime = Sat Sep 01 16:04:08 CST 2018
mZxid = 0x11d28
mtime = Sat Sep 01 16:04:08 CST 2018
pZxid = 0x11d28
cversion = 0
dataVersion = 0
aclVersion = 0
ephemeralOwner = 0x0
dataLength = 1
numChildren = 0

set

更改節點數據:

[zk: localhost:2181(CONNECTED) 9] set /test 2
cZxid = 0x11d28
ctime = Sat Sep 01 16:04:08 CST 2018
mZxid = 0x11d2a
mtime = Sat Sep 01 16:25:46 CST 2018
pZxid = 0x11d28
cversion = 0
dataVersion = 1
aclVersion = 0
ephemeralOwner = 0x0
dataLength = 1
numChildren = 0

能夠注意到版本號發生了變化。

delete

刪除節點:

[zk: localhost:2181(CONNECTED) 2] delete /test

只能刪除沒有子節點的Znode,若要將子節點一同刪除需使用rmr命令。

相關文章
相關標籤/搜索