Zookeeper系列一:Zookeeper基礎命令操作

niocoder發表於2018-06-03

有些事不是努力就可以改變的,五十塊的人民幣設計的再好看,也沒有一百塊的招人喜歡。

https://raw.githubusercontent.com/longfeizheng/longfeizheng.github.io/master/images/spring-security-OAuth209.png

前言

由於公司年底要更換辦公地點,所以最近投了一下簡歷,發現面試官現在很喜歡問dubbozookeeper和高併發等。由於公司沒有使用dubbo,只知道dubbo是一個遠端服務呼叫的分散式框架,zookeeper為分散式應用程式協調服務。因此,本週查閱資料整理下zookeeper學習筆記。

安裝zookeeper

安裝參考連結https://blog.csdn.net/qiunian144084/article/details/79192819

基礎命令操作

啟動zk服務

  • ./zkServer.sh start
[root@localhost bin]# ./zkServer.sh
ZooKeeper JMX enabled by default
Using config: /usr/home/zookeeper-3.4.11/bin/../conf/zoo.cfg
Usage: ./zkServer.sh {start|start-foreground|stop|restart|status|upgrade|print-cmd}
# 提示要以./zkCli.sh start 啟動zk
./zkCli.sh start 

檢視zk的執行狀態

  • ./zkServer.sh status 由於我已經配置了zk的叢集,所以此處顯示狀態為leader
[root@localhost bin]# ./zkServer.sh status
ZooKeeper JMX enabled by default
Using config: /usr/home/zookeeper-3.4.11/bin/../conf/zoo.cfg
Mode: leader

客戶端連結zk

[root@localhost bin]# ./zkCli.sh 
......
WatchedEvent state:SyncConnected type:None path:null
[zk: localhost:2181(CONNECTED) 0]

help 檢視客戶端幫助命令

  • help
[zk: localhost:2181(CONNECTED) 0] 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
[zk: localhost:2181(CONNECTED) 1] 

ls 檢視

  • ls 檢視命令(niocoder是我測試叢集建立的節點,預設只有zookeeper一個節點)
[zk: localhost:2181(CONNECTED) 1] ls /
[niocoder, zookeeper]
[zk: localhost:2181(CONNECTED) 2] ls /zookeeper 
[quota]
[zk: localhost:2181(CONNECTED) 4] ls /zookeeper/quota
[]

get 獲取節點資料和更新資訊

  • get內容為空
  • cZxid :建立節點的id
  • ctime : 節點的建立時間
  • mZxid :修改節點的id
  • mtime :修改節點的時間
  • pZxid :子節點的id
  • cversion : 子節點的版本
  • dataVersion : 當前節點資料的版本
  • aclVersion :許可權的版本
  • ephemeralOwner :判斷是否是臨時節點
  • dataLength : 資料的長度
  • numChildren :子節點的數量
[zk: localhost:2181(CONNECTED) 7] get /zookeeper #下面空行說明節點內容為空

cZxid = 0x0
ctime = Thu Jan 01 00:00:00 UTC 1970
mZxid = 0x0
mtime = Thu Jan 01 00:00:00 UTC 1970
pZxid = 0x0
cversion = -1
dataVersion = 0
aclVersion = 0
ephemeralOwner = 0x0
dataLength = 0
numChildren = 1
[zk: localhost:2181(CONNECTED) 8]

stat 獲得節點的更新資訊

  • stat
[zk: localhost:2181(CONNECTED) 8] stat /zookeeper
cZxid = 0x0
ctime = Thu Jan 01 00:00:00 UTC 1970
mZxid = 0x0
mtime = Thu Jan 01 00:00:00 UTC 1970
pZxid = 0x0
cversion = -1
dataVersion = 0
aclVersion = 0
ephemeralOwner = 0x0
dataLength = 0
numChildren = 1

ls2 ls命令和stat命令的整合

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

create 建立節點

  • create [-s] [-e] path data acl 可以注意一下各個版本的變化
