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/dataDir dataLogDir=/Users/userName/Documents/zookeeper/dataLogDir # 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文件命名为zoo1.cfg, 改动dataDir, dataLogDir和clientPort三个参数
dataDir=/Users/userName/Documents/zookeeper/dataDir1 dataLogDir=/Users/userName/Documents/zookeeper/dataLogDir1 clientPort=2181
3. 复制zoo.cfg文件命名为zoo2.cfg, 改动dataDir, dataLogDir和clientPort三个参数
dataDir=/Users/userName/Documents/zookeeper/dataDir2 dataLogDir=/Users/userName/Documents/zookeeper/dataLogDir2 clientPort=2182
4. 再次复制zoo.cfg文件命令为zoo3.cfg, 改动dataDir, dataLogDir和clientPort三个参数
dataDir=/Users/userName/Documents/zookeeper/dataDir3 dataLogDir=/Users/userName/Documents/zookeeper/dataLogDir3 clientPort=2183
5. 在配置的/Users/userName/Documents/zookeeper/目录下创建dataDir1, dataDir2, dataDir3, dataLogDir1, dataLogDir2, dataLogDir3目录
6. 在dataDir1 文件夹中创建文件myid, 内容填1 (和server.1=localhost:2881:3881中的1一致即可)
在dataDir2 文件夹中创建文件myid, 内容填2
在dataDir3文件夹中创建文件myid, 内容填3
注: myid文件中的内容和配置中的server.X的X一致即可
7. 启动三个服务
$ zkServer start zoo1.cfg $ zkServer start zoo2.cfg $ zkServer start zoo3.cfg
8. 通过zkServer status命令查看各自的角色
$ zkServer status zoo1.cfg $ zkServer status zoo2.cfg $ zkServer status zoo3.cfg
结果
$ zkServer status zoo1.cfg ZooKeeper JMX enabled by default Using config: /usr/local/etc/zookeeper/zoo1.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