Zookeeper操作

Zookeeper 安装教程

centos7 安装zookeeper
https://www.cnblogs.com/zhiyouwu/p/11546097.html

zookeeper安装教程
https://www.jianshu.com/p/881c0ba6adcd

zookeeper集群

https://www.cnblogs.com/zjfjava/p/9215374.html

zookeeper连接客户端

https://www.cnblogs.com/LiZhiW/p/4923693.html#:~:text=zookeeper%E7%9A%84%E5%B8%B8%E7%94%A8%E5%AE%A2%E6%88%B7%E7%AB%AF%E6%9C%893%E7%A7%8D%EF%BC%8C%E5%88%86%E5%88%AB%E6%98%AF%EF%BC%9A%20zookeeper%E5%8E%9F%E7%94%9F%E7%9A%84%E3%80%81A%20pache%20Curator%E3%80%81%E5%BC%80%E6%BA%90%E7%9A%84zkclient%EF%BC%8C%E4%B8%8B%E9%9D%A2%E5%88%86%E5%88%AB%E5%AF%B9%E4%BB%8B%E7%BB%8D%E5%AE%83%E4%BB%AC%EF%BC%9A.%20zookeeper%E8%87%AA%E5%B8%A6%E7%9A%84%E5%AE%A2%E6%88%B7%E7%AB%AF%E6%98%AF%E5%AE%98%E6%96%B9%E6%8F%90%E4%BE%9B%E7%9A%84%EF%BC%8C%E6%AF%94%E8%BE%83%E5%BA%95%E5%B1%82%E3%80%81%E4%BD%BF%E7%94%A8%E8%B5%B7%E6%9D%A5%E5%86%99%E4%BB%A3%E7%A0%81%E9%BA%BB%E7%83%A6%E3%80%81%E4%B8%8D%E5%A4%9F%E7%9B%B4%E6%8E%A5%E3%80%82.%20A%20pache,Curator%E6%98%AF%20A%20pache%E7%9A%84%E5%BC%80%E6%BA%90%E9%A1%B9%E7%9B%AE%EF%BC%8C%E5%B0%81%E8%A3%85%E4%BA%86zookeeper%E8%87%AA%E5%B8%A6%E7%9A%84%E5%AE%A2%E6%88%B7%E7%AB%AF%EF%BC%8C%E4%BD%BF%E7%94%A8%E7%9B%B8%E5%AF%B9%E7%AE%80%E4%BE%BF%EF%BC%8C%E6%98%93%E4%BA%8E%E4%BD%BF%E7%94%A8%E3%80%82.%20zkclient%E6%98%AF%E5%8F%A6%E4%B8%80%E4%B8%AA%E5%BC%80%E6%BA%90%E7%9A%84ZooKeeper%E5%AE%A2%E6%88%B7%E7%AB%AF%EF%BC%8C%E5%85%B6%E5%9C%B0%E5%9D%80%EF%BC%9A%20https%3A%2F%2Fgithub.com%2Fadyliu%2Fzkclient%20%E7%94%9F%E4%BA%A7%E7%8E%AF%E5%A2%83%E4%B8%8D%E6%8E%A8%E8%8D%90%20

注意:zookeeper客户端共有三个:

1、zk自带客服端
2、curator apache贡献客户端
3、zkClient 自定义客户端

zookeeper可视化客户端

zookeeper可视化客户端:ZooInspector

https://issues.apache.org/jira/secure/attachment/12436620/ZooInspector.zip

java -jar zookeeper-dev-ZooInspector.jar

Zookeeper原理

zookeeper通信协议jute

Jute是Zookeeper底层序列化组件。序列化可以把在程序中运行的对象或则基本数据类型对应的值转成特殊的格式进行网络传输或则磁盘保存(数据库或则磁盘文件中),需要使用的时候再通过反序列化把数据读入到程序中。

序列化与安装没有必然关系,不过可以通过序列化避免原数据结构直接暴露在网络中。不过公开的序列化和反序列化基本上没有保密性可言。

通信协议
基于TCP/IP协议,Zookeeper实现了自己的通信协议。

1.请求协议

在这里插入图片描述

从zookeeper.jute中可知RequestHeader包含了xid和type,xid用于记录客户端请求发起的先后序号,用来确保单个客户端请求的响应顺序,type代表请求的操作类型。

不同的请求类型请求体不同。

2.响应协议
在这里插入图片描述

xid与请求头中的xid一致,zxid表示Zookeeper服务器上当前最新的事务ID,err则是一个错误码,表示当请求处理过程出现异常情况时,就会在错误码中标识出来。

zkCli.sh 命令行操作

ZooKeeper command line tool zkCli.sh
Keywords: Linux Zookeeper

ZooKeeper provides a very simple command-line client, zkCli, under the bin directory of the ZooKeeper installation directory.

[root@s1 zk]# ls /usr/local/zookeeper/bin/
zkCleanup.sh zkCli.sh zkEnv.sh zkServer.sh
First connect to a ZooKeeper instance that has been started. For example:

