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
其中包括:
二、客戶(hù)端 1、連接客戶(hù)端1>啟動(dòng)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)會(huì)輸出 ZooKeeper 的相關(guān)環(huán)境以及配置信息。 可以在開(kāi)啟的會(huì)話(huà)中做一些客戶(hù)端操作 與 zookeeper服務(wù)進(jìn)行交互;
2、help使用 help命令來(lái)查看幫助
[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é)點(diǎn),默認(rèn)持久節(jié)點(diǎn)
create [-s] [-e] [-c] [-t ttl] path [data] [acl]
Zookeeper原生命令不支持級(jí)聯(lián)創(chuàng)建節(jié)點(diǎn)樹(shù)目錄,需要一級(jí)一級(jí)的創(chuàng)建;curator客戶(hù)端封裝了級(jí)聯(lián)創(chuàng)建的API。
1>創(chuàng)建持久節(jié)點(diǎn)Znode節(jié)點(diǎn)類(lèi)型有四種:
- 持久的(persistent)
- 臨時(shí)的(ephemeral)
- 持久有序的(persistent_sequential)
- 臨時(shí)有序的(ephemeral_sequential)
[zk: 127.0.0.1:2181(CONNECTED) 0] create /node_p1
Created /node_p1
2>創(chuàng)建臨時(shí)節(jié)點(diǎn)[zk: 127.0.0.1:2181(CONNECTED) 1] create -e /node_e1 "node value"
Created /node_e1
3>創(chuàng)建持久有序節(jié)點(diǎn)持久有序節(jié)點(diǎn):設(shè)置的節(jié)點(diǎn)路徑會(huì)被重命名為序列數(shù),不設(shè)置節(jié)點(diǎn)前綴時(shí),序列數(shù)從0000000000
開(kāi)始遞增;
不設(shè)置節(jié)點(diǎn)前綴時(shí):
[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é)點(diǎn)前綴時(shí):
[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)建臨時(shí)有序節(jié)點(diǎn)設(shè)置節(jié)點(diǎn)前綴時(shí):
[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é)點(diǎn)前綴時(shí):
[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é)點(diǎn)默認(rèn)禁用,需要在 zoo.cfg中添加 extendedTypesEnabled=true 開(kāi)啟。 否者報(bào)如下錯(cuò):
[zk: 127.0.0.1:2181(CONNECTED) 18] create -t 10 /node_ttl "ttl node value"
KeeperErrorCode = Unimplemented for /node_ttl
開(kāi)啟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不能用于臨時(shí)節(jié)點(diǎn) 。
6>創(chuàng)建容器節(jié)點(diǎn)[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
從測(cè)試結(jié)果來(lái)看,容器節(jié)點(diǎn)的最后一個(gè)子級(jí)被刪除之后,過(guò)了幾秒,容器節(jié)點(diǎn)才被刪除。
4、getget命令:獲取節(jié)點(diǎn)/文件內(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 選項(xiàng)在獲取到節(jié)點(diǎn)值的同時(shí),會(huì)帶出節(jié)點(diǎn)的狀態(tài)stat:
[zk: 127.0.0.1:2181(CONNECTED) 14] get -s /node_test
testNode value
cZxid = 0x14 # 數(shù)據(jù)節(jié)點(diǎn)創(chuàng)建時(shí)的事務(wù)ID
ctime = Wed Nov 30 15:09:39 CST 2022 # 數(shù)據(jù)節(jié)點(diǎn)創(chuàng)建時(shí)的時(shí)間
mZxid = 0x18 # 數(shù)據(jù)節(jié)點(diǎn)最后一次更新時(shí)的事務(wù)ID
mtime = Wed Nov 30 15:14:25 CST 2022 # 數(shù)據(jù)節(jié)點(diǎn)最后一次更新時(shí)的時(shí)間
pZxid = 0x14 # 數(shù)據(jù)節(jié)點(diǎn)的子節(jié)點(diǎn)最后一次被修改時(shí)的事務(wù)ID
cversion = 0 # 子節(jié)點(diǎn)的更改次數(shù)
dataVersion = 3 # 節(jié)點(diǎn)數(shù)據(jù)的更改次數(shù)
aclVersion = 0 # 節(jié)點(diǎn)的ACL的更改次數(shù)
ephemeralOwner = 0x0 # 如果節(jié)點(diǎn)是臨時(shí)節(jié)點(diǎn),則表示創(chuàng)建該節(jié)點(diǎn)的會(huì)話(huà)的SessionID,如果節(jié)點(diǎn)是持久節(jié)點(diǎn),則該屬性值為0.
dataLength = 24 # 數(shù)據(jù)內(nèi)容的長(zhǎng)度
numChildren = 0 # 數(shù)據(jù)節(jié)點(diǎn)當(dāng)前的子節(jié)點(diǎn)個(gè)數(shù)
5、setset命令:修改節(jié)點(diǎn)內(nèi)容;每次修改成功后,版本號(hào)會(huì)自動(dòng)+1,初始為0
set [-s] [-v version] path data
[zk: 127.0.0.1:2181(CONNECTED) 15] set /node_test "updated tesetNode value"
-s選項(xiàng),更新節(jié)點(diǎn)數(shù)據(jù)并顯示節(jié)點(diǎn)狀態(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選項(xiàng),指定數(shù)據(jù)版本號(hào),如果指定的數(shù)據(jù)版本號(hào)和數(shù)據(jù)當(dāng)前版本號(hào)不一致,則更新失敗(可以理解為樂(lè)觀鎖方式更新);
[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é)點(diǎn)信息
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é)點(diǎn)狀態(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é)點(diǎn)delete命令只能刪除沒(méi)有子節(jié)點(diǎn)的節(jié)點(diǎn);deleteall命令可以級(jí)聯(lián)刪除當(dāng)前節(jié)點(diǎn)和子節(jié)點(diǎn);
1>deletedelete命令:只能刪除沒(méi)有子節(jié)點(diǎn)的節(jié)點(diǎn)。如果其有子節(jié)點(diǎn)時(shí),無(wú)法刪除
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命令:級(jí)聯(lián)刪除該節(jié)點(diǎn)和子節(jié)點(diǎn)。
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)聽(tīng)器)允許用戶(hù)在指定節(jié)點(diǎn)上注冊(cè) Watcher,在特定事件發(fā)生時(shí),Watcher會(huì)被觸發(fā);Zookeeper服務(wù)端將事件通知到響應(yīng)的客戶(hù)端上去;
該機(jī)制是Zookeeper實(shí)現(xiàn)分布式協(xié)調(diào)服務(wù)的重要特性。
監(jiān)聽(tīng)節(jié)點(diǎn)的子節(jié)點(diǎn)變化,當(dāng)子節(jié)點(diǎn)發(fā)生改變時(shí)觸發(fā);
ls -w path
Session1:
# 創(chuàng)建節(jié)點(diǎn)
[zk: 127.0.0.1:2181(CONNECTED) 31] create /watch_node
Created /watch_node
# 監(jiān)聽(tīng)節(jié)點(diǎn)目錄變化
[zk: 127.0.0.1:2181(CONNECTED) 32] ls -w /watch_node
[]
Session2:
# 創(chuàng)建/watch_node節(jié)點(diǎn)的子節(jié)點(diǎn)
[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é)點(diǎn)的子節(jié)點(diǎn)第一次發(fā)生變化時(shí),會(huì)觸發(fā)session1的Watcher,觸發(fā)后會(huì)被刪除,無(wú)法再次觸發(fā)。
[zk: 127.0.0.1:2181(CONNECTED) 33]
WATCHER::
WatchedEvent state:SyncConnected type:NodeChildrenChanged path:/watch_node
圖示:
2>監(jiān)聽(tīng)節(jié)點(diǎn)數(shù)據(jù)變化當(dāng)該節(jié)點(diǎn)數(shù)據(jù)發(fā)生改變時(shí)觸發(fā),但子節(jié)點(diǎn)的數(shù)據(jù)變化不會(huì)觸發(fā)。一次性監(jiān)聽(tīng),觸發(fā)后會(huì)被刪除,無(wú)法再次觸發(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é)點(diǎn)的數(shù)據(jù)第一次發(fā)生變化時(shí),會(huì)觸發(fā)session1的Watcher,觸發(fā)后會(huì)被刪除,無(wú)法再次觸發(fā)。
[zk: 127.0.0.1:2181(CONNECTED) 34]
WATCHER::
WatchedEvent state:SyncConnected type:NodeDataChanged path:/watch_node
圖示:
2、永久的監(jiān)聽(tīng)器在 Zookeeper 3.6.0版本之后,客戶(hù)端可以在節(jié)點(diǎn)上創(chuàng)建永久監(jiān)聽(tīng),永久監(jiān)聽(tīng)在被觸發(fā)后不會(huì)被刪除。
addWatch [-m mode] path
永久監(jiān)聽(tīng)器在創(chuàng)建時(shí)可以通過(guò) -m 選項(xiàng)指定模式,有兩種模式:
PERSISTENT:該節(jié)點(diǎn)的數(shù)據(jù)變化以及子節(jié)點(diǎn)的變化會(huì)觸發(fā)相應(yīng)事件,子節(jié)點(diǎn)的數(shù)據(jù)變化不會(huì)觸發(fā)。
# 1、增加PERSISTENT模式的監(jiān)聽(tīng)器
[zk: 127.0.0.1:2181(CONNECTED) 34] addWatch -m PERSISTENT /watch_node
# 2、節(jié)點(diǎn)數(shù)據(jù)發(fā)生變更時(shí),觸發(fā)監(jiān)聽(tīng)器
[zk: 127.0.0.1:2181(CONNECTED) 35] set /watch_node "node12321"
WATCHER::
WatchedEvent state:SyncConnected type:NodeDataChanged path:/watch_node
# 3、節(jié)點(diǎn)的子節(jié)點(diǎn)新增時(shí),觸發(fā)監(jiān)聽(tīng)器
[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é)點(diǎn)的子節(jié)點(diǎn)數(shù)據(jù)發(fā)生變化時(shí),不會(huì)觸發(fā)監(jiān)聽(tīng)器
[zk: 127.0.0.1:2181(CONNECTED) 37] set /watch_node/node4 "node4-2"
# 5、節(jié)點(diǎn)的子節(jié)點(diǎn)刪除時(shí),觸發(fā)監(jiān)聽(tīng)器
[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é)點(diǎn)的數(shù)據(jù)變化以及所有子孫節(jié)點(diǎn)的目錄或者數(shù)據(jù)變化都會(huì)觸發(fā)相應(yīng)事件。
# 創(chuàng)建一個(gè)節(jié)點(diǎn)
[zk: 127.0.0.1:2181(CONNECTED) 40] create /watch_node2
Created /watch_node2
# 1、針對(duì)節(jié)點(diǎn)增加PERSISTENT_RECURSIVE模式的監(jiān)聽(tīng)器
[zk: 127.0.0.1:2181(CONNECTED) 41] addWatch -m PERSISTENT_RECURSIVE /watch_node2
# 2、節(jié)點(diǎn)數(shù)據(jù)發(fā)生變更時(shí),觸發(fā)監(jiān)聽(tīng)器
[zk: 127.0.0.1:2181(CONNECTED) 42] set /watch_node2 "hahahah"
WATCHER::
WatchedEvent state:SyncConnected type:NodeDataChanged path:/watch_node2
# 3、節(jié)點(diǎn)的子節(jié)點(diǎn)新增時(shí),觸發(fā)監(jiān)聽(tīng)器
[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é)點(diǎn)的子節(jié)點(diǎn)數(shù)據(jù)發(fā)生變化時(shí),觸發(fā)監(jiān)聽(tīng)器
[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é)點(diǎn)的子節(jié)點(diǎn)刪除時(shí),觸發(fā)監(jiān)聽(tīng)器
[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)聽(tīng)器刪除某個(gè)節(jié)點(diǎn)的監(jiān)聽(tīng)器,刪除后節(jié)點(diǎn)相應(yīng)的變更事件不會(huì)再觸發(fā)監(jiān)聽(tīng)器
removewatches path
[zk: 127.0.0.1:2181(CONNECTED) 48] removewatches /watch_node2
WATCHER::
WatchedEvent state:SyncConnected type:PersistentWatchRemoved path:/watch_node2
三、四字監(jiān)控命令見(jiàn)博文:https://saint.blog.csdn.net/article/details/128115252
你是否還在尋找穩(wěn)定的海外服務(wù)器提供商?創(chuàng)新互聯(lián)www.cdcxhl.cn海外機(jī)房具備T級(jí)流量清洗系統(tǒng)配攻擊溯源,準(zhǔn)確流量調(diào)度確保服務(wù)器高可用性,企業(yè)級(jí)服務(wù)器適合批量采購(gòu),新人活動(dòng)首月15元起,快前往官網(wǎng)查看詳情吧
網(wǎng)站名稱(chēng):Zookeeper生產(chǎn)常用命令大全(最新3.8.0版本)-創(chuàng)新互聯(lián)
路徑分享:http://m.newbst.com/article48/dpooep.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供移動(dòng)網(wǎng)站建設(shè)、搜索引擎優(yōu)化、企業(yè)網(wǎng)站制作、關(guān)鍵詞優(yōu)化、云服務(wù)器、網(wǎng)站建設(shè)
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶(hù)投稿、用戶(hù)轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請(qǐng)盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如需處理請(qǐng)聯(lián)系客服。電話(huà):028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來(lái)源: 創(chuàng)新互聯(lián)
猜你還喜歡下面的內(nèi)容