https://zookeeper.apache.org/doc/r3.8.0/zookeeperCLI.html
一、服务端zookeeper的shell命令都体现在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服务:
./zkServer.sh start
2>查看zookeeper服务状态:
./zkServer.sh status
3>停止zookeeper服务:
./zkServer.sh stop
4>重启zookeeper服务:
./zkServer.sh restart
直接使用sh zkCli.sh
命令可以连接到本地的zookeeper服务(127.0.0.1:2181);如果需要连接到其他地址的zookeeper服务,需要使用以下命令格式:
sh zkCli.sh -server [ip]:[port]
示例命令:
sh zkCli.sh -server 127.0.0.1:2181
连接成功后,系统会输出 ZooKeeper 的相关环境以及配置信息。 可以在开启的会话中做一些客户端操作 与 zookeeper服务进行交互;
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命令:创建节点,默认持久节点
create [-s] [-e] [-c] [-t ttl] path [data] [acl]
Zookeeper原生命令不支持级联创建节点树目录,需要一级一级的创建;curator客户端封装了级联创建的API。
1>创建持久节点Znode节点类型有四种:
- 持久的(persistent)
- 临时的(ephemeral)
- 持久有序的(persistent_sequential)
- 临时有序的(ephemeral_sequential)
[zk: 127.0.0.1:2181(CONNECTED) 0] create /node_p1
Created /node_p1
2>创建临时节点[zk: 127.0.0.1:2181(CONNECTED) 1] create -e /node_e1 "node value"
Created /node_e1
3>创建持久有序节点持久有序节点:设置的节点路径会被重命名为序列数,不设置节点前缀时,序列数从0000000000
开始递增;
不设置节点前缀时:
[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
设置节点前缀时:
[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>创建临时有序节点设置节点前缀时:
[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
不设置节点前缀时:
[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>创建ttl节点默认禁用,需要在 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不能用于临时节点 。
6>创建容器节点[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
从测试结果来看,容器节点的最后一个子级被删除之后,过了几秒,容器节点才被删除。
4、getget命令:获取节点/文件内容
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 选项在获取到节点值的同时,会带出节点的状态stat:
[zk: 127.0.0.1:2181(CONNECTED) 14] get -s /node_test
testNode value
cZxid = 0x14 # 数据节点创建时的事务ID
ctime = Wed Nov 30 15:09:39 CST 2022 # 数据节点创建时的时间
mZxid = 0x18 # 数据节点最后一次更新时的事务ID
mtime = Wed Nov 30 15:14:25 CST 2022 # 数据节点最后一次更新时的时间
pZxid = 0x14 # 数据节点的子节点最后一次被修改时的事务ID
cversion = 0 # 子节点的更改次数
dataVersion = 3 # 节点数据的更改次数
aclVersion = 0 # 节点的ACL的更改次数
ephemeralOwner = 0x0 # 如果节点是临时节点,则表示创建该节点的会话的SessionID,如果节点是持久节点,则该属性值为0.
dataLength = 24 # 数据内容的长度
numChildren = 0 # 数据节点当前的子节点个数
5、setset命令:修改节点内容;每次修改成功后,版本号会自动+1,初始为0
set [-s] [-v version] path data
[zk: 127.0.0.1:2181(CONNECTED) 15] set /node_test "updated tesetNode value"
-s选项,更新节点数据并显示节点状态信息
[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选项,指定数据版本号,如果指定的数据版本号和数据当前版本号不一致,则更新失败(可以理解为乐观锁方式更新);
[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命令:查看目录下的节点信息
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命令:查看节点状态。
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、删除节点delete命令只能删除没有子节点的节点;deleteall命令可以级联删除当前节点和子节点;
1>deletedelete命令:只能删除没有子节点的节点。如果其有子节点时,无法删除
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命令:级联删除该节点和子节点。
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(事件监听器)允许用户在指定节点上注册 Watcher,在特定事件发生时,Watcher会被触发;Zookeeper服务端将事件通知到响应的客户端上去;
该机制是Zookeeper实现分布式协调服务的重要特性。
监听节点的子节点变化,当子节点发生改变时触发;
ls -w path
Session1:
# 创建节点
[zk: 127.0.0.1:2181(CONNECTED) 31] create /watch_node
Created /watch_node
# 监听节点目录变化
[zk: 127.0.0.1:2181(CONNECTED) 32] ls -w /watch_node
[]
Session2:
# 创建/watch_node节点的子节点
[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
当/watch_node节点的子节点第一次发生变化时,会触发session1的Watcher,触发后会被删除,无法再次触发。
[zk: 127.0.0.1:2181(CONNECTED) 33]
WATCHER::
WatchedEvent state:SyncConnected type:NodeChildrenChanged path:/watch_node
图示:
2>监听节点数据变化当该节点数据发生改变时触发,但子节点的数据变化不会触发。一次性监听,触发后会被删除,无法再次触发。
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"
当/watch_node节点的数据第一次发生变化时,会触发session1的Watcher,触发后会被删除,无法再次触发。
[zk: 127.0.0.1:2181(CONNECTED) 34]
WATCHER::
WatchedEvent state:SyncConnected type:NodeDataChanged path:/watch_node
图示:
2、永久的监听器在 Zookeeper 3.6.0版本之后,客户端可以在节点上创建永久监听,永久监听在被触发后不会被删除。
addWatch [-m mode] path
永久监听器在创建时可以通过 -m 选项指定模式,有两种模式:
PERSISTENT:该节点的数据变化以及子节点的变化会触发相应事件,子节点的数据变化不会触发。
# 1、增加PERSISTENT模式的监听器
[zk: 127.0.0.1:2181(CONNECTED) 34] addWatch -m PERSISTENT /watch_node
# 2、节点数据发生变更时,触发监听器
[zk: 127.0.0.1:2181(CONNECTED) 35] set /watch_node "node12321"
WATCHER::
WatchedEvent state:SyncConnected type:NodeDataChanged path:/watch_node
# 3、节点的子节点新增时,触发监听器
[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、节点的子节点数据发生变化时,不会触发监听器
[zk: 127.0.0.1:2181(CONNECTED) 37] set /watch_node/node4 "node4-2"
# 5、节点的子节点删除时,触发监听器
[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:节点的数据变化以及所有子孙节点的目录或者数据变化都会触发相应事件。
# 创建一个节点
[zk: 127.0.0.1:2181(CONNECTED) 40] create /watch_node2
Created /watch_node2
# 1、针对节点增加PERSISTENT_RECURSIVE模式的监听器
[zk: 127.0.0.1:2181(CONNECTED) 41] addWatch -m PERSISTENT_RECURSIVE /watch_node2
# 2、节点数据发生变更时,触发监听器
[zk: 127.0.0.1:2181(CONNECTED) 42] set /watch_node2 "hahahah"
WATCHER::
WatchedEvent state:SyncConnected type:NodeDataChanged path:/watch_node2
# 3、节点的子节点新增时,触发监听器
[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、节点的子节点数据发生变化时,触发监听器
[zk: 127.0.0.1:2181(CONNECTED) 44] set /watch_node2/node1 "node1"
WATCHER::
WatchedEvent state:SyncConnected type:NodeDataChanged path:/watch_node2/node1
# 5、节点的子节点删除时,触发监听器
[zk: 127.0.0.1:2181(CONNECTED) 45] delete /watch_node2/node1
WATCHER::
WatchedEvent state:SyncConnected type:NodeDeleted path:/watch_node2/node1
3、删除监听器删除某个节点的监听器,删除后节点相应的变更事件不会再触发监听器
removewatches path
[zk: 127.0.0.1:2181(CONNECTED) 48] removewatches /watch_node2
WATCHER::
WatchedEvent state:SyncConnected type:PersistentWatchRemoved path:/watch_node2
三、四字监控命令见博文:https://saint.blog.csdn.net/article/details/128115252
你是否还在寻找稳定的海外服务器提供商?创新互联www.cdcxhl.cn海外机房具备T级流量清洗系统配攻击溯源,准确流量调度确保服务器高可用性,企业级服务器适合批量采购,新人活动首月15元起,快前往官网查看详情吧