Windows 中搭建Zookeeper的搭建

时间:2022-07-22
本文章向大家介绍Windows 中搭建Zookeeper的搭建,主要内容包括其使用实例、应用技巧、基本知识点总结和需要注意事项,具有一定的参考价值,需要的朋友可以参考一下。

下载

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传

下载地址: https://mirrors.cnnic.cn/apache/zookeeper/

PS:zookeeper 从3.5.5以后的版本带有bin标识的包,否则启动的时候会报错:“ 错误: 找不到或无法加载主类org.apache.zookeeper.server.quorum.QuorumPeerMain ” 。 tar.gz的包里面是只是源码的包无法直接使用, 带有bin名称的包才是我们想要的下载可以直接使用的里面有编译后的二进制的包 。

安装

无需安装,解压到你要存放的目录即可,我存放再D盘。

配置

  1. 修改文件名: 将zoo_simple.cfg文件名修改为zoo.cfg。
  2. 配置日志存放路径
# The number of milliseconds of each tick
#  Zookeeper 服务器之间或客户端与服务器之间维持心跳的时间间隔,也就是每个 tickTime 时间就会发送一个心跳。
tickTime=2000
# The number of ticks that the initial 
# synchronization phase can take
# 集群中的follower服务器(F)与leader服务器(L)之间初始连接时能容忍的最多心跳数(tickTime的数量)
initLimit=10
# The number of ticks that can pass between 
# sending a request and getting an acknowledgement
# 集群中的follower服务器与leader服务器之间请求和应答之间能容忍的最多心跳数(tickTime的数量)
syncLimit=5
# the directory where the snapshot is stored.
# do not use /tmp for storage, /tmp here is just 
# example sakes.
# Zookeeper 将写数据的日志文件也保存在这个目录里
# 因为后面配置集群,所以再data后面使用目录区分,单机版使用数字0目录
dataDir=D:\Install\zookeeper\apache-zookeeper-3.5.8-bin\data\0
# Zookeeper 保存日志文件的目录
dataLogDir=D:\Install\zookeeper\apache-zookeeper-3.5.8-bin\log\0
# the port at which the clients will connect
# 这个端口就是客户端连接 Zookeeper 服务器的端口,Zookeeper 会监听这个端口,接受客户端的访问请求。
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

单机版

启动

找到bin目录下zkServer.cmd双击打开

测试

检查启动是否成功,双击打开zkCli.cmd

PS:如果打开zkServer.cmd闪退可以再zkServer.cmd文件末尾增加pause ,这样就不会闪退了,方便查看报错信息,具体如下: echo on call %JAVA% "-Dzookeeper.log.dir=%ZOO_LOG_DIR%" "-Dzookeeper.root.logger=%ZOO_LOG4J_PROP%" "-Dzookeeper.log.file=%ZOO_LOG_FILE%" "-XX:+HeapDumpOnOutOfMemoryError" "-XX:OnOutOfMemoryError=cmd /c taskkill /pid %%%%p /t /f" -cp "%CLASSPATH%" %ZOOMAIN% "%ZOOCFG%" %* pause endlocal

集群版

配置

  1. 复制三个zoo.cfg,分别命名zoo1.cfgzoo2.cfgzoo3.cfg

zoo1.cfg

#dataDir=/tmp/zookeeper
# Zookeeper 将写数据的日志文件也保存在这个目录里
dataDir=D:\Install\zookeeper\apache-zookeeper-3.5.8-bin\data\1
# Zookeeper 保存日志文件的目录
dataLogDir=D:\Install\zookeeper\apache-zookeeper-3.5.8-bin\log\1
# the port at which the clients will connect
clientPort=2181
#添加集群配置
server.1=localhost:2887:3887
server.2=localhost:2888:3888
server.3=localhost:2889:3889

zoo2.cfg

# Zookeeper 将写数据的日志文件也保存在这个目录里
dataDir=D:\Install\zookeeper\apache-zookeeper-3.5.8-bin\data\2
# Zookeeper 保存日志文件的目录
dataLogDir=D:\Install\zookeeper\apache-zookeeper-3.5.8-bin\log\2
# the port at which the clients will connect
clientPort=2181
#添加集群配置
server.1=localhost:2887:3887
server.2=localhost:2888:3888
server.3=localhost:2889:3889

