一、下载zookeeper
https://mirrors.tuna.tsinghua.edu.cn/apache/zookeeper/
这里笔者下载的是zookeeper-3.3.6
二、配置zookeeper
1.解压到三个目录
我们想要在单机上搭建3个server的伪集群,需要将下载好的zookeeper压缩包解压到三个目录下。笔者的目录如下:
server1 : F:\paths\zookeeper\server1\zookeeper-3.3.6
server2 : F:\paths\zookeeper\server2\zookeeper-3.3.6
server3 : F:\paths\zookeeper\server3\zookeeper-3.3.6
2.创建配置文件(cfg文件)
解压之后,分别进入conf目录,可以看到zoo_sample.cfg,log4j.properties和configuration.xsl三个文件。
在该目录下创建一个zoo.cfg文件(也可以直接使用zoo_sample.cfg),配置如下:
[html] view plain copy
- <span style="font-size:14px;"># The number of milliseconds of each tick
- tickTime=2000(2000ms,心跳间隔,也是zookeeper的时间单位)
- # The number of ticks that the initial synchronization phase can take
- initLimit=10(10*tickTime = 20s,定义初始化的时间限制)
- # The number of ticks that can pass between sending a request and getting an acknowledgement
- syncLimit=5 (5*tickTime = 10s,定义同步的时间限制)
- # the directory where the snapshot is stored.
- dataDir=F:\paths\zookeeper\server1\zookeeper-3.3.6\data (自定义数据快照的存放目录,data目录需要自行创建)
- dataLogDir=F:\paths\zookeeper\server1\zookeeper-3.3.6\log (自定义日志存放目录,log目录需要自行创建)
- # the port at which the clients will connect
- clientPort=2181 (zookeeper服务端口)
- server.1=localhost:2887:3887 (伪集群zookeeper的server1标识)
- server.2=localhost:2888:3888 (伪集群zookeeper的server2标识)
- server.3=localhost:2889:3889 (伪集群zookeeper的server3标识)</span>
以上就是zookeeper伪集群中server1的配置文件。同理在其他两个解压路径的conf目录下创建server2和server3的配置文件zoo.cfg。参数区别仅在于dataDir、dataLogDir和clientPort
server2的zoo.cfg
[html] view plain copy
- <span style="font-size:14px;"># 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.
- dataDir=F:\paths\zookeeper\server2\zookeeper-3.3.6\data
- dataLogDir=F:\paths\zookeeper\server2\zookeeper-3.3.6\log
- # the port at which the clients will connect
- clientPort=2182
- server.1=localhost:2887:3887
- server.2=localhost:2888:3888
- server.3=localhost:2889:3889</span>
server3的zoo.cfg
[html] view plain copy
- <span style="font-size:14px;"># 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.
- dataDir=F:\paths\zookeeper\server3\zookeeper-3.3.6\data
- dataLogDir=F:\paths\zookeeper\server3\zookeeper-3.3.6\log
- # the port at which the clients will connect
- clientPort=2183
- server.1=localhost:2887:3887
- server.2=localhost:2888:3888
- server.3=localhost:2889:3889</span>
3.创建myid文件
在上个步骤中,我们在dataDir中指定了快照存放目录,切换到各目录下,分别创建一个文件名为myid的文件(没有后缀名)。文件内容为一个整型数。
在server1的data目录下的myid文件,其内容为1。
在server2的data目录下的myid文件,其内容为2。
在server3的data目录下的myid文件,其内容为3。
三、启动zookeeper
分别切换到三个解压路径下的bin目录,在cmd上输入zkServer.cmd启动服务,可以同时用三个cmd窗口分别启动三个server,笔者的启动顺序是server1 -> server2 -> server3。启动的过程中是会报错的,信息如下:
这是zookeeper的Leader选举算法的异常信息,当节点没有启动完毕的时候,Leader无法正常进行工作,这种错误信息是可以忽略的,等其他节点启动之后就正常了。
四、验证zookeeper服务是否启动
在cmd中输入jps,可以查看到相应的java进程。
原文地址:https://www.cnblogs.com/cxxjohnson/p/9121584.html