关于zookeeper:zookeeper-cli

69次阅读

共计 4933 个字符,预计需要花费 13 分钟才能阅读完成。

本文依据 ZooKeeper-cli: the ZooKeeper command line interface 对一些常见的 zookeeper cli 命令做一些笔记.

登录

咱们能够应用 zkCli 命令来登录上zookeeper.

# 本地登录, 默认为 localhost:2181
[thinktik@ThinkOnline bin]$ ./zkCli.sh
# 指定 host 和 port, 咱们能够应用这个命令加上 -server 参数来进行近程登录
[thinktik@ThinkOnline bin]$ ./zkCli.sh -server localhost:2181

查看文档

咱们能够应用 zookeeper 时通过 help 来查看文档

[zk: localhost:2181(CONNECTED) 0] 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] 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 val path
    stat [-w] path
    sync path
    version 
Command not found: Command not found help

创立节点

咱们能够应用 create [-s] [-e] [-c] [-t ttl] path [data] [acl] 来创立节点,默认节点类型为 persistent 节点

创立 persistent 节点

应用 create 创立的节点默认为 persistent 节点

[zk: localhost:2181(CONNECTED) 9] ls /
[zookeeper]
[zk: localhost:2181(CONNECTED) 10] create /persistent_node_test
Created /persistent_node_test
[zk: localhost:2181(CONNECTED) 11] create /persistent_node_test/p_data thinktik
Created /persistent_node_test/p_date
[zk: localhost:2181(CONNECTED) 12] get /persistent_node_test
null
[zk: localhost:2181(CONNECTED) 14] get /persistent_node_test/p_data
thinktik

创立 ephemeral 节点

咱们能够在创立节点的时候加上 -e 参数来创立长期节点,长期节点的特点是如果以后的连贯被断开,那么这个节点会主动的被删除.

[zk: localhost:2181(CONNECTED) 24] create -e /ephemeral_node_test thinktik
Created /ephemeral_node_test
[zk: localhost:2181(CONNECTED) 25] ls /
[ephemeral_node_test, zookeeper]
[zk: localhost:2181(CONNECTED) 27] get /ephemeral_node_test
thinktik

创立 sequential 节点

对于后面的 2 种节点,咱们也能够加上 -s 来创立 sequential 节点,sequential 节点会主动的为你加上一个枯燥递增的序列号

# 创立 persistent 并且为 sequential 的节点
[zk: localhost:2181(CONNECTED) 6] create -s /s- thinktik
Created /s-0000000008
[zk: localhost:2181(CONNECTED) 7] get /s-0000000008
thinktik
# 创立 ephemeral 并且为 sequential 的节点
[zk: localhost:2181(CONNECTED) 8] create -e -s /s- thinktik
Created /s-0000000009
[zk: localhost:2181(CONNECTED) 10] get /s-0000000009
thinktik
[zk: localhost:2181(CONNECTED) 9] ls /
[s-0000000008, s-0000000009, zookeeper]

创立 container 节点

container 节点是 zookeeper 3.5.3 开始提供的新的节点类型,它的次要特点就是如果 Container 节点上面没有子节点,则 Container 节点 在将来会被 Zookeeper 主动革除, 定时工作默认 60s 查看一次

The time interval in milliseconds for each check of candidate container and ttl nodes. Default is “60000”

咱们应用 -c 参数即可

# 创立容器节点
[zk: localhost:2181(CONNECTED) 19] create -c /container thinktik_c0
Created /container
# 创立容器节点中的子节点
[zk: localhost:2181(CONNECTED) 20] create /container/sub1 thinktik_c1
Created /container/sub1
[zk: localhost:2181(CONNECTED) 21] ls /
[container, zookeeper]
[zk: localhost:2181(CONNECTED) 22] ls /container
[sub1]
# 删除容器节点中的子节点
[zk: localhost:2181(CONNECTED) 23] delete /container/sub1
# 此时容器节点曾经空了,期待大概 60s,如果该节点中还是没有子节点,那么会被主动删除
[zk: localhost:2181(CONNECTED) 27] ls /container
[]
# 曾经被主动删除了
[zk: localhost:2181(CONNECTED) 30] ls /
[zookeeper]

