zookeeper windows install

下载zookeeper

https://mirrors.tuna.tsinghua.edu.cn/apache/zookeeper/

配置zookeeper

解压到三个目录

我们想要在单机上搭建3个server的伪集群,需要将下载好的zookeeper压缩包解压到三个目录下。

1
2
3
server1 : D:\zookeeper\server1
server2 : D:\zookeeper\server3
server3 : D:\zookeeper\server3

创建配置文件(cfg文件)

解压之后,分别进入conf目录,可以看到zoo_sample.cfg,log4j.properties和configuration.xsl三个文件。

在该目录下创建一个zoo.cfg文件(也可以直接使用zoo_sample.cfg),配置如下:

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
# 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=D:\\zookeeper\\server1\\data
dataLogDir=D:\\zookeeper\\server1\\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
# (伪集群zookeeper的server1标识)
server.1=localhost:2887:3887
# (伪集群zookeeper的server2标识)
server.2=localhost:2888:3888
# (伪集群zookeeper的server3标识)
server.3=localhost:2889:3889

以上就是zookeeper伪集群中server1的配置文件。同理在其他两个解压路径的conf目录下创建server2和server3的配置文件zoo.cfg。参数区别仅在于dataDir、dataLogDir和clientPort

server2的zoo.cfg

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
# 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=D:\\zookeeper\\server2\\data
dataLogDir=D:\\zookeeper\\server2\\logs
# the port at which the clients will connect
clientPort=2182
# 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
# (伪集群zookeeper的server1标识)
server.1=localhost:2887:3887
# (伪集群zookeeper的server2标识)
server.2=localhost:2888:3888
# (伪集群zookeeper的server3标识)
server.3=localhost:2889:3889

server3的zoo.cfg

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
# 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=D:\\zookeeper\\server3\\data
dataLogDir=D:\\zookeeper\\server3\\logs
# the port at which the clients will connect
clientPort=2183
# 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
# (伪集群zookeeper的server1标识)
server.1=localhost:2887:3887
# (伪集群zookeeper的server2标识)
server.2=localhost:2888:3888
# (伪集群zookeeper的server3标识)
server.3=localhost:2889:3889

创建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。启动的过程中是会报错的,信息如下:

进入cmd,切换目录到 /server1/bin/,执行命令 zkServer.cmd(此时会打印错误日志,别急,这是心跳检查连接其他zk服务,等启动集群数量一半以上的zk服务后,就不报错了)
进入cmd,切换目录到 /server2/bin/,执行命令 zkServer.cmd
进入cmd,切换目录到 /server3/bin/,执行命令 zkServer.cmd

验证zookeeper服务是否启动

cmd,切换目录到 /server1/bin,执行命令 zkCli.cmd -server localhost:2181

参考文章

评论