ZooKeeper Windows 集群搭建教程
ZooKeeper Windows 集群搭建
1 下载
========================================================================
ZooKeeper 下载地址:https://zookeeper.apache.org/releases.html#download
选择 Apache ZooKeeper 3.5.8(asc, sha512)
下载,解压文件到某一目录(目录不要带中文),本次测试解压后的目录是 apache-zookeeper-3.5.8
。
2 修改配置文件 zoo.cfg
打开 apache-zookeeper-3.5.8
目录下的 conf
目录,复制 zoo_sample.cfg
文件并重命名为 zoo.cfg
,使用 Notepad++
(或其他工具)打开文件 zoo.cfg
,修改 dataDir
位置为:
dataDir=C:\\Software\\apache-zookeeper-3.5.8\\data
上面的 data
是数据存储的文件夹,需要自己新建(位置自己决定)。
3 集群搭建
集群搭建需要多个 ZooKeeper
,本次使用:
- 1 个 Leader
- 2 个 Follower
- 1 个 Observer
一共 4 个 ZooKeeper
。
复制 3 次 apache-zookeeper-3.5.8
目录并重命名为 apache-zookeeper-3.5.8-2
、apache-zookeeper-3.5.8-3
、apache-zookeeper-3.5.8-4
,名字自己决定。
集群的搭建配置有三点需要修改:
- 配置文件修改
clientPort
- 配置文件添加集群配置
data
文件夹下添加myid
文件
3.1 配置文件修改 clientPort
依次打开各个 ZooKeeper 的配置文件 zoo.cfg
,修改 clientPort
,每个端口号都不能重复,可以设置为 2181
、2182
、2183
、2184
。
3.2 配置文件添加集群配置
每个 ZooKeeper 的配置文件下添加如下配置(端口号自己决定):
server.1=127.0.0.1:2881:3881
server.2=127.0.0.1:2882:3882
server.3=127.0.0.1:2883:3883
server.4=127.0.0.1:2884:3884:observer
3.3 data 文件夹下添加 myid 文件
在每个 ZooKeeper 的 data
目录下新建 myid
,注意文件名就叫 myid
,没有任何后缀,内容依次为 1
、2
、3
、4
。
3.4 全部配置展示
- apache-zookeeper-3.5.8
.\conf
下的 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=C:\\Software\\apache-zookeeper-3.5.8\\data
# 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=127.0.0.1:2881:3881
server.2=127.0.0.1:2882:3882
server.3=127.0.0.1:2883:3883
server.4=127.0.0.1:2884:3884:observer
.\data
下的 myid
:
1
- apache-zookeeper-3.5.8-2
.\conf
下的 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=C:\\Software\\apache-zookeeper-3.5.8-2\\data
# 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
server.1=127.0.0.1:2881:3881
server.2=127.0.0.1:2882:3882
server.3=127.0.0.1:2883:3883
server.4=127.0.0.1:2884:3884:observer
.\data
下的 myid
:
2
- apache-zookeeper-3.5.8-3
.\conf
下的 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=C:\\Software\\apache-zookeeper-3.5.8-3\\data
# 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
server.1=127.0.0.1:2881:3881
server.2=127.0.0.1:2882:3882
server.3=127.0.0.1:2883:3883
server.4=127.0.0.1:2884:3884:observer
.\data
下的 myid
:
3
- apache-zookeeper-3.5.8-4
.\conf
下的 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=C:\\Software\\apache-zookeeper-3.5.8-4\\data
# the port at which the clients will connect
clientPort=2184
# 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=127.0.0.1:2881:3881
server.2=127.0.0.1:2882:3882
server.3=127.0.0.1:2883:3883
server.4=127.0.0.1:2884:3884:observer
.\data
下的 myid
:
4
4 脚本
集群实例如果依次启动比较麻烦,可以创建脚本快速启动,新建文件 zookeeperstart.bat
,文件内容如下:
@echo off
title zookeeper集群启动
cd /d C:\Software\apache-zookeeper-3.5.8\bin
start zkServer.cmd
echo zookeeper集群节点1启动
cd /d C:\Software\apache-zookeeper-3.5.8-2\bin
start zkServer.cmd
echo zookeeper集群节点2启动
cd /d C:\Software\apache-zookeeper-3.5.8-3\bin
start zkServer.cmd
echo zookeeper集群节点3启动
cd /d C:\Software\apache-zookeeper-3.5.8-4\bin
start zkServer.cmd
echo zookeeper集群节点4启动
pause