zookeeper集群安装使用

介绍

  zookeeper的安装模式分为:单机模式,集群模式,伪集群模式。
  伪集群模式就是在单机上模拟集群模式。
  单机模式下的安安装已经记过了。


伪集群模式

安装

下载

  在官网下载zookeeper指定版本,https://zookeeper.apache.org/
  下载指定的压缩包apache-zookeeper-3.7.0-bin.tar.gz。
  将下载的压缩包解压到三个不同的文件夹server1,server2,server3中。

配置

  需要对三个zk实例进行不同配置,主要是配置端口,数据存放地址,通信地址等。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
# The number of milliseconds of each tick
# 服务端与客户端之间的交互基本时间单元
tickTime=2000
# The number of ticks that the initial
# synchronization phase can take
# 允许follower连接到leader之间,并同步到的初始化时间
initLimit=10
# The number of ticks that can pass between
# sending a request and getting an acknowledgement
# Leader服务器与follower服务器之间信息同步允许的最大时间间隔,如果超过次间隔,默认follower服务器与leader服务器之间断开链接
syncLimit=5
# the directory where the snapshot is stored.
# do not use /tmp for storage, /tmp here is just
# example sakes.
# 数据存放路径
dataDir=/Program1/zookeepers/server1/tmp/zookeeper
dataLogDir=/Program1/zookeepers/server1/log/zookeeper
# the port at which the clients will connect
# 启动端口与连接端口
clientPort=2181
# 这里的.后面的数字和下面myid
server.1= 127.0.0.1:2888:3888
server.2= 127.0.0.1:2889:3889
server.3= 127.0.0.1:2890:3890
# 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
# 开启4字命令白名单,zkui会使用
4lw.commands.whitelist=*

  配置完成后建立myid文件,分别在每个实例对应的数据存放文件夹下建立myid文件,没有后缀名,内容填写为1、2、3即可。

启动

  分别启动3个实例。
  未完全启动前会有报错,无法连接到其他实例,不用理会,继续启动即可。

1
2
3
4
5
6
7
8
cd server1
zkServer

cd server2
zkServer

cd server3
zkServer

集群模式

  集群模式需要在不同物理机器上安装,最好在奇数台服务器上安装。
  集群模式是目前最主要的应用场景模式。
  其他配置与伪集群模式的配置相同。