知识回顾:
之前我们学习了【Docker安装部署Zookeeper集群】,以及关于zookeeper的一些面试常考的点(需要学习的请请点击这里看专栏呦),接下我们来学习一下zookeeper客户端常用的命令。
docker exec -it zookeeper-master /bin/bash
docker exec -it zookeeper-node1 /bin/bash
docker exec -it zookeeper-node2 /bin/bash
cd bin
ls
zkCli.sh
zkCli.sh -timeout 5000 -server zookeeper-node1/zookeeper-node2/zookeeper-master(选择要连接的服务器就可以啦)
参数说明:
-timeout 连接超时时长
-server 需要连接的服务器地址默认端口是2181,可以省略
1. PERSISTENT :持久化节点 ,节点创建后会被持久化,只有主动调用delete方法的时候才可以删除节点。
2. PERSISTENT_SEQUENTIAL:持久化序列号持久化节点, 排序节点:创建的节点名称后自动添加序号,如节点名称为"node-",自动添加为"node-1",顺序添加为"node-2"
3. EPHEMERAL:临时节点, 临时节点:节点创建后在创建者超时连接或失去连接的时候,节点会被删除。客户端session超时这类节点就会被自动删除。临时节点下不能存在子节点。
4. EPHEMERAL_SEQUENTIAL:临时排序节点, 排序节点:创建的节点名称后自动添加序号,如节点名称为"node-",自动添加为"node-1",顺序添加为"node-2"
[zk: localhost:2181(CONNECTED) 7] 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
[zk: localhost:2181(CONNECTED) 8]
命令说明 | 解释说明 |
---|---|
connect | 在连接服务器的情况连接到其他节点上去 |
close | 关闭和服务器的连接 |
ls | 用于获取某个节点下的节点信息 |
create | 用于创建节点,其中-s为顺序节点,-e临时节点 |
delete | 只能删除没有子节点的节点 |
deleteall | 可以删除有子节点的节点 |
get | 用户获取节点信息 |
stat | 查看节点的状态 |
set | 设置节点的数据 |
listquota | listquota命令用于显示配额,zookeeper的quota并没有实际的限制作用,超出了也只是打印WARN级别日志 |
setquota | setquota命令用于设置节点个数以及数据长度的配额 |
delquota | 用于删除配额,-n为子节点个数,-b为节点数据长度,如:delquota –n 2 |
history | 用于列出最近的命令历史,可以和redo配合使用 |
redo | 命令用于再次执行某个命令 |
addauth | 用于节点认证,使用方式:如addauth digest username:password |
setAcl | setAcl命令用于设置节点AclAcl由三部分构成:1为scheme,2为user,3为permission,一般情况下表示为scheme: id :permissions |
getAcl | 获取节点的Acl,如getAcl /node1 |
sync | 用于强制同步,由于请求在半数以上的zk server上生效就表示此请求生效,那么就会有一些zk server上的数据是旧的。sync命令就是强制同步所有的更新操作 |
printwatchers | 用于设置和显示监视状态,值为on或则off |
quit | 退出客户端 |
[zk: localhost:2181(CONNECTED) 9] ls /
[node1, node2, node3, zookeeper]
[zk: localhost:2181(CONNECTED) 10]
[zk: localhost:2181(CONNECTED) 10] get /
[zk: localhost:2181(CONNECTED) 11] get
get [-s] [-w] path
[zk: localhost:2181(CONNECTED) 12] get /node1
hellozk
[zk: localhost:2181(CONNECTED) 13] get -s /node1
hellozk
cZxid = 0x100000002
ctime = Wed Oct 19 02:30:20 UTC 2022
mZxid = 0x100000003
mtime = Wed Oct 19 02:33:01 UTC 2022
pZxid = 0x100000006
cversion = 2
dataVersion = 1
aclVersion = 0
ephemeralOwner = 0x0
dataLength = 7
numChildren = 2
[zk: localhost:2181(CONNECTED) 14]
节点中详细信息的说明:
节点中信息 | 解释说明 |
---|---|
cZxid | Znode创建的事务id |
ctime | 节点创建时间 |
mZxid | Znode被修改的事务id,即每次对znode的修改都会更新mZxid |
mtime | 节点最近一次更新的时间 |
pZxid | 是与该节点的子节点(或该节点)的最近一次 创建 / 删除 的时间戳对应 |
cversion | 子节点数据更新次数 |
dataVersion | 本节点数据更新次数 |
aclVersion | 节点ACL(授权信息)的更新次数 |
ephemeralOwner | 如果该节点为临时节点,ephemeralOwner值表示与该节点绑定的session id. 如果该节点不是临时节点,ephemeralOwner值为0 |
dataLength | 节点数据长度 |
numChildren | 子节点个数 |
create [-s] [-e] path data acl
参数配置:
create命令参数配置 | 解释说明 |
---|---|
-s | 有序 |
-e | 短暂 |
path | 节点路径及名称 |
data | 节点保存的数据 |
acl | 节点的权限 |
创建永久节点
[zk: zookeeper-node1(CONNECTED) 63] create /node4/per1 p1
Node does not exist: /node4/per1
[zk: zookeeper-node1(CONNECTED) 64] create /node4
Created /node4
[zk: zookeeper-node1(CONNECTED) 65] create /node4/per p1
Created /node4/per
[zk: zookeeper-node1(CONNECTED) 66] get /node4/per
p1
[zk: zookeeper-node1(CONNECTED) 67] get /node4
null
[zk: zookeeper-node1(CONNECTED) 68] get -s /node/per
org.apache.zookeeper.KeeperException$NoNodeException: KeeperErrorCode = NoNode for /node/per
[zk: zookeeper-node1(CONNECTED) 69] get /node/per
org.apache.zookeeper.KeeperException$NoNodeException: KeeperErrorCode = NoNode for /node/per
[zk: zookeeper-node1(CONNECTED) 70] get -s /node4/per
p1
cZxid = 0x10000001f
ctime = Wed Oct 19 04:03:31 UTC 2022
mZxid = 0x10000001f
mtime = Wed Oct 19 04:03:31 UTC 2022
pZxid = 0x10000001f
cversion = 0
dataVersion = 0
aclVersion = 0
ephemeralOwner = 0x0
dataLength = 2
numChildren = 0
[zk: zookeeper-node1(CONNECTED) 71]
创建永久序列节点
[zk: zookeeper-node1(CONNECTED) 74] create /node5
Created /node5
[zk: zookeeper-node1(CONNECTED) 75] create /node5/s1 s1
Created /node5/s1
[zk: zookeeper-node1(CONNECTED) 76] create -s /node5/s2 s2
Created /node5/s20000000001
[zk: zookeeper-node1(CONNECTED) 77] create -s /node5/s3 s3
Created /node5/s30000000002
[zk: zookeeper-node1(CONNECTED) 78] get -s /node5/s2
org.apache.zookeeper.KeeperException$NoNodeException: KeeperErrorCode = NoNode for /node5/s2
[zk: zookeeper-node1(CONNECTED) 79] get /node5
null
[zk: zookeeper-node1(CONNECTED) 80] get /node5/s2
org.apache.zookeeper.KeeperException$NoNodeException: KeeperErrorCode = NoNode for /node5/s2
[zk: zookeeper-node1(CONNECTED) 81] ls node5
Path must start with / character
[zk: zookeeper-node1(CONNECTED) 82] ls /node5
[s1, s20000000001, s30000000002]
[zk: zookeeper-node1(CONNECTED) 83] get /node5/s20000000001
s2
[zk: zookeeper-node1(CONNECTED) 84] get -s /node5/s20000000001
s2
cZxid = 0x100000025
ctime = Wed Oct 19 04:07:16 UTC 2022
mZxid = 0x100000025
mtime = Wed Oct 19 04:07:16 UTC 2022
pZxid = 0x100000025
cversion = 0
dataVersion = 0
aclVersion = 0
ephemeralOwner = 0x0
dataLength = 2
numChildren = 0
创建临时节点
[zk: zookeeper-node1(CONNECTED) 85] create /node6
Created /node6
[zk: zookeeper-node1(CONNECTED) 86] create -e /node6/node6 6
Created /node6/node6
[zk: zookeeper-node1(CONNECTED) 87] get /node6/node6
6
[zk: zookeeper-node1(CONNECTED) 88]
创建临时序列节点
[zk: zookeeper-node1(CONNECTED) 88] create /node7
Created /node7
[zk: zookeeper-node1(CONNECTED) 89] create -s -e /node7/node7 7
Created /node7/node70000000000
[zk: zookeeper-node1(CONNECTED) 90] get /node7/node70000000000
7
[zk: zookeeper-node1(CONNECTED) 91]
[zk: zookeeper-node1(CONNECTED) 91] create /node7/node7/node
Node does not exist: /node7/node7/node
[zk: zookeeper-node1(CONNECTED) 92]
[zk: zookeeper-node1(CONNECTED) 89] create -s -e /node7/node7 7
Created /node7/node70000000000
[zk: zookeeper-node1(CONNECTED) 90] get /node7/node70000000000
7
[zk: zookeeper-node1(CONNECTED) 94] ls /
[node1, node2, node3, node4, node5, node6, node7, zookeeper]
[zk: zookeeper-node1(CONNECTED) 95] ls /node7
[]
[zk: zookeeper-node1(CONNECTED) 96]
[zk: zookeeper-node1(CONNECTED) 97] ls /
[node1, node2, node3, node4, node5, node6, node7, zookeeper]
[zk: zookeeper-node1(CONNECTED) 98] get /node1
hellozk
[zk: zookeeper-node1(CONNECTED) 99] set /node1 zk
[zk: zookeeper-node1(CONNECTED) 100] get /node1
zk
[zk: zookeeper-node1(CONNECTED) 101]
[zk: zookeeper-node1(CONNECTED) 102] delete /node7
[zk: zookeeper-node1(CONNECTED) 103] ls /
[node1, node2, node3, node4, node5, node6, zookeeper]
[zk: zookeeper-node1(CONNECTED) 104]
[zk: zookeeper-node1(CONNECTED) 112] deleteall /node5
[zk: zookeeper-node1(CONNECTED) 113] ls /
[node1, node2, node3, node4, zookeeper]
[zk: zookeeper-node1(CONNECTED) 114]
[zk: zookeeper-node1(CONNECTED) 114] stat /node3
cZxid = 0x10000000b
ctime = Wed Oct 19 02:39:30 UTC 2022
mZxid = 0x10000000b
mtime = Wed Oct 19 02:39:30 UTC 2022
pZxid = 0x10000000d
cversion = 2
dataVersion = 0
aclVersion = 0
ephemeralOwner = 0x0
dataLength = 0
numChildren = 2
[zk: zookeeper-node1(CONNECTED) 115]
好了,到这里,zookeeper核心功能之数据存储已经学习完成了,还有另外一个核心功能就是监控–watch,下面文章我们将来学习zookeeper的另外一个核心功能之监控。