#建立merryyou節點,節點的內容為merryyou
[zk: localhost:2181(CONNECTED) 1] create /merryyou merryyou
Created /merryyou
#獲得merryyou節點內容
[zk: localhost:2181(CONNECTED) 3] get /merryyou
merryyou
cZxid = 0x200000004
ctime = Sat Jun 02 14:20:06 UTC 2018
mZxid = 0x200000004
mtime = Sat Jun 02 14:20:06 UTC 2018
pZxid = 0x200000004
cversion = 0
dataVersion = 0
aclVersion = 0
ephemeralOwner = 0x0
dataLength = 8
numChildren = 0

create -e 建立臨時節點

  • create -e
#建立臨時節點
[zk: localhost:2181(CONNECTED) 4] create -e  /merryyou/temp merryyou
Created /merryyou/temp
[zk: localhost:2181(CONNECTED) 5] get /merryyou
merryyou
cZxid = 0x200000004
ctime = Sat Jun 02 14:20:06 UTC 2018
mZxid = 0x200000004
mtime = Sat Jun 02 14:20:06 UTC 2018
pZxid = 0x200000005
cversion = 1
dataVersion = 0
aclVersion = 0
ephemeralOwner = 0x0
dataLength = 8
numChildren = 1
[zk: localhost:2181(CONNECTED) 6] get /merryyou/temp
merryyou
cZxid = 0x200000005
ctime = Sat Jun 02 14:22:24 UTC 2018
mZxid = 0x200000005
mtime = Sat Jun 02 14:22:24 UTC 2018
pZxid = 0x200000005
cversion = 0
dataVersion = 0
aclVersion = 0
ephemeralOwner = 0x2000000d4500000
dataLength = 8
numChildren = 0
[zk: localhost:2181(CONNECTED) 7] 
#斷開重連之後,臨時節點自動消失
WATCHER::

WatchedEvent state:SyncConnected type:None path:null
#因為預設的心跳機制,此時查詢臨時節點還存在
[zk: localhost:2181(CONNECTED) 0] ls /merryyou
[temp]
#再次查詢,臨時節點消失
[zk: localhost:2181(CONNECTED) 1] ls /merryyou
[]
[zk: localhost:2181(CONNECTED) 2] 

create -s 建立順序節點 自動累加

  • create -s
# 建立順序節點,順序節點會自動累加
[zk: localhost:2181(CONNECTED) 2] create -s /merryyou/sec seq
Created /merryyou/sec0000000001
[zk: localhost:2181(CONNECTED) 3] create -s /merryyou/sec seq
Created /merryyou/sec0000000002 

set path data [version] 修改節點

[zk: localhost:2181(CONNECTED) 6] get /merryyou
merryyou
cZxid = 0x200000004
ctime = Sat Jun 02 14:20:06 UTC 2018
mZxid = 0x200000004
mtime = Sat Jun 02 14:20:06 UTC 2018
pZxid = 0x200000009
cversion = 4
dataVersion = 0
aclVersion = 0
ephemeralOwner = 0x0
dataLength = 8
numChildren = 2
# 修改節點內容為new-merryyou
[zk: localhost:2181(CONNECTED) 7] set /merryyou new-merryyou
cZxid = 0x200000004
ctime = Sat Jun 02 14:20:06 UTC 2018
mZxid = 0x20000000a
mtime = Sat Jun 02 14:29:23 UTC 2018
pZxid = 0x200000009
cversion = 4
dataVersion = 1
aclVersion = 0
ephemeralOwner = 0x0
dataLength = 12
numChildren = 2
#再次查詢,節點內容已經修改
[zk: localhost:2181(CONNECTED) 8] get /merryyou
new-merryyou
cZxid = 0x200000004
ctime = Sat Jun 02 14:20:06 UTC 2018
mZxid = 0x20000000a
mtime = Sat Jun 02 14:29:23 UTC 2018
pZxid = 0x200000009
cversion = 4
dataVersion = 1
aclVersion = 0
ephemeralOwner = 0x0
dataLength = 12
numChildren = 2
#set 根據版本號更新 dataVersion 樂觀鎖
[zk: localhost:2181(CONNECTED) 9] set /merryyou test-merryyou 1
cZxid = 0x200000004
ctime = Sat Jun 02 14:20:06 UTC 2018
mZxid = 0x20000000b
mtime = Sat Jun 02 14:31:30 UTC 2018
pZxid = 0x200000009
cversion = 4
dataVersion = 2
aclVersion = 0
ephemeralOwner = 0x0
dataLength = 13
numChildren = 2
#因為資料的版本號已經修改為2 再次使用版本號1修改節點提交錯誤
[zk: localhost:2181(CONNECTED) 10] set /merryyou test-merryyou 1
version No is not valid : /merryyou

