目录
一、环境准备
1、准备两台linux服务器,用来搭建zookeeper集群,ip地址如下
节点 | ip |
node1 | 192.168.72.132 |
node2 | 192.168.72.133 |
node3 | 192.168.72.134 |
2、安装jdk,具体安装方法可看
3、下载zookeeper安装包
下载地址:https://downloads.apache.org/zookeeper/
创建安装路径,示例是安装在local路径下
将zookeeper安装包传到该路径下
解压缩
二、安装zookeeper
1、进入安装目录的conf目录下,复制zoo_sample.cfg为zoo.cfg
2、打开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=/data/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.
#
# https://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.httpHost=0.0.0.0
#metricsProvider.httpPort=7000
#metricsProvider.exportJvmInfo=true#设置集群信息,其中2888为follower和leader交换消息所使用的端口,3888为选举leader所使用的端口,这两个端口可自己设定
server.1=192.168.72.132:2888:3888
server.2=192.168.72.133:2888:3888
server.3=192.168.72.134:2888:3888
3、在数据文件目录dataDir下面创建文件myid ,并且写入服务器信息
此时我们zookeeper集群中的一台就配置好了
4、将该服务器的配置同步到另外两台上面,只需修改每台服务器的myid文件就行,我的分别为1.2.3
三、启动集群
1、依次启动每台zookeeper
启动成功
可以查看日志是否启动成功,日志在安装目录下的logs文件夹中的.out文件
2、查看当前服务器是leader还是follower
./zkServer.sh status
3、通过客户端连接zookeeper集群,集群是一个整体,所以连接一台即可
./zkCli.sh -server 192.168.72.132:2181
四、可能遇到的问题
1、启动集群的时候可能会报java.net.NoRouteToHostException: NoRouteToHostException: 没有到主机的路由,解决方案可见