Zookeeper03之客户端命令操作

本文涉及的产品
注册配置 MSE Nacos/ZooKeeper,118元/月
云原生网关 MSE Higress,422元/月
可视分析地图(DataV-Atlas),3 个项目,100M 存储空间
简介: Zookeeper03之客户端命令操作


bin目录下常用的脚本解释

image.png

客户端命令行操作

1.配置zookeeper的环境变量

 为了执行命令方便而不用每次都进入到zookeeper的安装目录去启动命令,我们可以将zookeeper配置到系统环境变量中。方便每次的操作

vim /etc/profile
# zookeeper 配置
export ZOOKEEPER_HOME=/root/app/zookeeper-3.4.10
export PATH=$PATH:$ZOOKEEPER_HOME/bin

image.png

重新加载配置文件

source /etc/profile

2.连接zookeeper

连接当前节点上的zookeeper服务器

zkCli.sh 

image.png

想要连接其他节点上的服务器

zkCli.sh -timeout 5000 -server hadoop-node03[:2181]

参数 说明

-timeout 连接超时时长

-server 需要连接的服务器地址默认端口是2181,可以省略

image.png3.数据操作

3.1zookeeper的数据存储结构

   层次化的目录结构,命名符合常规文件系统规范

   每个节点在zookeeper中叫做znode,并且有一个唯一的路径标识

   节点znode可以包含数据和子节点(但是EPHEMERAL类型的节点不能有子节点)

   客户端应用可以在节点上设置监听器

image.png

3.2节点的类型

1).znode有两种类型:

短暂性(ephemeral)(断开连接自己删除)

持久性(persistent)(断开连接不删除)

2).znode有四种形式的目录节点(默认是persistent)如下

序号 节点类型 描述

1 PERSISTENT 持久节点

2 PERSISTENT_SEQUENTIAL 持久有序节点

3 EPHEMERAL 短暂节点

4 EPHEMERAL_SEQUENTIAL 短暂有序节点

   创建znode时设置顺序标识,znode名称后会附加一个值,顺序号是一个单调递增的计数器,有父节点维护

   在分布式系统中,顺序号可以被用于为所有的事件进行全局排序,这样客户端可以通过顺序号推断事件的顺序

3.3命令说明

zookeeper中的常用命令不是很多,我们可以通过help命令来查看

[zk: hadoop-node03(CONNECTED) 0] help
ZooKeeper -server host:port cmd args
  stat path [watch]
  set path data [version]
  ls path [watch]
  delquota [-n|-b] path
  ls2 path [watch]
  setAcl path acl
  setquota -n|-b val path
  history 
  redo cmdno
  printwatches on|off
  delete path [version]
  sync path
  listquota path
  rmr path
  get path [watch]
  create [-s] [-e] path data acl
  addauth scheme auth
  quit 
  getAcl path
  close 
  connect host:port

命令说明

序号 命令 说明

1 connect 在连接服务器的情况连接到其他节点上去

2 close 关闭和服务器的连接

3 ls 用于获取某个节点下的节点信息

4 ls2 ls2命令是ls命令的增强版,比ls命令多输出本节点信息

5 create 用于创建节点,其中-s为顺序节点,-e临时节点

6 delete 只能删除没有子节点的节点

7 rmr 可以删除有子节点的节点

8 get 用户获取节点信息

9 stat 查看节点的状态

10 set 设置节点的数据

11 listquota listquota命令用于显示配额,zookeeper的quota并没有实际的限制作用,超出了也只是打印WARN级别日志

12 setquota setquota命令用于设置节点个数以及数据长度的配额

13 delquota 用于删除配额,-n为子节点个数,-b为节点数据长度,如:delquota –n 2

14 history 用于列出最近的命令历史,可以和redo配合使用

15 redo 命令用于再次执行某个命令

16 addauth 用于节点认证,使用方式:如addauth digest username:password

17 setAcl setAcl命令用于设置节点Acl

Acl由三部分构成:

1为scheme,2为user,3为permission,一般情况下表示为scheme: id :permissions

18 getAcl 获取节点的Acl,如getAcl /node1

19 sync 用于强制同步,由于请求在半数以上的zk server上生效就表示此请求生效,那么就会有一些zk server上的数据是旧的。sync命令就是强制同步所有的更新操作

20 printwatchers 用于设置和显示监视状态,值为on或则off

21 quit 退出客户端

3.4常用操作

ls命令

 ls命令是用来查看某个节点下的子节点信息,zookeeper的根节点是’/’,在初始的时候’/'下是没有任何子节点的

