【java】 搭建zookeeper 集群

zookeeper 集群配置

集群角色规划:

名称 IP 角色
hadoop01 192.168.0.10 zookeeper
hadoop02 192.168.0.11 zookeeper
hadoop03 192.168.0.12 zookeeper

当我们将conf下的 zoo_sample.cfg 文件复制并重命名为 zoo.cfg 文件后,通过 vim 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=/tmp/zookeeper
# 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

server.1=192.168.0.10:2888:3888
server.2=192.168.0.11:2888:3888
server.3=192.168.0.12:2888:3888

参数说明:

参数 说明
tickTime 客户端与服务端或者服务端和服务端之间维持心跳的时间间隔,每隔tickTime时间就会发送一个心跳,通过心跳不仅能够用来监听机器的工作状态,还可以通过心跳来控制follower和Leader的通信时间,默认情况下FL(Follower和Leader)的会话通常是心跳间隔的两倍,单位为毫秒。
initLimit 集群中的follower服务器与Leader服务器之间的初始连接时能容忍的最多心跳数量
syncLimit 急群众的follower服务器与leader服务器之间的请求和回答最多能容忍的心跳数量
dataDir 目录地址,用来存放myid信息和一些版本、日志、服务器唯一ID等信息
server.n=127.0.0.1:2888:3888 n:代表的是一个数字,表示这个服务器的标号
127.0.0.1:IP服务器地址
2888:ZooKeeper服务器之间的通信端口
3888:Leader选举的端口

创建并写入节点标识到 myid 文件:

# hadoop001主机
echo "1" > /usr/local/zookeeper-cluster/data/myid
# hadoop002主机
echo "2" > /usr/local/zookeeper-cluster/data/myid
# hadoop003主机
echo "3" > /usr/local/zookeeper-cluster/data/myid

由于已经配置过环境变量,直接使用下面命令启动即可:

./bin/zkServer.sh start

启动后使用 zkServer.sh status 查看集群各个节点状态。如图所示:三个节点进程均启动成功,并且 hadoop002 为 leader 节点,hadoop001 和 hadoop003 为 follower 节点。

 

参考资料

1. Zookeeper单机环境和集群环境搭建

posted @ 2025-04-01 09:55  苏格拉底的落泪  阅读(33)  评论(0)    收藏  举报