https://zookeeper.apache.org/doc/r3.8.0/zookeeperCLI.html
一、服務(wù)端zookeeper的shell命令都體現(xiàn)在apache-zookeeper-3.8.0-bin/bin/
目錄下:
? bin ls
README.txt zkEnv.sh zkSnapShotToolkit.sh
zkCleanup.sh zkServer-initialize.sh zkSnapshotComparer.cmd
zkCli.cmd zkServer.cmd zkSnapshotComparer.sh
zkCli.sh zkServer.sh zkTxnLogToolkit.cmd
zkEnv.cmd zkSnapShotToolkit.cmd zkTxnLogToolkit.sh
其中包括:
二、客戶端 1、連接客戶端1>啟動zookeeper服務(wù):
./zkServer.sh start
2>查看zookeeper服務(wù)狀態(tài):
./zkServer.sh status
3>停止zookeeper服務(wù):
./zkServer.sh stop
4>重啟zookeeper服務(wù):
./zkServer.sh restart
直接使用sh zkCli.sh
命令可以連接到本地的zookeeper服務(wù)(127.0.0.1:2181);如果需要連接到其他地址的zookeeper服務(wù),需要使用以下命令格式:
sh zkCli.sh -server [ip]:[port]
示例命令:
sh zkCli.sh -server 127.0.0.1:2181
連接成功后,系統(tǒng)會輸出 ZooKeeper 的相關(guān)環(huán)境以及配置信息。 可以在開啟的會話中做一些客戶端操作 與 zookeeper服務(wù)進行交互;
2、help使用 help命令來查看幫助
[zk: 127.0.0.1:2181(CONNECTED) 21] help
ZooKeeper -server host:port -client-configuration properties-file cmd args
addWatch [-m mode] path # optional mode is one of [PERSISTENT, PERSISTENT_RECURSIVE] - default is PERSISTENT_RECURSIVE
addauth scheme auth
close
config [-c] [-w] [-s]
connect host:port
create [-s] [-e] [-c] [-t ttl] path [data] [acl]
delete [-v version] path
deleteall path [-b batch size]
delquota [-n|-b|-N|-B] path
get [-s] [-w] path
getAcl [-s] path
getAllChildrenNumber path
getEphemerals path
history
listquota path
ls [-s] [-w] [-R] path
printwatches on|off
quit
reconfig [-s] [-v version] [[-file path] | [-members serverID=host:port1:port2;port3[,...]*]] | [-add serverId=host:port1:port2;port3[,...]]* [-remove serverId[,...]*]
redo cmdno
removewatches path [-c|-d|-a] [-l]
set [-s] [-v version] path data
setAcl [-s] [-v version] [-R] path acl
setquota -n|-b|-N|-B val path
stat [-w] path
sync path
version
whoami
Command not found: Command not found help
3、createcreate命令:創(chuàng)建節(jié)點,默認(rèn)持久節(jié)點
create [-s] [-e] [-c] [-t ttl] path [data] [acl]
Zookeeper原生命令不支持級聯(lián)創(chuàng)建節(jié)點樹目錄,需要一級一級的創(chuàng)建;curator客戶端封裝了級聯(lián)創(chuàng)建的API。
1>創(chuàng)建持久節(jié)點Znode節(jié)點類型有四種:
- 持久的(persistent)
- 臨時的(ephemeral)
- 持久有序的(persistent_sequential)
- 臨時有序的(ephemeral_sequential)
[zk: 127.0.0.1:2181(CONNECTED) 0] create /node_p1
Created /node_p1
2>創(chuàng)建臨時節(jié)點[zk: 127.0.0.1:2181(CONNECTED) 1] create -e /node_e1 "node value"
Created /node_e1
3>創(chuàng)建持久有序節(jié)點持久有序節(jié)點:設(shè)置的節(jié)點路徑會被重命名為序列數(shù),不設(shè)置節(jié)點前綴時,序列數(shù)從0000000000
開始遞增;
不設(shè)置節(jié)點前綴時:
[zk: 127.0.0.1:2181(CONNECTED) 5] create /node_p2
Created /node_p2
[zk: 127.0.0.1:2181(CONNECTED) 6] create -s /node_p2/
Created /node_p2/0000000000
[zk: 127.0.0.1:2181(CONNECTED) 7] create -s /node_p2/
Created /node_p2/0000000001
設(shè)置節(jié)點前綴時:
[zk: 127.0.0.1:2181(CONNECTED) 12] create -s /node_p2/seq_
Created /node_p2/seq_0000000002
[zk: 127.0.0.1:2181(CONNECTED) 13] create -s /node_p2/seq_
Created /node_p2/seq_0000000003
4>創(chuàng)建臨時有序節(jié)點設(shè)置節(jié)點前綴時:
[zk: 127.0.0.1:2181(CONNECTED) 14] create -s -e /node_p2/e_
Created /node_p2/e_0000000004
[zk: 127.0.0.1:2181(CONNECTED) 15] create -s -e /node_p2/e_
Created /node_p2/e_0000000005
不設(shè)置節(jié)點前綴時:
[zk: 127.0.0.1:2181(CONNECTED) 16] create -s -e /node_p2/
Created /node_p2/0000000006
[zk: 127.0.0.1:2181(CONNECTED) 17] create -s -e /node_p2/
Created /node_p2/0000000007
5>創(chuàng)建ttl節(jié)點默認(rèn)禁用,需要在 zoo.cfg中添加 extendedTypesEnabled=true 開啟。 否者報如下錯:
[zk: 127.0.0.1:2181(CONNECTED) 18] create -t 10 /node_ttl "ttl node value"
KeeperErrorCode = Unimplemented for /node_ttl
開啟ttl之后:
[zk: 127.0.0.1:2181(CONNECTED) 8] create -t 10 /node_ttl "ttl node value"
Created /node_ttl
[zk: 127.0.0.1:2181(CONNECTED) 9] get /node_ttl
ttl node value
[zk: 127.0.0.1:2181(CONNECTED) 10] get /node_ttl
Node does not exist: /node_ttl
注意:ttl不能用于臨時節(jié)點 。
6>創(chuàng)建容器節(jié)點[zk: 127.0.0.1:2181(CONNECTED) 0] create -c /container_node mydata
Created /container_node
[zk: 127.0.0.1:2181(CONNECTED) 1] create -c /container_node/child_1 mydata
Created /container_node/child_1
[zk: 127.0.0.1:2181(CONNECTED) 2] create -c /container_node/child_2 mydata
Created /container_node/child_2
[zk: 127.0.0.1:2181(CONNECTED) 3] delete /container_node/child_1
[zk: 127.0.0.1:2181(CONNECTED) 4] delete /container_node/child_2
[zk: 127.0.0.1:2181(CONNECTED) 5] get /container_node
mydata
[zk: 127.0.0.1:2181(CONNECTED) 6] get /container_node
mydata
[zk: 127.0.0.1:2181(CONNECTED) 7] get /container_node
mydata
[zk: 127.0.0.1:2181(CONNECTED) 8] get /container_node
Node does not exist: /container_node
從測試結(jié)果來看,容器節(jié)點的最后一個子級被刪除之后,過了幾秒,容器節(jié)點才被刪除。
4、getget命令:獲取節(jié)點/文件內(nèi)容
get [-s] [-w] path
[zk: 127.0.0.1:2181(CONNECTED) 12] create /node_test "testNode value"
Created /node_test
[zk: 127.0.0.1:2181(CONNECTED) 13] get /node_test
testNode value
-s 選項在獲取到節(jié)點值的同時,會帶出節(jié)點的狀態(tài)stat:
[zk: 127.0.0.1:2181(CONNECTED) 14] get -s /node_test
testNode value
cZxid = 0x14 # 數(shù)據(jù)節(jié)點創(chuàng)建時的事務(wù)ID
ctime = Wed Nov 30 15:09:39 CST 2022 # 數(shù)據(jù)節(jié)點創(chuàng)建時的時間
mZxid = 0x18 # 數(shù)據(jù)節(jié)點最后一次更新時的事務(wù)ID
mtime = Wed Nov 30 15:14:25 CST 2022 # 數(shù)據(jù)節(jié)點最后一次更新時的時間
pZxid = 0x14 # 數(shù)據(jù)節(jié)點的子節(jié)點最后一次被修改時的事務(wù)ID
cversion = 0 # 子節(jié)點的更改次數(shù)
dataVersion = 3 # 節(jié)點數(shù)據(jù)的更改次數(shù)
aclVersion = 0 # 節(jié)點的ACL的更改次數(shù)
ephemeralOwner = 0x0 # 如果節(jié)點是臨時節(jié)點,則表示創(chuàng)建該節(jié)點的會話的SessionID,如果節(jié)點是持久節(jié)點,則該屬性值為0.
dataLength = 24 # 數(shù)據(jù)內(nèi)容的長度
numChildren = 0 # 數(shù)據(jù)節(jié)點當(dāng)前的子節(jié)點個數(shù)
5、setset命令:修改節(jié)點內(nèi)容;每次修改成功后,版本號會自動+1,初始為0
set [-s] [-v version] path data
[zk: 127.0.0.1:2181(CONNECTED) 15] set /node_test "updated tesetNode value"
-s選項,更新節(jié)點數(shù)據(jù)并顯示節(jié)點狀態(tài)信息
[zk: 127.0.0.1:2181(CONNECTED) 16] set -s /node_test "updated tesetNode value2"
cZxid = 0x14
ctime = Wed Nov 30 15:09:39 CST 2022
mZxid = 0x16
mtime = Wed Nov 30 15:13:40 CST 2022
pZxid = 0x14
cversion = 0
dataVersion = 2
aclVersion = 0
ephemeralOwner = 0x0
dataLength = 24
numChildren = 0
-v選項,指定數(shù)據(jù)版本號,如果指定的數(shù)據(jù)版本號和數(shù)據(jù)當(dāng)前版本號不一致,則更新失?。梢岳斫鉃闃酚^鎖方式更新);
[zk: 127.0.0.1:2181(CONNECTED) 17] set -v 3 /node_test "updated tesetNode value3"
version No is not valid : /node_test
[zk: 127.0.0.1:2181(CONNECTED) 18] set -s -v 2 /node_test "updated tesetNode value3"
cZxid = 0x14
ctime = Wed Nov 30 15:09:39 CST 2022
mZxid = 0x18
mtime = Wed Nov 30 15:14:25 CST 2022
pZxid = 0x14
cversion = 0
dataVersion = 3
aclVersion = 0
ephemeralOwner = 0x0
dataLength = 24
numChildren = 0
ls命令:查看目錄下的節(jié)點信息
ls [-s] [-w] [-R] path
[zk: 127.0.0.1:2181(CONNECTED) 19] create /node_test/sub_node1 "1"
Created /node_test/sub_node1
[zk: 127.0.0.1:2181(CONNECTED) 20] create /node_test/sub_node2 "2"
Created /node_test/sub_node2
[zk: 127.0.0.1:2181(CONNECTED) 21] ls /node_test
[sub_node1, sub_node2]
[zk: 127.0.0.1:2181(CONNECTED) 22] ls -s /node_test
[sub_node1, sub_node2]
cZxid = 0x14
ctime = Wed Nov 30 15:09:39 CST 2022
mZxid = 0x18
mtime = Wed Nov 30 15:14:25 CST 2022
pZxid = 0x1a
cversion = 2
dataVersion = 3
aclVersion = 0
ephemeralOwner = 0x0
dataLength = 24
numChildren = 2
[zk: 127.0.0.1:2181(CONNECTED) 23] ls -R /node_test
/node_test
/node_test/sub_node1
/node_test/sub_node2
[zk: 127.0.0.1:2181(CONNECTED) 24]
7、statstat命令:查看節(jié)點狀態(tài)。
stat [-w] path
[zk: 127.0.0.1:2181(CONNECTED) 24] stat /node_test
cZxid = 0x14
ctime = Wed Nov 30 15:09:39 CST 2022
mZxid = 0x18
mtime = Wed Nov 30 15:14:25 CST 2022
pZxid = 0x1a
cversion = 2
dataVersion = 3
aclVersion = 0
ephemeralOwner = 0x0
dataLength = 24
numChildren = 2
8、刪除節(jié)點delete命令只能刪除沒有子節(jié)點的節(jié)點;deleteall命令可以級聯(lián)刪除當(dāng)前節(jié)點和子節(jié)點;
1>deletedelete命令:只能刪除沒有子節(jié)點的節(jié)點。如果其有子節(jié)點時,無法刪除
delete [-v version] path
[zk: 127.0.0.1:2181(CONNECTED) 25] delete /node_test/sub_node1
[zk: 127.0.0.1:2181(CONNECTED) 26] ls -R /node_test
/node_test
/node_test/sub_node2
2>deletealldeleteall命令:級聯(lián)刪除該節(jié)點和子節(jié)點。
deleteall path
[zk: 127.0.0.1:2181(CONNECTED) 27] deleteall /node_test
[zk: 127.0.0.1:2181(CONNECTED) 28] ls -R /node_test
Node does not exist: /node_test
[zk: 127.0.0.1:2181(CONNECTED) 29] ls /node_test/sub_node2
Node does not exist: /node_test/sub_node2
8、其他命令Watcher(事件監(jiān)聽器)允許用戶在指定節(jié)點上注冊 Watcher,在特定事件發(fā)生時,Watcher會被觸發(fā);Zookeeper服務(wù)端將事件通知到響應(yīng)的客戶端上去;
該機制是Zookeeper實現(xiàn)分布式協(xié)調(diào)服務(wù)的重要特性。
監(jiān)聽節(jié)點的子節(jié)點變化,當(dāng)子節(jié)點發(fā)生改變時觸發(fā);
ls -w path
Session1:
# 創(chuàng)建節(jié)點
[zk: 127.0.0.1:2181(CONNECTED) 31] create /watch_node
Created /watch_node
# 監(jiān)聽節(jié)點目錄變化
[zk: 127.0.0.1:2181(CONNECTED) 32] ls -w /watch_node
[]
Session2:
# 創(chuàng)建/watch_node節(jié)點的子節(jié)點
[zk: 127.0.0.1:2181(CONNECTED) 0] create /watch_node/node1 "node1 value"
Created /watch_node/node1
[zk: 127.0.0.1:2181(CONNECTED) 1] create /watch_node/node2 "node2 value"
Created /watch_node/node2
當(dāng)/watch_node節(jié)點的子節(jié)點第一次發(fā)生變化時,會觸發(fā)session1的Watcher,觸發(fā)后會被刪除,無法再次觸發(fā)。
[zk: 127.0.0.1:2181(CONNECTED) 33]
WATCHER::
WatchedEvent state:SyncConnected type:NodeChildrenChanged path:/watch_node
圖示:
2>監(jiān)聽節(jié)點數(shù)據(jù)變化當(dāng)該節(jié)點數(shù)據(jù)發(fā)生改變時觸發(fā),但子節(jié)點的數(shù)據(jù)變化不會觸發(fā)。一次性監(jiān)聽,觸發(fā)后會被刪除,無法再次觸發(fā)。
get -w path
Session1:
[zk: 127.0.0.1:2181(CONNECTED) 33] get -w /watch_node
null
Session2:
[zk: 127.0.0.1:2181(CONNECTED) 3] set /watch_node "hahaha"
當(dāng)/watch_node節(jié)點的數(shù)據(jù)第一次發(fā)生變化時,會觸發(fā)session1的Watcher,觸發(fā)后會被刪除,無法再次觸發(fā)。
[zk: 127.0.0.1:2181(CONNECTED) 34]
WATCHER::
WatchedEvent state:SyncConnected type:NodeDataChanged path:/watch_node
圖示:
2、永久的監(jiān)聽器在 Zookeeper 3.6.0版本之后,客戶端可以在節(jié)點上創(chuàng)建永久監(jiān)聽,永久監(jiān)聽在被觸發(fā)后不會被刪除。
addWatch [-m mode] path
永久監(jiān)聽器在創(chuàng)建時可以通過 -m 選項指定模式,有兩種模式:
PERSISTENT:該節(jié)點的數(shù)據(jù)變化以及子節(jié)點的變化會觸發(fā)相應(yīng)事件,子節(jié)點的數(shù)據(jù)變化不會觸發(fā)。
# 1、增加PERSISTENT模式的監(jiān)聽器
[zk: 127.0.0.1:2181(CONNECTED) 34] addWatch -m PERSISTENT /watch_node
# 2、節(jié)點數(shù)據(jù)發(fā)生變更時,觸發(fā)監(jiān)聽器
[zk: 127.0.0.1:2181(CONNECTED) 35] set /watch_node "node12321"
WATCHER::
WatchedEvent state:SyncConnected type:NodeDataChanged path:/watch_node
# 3、節(jié)點的子節(jié)點新增時,觸發(fā)監(jiān)聽器
[zk: 127.0.0.1:2181(CONNECTED) 36] create /watch_node/node4 "node4"
WATCHER::Created /watch_node/node4
WatchedEvent state:SyncConnected type:NodeChildrenChanged path:/watch_node
# 4、節(jié)點的子節(jié)點數(shù)據(jù)發(fā)生變化時,不會觸發(fā)監(jiān)聽器
[zk: 127.0.0.1:2181(CONNECTED) 37] set /watch_node/node4 "node4-2"
# 5、節(jié)點的子節(jié)點刪除時,觸發(fā)監(jiān)聽器
[zk: 127.0.0.1:2181(CONNECTED) 38] delete /watch_node/node4
WATCHER::
WatchedEvent state:SyncConnected type:NodeChildrenChanged path:/watch_node
2>PERSISTENT_RECURSIVEPERSISTENT_RECURSIVE:節(jié)點的數(shù)據(jù)變化以及所有子孫節(jié)點的目錄或者數(shù)據(jù)變化都會觸發(fā)相應(yīng)事件。
# 創(chuàng)建一個節(jié)點
[zk: 127.0.0.1:2181(CONNECTED) 40] create /watch_node2
Created /watch_node2
# 1、針對節(jié)點增加PERSISTENT_RECURSIVE模式的監(jiān)聽器
[zk: 127.0.0.1:2181(CONNECTED) 41] addWatch -m PERSISTENT_RECURSIVE /watch_node2
# 2、節(jié)點數(shù)據(jù)發(fā)生變更時,觸發(fā)監(jiān)聽器
[zk: 127.0.0.1:2181(CONNECTED) 42] set /watch_node2 "hahahah"
WATCHER::
WatchedEvent state:SyncConnected type:NodeDataChanged path:/watch_node2
# 3、節(jié)點的子節(jié)點新增時,觸發(fā)監(jiān)聽器
[zk: 127.0.0.1:2181(CONNECTED) 43] create /watch_node2/node1
WATCHER::Created /watch_node2/node1
WatchedEvent state:SyncConnected type:NodeCreated path:/watch_node2/node1
# 4、節(jié)點的子節(jié)點數(shù)據(jù)發(fā)生變化時,觸發(fā)監(jiān)聽器
[zk: 127.0.0.1:2181(CONNECTED) 44] set /watch_node2/node1 "node1"
WATCHER::
WatchedEvent state:SyncConnected type:NodeDataChanged path:/watch_node2/node1
# 5、節(jié)點的子節(jié)點刪除時,觸發(fā)監(jiān)聽器
[zk: 127.0.0.1:2181(CONNECTED) 45] delete /watch_node2/node1
WATCHER::
WatchedEvent state:SyncConnected type:NodeDeleted path:/watch_node2/node1
3、刪除監(jiān)聽器刪除某個節(jié)點的監(jiān)聽器,刪除后節(jié)點相應(yīng)的變更事件不會再觸發(fā)監(jiān)聽器
removewatches path
[zk: 127.0.0.1:2181(CONNECTED) 48] removewatches /watch_node2
WATCHER::
WatchedEvent state:SyncConnected type:PersistentWatchRemoved path:/watch_node2
三、四字監(jiān)控命令見博文:https://saint.blog.csdn.net/article/details/128115252
你是否還在尋找穩(wěn)定的海外服務(wù)器提供商?創(chuàng)新互聯(lián)www.cdcxhl.cn海外機房具備T級流量清洗系統(tǒng)配攻擊溯源,準(zhǔn)確流量調(diào)度確保服務(wù)器高可用性,企業(yè)級服務(wù)器適合批量采購,新人活動首月15元起,快前往官網(wǎng)查看詳情吧
網(wǎng)站名稱:Zookeeper生產(chǎn)常用命令大全(最新3.8.0版本)-創(chuàng)新互聯(lián)
路徑分享:http://muchs.cn/article48/dpooep.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供移動網(wǎng)站建設(shè)、搜索引擎優(yōu)化、企業(yè)網(wǎng)站制作、關(guān)鍵詞優(yōu)化、云服務(wù)器、網(wǎng)站建設(shè)
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時需注明來源: 創(chuàng)新互聯(lián)
猜你還喜歡下面的內(nèi)容