[zk: hadoop-node01(CONNECTED) 5] ls
[zk: hadoop-node01(CONNECTED) 6] 

创建几个节点再查看:

[zk: hadoop-node01(CONNECTED) 8] create /node1 node1
Created /node1
[zk: hadoop-node01(CONNECTED) 9] create /node2 node2
Created /node2
[zk: hadoop-node01(CONNECTED) 10] ls /
[node2, zookeeper, node1]

ls2

 ls2命令是ls命令的增强版,比ls命令多输出本节点信息

[zk: hadoop-node01(CONNECTED) 11] ls2 /
[node2, zookeeper, node1]
cZxid = 0x0
ctime = Thu Jan 01 08:00:00 CST 1970
mZxid = 0x0
mtime = Thu Jan 01 08:00:00 CST 1970
pZxid = 0x30000000c
cversion = 1
dataVersion = 0
aclVersion = 0
ephemeralOwner = 0x0
dataLength = 0
numChildren = 3

节点的信息描述在下面的get命令中描述

get命令

 get命令查看节点的信息

[zk: hadoop-node01(CONNECTED) 11] ls2 /
[node2, zookeeper, node1]
cZxid = 0x0
ctime = Thu Jan 01 08:00:00 CST 1970
mZxid = 0x0
mtime = Thu Jan 01 08:00:00 CST 1970
pZxid = 0x30000000c
cversion = 1
dataVersion = 0
aclVersion = 0
ephemeralOwner = 0x0
dataLength = 0
numChildren = 3

节点信息说明

序号 属性 说明

1 cZxid Znode创建的事务id

2 ctime 节点创建时间

3 mZxid Znode被修改的事务id,即每次对znode的修改都会更新mZxid

4 mtime 节点最近一次更新的时间

5 pZxid 是与该节点的子节点(或该节点)的最近一次 创建 / 删除 的时间戳对应

6 cversion 子节点数据更新次数

7 dataVersion 本节点数据更新次数

8 aclVersion 节点ACL(授权信息)的更新次数

9 ephemeralOwner 如果该节点为临时节点,ephemeralOwner值表示与该节点绑定的session id. 如果该节点不是临时节点,ephemeralOwner值为0

10 dataLength 节点数据长度

11 numChildren 子节点个数

zxid特别说明

 对于zk来说,每次的变化都会产生一个唯一的事务id,zxid(ZooKeeper Transaction Id)。通过zxid,可以确定更新操作的先后顺序。例如,如果zxid1小于zxid2,说明zxid1操作先于zxid2发生。需要指出的是,zxid对于整个zk都是唯一的,即使操作的是不同的znode。

create命令

 create命令用户创建ZNode节点。

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

参数 说明

-s 有序

-e 短暂

path 节点路径及名称

data 节点保存的数据

acl 节点的权限

创建永久节点

[zk: hadoop-node01(CONNECTED) 17] create /node1/node01  node01
Created /node1/node01
[zk: hadoop-node01(CONNECTED) 19] get /node1/node01
node01
cZxid = 0x30000000d
ctime = Tue Feb 26 06:00:43 CST 2019
mZxid = 0x30000000d
mtime = Tue Feb 26 06:00:43 CST 2019
pZxid = 0x30000000d
cversion = 0
dataVersion = 0
aclVersion = 0
ephemeralOwner = 0x0
dataLength = 6
numChildren = 0
[zk: hadoop-node01(CONNECTED) 20] ls /node1       
[node01]

创建永久有序节点

[zk: hadoop-node01(CONNECTED) 21] create /node1/node02 node02
Created /node1/node02
[zk: hadoop-node01(CONNECTED) 22] delete /node1/node02          
[zk: hadoop-node01(CONNECTED) 23] create -s /node1/node02 node02
Created /node1/node020000000002
[zk: hadoop-node01(CONNECTED) 24] get /node1/node020000000002
node02
cZxid = 0x300000010
ctime = Tue Feb 26 06:02:44 CST 2019
mZxid = 0x300000010
mtime = Tue Feb 26 06:02:44 CST 2019
pZxid = 0x300000010
cversion = 0
dataVersion = 0
aclVersion = 0
ephemeralOwner = 0x0
dataLength = 6
numChildren = 0

永久节点即使我们断开连接后再次连接后也不会消失