zkCli.sh -server localhost:2181
A lot of information will be output during the connection. When the connection is successful, it will enter ZooKeeper’s interactive mode:

WatchedEvent state:SyncConnected type:None path:null

[zk: localhost:2181(CONNECTED) 0] # Enter the commands allowed by ZooKeeper here
For example, input help and output help information.

[zk: localhost:2181(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
[zk: localhost:2181(CONNECTED) 1]
There is an increasing number in the interactive command prompt, for example, when the above operation is performed sequentially, it changes from 0 to 1. This number represents the historical number of the command.

You can also use the non-interactive mode of zkCli.sh to perform one-time operations in the following format:

zkCli.sh -server IP:PORT COMMAND
For example, the help command:

zkCli.sh -server localhost:2181 help
ZooKeeper’s commands are rarely simple and can be roughly divided into several types:

create: create a node somewhere in the tree.
delete: delete a node.
Exists: Test whether a node exists.
get data: Read node data.
set data: Write data to a node.
get children: Retrieve a list of child nodes of a node.
sync: Waiting for data to be propagated.
Note that this is only the functional type of commands, and does not mean that they are commands.

For example, create a znode node:

[zk: localhost:2181(CONNECTED) 1] create /zk_test mydata1
Created /zk_test
Where / zk_test is the node name and mydata1 is the data carried by the node.

Create a child node:

[zk: localhost:2181(CONNECTED) 2] create /zk_test/child_node1 “mydata2”
Created /zk_test/child_node1
View the child nodes under a node. Note that there is no recursive retrieval.

[zk: localhost:2181(CONNECTED) 3] ls /
[zookeeper, zk_test]

[zk: localhost:2181(CONNECTED) 4] ls /zk_test
[child_node1]
Get node data, status information:

[zk: localhost:2181(CONNECTED) 6] get /zk_test
mydata1
cZxid = 0x4
ctime = Wed Jun 27 02:05:44 CST 2018
mZxid = 0x4
mtime = Wed Jun 27 02:05:44 CST 2018
pZxid = 0x5
cversion = 1
dataVersion = 0
aclVersion = 0
ephemeralOwner = 0x0
dataLength = 7
numChildren = 1

[zk: localhost:2181(CONNECTED) 7] get /zk_test/child_node1
mydata2
cZxid = 0x5
ctime = Wed Jun 27 02:29:12 CST 2018
mZxid = 0x5
mtime = Wed Jun 27 02:29:12 CST 2018
pZxid = 0x5
cversion = 0
dataVersion = 0
aclVersion = 0
ephemeralOwner = 0x0
dataLength = 7
numChildren = 0
If you just want to get the status information of the node, you can use the stat command:

[zk: localhost:2181(CONNECTED) 8] stat /zk_test
cZxid = 0x4
ctime = Wed Jun 27 02:05:44 CST 2018
mZxid = 0x4
mtime = Wed Jun 27 02:05:44 CST 2018
pZxid = 0x5
cversion = 1
dataVersion = 0
aclVersion = 0
ephemeralOwner = 0x0
dataLength = 7
numChildren = 1
The ls2 command can obtain the child nodes under a given node and the state information of the given node:

[zk: localhost:2181(CONNECTED) 12] ls2 /
[zookeeper, zk_test] # List of child nodes
cZxid = 0x0
ctime = Thu Jan 01 08:00:00 CST 1970
mZxid = 0x0
mtime = Thu Jan 01 08:00:00 CST 1970
pZxid = 0x4
cversion = 0
dataVersion = 0
aclVersion = 0
ephemeralOwner = 0x0
dataLength = 0
numChildren = 2 # Number of child nodes
The set command is used to modify the node:

[zk: localhost:2181(CONNECTED) 13] set /zk_test “mydata2”
cZxid = 0x4
ctime = Wed Jun 27 02:05:44 CST 2018
mZxid = 0x9
mtime = Wed Jun 27 02:42:45 CST 2018
pZxid = 0x7
cversion = 2
dataVersion = 1
aclVersion = 0
ephemeralOwner = 0x0
dataLength = 7
numChildren = 0
Delete can delete nodes. Note that it will not be deleted recursively, so to delete a node containing a child node, you need to delete all the child nodes first.

[zk: localhost:2181(CONNECTED) 21] delete /zk_test
Node not empty: /zk_test

[zk: localhost:2181(CONNECTED) 22] delete /zk_test/child_node1
The history command lists 10 command histories of recent operations and gives the number of each historical command. The redo commands can be re-invoked based on the number of historical commands.

[zk: localhost:2181(CONNECTED) 18] history
8 - stat /zk_test
9 - get /
10 - get child /
11 - get children /
12 - ls2 /
13 - ls /
14 - ls2 /zk_test
15 - get /
16 - help
17 - ls /zk
18 - history
Re-execute the historical order No. 13:

[zk: localhost:2181(CONNECTED) 27] redo 13
[zookeeper, zk_test]
quit command exits ZooKeeper’s interactive command line.

[zk: localhost:2181(CONNECTED) 29] quit
Quitting…

Logo

权威|前沿|技术|干货|国内首个API全生命周期开发者社区

更多推荐