delete path [version] 刪除節點

[zk: localhost:2181(CONNECTED) 13] delete /merryyou/sec000000000

sec0000000001   sec0000000002
[zk: localhost:2181(CONNECTED) 13] delete /merryyou/sec0000000001
[zk: localhost:2181(CONNECTED) 14] ls /merryyou
[sec0000000002]
[zk: localhost:2181(CONNECTED) 15] 
#版本號操作與set類似 version

watcher通知機制

關於watcher機制大體的理解可以為,當每個節點發生變化,都會觸發watcher事件,類似於mysql的觸發器。zkwatcher是一次性的,觸發後立即銷燬。可以參考https://blog.csdn.net/hohoo1990/article/details/78617336
- stat path [watch] 設定watch事件
- get path [watch]設定watch事件
- 子節點建立和刪除時觸發watch事件,子節點修改不會觸發該事件

stat path [watch] 設定watch事件
#新增watch 事件
[zk: localhost:2181(CONNECTED) 18] stat /longfei watch
Node does not exist: /longfei
#建立longfei節點時觸發watcher事件
[zk: localhost:2181(CONNECTED) 19] create /longfei test

WATCHER::

WatchedEvent state:SyncConnected type:NodeCreated path:/longfei
Created /longfei
get path [watch] 設定watch事件
#使用get命令新增watch事件
[zk: localhost:2181(CONNECTED) 20] get /longfei watch
test
cZxid = 0x20000000e
ctime = Sat Jun 02 14:43:15 UTC 2018
mZxid = 0x20000000e
mtime = Sat Jun 02 14:43:15 UTC 2018
pZxid = 0x20000000e
cversion = 0
dataVersion = 0
aclVersion = 0
ephemeralOwner = 0x0
dataLength = 4
numChildren = 0
#修改節點觸發watcher事件
[zk: localhost:2181(CONNECTED) 21] set /longfei new_test

WATCHER::

WatchedEvent state:SyncConnected type:NodeDataChanged path:/longfei
cZxid = 0x20000000e
ctime = Sat Jun 02 14:43:15 UTC 2018
mZxid = 0x20000000f
mtime = Sat Jun 02 14:45:06 UTC 2018
pZxid = 0x20000000e
cversion = 0
dataVersion = 1
aclVersion = 0
ephemeralOwner = 0x0
dataLength = 8
numChildren = 0
[zk: localhost:2181(CONNECTED) 22] 
#刪除觸發watcher事件
[zk: localhost:2181(CONNECTED) 23] get /longfei watch
new_test
cZxid = 0x20000000e
ctime = Sat Jun 02 14:43:15 UTC 2018
mZxid = 0x20000000f
mtime = Sat Jun 02 14:45:06 UTC 2018
pZxid = 0x20000000e
cversion = 0
dataVersion = 1
aclVersion = 0
ephemeralOwner = 0x0
dataLength = 8
numChildren = 0
[zk: localhost:2181(CONNECTED) 24] delete /longfei

WATCHER::

WatchedEvent state:SyncConnected type:NodeDeleted path:/longfei
[zk: localhost:2181(CONNECTED) 25] 

ACL許可權控制

ZK的節點有5種操作許可權:CREATEREADWRITEDELETEADMIN 也就是 增、刪、改、查、管理許可權,這5種許可權簡寫為crwda(即:每個單詞的首字元縮寫)。
注:這5種許可權中,delete是指對子節點的刪除許可權,其它4種許可權指對自身節點的操作許可權