[zk: hadoop-node01(CONNECTED) 25] close
2019-02-25 23:07:21,968 [myid:] - INFO  [main:ZooKeeper@684] - Session: 0x16920a6e1e90000 closed
[zk: hadoop-node01(CLOSED) 26] 2019-02-25 23:07:21,968 [myid:] - INFO  [main-EventThread:ClientCnxn$EventThread@519] - EventThread shut down for session: 0x16920a6e1e90000
[zk: hadoop-node01(CLOSED) 26] connect 
2019-02-25 23:07:33,387 [myid:] - INFO  [main:ZooKeeper@438] - Initiating client connection, connectString=hadoop-node01 sessionTimeout=5000 watcher=org.apache.zookeeper.ZooKeeper
Main$MyWatcher@383534aa[zk: hadoop-node01(CONNECTING) 27] 2019-02-25 23:07:33,401 [myid:] - INFO  [main-SendThread(hadoop-node01:2181):ClientCnxn$SendThread@1032] - Opening socket connection to server h
adoop-node01/192.168.88.121:2181. Will not attempt to authenticate using SASL (unknown error)2019-02-25 23:07:33,406 [myid:] - INFO  [main-SendThread(hadoop-node01:2181):ClientCnxn$SendThread@876] - Socket connection established to hadoop-node01/192.168.88.121:2181, initi
ating session2019-02-25 23:07:33,423 [myid:] - INFO  [main-SendThread(hadoop-node01:2181):ClientCnxn$SendThread@1299] - Session establishment complete on server hadoop-node01/192.168.88.121:21
81, sessionid = 0x16920a6e1e90001, negotiated timeout = 5000
WATCHER::
WatchedEvent state:SyncConnected type:None path:null
[zk: hadoop-node01(CONNECTED) 27] ls /node1
[node020000000002, node01]

创建短暂节点

[zk: hadoop-node01(CONNECTED) 28] create -e /node1/node03 node03
Created /node1/node03
[zk: hadoop-node01(CONNECTED) 29] ls /node1
[node020000000002, node01, node03]

创建短暂有序节点

[zk: hadoop-node01(CONNECTED) 30] create -s -e /node1/node03 node04
Created /node1/node030000000004
[zk: hadoop-node01(CONNECTED) 31] ls /node1                        
[node020000000002, node01, node03, node030000000004]
[zk: hadoop-node01(CONNECTED) 32] 

注意

   短暂节点不能创建子节点

   创建短暂节点的客户端断开连接后数据会被自动删除

[zk: hadoop-node01(CONNECTED) 31] ls /node1                        
[node020000000002, node01, node03, node030000000004]
[zk: hadoop-node01(CONNECTED) 32] create -e /node1/node03/aaa aaa
Ephemerals cannot have children: /node1/node03/aaa
[zk: hadoop-node01(CONNECTED) 33] close
2019-02-25 23:10:35,224 [myid:] - INFO  [main:ZooKeeper@684] - Session: 0x16920a6e1e90001 closed
[zk: hadoop-node01(CLOSED) 34] 2019-02-25 23:10:35,224 [myid:] - INFO  [main-EventThread:ClientCnxn$EventThread@519] - EventThread shut down for session: 0x16920a6e1e90001
[zk: hadoop-node01(CLOSED) 34] connect
2019-02-25 23:10:44,441 [myid:] - INFO  [main:ZooKeeper@438] - Initiating client connection, connectString=hadoop-node01 sessionTimeout=5000 watcher=org.apache.zookeeper.ZooKeeper
Main$MyWatcher@6bc168e5[zk: hadoop-node01(CONNECTING) 35] 2019-02-25 23:10:44,446 [myid:] - INFO  [main-SendThread(hadoop-node01:2181):ClientCnxn$SendThread@1032] - Opening socket connection to server h
adoop-node01/192.168.88.121:2181. Will not attempt to authenticate using SASL (unknown error)2019-02-25 23:10:44,448 [myid:] - INFO  [main-SendThread(hadoop-node01:2181):ClientCnxn$SendThread@876] - Socket connection established to hadoop-node01/192.168.88.121:2181, initi
ating session2019-02-25 23:10:44,464 [myid:] - INFO  [main-SendThread(hadoop-node01:2181):ClientCnxn$SendThread@1299] - Session establishment complete on server hadoop-node01/192.168.88.121:21
81, sessionid = 0x16920a6e1e90002, negotiated timeout = 5000
WATCHER::
WatchedEvent state:SyncConnected type:None path:null
[zk: hadoop-node01(CONNECTED) 35] ls /node1
[node020000000002, node01]

set命令

 set命令用来修改节点存储的信息

[zk: hadoop-node01(CONNECTED) 39] set /node1/node01  bbbb
cZxid = 0x30000000d
ctime = Tue Feb 26 06:00:43 CST 2019
mZxid = 0x300000018
mtime = Tue Feb 26 06:10:45 CST 2019
pZxid = 0x30000000d
cversion = 0
dataVersion = 1
aclVersion = 0
ephemeralOwner = 0x0
dataLength = 4
numChildren = 0
[zk: hadoop-node01(CONNECTED) 40] get /node1/node01
bbbb
cZxid = 0x30000000d
ctime = Tue Feb 26 06:00:43 CST 2019
mZxid = 0x300000018
mtime = Tue Feb 26 06:10:45 CST 2019
pZxid = 0x30000000d
cversion = 0
dataVersion = 1
aclVersion = 0
ephemeralOwner = 0x0
dataLength = 4
numChildren = 0

delete命令

 delete命令用来删除节点,但是不能删除非空的节点

[zk: hadoop-node01(CONNECTED) 41] delete /node1
Node not empty: /node1
[zk: hadoop-node01(CONNECTED) 42] create /node3 node3
Created /node3
[zk: hadoop-node01(CONNECTED) 43] delete /node3
[zk: hadoop-node01(CONNECTED) 44] ls /
[node2, zookeeper, node1]

rmr命令

 rmr命令也是用来删除节点的,而且rmr命令可以删除非空的节点。

[zk: hadoop-node01(CONNECTED) 45] ls /node1
[node020000000002, node01]
[zk: hadoop-node01(CONNECTED) 46] rmr /node1
[zk: hadoop-node01(CONNECTED) 47] ls /
[node2, zookeeper]

stat命令

 stat命令查看节点的状态信息

[zk: hadoop-node01(CONNECTED) 49] stat /node2
cZxid = 0x30000000c
ctime = Tue Feb 26 05:38:33 CST 2019
mZxid = 0x30000000c
mtime = Tue Feb 26 05:38:33 CST 2019
pZxid = 0x30000000c
cversion = 0
dataVersion = 0
aclVersion = 0
ephemeralOwner = 0x0
dataLength = 5
numChildren = 0

close命令

 关闭和服务器的连接

[zk: hadoop-node01(CONNECTED) 50] close
2019-02-25 23:24:54,024 [myid:] - INFO  [main:ZooKeeper@684] - Session: 0x16920a6e1e90002 closed
[zk: hadoop-node01(CLOSED) 51] 2019-02-25 23:24:54,025 [myid:] - INFO  [main-EventThread:ClientCnxn$EventThread@519] - EventThread shut down for session: 0x16920a6e1e90002
[zk: hadoop-node01(CLOSED) 51] 

connect命令

 再次连接服务器

[zk: hadoop-node03(CONNECTED) 0] close
2019-02-25 23:25:29,318 [myid:] - INFO  [main:ZooKeeper@684] - Session: 0x3692632cde90001 closed
[zk: hadoop-node03(CLOSED) 1] 2019-02-25 23:25:29,321 [myid:] - INFO  [main-EventThread:ClientCnxn$EventThread@519] - EventThread shut down for session: 0x3692632cde90001
[zk: hadoop-node03(CLOSED) 1] connect hadoop-node02:2181
2019-02-25 23:25:42,251 [myid:] - INFO  [main:ZooKeeper@438] - Initiating client connection, connectString=hadoop-node02:2181 sessionTimeout=5000 watcher=org.apache.zookeeper.ZooK
eeperMain$MyWatcher@5d22bbb7[zk: hadoop-node02:2181(CONNECTING) 2] 2019-02-25 23:25:42,263 [myid:] - INFO  [main-SendThread(hadoop-node02:2181):ClientCnxn$SendThread@1032] - Opening socket connection to serv
er hadoop-node02/192.168.88.122:2181. Will not attempt to authenticate using SASL (unknown error)2019-02-25 23:25:42,275 [myid:] - INFO  [main-SendThread(hadoop-node02:2181):ClientCnxn$SendThread@876] - Socket connection established to hadoop-node02/192.168.88.122:2181, initi
ating session2019-02-25 23:25:42,286 [myid:] - INFO  [main-SendThread(hadoop-node02:2181):ClientCnxn$SendThread@1299] - Session establishment complete on server hadoop-node02/192.168.88.122:21
81, sessionid = 0x26924b577110003, negotiated timeout = 5000
WATCHER::
WatchedEvent state:SyncConnected type:None path:null
[zk: hadoop-node02:2181(CONNECTED) 2] 

quit命令

 退出客户端连接

