- 模式:集群
- 系统:Linux
- 机器数量:单机使用ip:port模拟多台服务器
三台机器搭建一个ZooKeeper集群
在服务器上安装JDK
安装ZooKeeper
https://mirrors.tuna.tsinghua.edu.cn/apache/zookeeper/stable/
$ tar -zxvf zookeeper-3.4.12.tar.gz -C .
配置zoo.cfg
复制一份新的zoo.cfg
$ mv %your_path%/conf/zoo_sample.cfg zoo.cfg
更新zoo.cfg参数
tickTime=2000
initLimit=10
syncLimit=5
dataDir=/root/soft/zookeeper/data/
dataLogDir=/root/soft/zookeeper/logs/
#由于在单台机器上模拟集群模式,所以clientPort需要不一样
#机器1使用2181
#机器2使用2182
#机器3使用2183
clientPort=2181
server.1=127.0.0.1:2888:3888
server.2=127.0.0.1:2889:3889
server.3=127.0.0.1:2890:3890
在集群模式下,每台机器都需要感知彼此的存在,在配置文件中可按照以下的格式进行配置,每一行代表一个机器配置:
server.id=host:port1:port2
# id: 集群中机器的编号 1~255
# port1: 集合体中的follower用于通信leader的端口
# port2: 集合中的的follower用于选举leader的端口
创建myid文件
在dataDir所配置的目录下,创建一个名为myid的文件,在该文件的第一行写一个数字,和zoo.cfg中当前机器的编号对上
启动服务器
$ %your_path%/bin/zkServer.sh start
注意:在单机下模拟集群模式,需要下载三个ZooKeeper软件,并重复上面的操作,然后再启动ZooKeeper服务器
验证服务器
$ telnet 127.0.0.1 2181
#以下为回显
Trying 127.0.0.1...
Connected to 127.0.0.1.
Escape character is '^]'.
#然后输入stat
stat
#以下为回显
Zookeeper version: 3.4.12-e5259e437540f349646870ea94dc2658c4e44b3b, built on 03/27/2018 03:55 GMT
Clients:
/127.0.0.1:58896[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: standalone
Node count: 4