身份的認證有4種方式:
- world:預設方式,相當於全世界都能訪問
- auth:代表已經認證通過的使用者(cli中可以通過addauth digest user:pwd 來新增當前上下文中的授權使用者)
- digest:即使用者名稱:密碼這種方式認證,這也是業務系統中最常用的
- ip:使用Ip地址認證

使用[scheme:id:permissions]來表示acl許可權

getAcl:獲取某個節點的acl許可權資訊
#獲取節點許可權資訊預設為 world:cdrwa任何人都可以訪問
[zk: localhost:2181(CONNECTED) 34] getAcl /merryyou
'world,'anyone
: cdrwa
[zk: localhost:2181(CONNECTED) 35] 
setAcl 設定許可權
[zk: localhost:2181(CONNECTED) 35] create /merryyou/test test
Created /merryyou/test
[zk: localhost:2181(CONNECTED) 36] getAcl /merryyou/test
'world,'anyone
: cdrwa
#設定節點許可權 crwa 不允許刪除
[zk: localhost:2181(CONNECTED) 37] setAcl /merryyou/test world:anyone:crwa
cZxid = 0x200000018
ctime = Sat Jun 02 16:18:18 UTC 2018
mZxid = 0x200000018
mtime = Sat Jun 02 16:18:18 UTC 2018
pZxid = 0x200000018
cversion = 0
dataVersion = 0
aclVersion = 1
ephemeralOwner = 0x0
dataLength = 4
numChildren = 0
#查詢剛才設定的acl許可權資訊 crwa 沒有刪除許可權
[zk: localhost:2181(CONNECTED) 38] getAcl /merryyou/test
'world,'anyone
: crwa
[zk: localhost:2181(CONNECTED) 39] 
[zk: localhost:2181(CONNECTED) 39] create /merryyou/test/abc abc
Created /merryyou/test/abc
#刪除子節點的時候提交許可權不足
[zk: localhost:2181(CONNECTED) 40] delete /merryyou/test/abc
Authentication is not valid : /merryyou/test/abc
#設定節點的許可權資訊為rda
[zk: localhost:2181(CONNECTED) 41] setAcl /merryyou/test world:anyone:rda 
cZxid = 0x200000018
ctime = Sat Jun 02 16:18:18 UTC 2018
mZxid = 0x200000018
mtime = Sat Jun 02 16:18:18 UTC 2018
pZxid = 0x20000001a
cversion = 1
dataVersion = 0
aclVersion = 2
ephemeralOwner = 0x0
dataLength = 4
numChildren = 1
[zk: localhost:2181(CONNECTED) 42] getAcl /merryyou/test
'world,'anyone
: dra
#可以成功刪除
[zk: localhost:2181(CONNECTED) 43] delete /merryyou/test/abc             
[zk: localhost:2181(CONNECTED) 46] ls /merryyou/test
[]
[zk: localhost:2181(CONNECTED) 47] 
#設定節點資訊為a admin
[zk: localhost:2181(CONNECTED) 47] setAcl /merryyou/test world:anyone:a  
cZxid = 0x200000018
ctime = Sat Jun 02 16:18:18 UTC 2018
mZxid = 0x200000018
mtime = Sat Jun 02 16:18:18 UTC 2018
pZxid = 0x20000001d
cversion = 2
dataVersion = 0
aclVersion = 3
ephemeralOwner = 0x0
dataLength = 4
numChildren = 0
#獲取 設定都提示許可權不足
[zk: localhost:2181(CONNECTED) 49] get /merryyou/test
Authentication is not valid : /merryyou/test
[zk: localhost:2181(CONNECTED) 50] set /merryyou/test 123
Authentication is not valid : /merryyou/test
[zk: localhost:2181(CONNECTED) 51] 
acl Auth 密碼明文設定
[zk: localhost:2181(CONNECTED) 53] create /niocoder/merryyou merryyou
Created /niocoder/merryyou
#查詢預設節點許可權資訊
[zk: localhost:2181(CONNECTED) 54] getAcl /niocoder/merryyou
'world,'anyone
: cdrwa
[zk: localhost:2181(CONNECTED) 55] 
#使用auth設定節點許可權資訊
[zk: localhost:2181(CONNECTED) 2] setAcl /niocoder/merryyou auth:test:test:cdrwa  
Acl is not valid : /niocoder/merryyou
# 註冊test:test 賬號密碼
[zk: localhost:2181(CONNECTED) 3] addauth digest test:test
[zk: localhost:2181(CONNECTED) 4] setAcl /niocoder/merryyou auth:test:test:cdrwa
cZxid = 0x200000020
ctime = Sat Jun 02 16:32:08 UTC 2018
mZxid = 0x200000020
mtime = Sat Jun 02 16:32:08 UTC 2018
pZxid = 0x200000020
cversion = 0
dataVersion = 0
aclVersion = 1
ephemeralOwner = 0x0
dataLength = 8
numChildren = 0
#查詢節點許可權資訊 密碼為密文格式
[zk: localhost:2181(CONNECTED) 5] getAcl /niocoder/merryyou
'digest,'test:V28q/NynI4JI3Rk54h0r8O5kMug=
: cdrwa
[zk: localhost:2181(CONNECTED) 6] 
acl digest 密碼密文設定
[zk: localhost:2181(CONNECTED) 13] create /names test
Created /names
[zk: localhost:2181(CONNECTED) 14] getAcl /names
'world,'anyone
: cdrwa
#使用digest設定節點的許可權資訊 密碼為test密文
[zk: localhost:2181(CONNECTED) 15] setAcl /names digest:test:V28q/NynI4JI3Rk54h0r8O5kMug=:cdra
cZxid = 0x400000006
ctime = Sun Jun 03 01:01:17 UTC 2018
mZxid = 0x400000006
mtime = Sun Jun 03 01:01:17 UTC 2018
pZxid = 0x400000006
cversion = 0
dataVersion = 0
aclVersion = 1
ephemeralOwner = 0x0
dataLength = 4
numChildren = 0
#查詢節點許可權資訊
[zk: localhost:2181(CONNECTED) 16] getAcl /names
'digest,'test:V28q/NynI4JI3Rk54h0r8O5kMug=
: cdra
#獲取節點資訊提示許可權不足
[zk: localhost:2181(CONNECTED) 5] get /names
Authentication is not valid : /names
# 註冊賬戶
[zk: localhost:2181(CONNECTED) 4] addauth digest test:test
# 可以正常獲取
[zk: localhost:2181(CONNECTED) 17] get /names          
test
cZxid = 0x400000006
ctime = Sun Jun 03 01:01:17 UTC 2018
mZxid = 0x400000006
mtime = Sun Jun 03 01:01:17 UTC 2018
pZxid = 0x400000006
cversion = 0
dataVersion = 0
aclVersion = 1
ephemeralOwner = 0x0
dataLength = 4
numChildren = 0
#由於沒有設定寫許可權不能修改節點 w
[zk: localhost:2181(CONNECTED) 18] set /names 111
Authentication is not valid : /names
[zk: localhost:2181(CONNECTED) 19] delete /names
[zk: localhost:2181(CONNECTED) 20] 
acl ip 控制客戶端
[zk: localhost:2181(CONNECTED) 22] create /niocoder/ip aa
Created /niocoder/ip
[zk: localhost:2181(CONNECTED) 23] get /niocoder/ip
aa
cZxid = 0x40000000a
ctime = Sun Jun 03 01:06:47 UTC 2018
mZxid = 0x40000000a
mtime = Sun Jun 03 01:06:47 UTC 2018
pZxid = 0x40000000a
cversion = 0
dataVersion = 0
aclVersion = 0
ephemeralOwner = 0x0
dataLength = 2
numChildren = 0
# 新增ip控制的許可權資訊
[zk: localhost:2181(CONNECTED) 24] setAcl /niocoder/ip ip:192.168.0.68:cdrwa
cZxid = 0x40000000a
ctime = Sun Jun 03 01:06:47 UTC 2018
mZxid = 0x40000000a
mtime = Sun Jun 03 01:06:47 UTC 2018
pZxid = 0x40000000a
cversion = 0
dataVersion = 0
aclVersion = 1
ephemeralOwner = 0x0
dataLength = 2
numChildren = 0
[zk: localhost:2181(CONNECTED) 25] getAcl /niocoder/ip
'ip,'192.168.0.68
: cdrwa
[zk: localhost:2181(CONNECTED) 26] 
acl super超級管理員