[zk: hadoop-node03(CONNECTED) 0] quit
Quitting...
2019-02-25 23:26:25,437 [myid:] - INFO  [main-EventThread:ClientCnxn$EventThread@519] - EventThread shut down for session: 0x3692632cde90002
2019-02-25 23:26:25,438 [myid:] - INFO  [main:ZooKeeper@684] - Session: 0x3692632cde90002 closed
[root@hadoop-node02 ~]# 

zookeeper中的事件监听

 在刚开始的时候我们介绍过zookeeper的两个核心功能是存储和监听,通过上面的命令我们已经可以对数据做存储操作了,接下来看下监听操作

1.数据改变的监听

监听的是节点中的内容改变了就会触发,针对的是具体的某个节点的信息,如下:

设置对某个节点内容改变的监听

[zk: localhost:2181(CONNECTED) 0] get /node2 watch
node2
cZxid = 0x30000000c
ctime = Tue Feb 26 05:38:33 CST 2019
mZxid = 0x30000000c
mtime = Tue Feb 26 05:38:33 CST 2019
pZxid = 0x30000000c
cversion = 0
dataVersion = 0
aclVersion = 0
ephemeralOwner = 0x0
dataLength = 5
numChildren = 0

在其他节点上修改node2的内容观察效果

image.pngimage.png

但是要注意监听效果只会触发一次!!!

2.子节点改变的监听

给node2节点绑定子节点改变的监听器

[zk: localhost:2181(CONNECTED) 2] ls /node2 watch
[]

然后在node2下创建新的子节点观察效果

image.pngimage.png

同样此监听器也只会触发一次!!!

好了常用的命令就介绍到此,下篇文章介绍节点的监听


相关实践学习
基于MSE实现微服务的全链路灰度
通过本场景的实验操作,您将了解并实现在线业务的微服务全链路灰度能力。
相关文章
|
3月前
|
Shell
Zookeeper常用命令
Zookeeper常用命令
54 1
|
4月前
|
存储 数据安全/隐私保护
zookeeper 节点介绍及节点常用命令总结
zookeeper 节点介绍及节点常用命令总结
152 4
|
3月前
|
存储 API Apache
【zookeeper 第三篇章】客户端 API
本文介绍了Apache ZooKeeper客户端的一些常用命令及其用法。首先,`create`命令用于创建不同类型的节点并为其赋值,如持久化节点、有序节点及临时节点等。通过示例展示了如何创建这些节点,并演示了创建过程中的输出结果。其次,`ls`命令用于列出指定路径下的所有子节点。接着,`set`命令用于更新节点中的数据,可以指定版本号实现乐观锁机制。
35 0
|
1月前
|
分布式计算 Java Hadoop
Hadoop-30 ZooKeeper集群 JavaAPI 客户端 POM Java操作ZK 监听节点 监听数据变化 创建节点 删除节点
Hadoop-30 ZooKeeper集群 JavaAPI 客户端 POM Java操作ZK 监听节点 监听数据变化 创建节点 删除节点
62 1
|
1月前
|
分布式计算 监控 Hadoop
Hadoop-29 ZooKeeper集群 Watcher机制 工作原理 与 ZK基本命令 测试集群效果 3台公网云服务器
Hadoop-29 ZooKeeper集群 Watcher机制 工作原理 与 ZK基本命令 测试集群效果 3台公网云服务器
42 1
|
2月前
|
负载均衡 API 数据安全/隐私保护
Zookeeper的客户端-原生的API
Zookeeper的客户端-原生的API
|
2月前
|
存储 运维 监控
zookeeper的命令
zookeeper的命令
|
4月前
|
API
【想进大厂还不会阅读源码】ShenYu源码-替换ZooKeeper客户端
ShenYu源码阅读。相信大家碰到源码时经常无从下手,不知道从哪开始阅读😭。我认为有一种办法可以解决大家的困扰!至此,我们发现自己开始从大量堆砌的源码中脱离开来😀。ShenYu是一个异步的,高性能的,跨语言的,响应式的 API 网关。
|
5月前
|
Shell 虚拟化
分布式系统详解--框架(Zookeeper-基本shell命令)
分布式系统详解--框架(Zookeeper-基本shell命令)
53 1
|
6月前
|
Java API Apache
ZooKeeper【基础 03】Java 客户端 Apache Curator 基础 API 使用举例(含源代码)
【4月更文挑战第11天】ZooKeeper【基础 03】Java 客户端 Apache Curator 基础 API 使用举例(含源代码)
74 11
下一篇
无影云桌面