- 前言
Zookeeper 是基于 JAVA 语言开发,所以部署ZK的服务器须安装JRE,我们预先安装了JDK8。
若出现zk集群中某一台宕机,Zookeeper的选举机制会多数服从少数选举下一个 leader,如果部署zk集群时,数量必须是奇数。
- 下载Zookeeper
下载地址: https://mirrors.tuna.tsinghua.edu.cn/apache/zookeeper/
建议下载 apache-zookeeper-3.X.X-bin.tar.gz ,名字中带有-bin的版本,因为后续需要导入其他中间件配置时比较方便,否则会出现 “无法加载主类”等情况。
演示使用的是 3.6.1版本
- 安装
使用tar -zxf 命令,将下载好的.tar.gz文件解压三份。
使用 mv 命令将 conf/zoo_sample.cfg 配置文件重命名为 zoo.cfg,zk启动时会自动加载 zoo.cfg配置文件。
zoo.cfg 配置如下
# The number of milliseconds of each tick
tickTime=2000
# The number of ticks that the initial
# synchronization phase can take
initLimit=10
# The number of ticks that can pass between
# sending a request and getting an acknowledgement
syncLimit=5
# the directory where the snapshot is stored.
# do not use /tmp for storage, /tmp here is just
# example sakes.
dataDir=/usr/local/zookeeper/data
# the port at which the clients will connect
clientPort=2181
# the maximum number of client connections.
# increase this if you need to handle more clients
#maxClientCnxns=60
#
# Be sure to read the maintenance section of the
# administrator guide before turning on autopurge.
#
# http://zookeeper.apache.org/doc/current/zookeeperAdmin.html#sc_maintenance
#
# The number of snapshots to retain in dataDir
#autopurge.snapRetainCount=3
# Purge task interval in hours
# Set to "0" to disable auto purge feature
#autopurge.purgeInterval=1
## Metrics Providers
#
# https://prometheus.io Metrics Exporter
#metricsProvider.className=org.apache.zookeeper.metrics.prometheus.PrometheusMetricsProvider
#metricsProvider.httpPort=7000
#metricsProvider.exportJvmInfo=true
admin.serverPort=8888
# zookeeper集群配置
# 配置格式
# server.服务编号=服务地址、LF通信端口、选举端口
server.1=127.0.0.1:2888:3888
server.2=127.0.0.1:2882:3882
server.3=127.0.0.1:2883:3883
实际生产环境中zookeeper/conf/zoo.cfg文件是相同的,因为本机演示在同一实例上部署三个zk,所有通过修改端口的方式配置 zk
其他两个zookeeper配置,将 clientPort=2181 改为了 2182、2183。
PS :生产环境无须做此处理,直接copy即可。
根据配置文件中的dataDir创建dada目录,并且在data目录下新建 myid 文件,文件的内容zoo.cfg文件中红 server.X 的值一致。如下
其余两台安装此方式配置。
- 启动
在bin目录中执行 ./zkServer.sh start ,出现 Starting zookeeper...STARTED 日志,即启动成功。
在bin目录中执行 ./zkServer.sh status ,查询当前zk的运行状态,Mode: follower 表示当前zk “从机”。