创立 TTL 节点

TTL 节点就是有生命周期的节点,你在创立的时候指定过期工夫,那么它会在你规定的工夫后主动的被删除.

# 如果你不开启 -Dzookeeper.extendedTypesEnabled=true 的话,TTL 节点是无奈创立.
[zk: localhost:2181(CONNECTED) 1] create -t 60 /ttl_test thinktik
KeeperErrorCode = Unimplemented for /ttl_test
# 开启后就能够了
[zk: localhost:2181(CONNECTED) 0] create -t 60000 /ttl_node thinktik
Created /ttl_node

咱们能够这样增加启动参数

# 批改启动脚本,增加 -Dzookeeper.extendedTypesEnabled=true
[thinktik@ThinkOnline bin]$ vim ./zkServer.sh

删除节点

咱们能够应用 delete 来删除节点

# 应用 delete 能够删除节点,然而它只能间接删除某个节点,如果该节点下有子节点时不能递归删除
[zk: localhost:2181(CONNECTED) 22] delete /persistent_node_test
Node not empty: /persistent_node_test
# 咱们能够应用 deleteall 来进行递归删除
[zk: localhost:2181(CONNECTED) 23] deleteall /persistent_node_test

更新节点的值

咱们能够应用 set 命令来更新节点的值

[zk: localhost:2181(CONNECTED) 0] ls /
[zookeeper]
[zk: localhost:2181(CONNECTED) 1] create /p1 thinktik
Created /p1
[zk: localhost:2181(CONNECTED) 2] get /p1
thinktik
# 更新 /p1 节点的值
[zk: localhost:2181(CONNECTED) 3] set /p1 think
[zk: localhost:2181(CONNECTED) 4] get /p1
think

查看节点状态

咱们能够查看节点状态

[zk: localhost:2181(CONNECTED) 6] stat /p1
cZxid = 0x3a
ctime = Mon Feb 15 21:50:10 CST 2021
mZxid = 0x3b
mtime = Mon Feb 15 21:50:20 CST 2021
pZxid = 0x3a
cversion = 0
dataVersion = 1
aclVersion = 0
ephemeralOwner = 0x0
dataLength = 5
numChildren = 0

查看节点

[zk: localhost:2181(CONNECTED) 13] ls /
[p1, zookeeper]
# 咱们能够增加 - R 来递归查看子节点
[zk: localhost:2181(CONNECTED) 12] ls -R /p1
/p1
/p1/sp1

查看版本

咱们能够这样查看 zookeeper version

[zk: localhost:2181(CONNECTED) 7] version
ZooKeeper CLI version: 3.6.2--803c7f1a12f85978cb049af5e4ef23bd8b688715, built on 09/04/2020 12:44 GMT

监听节点

咱们能够在获取节点的时候增加 -w 来监听节点变动

咱们须要开启 2 个窗口在获取节点值的时候增加 -w 来设置一个 watch

A 窗口获取值并增加监听器:

[zk: localhost:2181(CONNECTED) 2] get -w /p1 
think
[zk: localhost:2181(CONNECTED) 3] 
WATCHER::

WatchedEvent state:SyncConnected type:NodeDataChanged path:/p1

B 窗口更改值:

[zk: localhost:2181(CONNECTED) 15] set /p1 thinktik

查看历史记录

[zk: localhost:2181(CONNECTED) 16] history
6 - stat /p1
7 - version
8 - ls /
9 - create /p1/sp1
10 - ls /
11 - ls -R /
12 - ls -R /p1
13 - ls /
14 - ls /
15 - set /p1 thinktik
16 - history

本文原创链接: zookeeper cli

正文完
 0