zoo3.cfg

# Zookeeper 将写数据的日志文件也保存在这个目录里
dataDir=D:\Install\zookeeper\apache-zookeeper-3.5.8-bin\data\3
# Zookeeper 保存日志文件的目录
dataLogDir=D:\Install\zookeeper\apache-zookeeper-3.5.8-bin\log\3
# the port at which the clients will connect
clientPort=2181
#添加集群配置
server.1=localhost:2887:3887
server.2=localhost:2888:3888
server.3=localhost:2889:3889
  1. 在data目录下分别创建1、2、3目录

分别在1、2、3目录中创建myid文件。1目录下的myid存放1;2目录下的myid存放2;3目录下的myid存放3。

  1. 复制三个zkServer.cmd,分别命名:zkServer-1.cmdzkServer-2.cmdzkServer-3.cmd

zkServer-1.cmdzkServer-2.cmdzkServer-3.cmd配置cfg文件路径,添加一句

set ZOOCFG=..confzoo1.cfg

zkServer-1.cmd

setlocal
call "%~dp0zkEnv.cmd"

set ZOOMAIN=org.apache.zookeeper.server.quorum.QuorumPeerMain
set ZOOCFG=..confzoo1.cfg
set ZOO_LOG_FILE=zookeeper-%USERNAME%-server-%COMPUTERNAME%.log

echo on
call %JAVA% "-Dzookeeper.log.dir=%ZOO_LOG_DIR%" "-Dzookeeper.root.logger=%ZOO_LOG4J_PROP%" "-Dzookeeper.log.file=%ZOO_LOG_FILE%" "-XX:+HeapDumpOnOutOfMemoryError" "-XX:OnOutOfMemoryError=cmd /c taskkill /pid %%%%p /t /f" -cp "%CLASSPATH%" %ZOOMAIN% "%ZOOCFG%" %*

endlocal

zkServer-2.cmd

setlocal
call "%~dp0zkEnv.cmd"

set ZOOMAIN=org.apache.zookeeper.server.quorum.QuorumPeerMain
set ZOOCFG=..confzoo2.cfg
set ZOO_LOG_FILE=zookeeper-%USERNAME%-server-%COMPUTERNAME%.log

echo on
call %JAVA% "-Dzookeeper.log.dir=%ZOO_LOG_DIR%" "-Dzookeeper.root.logger=%ZOO_LOG4J_PROP%" "-Dzookeeper.log.file=%ZOO_LOG_FILE%" "-XX:+HeapDumpOnOutOfMemoryError" "-XX:OnOutOfMemoryError=cmd /c taskkill /pid %%%%p /t /f" -cp "%CLASSPATH%" %ZOOMAIN% "%ZOOCFG%" %*

endlocal

zkServer-3.cmd

setlocal
call "%~dp0zkEnv.cmd"

set ZOOMAIN=org.apache.zookeeper.server.quorum.QuorumPeerMain
set ZOOCFG=..confzoo3.cfg
set ZOO_LOG_FILE=zookeeper-%USERNAME%-server-%COMPUTERNAME%.log

echo on
call %JAVA% "-Dzookeeper.log.dir=%ZOO_LOG_DIR%" "-Dzookeeper.root.logger=%ZOO_LOG4J_PROP%" "-Dzookeeper.log.file=%ZOO_LOG_FILE%" "-XX:+HeapDumpOnOutOfMemoryError" "-XX:OnOutOfMemoryError=cmd /c taskkill /pid %%%%p /t /f" -cp "%CLASSPATH%" %ZOOMAIN% "%ZOOCFG%" %*

endlocal

启动

双击打开zkServer-1.cmdzkServer-2.cmdzkServer-3.cmd,开始启动zkServer-1.cmd时会报错原因是zkServer-2.cmdzkServer-3.cmd还未启动,因此zookeeper之间交互的时候是不通的。等剩下的集群服务器启动以后,就不会出现报错了。