使用super許可權需要修改zkServer.sh,新增super管理員,重啟zkServer.sh

"-Dzookeeper.DigestAuthenticationProvider.superDigest=test:V28q/NynI4JI3Rk54h0r8O5kMug="
 nohup "$JAVA" "-Dzookeeper.log.dir=${ZOO_LOG_DIR}" "-Dzookeeper.root.logger=${ZOO_LOG4J_PROP}" "-Dzookeeper.DigestAuthenticationprovider.superDigest=test:V28q/NynI4JI3Rk54h0r8O5kMug=" \
    -cp "$CLASSPATH" $JVMFLAGS $ZOOMAIN "$ZOOCFG" > "$_ZOO_DAEMON_OUT" 2>&1 < /dev/null &

#重啟進入zkCli
#由於之前設定ip許可權,所以不允許訪問
[zk: localhost:2181(CONNECTED) 2] ls /niocoder/ip
Authentication is not valid : /niocoder/ip
#登入賬號資訊,即為管理員賬號
[zk: localhost:2181(CONNECTED) 3] addauth digest test:test
#正常訪問,節點內容為空
[zk: localhost:2181(CONNECTED) 4] ls /niocoder/ip
[]
[zk: localhost:2181(CONNECTED) 5] get /niocoder/ip
aa
cZxid = 0x40000000a
ctime = Sun Jun 03 01:06:47 UTC 2018
mZxid = 0x40000000a
mtime = Sun Jun 03 01:06:47 UTC 2018
pZxid = 0x40000000a
cversion = 0
dataVersion = 0
aclVersion = 1
ephemeralOwner = 0x0
dataLength = 2
numChildren = 0
[zk: localhost:2181(CONNECTED) 6] 

