【Zookeeper】03 單機操作
阿新 • • 發佈:2020-08-07
訪問Zookeeper
開啟Zookeeper服務程式:
./zkServer.sh start
啟動Zookeeper客戶端進行訪問:
./zkCli.sh
執行列印:
[root@VM-0-7-centos bin]# ./zkCli.sh
/usr/bin/java
Connecting to localhost:2181
2020-08-07 09:08:15,680 [myid:] - INFO [main:Environment@109] - Client environment:zookeeper.version=3.5.8-f439ca583e70862c3068a1f2a7d4d068eec33315, built on 05/04/2020 15:07 GMT
2020-08-07 09:08:15,683 [myid:] - INFO [main:Environment@109] - Client environment:host.name=VM-0-7-centos
2020-08-07 09:08:15,683 [myid:] - INFO [main:Environment@109] - Client environment:java.version=1.8.0_252
2020-08-07 09:08:15,687 [myid:] - INFO [main:Environment@109] - Client environment:java.vendor=Oracle Corporation
2020-08-07 09:08:15,687 [myid:] - INFO [main:Environment@109] - Client environment:java.home=/usr/lib/jvm/java-1.8.0-openjdk-1.8.0.252.b09-2.el7_8.x86_64/jre
2020-08-07 09:08:15,687 [myid:] - INFO [main:Environment@109] - Client environment:java.class.path=/usr/local/zookeeper/bin/../zookeeper-server/target/classes:/usr/local/zookeeper/bin/../build/classes:/usr/local/zookeeper/bin/../zookeeper-server/target/lib/* .jar:/usr/local/zookeeper/bin/../build/lib/*.jar:/usr/local/zookeeper/bin/../lib/zookeeper-jute-3.5.8.jar:/usr/local/zookeeper/bin/../lib/zookeeper-3.5.8.jar:/usr/local/zookeeper/bin/../lib/slf4j-log4j12-1.7.25.jar:/usr/local/zookeeper/bin/../lib/slf4j-api-1.7.25.jar:/usr/local/zookeeper/bin/../lib/netty-transport-native-unix-common-4.1.48.Final.jar:/usr/local/zookeeper/bin/../lib/netty-transport-native-epoll-4.1.48.Final.jar:/usr/local/zookeeper/bin/../lib/netty-transport-4.1.48.Final.jar:/usr/local/zookeeper/bin/../lib/netty-resolver-4.1.48.Final.jar:/usr/local/zookeeper/bin/../lib/netty-handler-4.1.48.Final.jar:/usr/local/zookeeper/bin/../lib/netty-common-4.1.48.Final.jar:/usr/local/zookeeper/bin/../lib/netty-codec-4.1.48.Final.jar:/usr/local/zookeeper/bin/../lib/netty-buffer-4.1.48.Final.jar:/usr/local/zookeeper/bin/../lib/log4j-1.2.17.jar:/usr/local/zookeeper/bin/../lib/json-simple-1.1.1.jar:/usr/local/zookeeper/bin/../lib/jline-2.11.jar:/usr/local/zookeeper/bin/../lib/jetty-util-9.4.24.v20191120.jar:/usr/local/zookeeper/bin/../lib/jetty-servlet-9.4.24.v20191120.jar:/usr/local/zookeeper/bin/../lib/jetty-server-9.4.24.v20191120.jar:/usr/local/zookeeper/bin/../lib/jetty-security-9.4.24.v20191120.jar:/usr/local/zookeeper/bin/../lib/jetty-io-9.4.24.v20191120.jar:/usr/local/zookeeper/bin/../lib/jetty-http-9.4.24.v20191120.jar:/usr/local/zookeeper/bin/../lib/javax.servlet-api-3.1.0.jar:/usr/local/zookeeper/bin/../lib/jackson-databind-2.10.3.jar:/usr/local/zookeeper/bin/../lib/jackson-core-2.10.3.jar:/usr/local/zookeeper/bin/../lib/jackson-annotations-2.10.3.jar:/usr/local/zookeeper/bin/../lib/commons-cli-1.2.jar:/usr/local/zookeeper/bin/../lib/audience-annotations-0.5.0.jar:/usr/local/zookeeper/bin/../zookeeper-*.jar:/usr/local/zookeeper/bin/../zookeeper-server/src/main/resources/lib/*.jar:/usr/local/zookeeper/bin/../conf:
2020-08-07 09:08:15,688 [myid:] - INFO [main:Environment@109] - Client environment:java.library.path=/usr/java/packages/lib/amd64:/usr/lib64:/lib64:/lib:/usr/lib
2020-08-07 09:08:15,688 [myid:] - INFO [main:Environment@109] - Client environment:java.io.tmpdir=/tmp
2020-08-07 09:08:15,688 [myid:] - INFO [main:Environment@109] - Client environment:java.compiler=<NA>
2020-08-07 09:08:15,688 [myid:] - INFO [main:Environment@109] - Client environment:os.name=Linux
2020-08-07 09:08:15,688 [myid:] - INFO [main:Environment@109] - Client environment:os.arch=amd64
2020-08-07 09:08:15,688 [myid:] - INFO [main:Environment@109] - Client environment:os.version=3.10.0-1062.18.1.el7.x86_64
2020-08-07 09:08:15,688 [myid:] - INFO [main:Environment@109] - Client environment:user.name=root
2020-08-07 09:08:15,688 [myid:] - INFO [main:Environment@109] - Client environment:user.home=/root
2020-08-07 09:08:15,688 [myid:] - INFO [main:Environment@109] - Client environment:user.dir=/usr/local/zookeeper/bin
2020-08-07 09:08:15,688 [myid:] - INFO [main:Environment@109] - Client environment:os.memory.free=23MB
2020-08-07 09:08:15,690 [myid:] - INFO [main:Environment@109] - Client environment:os.memory.max=247MB
2020-08-07 09:08:15,691 [myid:] - INFO [main:Environment@109] - Client environment:os.memory.total=29MB
2020-08-07 09:08:15,694 [myid:] - INFO [main:ZooKeeper@868] - Initiating client connection, connectString=localhost:2181 sessionTimeout=30000 watcher=org.apache.zookeeper.ZooKeeperMain$MyWatcher@484b61fc
2020-08-07 09:08:15,704 [myid:] - INFO [main:X509Util@79] - Setting -D jdk.tls.rejectClientInitiatedRenegotiation=true to disable client-initiated TLS renegotiation
2020-08-07 09:08:15,711 [myid:] - INFO [main:ClientCnxnSocket@237] - jute.maxbuffer value is 4194304 Bytes
2020-08-07 09:08:15,727 [myid:] - INFO [main:ClientCnxn@1653] - zookeeper.request.timeout value is 0. feature enabled=
2020-08-07 09:08:15,737 [myid:localhost:2181] - INFO [main-SendThread(localhost:2181):ClientCnxn$SendThread@1112] - Opening socket connection to server localhost/0:0:0:0:0:0:0:1:2181. Will not attempt to authenticate using SASL (unknown error)
Welcome to ZooKeeper!
JLine support is enabled
2020-08-07 09:08:15,876 [myid:localhost:2181] - INFO [main-SendThread(localhost:2181):ClientCnxn$SendThread@959] - Socket connection established, initiating session, client: /0:0:0:0:0:0:0:1:48090, server: localhost/0:0:0:0:0:0:0:1:2181
[zk: localhost:2181(CONNECTING) 0] 2020-08-07 09:08:15,920 [myid:localhost:2181] - INFO [main-SendThread(localhost:2181):ClientCnxn$SendThread@1394] - Session establishment complete on server localhost/0:0:0:0:0:0:0:1:2181, sessionid = 0x100048bdb4c0000, negotiated timeout = 30000
WATCHER::
輸入quit回車退出zookeeper客戶端:
WatchedEvent state:SyncConnected type:None path:null
quit
WATCHER::
WatchedEvent state:Closed type:None path:null
2020-08-07 09:08:23,890 [myid:] - INFO [main:ZooKeeper@1422] - Session: 0x100048bdb4c0000 closed
2020-08-07 09:08:23,891 [myid:] - INFO [main-EventThread:ClientCnxn$EventThread@524] - EventThread shut down for session: 0x100048bdb4c0000
建立節點:
語法:
create [-s] [-e] path data acl
引數說明:
-s表示順序節點, -e表示臨時節點 預設情況下,建立的是持久節點。 path是節點路徑, data是節點資料, acl用來進行許可權控制
案例:
create /sanguo luoguanzhong 建立一個持久節點目錄為/sanguo 值為 luoguanzhong create -s /shuihu shinaian 建立一個持久順序節點目錄為/shuihu 值為 shinaian create -e /xiyou wuchengren 建立一個臨時節點目錄為/xiyou 值為 wuchengren create -e -s /honglou caoxueqing 建立一個臨時順序節點目錄為/honglou 值為 caoxueqing
我的測試:
WATCHER::
WatchedEvent state:SyncConnected type:None path:null
[zk: localhost:2181(CONNECTED) 0] create -e /zkNode01 dzz
Created /zkNode01
[zk: localhost:2181(CONNECTED) 1]
檢視節點內容:
使用get命令,獲取zookeeper指定節點的內容和屬性資訊
[zk: localhost:2181(CONNECTED) 1] get /zkNode01
dzz
如果輸入完整資訊get
[zk: localhost:2181(CONNECTED) 2] get /zkNode01 dzz
'get path [watch]' has been deprecated. Please use 'get [-s] [-w] path' instead.
ZK提示此命令已經過時不適用了,要求使用 -s & -w引數進行獲取
[zk: localhost:2181(CONNECTED) 4] get -s -w /zkNode01
dzz
cZxid = 0x4
ctime = Fri Aug 07 09:16:10 CST 2020
mZxid = 0x4
mtime = Fri Aug 07 09:16:10 CST 2020
pZxid = 0x4
cversion = 0
dataVersion = 0
aclVersion = 0
ephemeralOwner = 0x100048bdb4c0001
dataLength = 3
numChildren = 0
也可以使用stat命名檢視:
stat /zkNode01
若是需要檢視子節點:
ls / 檢視根節點下的所有子節點 ls 指定的節點 檢視該節點下的所有子節點
檢視根的子節點們:
[zk: localhost:2181(CONNECTED) 5] ls /
[zkNode01, zookeeper]
除了我們剛剛建立zkNode01,發現還有一個節點,名稱就為zookeeper
檢視這個節點的資訊:
[zk: localhost:2181(CONNECTED) 6] get -s -w /zookeeper
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 = -2
dataVersion = 0
aclVersion = 0
ephemeralOwner = 0x0
dataLength = 0
numChildren = 2
更新修改節點內容:
使用set命令,更新節點內容。
語法:
set path data
其中的data就是要更新的新內容。
其實更新的是節點的data
[zk: localhost:2181(CONNECTED) 31] get -s -w /zkNode01
123
cZxid = 0x4
ctime = Fri Aug 07 09:16:10 CST 2020
mZxid = 0x8
mtime = Fri Aug 07 09:39:15 CST 2020
pZxid = 0x4
cversion = 0
dataVersion = 4
aclVersion = 0
ephemeralOwner = 0x100048bdb4c0001
dataLength = 3
numChildren = 0
每一次更新 dataVersion都會追加修改的記錄次數
刪除節點:
首先節點結構是一個樹狀結構,也就是說一個節點下面會有N個子節點,也可能沒有
如果我們某一個節點進行刪除,那麼也就代表著該節點下面的後代節點都將被刪除,
Zookeeper在我們執行刪除節點的操作時會判斷有無後代節點,如果有是不允許刪除該節點的
例如這樣:
[zk: localhost:2181(CONNECTED) 31] delete /someNode
Node not empty : /someNode
我們剛剛建立的節點下面並沒有其他子節點,所有刪除正常:
[zk: localhost:2181(CONNECTED) 32] delete /zkNode01
WATCHER::
WatchedEvent state:SyncConnected type:NodeDeleted path:/zkNode01
但是如果需要刪除該節點和所有子節點操作的話,是使用此命令:
rmr 節點路徑
監控節點:
我們再開啟一個XSHELL訪問:
在下面客戶端建立一個新節點,並且在上面的客戶端中獲取節點資訊並且監視節點
當節點的資料進行修改,上面的客戶端就會監聽到改動: