获取到zookeeper压缩包解压到:F:\student\zookeeper-3.4.5目录下,zookeeper的启动脚本在bin目录下,windows下启动脚本为:zkServer.cmd。在启动脚本之前需要修改基本的配置文件,配置文件在conf目录下,修改文件名:zoo_sample.cfg为 zoo.cfg
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=F:\\student\\zookeeper-3.4.5\\data
dataLogDir=F:\\student\\zookeeper-3.4.5\\log
# the port at which the clients will connect
clientPort=2181
#
# 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
- tickTime:这个时间是作为 Zookeeper 服务器之间或客户端与服务器之间维持心跳的时间间隔,也就是每个 tickTime 时间就会发送一个心跳。
- dataDir:顾名思义就是 Zookeeper 保存数据的目录,默认情况下,Zookeeper 将写数据的日志文件也保存在这个目录里。
- dataLogDir:顾名思义就是 Zookeeper 保存日志文件的目录
- clientPort:这个端口就是客户端连接 Zookeeper 服务器的端口,Zookeeper 会监听这个端口,接受客户端的访问请求。
客户端连接zookeeper服务:在cmd命令窗口,cd到bin目录执行:zkCli.cmd -server 127.0.0.1:2181 即可
集群配置(一同一台机子对应多个实例为例):
1 将F:\student\zookeeper-3.4.5 (A) 目录复制两份分别改名为:
F:\student\zookeeper-3.4.5_1 (B)
F:\student\zookeeper-3.4.5_2 (C)
2 修改A对应的配置文件为:
# 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=F:\\student\\zookeeper-3.4.5\\data
dataLogDir=F:\\student\\zookeeper-3.4.5\\log
# the port at which the clients will connect
clientPort=2181
#
# 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:2888:3888
server.2=127.0.0.1:2889:3889
server.3=127.0.0.1:2890:3890
- initLimit:这个配置项是用来配置 Zookeeper 接受客户端(这里所说的客户端不是用户连接 Zookeeper 服务器的客户端,而是 Zookeeper 服务器集群中连接到 Leader 的 Follower 服务器)初始化连接时最长能忍受多少个心跳时间间隔数。当已经超过 10 个心跳的时间(也就是 tickTime)长度后 Zookeeper 服务器还没有收到客户端的返回信息,那么表明这个客户端连接失败。总的时间长度就是 5*2000=10 秒
- syncLimit:这个配置项标识 Leader 与 Follower 之间发送消息,请求和应答时间长度,最长不能超过多少个 tickTime 的时间长度,总的时间长度就是 2*2000=4 秒
- server.A=B:C:D:其中 A 是一个数字,表示这个是第几号服务器;B 是这个服务器的 ip 地址;C 表示的是这个服务器与集群中的 Leader 服务器交换信息的端口;D 表示的是万一集群中的 Leader 服务器挂了,需要一个端口来重新进行选举,选出一个新的 Leader,而这个端口就是用来执行选举时服务器相互通信的端口。如果是伪集群的配置方式,由于 B 都是一样,所以不同的 Zookeeper 实例通信端口号不能一样,所以要给它们分配不同的端口号。
修改B对应的配置文件为:
tickTime=2000
initLimit=10
syncLimit=5
dataDir=F:\\student\\zookeeper-3.4.5_1\\data #data目录要不同
dataLogDir=F:\\student\\zookeeper-3.4.5_1\\log #日志目录要不同
clientPort=2182 #端口号要不同
server.1=127.0.0.1:2888:3888
server.2=127.0.0.1:2889:3889
server.3=127.0.0.1:2890:3890
修改C对应的配置文件为:
tickTime=2000
initLimit=10
syncLimit=5
dataDir=F:\\student\\zookeeper-3.4.5_2\\data #data目录要不同
dataLogDir=F:\\student\\zookeeper-3.4.5_2\\log #日志目录要不同
clientPort=2183 #端口号要不同
server.1=127.0.0.1:2888:3888
server.2=127.0.0.1:2889:3889
server.3=127.0.0.1:2890:3890
3 执行完上述两步分别启动各个服务时,会发现命令窗口一闪而过,服务启动不起来,这个是由于该zookeeper实例 不知道具体该对应哪个server,所以扔需要在dataDir目录下生产myid文件,该文件中记录属于哪个server
如:A 对应的myid中值为:1
B 对应的myid中值为:2
C 对应的myid中值为:3
即可
zoo.cfg配置讲解
clientPort:监听客户端连接的端口;
dataDir:存储内存中数据库快照的位置;
注意 应该谨慎地选择日志存放的位置,使用专用的日志存储设备能够大大地提高系统的性能,如果将日志存储在比较繁忙的存储设备上,那么将会在很大程度上影响系统的性能。
tickTime:基本事件单元,以毫秒为单位。它用来控制心跳和超时,默认情况下最小的会话超时时间为两倍的 tickTime 。
dataLogDir:这个操作将管理机器把事务日志写入到“ dataLogDir ”所指定的目录,而不是“ dataDir ”所指定的目录。这将允许使用一个专用的日志设备并且帮助我们避免日志和快照之间的竞争
配置如下:dataLogDir=F:\\student\\zookeeper-3.4.5_1\\log
maxClientCnxns:表示连接到服务端的最大客户端个数,为了限制并发数,为零时表示不作限制
将:maxClientCnxns=1 当用两个客户端连接该服务时报错如下
客户端报错如下:
服务端报错如下:
minSessionTimeout 和 maxSessionTimeout:最小的会话超时时间以及最大的会话超时时间。其中,最小的会话超时时间默认情况下为 2 倍的 tickTme 时间,最大的会话超时时间默认情况下为 20 倍的会话超时时间。在启动时,系统会显示相应信息,见下图 2 所示,默认会话超时时间:
图2 :默认会话超时时间
从上图中可以看书, minSessionTimeout 以及 maxSessionTimeout 的值均为 -1 ,现在我们来设置系统的最小会话超时时间以及最大会话超时时间,如下所示:
#set minSessionTimeout
minSessionTimeout=1000
#set maxSessionTImeout
maxSessionTimeout=10000
在配置 minSessionTmeout 以及 maxSessionTimeout 的值的时候需要注意,如果将此值设置的太小的话,那么会话很可能刚刚建立便由于超时而不得不退出。一般情况下,不能将此值设置的比 tickTime 的值还小