Apache ZooKeeper
zookeeper.apache.org
对大数据组件的协调、管理。
生产的组件都会组HA保证高可用,如hdfs的两个NN节点可以交给zk实现主备切换。而zk自己也要组HA。
主备架构需要解决的问题:
- 选举
- 通信、心跳
- 超时、下线
部署
官网或 archive.apache.org/dist/ 都可以下载压缩包。
解压后:
- bin:
zkCli.sh(客户端的命令)、zkServer.sh(服务端命令) - conf:
// 从模板复制一份配置文件 cp zoo_sample.cfg zoo.cfg // tickTime=2000 // 心跳的时间[毫秒] (保持默认) initLimit=10 // 主从 Leader 和 Follower 初始化通信的时长 (保持默认) syncLimit=5 // 主从数据同步的时长 (保持默认) dataDir=... // zk数据存放的目录 (需要改) clientPort=2181 // 客户端通信端口 (保持默认)
服务端启动
配置环境变量和path。
查看 zkServer.sh 的命令帮助
]$ zkServer.sh
Usage: ./zkServer.sh [--config <conf-dir>] {start|start-foreground|stop|version|restart|status|print-cmd}
那么启动的命令:
]$ zkServer.sh start
but:
[liqiang@Gargantua bin]$ zkServer.sh start
ZooKeeper JMX enabled by default
Using config: /home/liqiang/app/zookeeper/bin/../conf/zoo.cfg
Starting zookeeper ... FAILED TO START
接着去找一下有没有日志信息…在 logs下有一个
Error: Could not find or load main class org.apache.zookeeper.server.quorum.QuorumPeerMain
因为我下载的是xx.tar.gz,回顾下一些知识
XX-bin.tar.gz // 解开就能用,包含的是二进制程序和一些配置;
XX.tar.gz // 解开是完整开发目录,需要去编译才能安装使用。
重新下载解压以及环境变量后再次启动。再次失败,继续查看 logs下的日志发现是8080端口占用。
打开zoo.cfg,添加服务端端口配置
admin.serverPort=[没被使用的端口]
成功启动。启动后有一个进程:QuorumPeerMain
zkServer.sh status // 查看启动状态 standalone
ZooKeeper JMX enabled by default
Using config: /home/liqiang/app/zookeeper/bin/../conf/zoo.cfg
Client port found: 2181. Client address: localhost. Client SSL: false.
Mode: standalone
去到刚才设置的dataDir
[liqiang@Gargantua zookeeper]$ cd ~/tmp/zookeeper
[liqiang@Gargantua zookeeper]$ ll
total 8
drwxrwxr-x 2 liqiang liqiang 4096 Apr 17 21:28 version-2
-rw-rw-r-- 1 liqiang liqiang 5 Apr 17 21:28 zookeeper_server.pid // 保存了启动zkServer的进程号
// 使用 tree命令查看目录结构 【yum -y install tree】
[liqiang@Gargantua zookeeper]$ tree
.
├── version-2
│ ├── log.1
│ ├── log.3
│ ├── snapshot.0
│ └── snapshot.2
└── zookeeper_server.pid
客户端启动
再打开一个shell,启动客户端去连接服务端。
zkCli.sh
Connecting to localhost:2181
...
Welcome to ZooKeeper!
JLine support is enabled
[zk: localhost:2181(CONNECTED) 0] // 可以命令操作客户端了
[zk: localhost:2181(CONNECTED) 0] help // 查看客户端命令帮助
close
config [-c] [-w] [-s]
connect host:port
create [-s] [-e] [-c] [-t ttl] path [data] [acl] // 创建节点 -s有序,根据节点名字自动加顺序号;-e临时;-c
delete [-v version] path // 删除 [可删除指定版本]
deleteall path [-b batch size] // 删除 all
delquota [-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
set [-s] [-v version] path data // 修改节点的数据
setAcl [-s] [-v version] [-R] path acl
setquota -n|-b val path
stat [-w] path // 对path进行监听(只能监听一个操作..监听到了就会失效)
启动客户端后会在服务端增加一个进程:ZooKeeperMain
ZK的数据
节点
临时节点:作用在session中,且不能创建子节点。
永久节点:一直存在,除非手工删除。
每个节点都有版本号,znode下可以有数据,也可以有子znode。
回到客户端。
/* 在zk中也是以/ 作为根节点,其下的数据也有层级的树状结构 */
[zk: localhost:2181(CONNECTED) 0] ls /
[zookeeper]
[zk: localhost:2181(CONNECTED) 3] ls /zookeeper
[config, quota]
[zk: localhost:2181(CONNECTED) 3] stat /zookeeper/quota # 查看这个节点的状态
cZxid = 0x0 // zk transcation id
ctime = Thu Jan 01 08:00:00 CST 1970 // 创建时间
mZxid = 0x0 // 修改后的 Zxid
mtime = Thu Jan 01 08:00:00 CST 1970
pZxid = 0x0
cversion = 0
dataVersion = 0 // znode中数据的版本号。数据执行过set操作后,这个版本号就会改变
aclVersion = 0
ephemeralOwner = 0x0 // 是否是临时节点 [0x0 就是永久,一长串的是临时]
dataLength = 0 // 数据长度
numChildren = 0 // 子节点的数量
// 创建永久节点 (不能级联创建多级节点 /a/b/c)
creat /ruoze ruoze-data
// 创建临时节点
creat -e /tmp tmp-data
// 查看节点的数据
get /ruoze
// 修改节点的数据 指定版本号:[-v version]
set /ruoze new-data
监听器,一旦发生变化就能自动感知。
只能监听一次。且不能监听到子节点中的数据的改变(可以监听子节点的创建删除)。
// 对 /zuoze这个节点设置监听
set -w /ruoze
四字命令(服务端)
https://zookeeper.apache.org/doc/current/zookeeperAdmin.html /The Four Letter Words
zookeeper.4lw.commands.whitelist:3.4.10中的新增功能:此属性包含以逗号分隔的四个字母单词命令的列表。引入它是为了对ZooKeeper可执行的命令集提供精细的控制,因此用户可以在必要时关闭某些命令。
conf : New in 3.3.0: Print details about serving configuration.
cons : New in 3.3.0: List full connection/session details for all clients connected to this server. Includes information on numbers of packets received/sent, session id, operation latencies, last operation performed, etc...
crst : New in 3.3.0: Reset connection/session statistics for all connections.
dump : Lists the outstanding sessions and ephemeral nodes.
envi : Print details about serving environment
ruok : Tests if the server is running in a non-error state. When the whitelist enables ruok, the server will respond with imok if it is running, otherwise it will not respond at all. When ruok is disabled, the server responds with: "ruok is not executed because it is not in the whitelist." A response of "imok" does not necessarily indicate that the server has joined the quorum, just that the server process is active and bound to the specified client port. Use "stat" for details on state wrt quorum and client connection information.
srst : Reset server statistics.
srvr : New in 3.3.0: Lists full details for the server.
stat : Lists brief details for the server and connected clients.
wchs : New in 3.3.0: Lists brief information on watches for the server.
wchc : New in 3.3.0: Lists detailed information on watches for the server, by session. This outputs a list of sessions(connections) with associated watches (paths). Note, depending on the number of watches this operation may be expensive (ie impact server performance), use it carefully.
dirs : New in 3.5.1: Shows the total size of snapshot and log files in bytes
...
// 查看状态信息
echo stat | nc gargantua 2181
如果报错: stat is not executed because it is not in the whitelist.
在bin目录下的zkServer.sh脚本中添加如下代码,注意不是随意位置,要在ZOOMAIN赋值语句的后边。参考https://blog.csdn.net/legendaryhaha/article/details/110679604
ZOOMAIN="-Dzookeeper.4lw.commands.whitelist=* ${ZOOMAIN}"
// 测试服务是否正常运行
echo ruok | nc gargantua 2181
imok
// 描述每个session中节点
echo dump | nc gargantua 2181
ZookeeperAPI
ZK02
<!-- 原生zookeeper的API -->
<dependency>
<groupId>org.apache.zookeeper</groupId>
<artifactId>zookeeper</artifactId>
<version>3.6.3</version>
</dependency>
原生zookeeper的API并不好用,如不支持节点级联创建等…
使用 Curator 会更简单 curator.apache.org
<!-- curator 操作zookeeper的开源框架 -->
<dependency>
<groupId>org.apache.curator</groupId>
<artifactId>curator-framework</artifactId><!-- 已经依赖了zookeeper -->
<version>5.1.0</version>
</dependency>
<dependency>
<groupId>org.apache.curator</groupId>
<artifactId>curator-recipes</artifactId>
<version>5.1.0</version>
</dependency>