四字命令Four Letter Words

  • 使用四字命令需要安裝nc命令,(yum install nc)
stat 檢視狀態資訊
[root@localhost bin]# echo stat | nc 192.168.0.68 2181
Zookeeper version: 3.4.11-37e277162d567b55a07d1755f0b31c32e93c01a0, built on 11/01/2017 18:06 GMT
Clients:
 /192.168.0.68:49346[0](queued=0,recved=1,sent=0)

Latency min/avg/max: 0/0/4
Received: 62
Sent: 61
Connections: 1
Outstanding: 0
Zxid: 0x50000000a
Mode: follower
Node count: 10
[root@localhost bin]# 
ruok 檢視zookeeper是否啟動
[root@localhost bin]# echo ruok | nc 192.168.0.68 2181
imok[root@localhost bin]# 
dump 列出沒有處理的節點,臨時節點
imok[root@localhost bin]# echo dump | nc 192.168.0.68 2181
SessionTracker dump:
org.apache.zookeeper.server.quorum.LearnerSessionTracker@29805957
ephemeral nodes dump:
Sessions with Ephemerals (0):
[root@localhost bin]# 
conf 檢視伺服器配置
[root@localhost bin]# echo conf | nc 192.168.0.68 2181
clientPort=2181
dataDir=/usr/home/zookeeper-3.4.11/data/version-2
dataLogDir=/usr/home/zookeeper-3.4.11/data/version-2
tickTime=2000
maxClientCnxns=60
minSessionTimeout=4000
maxSessionTimeout=40000
serverId=2
initLimit=10
syncLimit=5
electionAlg=3
electionPort=3888
quorumPort=2888
peerType=0
[root@localhost bin]# 
cons 顯示連線到服務端的資訊
[root@localhost bin]# echo cons | nc 192.168.0.68 2181
 /192.168.0.68:49354[0](queued=0,recved=1,sent=0)

