1. 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/var/run/zookeeper/data
dataDir=/Users/userName/Documents/zookeeper/data
dataLogDir=/Users/userName/Documents/zookeeper/logs

# 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

server.1=localhost:2881:3881
server.2=localhost:2882:3882
server.3=localhost:2883:3883

2. 复制zoo.cfg文件命名为zoo2.cfg, 改动dataDir, dataLogDir和clientPort三个参数

dataDir=/Users/userName/Documents/zookeeper/data2
dataLogDir=/Users/userName/Documents/zookeeper/logs2
clientPort=2182

3. 再次复制zoo.cfg文件命令为zoo3.cfg, 改动dataDir, dataLogDir和clientPort三个参数

dataDir=/Users/userName/Documents/zookeeper/data3
dataLogDir=/Users/userName/Documents/zookeeper/logs3
clientPort=2183

4. 在配置的/Users/userName/Documents/zookeeper/目录下创建data, data2, data3, logs, logs2, logs3目录

5. 在data文件夹中创建文件myid, 内容填1 (和server.1=localhost:2881:3881中的1一致即可)

    在data2文件夹中创建文件myid, 内容填2

    在data3文件夹中创建文件myid, 内容填3

注: myid文件中的内容和配置中的server.X的X一致即可

6. 启动三个服务

$ zkServer start zoo.cfg
ZooKeeper JMX enabled by default
Using config: /usr/local/etc/zookeeper/zoo.cfg
Starting zookeeper ... STARTED
$ zkServer start zoo2.cfg
ZooKeeper JMX enabled by default
Using config: /usr/local/etc/zookeeper/zoo2.cfg
Starting zookeeper ... STARTED
$ zkServer start zoo3.cfg
ZooKeeper JMX enabled by default
Using config: /usr/local/etc/zookeeper/zoo3.cfg
Starting zookeeper ... STARTED

7. 通过zkServer status命令查看各自的角色

$ zkServer status zoo.cfg
ZooKeeper JMX enabled by default
Using config: /usr/local/etc/zookeeper/zoo.cfg
Mode: follower
$ zkServer status zoo2.cfg
ZooKeeper JMX enabled by default
Using config: /usr/local/etc/zookeeper/zoo2.cfg
Mode: leader
$ zkServer status zoo3.cfg
ZooKeeper JMX enabled by default
Using config: /usr/local/etc/zookeeper/zoo3.cfg
Mode: follower

可看出, 选举的结果为: server2为leader(master), server1和server3为follower(slave)节点


注意事项:

1. 第4步中的几个文件夹需要提前创建, 否则可能报错

2. 注意cfg中的配置(server.1=localhost:2881:3881), localhost别写错了, 2881端口别写成2181了

3. 其他错误可参见: https://blog.csdn.net/xiewendong93/article/details/50500471

Logo

权威|前沿|技术|干货|国内首个API全生命周期开发者社区

更多推荐