zookeeper 客户端 zkCli 命令详解

本文详细介绍了Zookeeper客户端zkCli的安装、配置和常用命令,包括节点创建、删除、修改、查询等操作,以及ACL权限设置和Quota限制。通过实例演示了如何使用zkCli进行Zookeeper的日常管理和监控,帮助读者深入理解Zookeeper的工作原理和操作技巧。
摘要由CSDN通过智能技术生成

安装 zookeeper
brew install zookeeper

启动/停止 zookeeper
brew services start zookeeper

brew services stop zookeeper

配置文件
配置文件位置:/usr/local/etc/zookeeper/

下面是配置文件的内容:

tickTime=2000 
# The number of ticks that the initial
# synchronization phase can take
initLimit=10
# The number of ticks that can pass between
# sending a request and getting an acknowledgement
syncLimit=5

dataDir=/usr/local/var/run/zookeeper/data

clientPort=2181

tickTime 是zookeeper中的基本时间单元,单位是毫秒。

datadir是zookeeper持久化数据存放的目录。

clientPort是zookeeper监听客户端连接的端口,默认是2181

initLimit:follower连接和同步leader的时长。如果大多数follower这个时长内同步失败,将重新选举一个leader代替之前的leader。如果经常发生这种情况,说明这个值设置的太低。

syncLimit:folloer同步leader的时长。如果follower在这个时长内同步失败,follower将自动重启。连接他的client将连接到其他的follower上。

zookeeper 命令
我们通过nc或者telnet命令访问2181端口,通过执行ruok(Are you OK?)命令来检查zookeeper是否启动成功:

% echo ruok | nc localhost 2181
imok

那么我看见zookeeper回答我们“I’m OK”。下表中是所有的zookeeper的命令,都是由4个字符组成。

Category    Command    Description
Server status    ruok    Prints imok if the server is running and not in an error state.
conf    Prints the server configuration (from zoo.cfg).
envi    Prints the server environment, including ZooKeeper version, Java version, and other system properties.
srvr    Prints server statistics, including latency statistics, the number of znodes, and the server mode (standalone, leader, or follower).
stat    Prints server statistics and connected clients.
srst    Resets server statistics.
isro    Shows whether the server is in read-only (ro) mode (due to a network partition) or read/write mode (rw).
Client connections    dump    Lists all the sessions and ephemeral znodes for the ensemble. You must connect to the leader (see srvr) for this command.
cons    Lists connection statistics for all the server’s clients.
crst    Resets connection statistics.
Watches    wchs    Lists summary information for the server’s watches.
wchc    Lists all the server’s watches by connection. Caution: may impact server performance for a large number of watches.
wchp    Lists all the server’s watches by znode path. Caution: may impact server performance for a large number of watches.
Monitoring    mntr    Lists server statistics in Java properties format, suitable as a source for monitoring systems such as Ganglia and Nagios.
3.5.0以上的版本会有一个内嵌的web服务,通过访问http://localhost:8080/commands来访问以上的命令列表。

zookeeper cli
使用 brew 安装,已经把 zookeeper bin 目录下的命令添加的系统中,因此在终端直接执行 zkCli,就创建了一个 zk 客户端,连接 zk 服务。

输入 help 命令(其实输入任何 zkCli 不能识别的命令,都会列出所有的命令),查看可用的命令:

对 znode 进行增删改查
创建节点 create
语法

create [-s] [-e] path data acl

-s 创建有序节点

如果在创建znode时,我们使用排序标志的话,ZooKeeper会在我们指定的znode名字后面增加一个数字。我们继续加入相同名字的znode时,这个数字会不断增加。这个序号的计数器是由这些排序znode的父节点来维护的。

-e 创建临时节点

znode有两种类型:ephemeral和persistent。在创建znode时,我们指定znode的类型,并且在之后不会再被修改。当创建znode的客户端的session结束后,ephemeral类型的znode将被删除。persistent类型的znode在创建以后,就与客户端没什么联系了,除非主动去删除它,否则他会一直存在。Ephemeral znode没有任何子节点。

acl 在下面的《 ACL 操作》中详细介绍。

使用方法:

普通节点

[zk: localhost:2181(CONNECTED) 3] create /mynode hello
Created /mynode
[zk: localhost:2181(CONNECTED) 4] create /mynode/subnode world
Created /mynode/subnode

有序节点

[zk: localhost:2181(CONNECTED) 4] create -s /mynode hello
Created /mynode0000000004
[zk: localhost:2181(CONNECTED) 6] create -s /mynode world
Created /mynode0000000005

临时节点

[zk: localhost:2181(CONNECTED) 7] create -e /temp hello
Created /temp<

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值