[root@localhost bin]# 
envi 顯示環境變數資訊
[root@localhost bin]# echo envi | nc 192.168.0.68 2181
Environment:
zookeeper.version=3.4.11-37e277162d567b55a07d1755f0b31c32e93c01a0, built on 11/01/2017 18:06 GMT
host.name=localhost
java.version=1.8.0_111
java.vendor=Oracle Corporation
java.home=/usr/local/jdk1.8.0_111/jre
java.class.path=/usr/home/zookeeper-3.4.11/bin/../build/classes:/usr/home/zookeeper-3.4.11/bin/../build/lib/*.jar:/usr/home/zookeeper-3.4.11/bin/../lib/slf4j-log4j12-1.6.1.jar:/usr/home/zookeeper-3.4.11/bin/../lib/slf4j-api-1.6.1.jar:/usr/home/zookeeper-3.4.11/bin/../lib/netty-3.10.5.Final.jar:/usr/home/zookeeper-3.4.11/bin/../lib/log4j-1.2.16.jar:/usr/home/zookeeper-3.4.11/bin/../lib/jline-0.9.94.jar:/usr/home/zookeeper-3.4.11/bin/../lib/audience-annotations-0.5.0.jar:/usr/home/zookeeper-3.4.11/bin/../zookeeper-3.4.11.jar:/usr/home/zookeeper-3.4.11/bin/../src/java/lib/*.jar:/usr/home/zookeeper-3.4.11/bin/../conf:
java.library.path=/usr/java/packages/lib/amd64:/usr/lib64:/lib64:/lib:/usr/lib
java.io.tmpdir=/tmp
java.compiler=<NA>
os.name=Linux
os.arch=amd64
os.version=3.10.0-514.10.2.el7.x86_64
user.name=root
user.home=/root
user.dir=/usr/home/zookeeper-3.4.11/bin
[root@localhost bin]#
mntr 檢視zk的健康資訊
[root@localhost bin]# echo mntr | nc 192.168.0.68 2181
zk_version  3.4.11-37e277162d567b55a07d1755f0b31c32e93c01a0, built on 11/01/2017 18:06 GMT
zk_avg_latency  0
zk_max_latency  4
zk_min_latency  0
zk_packets_received 68
zk_packets_sent 67
zk_num_alive_connections    1
zk_outstanding_requests 0
zk_server_state follower
zk_znode_count  10
zk_watch_count  0
zk_ephemerals_count 0
zk_approximate_data_size    124
zk_open_file_descriptor_count   32
zk_max_file_descriptor_count    4096
[root@localhost bin]# 
wchs 展示watch的資訊
[root@localhost bin]# echo wchs | nc 192.168.0.68 2181
0 connections watching 0 paths
Total watches:0
[root@localhost bin]# 
wchc和wchp 顯示session的watch資訊 path的watch資訊
  • 需要在 配置zoo.cfg檔案中新增 4lw.commands.whitelist=*
[root@localhost bin]# echo wchc | nc 192.168.0.68 2181
wchc is not executed because it is not in the whitelist.
[root@localhost bin]# echo wchp | nc 192.168.0.68 2181
wchp is not executed because it is not in the whitelist.

總結

熟悉zookeeper基礎命令操作後,再使用zookeeper提供的api或者第三方封裝的api,會感覺一目瞭然。

推薦文章

  1. Java建立區塊鏈系列
  2. Spring Security原始碼分析系列
  3. Spring Data Jpa 系列
  4. 【譯】資料結構中關於樹的一切(java版)
  5. SpringBoot+Docker+Git+Jenkins實現簡易的持續整合和持續部署

https://i.iter01.com/images/23eaca192ea408ccff66ee52b92329eac80763129435fd845dc931fdde7a4362.png

������關注微信小程式java架構師歷程
上下班的路上無聊嗎?還在看小說、新聞嗎?不知道怎樣提高自己的技術嗎?來吧這裡有你需要的java架構文章,1.5w+的java工程師都在看,你還在等什麼?

相關文章