ZooKeeper 初体验

安装Zookeeper

Mac OSnode

Mac 用户能够使用 Homebrew 安装和管理 Zookeeper 服务:docker

brew install zookeeper

配置文件地址在: /usr/local/etc/zookeeper。bash

启动 zookeeper 服务:工具

brew services start zookeeper

进入命令行客户端zkCli:ui

zkCli

默认链接localhost:2181, 手动指定服务地址:命令行

zkCli -server localhost:2181

Dockercode

能够使用官方提供的Docker镜像快速启动Zookeeper。server

启动服务端:递归

docker run --name my_zookeeper -d zookeeper

ZNode 操做

使用终端工具ZkCli链接:get

docker run -it --rm --link my_zookeeper:zookeeper zookeeper zkCli.sh -server zookeeper

命令行客户端 zkCli 能够交互式操做 Zookeeper, 其命令风格相似于 Unix 终端。

ls

查看某个路径包含的全部节点:

[zk: localhost:2181(CONNECTED) 1] ls /
[cluster, zookeeper, admin, config]
[zk: localhost:2181(CONNECTED) 2] ls /zookeeper
[quota]

ls2

查看某个路径包含的全部节点,以及节点元数据:

[zk: localhost:2181(CONNECTED) 7] ls2 /zookeeper
[quota]
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 = -1
dataVersion = 0
aclVersion = 0
ephemeralOwner = 0x0
dataLength = 0
numChildren = 1

create

建立节点:

[zk: localhost:2181(CONNECTED) 0] create /test 1
Created /test

/test为建立节点的路径,1为Znode的数据data

create 命令没法递归建立节点,即/test节点不存在时不能直接建立/test/t1

使用-e 选项建立临时节点:

[zk: localhost:2181(CONNECTED) 0] create -e /test/t2 t2
Created /test/t2
[zk: localhost:2181(CONNECTED) 1] get /test/t2
t2
[zk: localhost:2181(CONNECTED) 2] quit
Quitting...
[zk: localhost:2181(CONNECTED) 0] get /test/t2
Node does not exist: /test/t2

退出zkCli后从新进入,临时节点已经消失。

get

获取节点数据与元数据:

[zk: localhost:2181(CONNECTED) 8] get /test
1
cZxid = 0x11d28
ctime = Sat Sep 01 16:04:08 CST 2018
mZxid = 0x11d28
mtime = Sat Sep 01 16:04:08 CST 2018
pZxid = 0x11d28
cversion = 0
dataVersion = 0
aclVersion = 0
ephemeralOwner = 0x0
dataLength = 1
numChildren = 0

set

更改节点数据:

[zk: localhost:2181(CONNECTED) 9] set /test 2
cZxid = 0x11d28
ctime = Sat Sep 01 16:04:08 CST 2018
mZxid = 0x11d2a
mtime = Sat Sep 01 16:25:46 CST 2018
pZxid = 0x11d28
cversion = 0
dataVersion = 1
aclVersion = 0
ephemeralOwner = 0x0
dataLength = 1
numChildren = 0

能够注意到版本号发生了变化。

delete

删除节点:

[zk: localhost:2181(CONNECTED) 2] delete /test

只能删除没有子节点的Znode,若要将子节点一同删除需使用rmr命令。

相关文章
相关标签/搜索