分布式学习手记05 - ZooKeeper 简单部署及使用
ZooKeeper 版本 3.4.12 & JAVA 版本 1.8.0 & CentOS 版本 7.5.1804
部署
使用最小化安装 CentOS
环境
各节点均需执行
域名解析
# vim /etc/hosts
127.0.0.1 localhost
192.168.52.137 zoo1
192.168.52.138 zoo2
192.168.52.139 zoo3
JAVA
# yum install vim wget epel-release -y
# yum update -y
# yum install java -y
# java -version
openjdk version "1.8.0_171"
OpenJDK Runtime Environment (build 1.8.0_171-b10)
OpenJDK 64-Bit Server VM (build 25.171-b10, mixed mode)
ZooKeeper
此时最新稳定版本为 3.4.12
# cd /opt/
# wget http://apache.claz.org/zookeeper/zookeeper-3.4.12/zookeeper-3.4.12.tar.gz
# tar xvf zookeeper-3.4.12.tar.gz
配置文件
# cd /opt/zookeeper-3.4.12/conf
# cp zoo_sample.cfg zoo.cfg
# vim zoo.cfg
tickTime=2000
initLimit=10
syncLimit=5
dataDir=/var/lib/zookeeper
dataLogDir=/opt/zksync
clientPort=2181
server.1=zoo1:2888:3888
server.2=zoo2:2888:3888
server.3=zoo3:2888:3888
说明
tickTime - 定义集群单位时间长度,单位是 ms ,其他时间参数设为该时长倍数;
initLimit - Leader 等待 Follower 启动,并完成数据同步的时间,此处设为 10 个 tickTime;
syncLimit - Leader 与 Follower 之间心跳检测的时间,可根据网络质量适当调整,此处设为 5 个 tickTime;
dataDir - 集群存储快照文件的目录;
dataLogDir - 集群事务日志文件目录,建议挂载独立高性能磁盘;
clientPort - 对外服务端口,供 Client 端接入访问;
server.X - 集群机器列表,格式为 server.id=host:portA:portB;
注: portA - Follower 与 Leader 通信和数据同步所用端口; portB - Leader 选举投票端口;id 范围为 1~255
各节点创建 dataDir 及 myid 文件
zoo1
# mkdir /var/lib/zookeeper/
# vim /var/lib/zookeeper/myid
1
zoo2
# mkdir /var/lib/zookeeper/
# vim /var/lib/zookeeper/myid
2
zoo3
# mkdir /var/lib/zookeeper/
# vim /var/lib/zookeeper/myid
3
使用
服务端
各节点启动服务
# sh /opt/zookeeper-3.4.12/bin/zkServer.sh start
ZooKeeper JMX enabled by default
Using config: /opt/zookeeper-3.4.12/bin/../conf/zoo.cfg
Starting zookeeper ... STARTED
查看状态
# telnet zoo1 2181
Trying 192.168.52.137...
Connected to zoo1.
Escape character is '^]'.
stat
Zookeeper version: 3.4.12-e5259e437540f349646870ea94dc2658c4e44b3b, built on 03/27/2018 03:55 GMT
Clients:
/192.168.52.137:38970[0](queued=0,recved=1,sent=0)
Latency min/avg/max: 0/0/0
Received: 1
Sent: 0
Connections: 1
Outstanding: 0
Zxid: 0x0
Mode: follower
Node count: 4
Connection closed by foreign host.
控制脚本
# cd /opt/zookeeper-3.4.12/bin/
# ls *.sh
zkCleanup.sh zkCli.sh zkEnv.sh zkServer.sh
zkCleanup - 清理历史数据,包括事务日志和快照数据
zkCli - 简易客户端
zkEnv - 设置环境变量
zkServer - 服务启动、停止和重启
客户端
-
创建
-s 创建顺序节点; -e 创建临时节点; acl 参数用于权限控制;
create [-s] [-e] path data acl
在 ZooKeeper 根节点下创建 /zk-book 节点,数据内容为 123,此处省略权限控制
# sh zkCli.sh Connecting to localhost:2181 ... [zk: localhost:2181(CONNECTED) 0] create /zk-book 123 Created /zk-book
-
读取
ls 命令列出指定节点下的所有子节点,path 表示指定数据节点的节点路径
ls path [watch]
默认有一个 /zookeeper 保留节点
[zk: localhost:2181(CONNECTED) 1] ls / [zk-book, zookeeper] [zk: localhost:2181(CONNECTED) 2] ls /zk-book []
get 命令获取指定节点的数据内容和属性信息
get path [watch]
查看我们创建的节点 /zk-book
[zk: localhost:2181(CONNECTED) 3] get /zk-book 123 cZxid = 0x200000004 ctime = Tue Jun 26 11:26:02 CST 2018 mZxid = 0x200000004 mtime = Tue Jun 26 11:26:02 CST 2018 pZxid = 0x200000004 cversion = 0 dataVersion = 0 aclVersion = 0 ephemeralOwner = 0x0 dataLength = 3 numChildren = 0
-
更新
set 命令,更新 data 内容,version 参数指定本次更新操作基于 ZNode 的哪一个数据版本
set path data [version]
更新后, dataVersion 也自动变化了
[zk: localhost:2181(CONNECTED) 20] set /zk-book 456 cZxid = 0x200000004 ctime = Tue Jun 26 11:26:02 CST 2018 mZxid = 0x200000005 mtime = Tue Jun 26 14:44:20 CST 2018 pZxid = 0x200000004 cversion = 0 dataVersion = 1 aclVersion = 0 ephemeralOwner = 0x0 dataLength = 3 numChildren = 0
-
删除
delete 命令删除指定节点
delete path [version]
[zk: localhost:2181(CONNECTED) 32] delete /zk-book [zk: localhost:2181(CONNECTED) 33] ls / [zookeeper]