参阅自官网:https://zookeeper.apache.org/doc/current/zookeeperStarted.html#sc_Download
比较全的配置地址:https://blog.youkuaiyun.com/qianshangding0708/article/details/50067483
一、下载
1、通过以下网址下载到zookeeper-3.4.12.tar.gz包:
http://zookeeper.apache.org/releases.html
2、此压缩包可以在windows与Linux运行。
二、安装
1、解压安装包进入F:\ProgramTools\zookeeper-3.4.12\conf目录,有zoo.cfg文件,此文件可以随便取名,内容如下:
# The number of milliseconds of each tick
# ZooKeeper使用的基本时间单位(以毫秒为单位)。 它用于做心跳,最小会话超时将是tickTime的两倍。
tickTime=2000
# The number of ticks that can pass between
# sending a request and getting an acknowledgement
# 此配置项表示Leader与Follower之间发送消息时,请求和应答时间长度。
# 如果follower在设置时间内不能与leader通信,那么此follower将会被丢弃。
syncLimit=5
# the directory where the snapshot is stored.
# do not use /tmp for storage, /tmp here is just
# example sakes.
# 存储内存数据库快照的位置,除非另有说明,否则指向数据库更新的事务日志。
# 放置事务日志的位置要小心。 专用的事务日志设备是始终如一的良好性能的关键。 将日志置于繁忙的设备上会对性能产生负面影响。
dataDir=F:\\ProgramTools\\zookeeper\\zookeeper-1\\dataDir\\data
#此选项将指示计算机将事务日志写入dataLogDir而不是dataDir。 这允许使用专用的日志设备,并有助于避免日志记录和快照之间的竞争。
# 拥有专用的日志设备会对吞吐量和稳定的延迟产生很大影响。 强烈建议专用日志设备并将dataLogDir设置为指向该设备上的目录,然后确保将dataDir指向不驻留在该设备上的目录。
dataLogDir=F:\\ProgramTools\\zookeeper\\zookeeper-1\\dataDir\\log
# 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
# 限制由IP地址标识的单个客户端可以对ZooKeeper集合的单个成员进行的并发连接数(在套接字级别)。
# 这用于防止某些类别的DoS攻击,包括文件描述符耗尽。 默认值为60.将此值设置为0将完全删除并发连接的限制。
#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
# 启用后,ZooKeeper自动清除功能会分别在dataDir和dataLogDir中保留autopurge.snapRetainCount最新快照和相应的事务日志,并删除其余日志。 默认为3.最小值为3。
# autopurge.snapRetainCount=3
# Purge task interval in hours
# Set to "0" to disable auto purge feature
# 必须触发清除任务的时间间隔(以小时为单位)。 设置为正整数(1和更高)以启用自动清除。 默认为0。
# autopurge.purgeInterval=1
# ZooKeeper使用事务日志和快照来持久化每个事务(注意是日志先写)。该配置项指定ZooKeeper在将内存数据库序列化为快照之前,需要先写多少次事务日志。
# 也就是说,每写几次事务日志,就快照一次。默认值为100000。为了防止所有的ZooKeeper服务器节点同时生成快照(一般情况下,所有实例的配置文件是完全相同的),
# 当某节点的先写事务数量在(snapCount/2+1,snapCount)范围内时(挑选一个随机值),这个值就是该节点拍快照的时机。snapCount最小为2,如果设置为1,则默认为2.
snapCount=1
# (Java system property: zookeeper.observer.syncEnabled)
# New in 3.4.6, 3.5.0: The observers now log transaction and write snapshot to disk by default like the participants.
# This reduces the recovery time of the observers on restart. Set to "false" to disable this feature. Default is "true"
# 观察者现在默认记录事务并将快照写入磁盘,就像参与者一样。 这减少了重启时观察者的恢复时间。 设置为“false”以禁用此功能。 默认为“true”
syncEnabled=true
# (Java system property only: zookeeper.extendedTypesEnabled)
# New in 3.5.4, 3.6.0: Define to "true" to enable extended features such as the creation of TTL Nodes.
# They are disabled by default. IMPORTANT: when enabled server IDs must be less than 255 due to internal limitations.
# 定义为“true”以启用扩展功能,例如创建TTL节点。 它们默认是禁用的。 重要信息:由于内部限制,启用的服务器ID必须小于255。
zookeeper.extendedTypesEnabled=false
# New in 3.5.4, 3.6.0: Due to ZOOKEEPER-2901 TTL nodes created in version 3.5.3 are not supported in 3.5.4/3.6.0. However,
# a workaround is provided via the zookeeper.emulate353TTLNodes system property. If you used TTL nodes in ZooKeeper 3.5.3 and
# need to maintain compatibility set zookeeper.emulate353TTLNodes to "true" in addition to zookeeper.extendedTypesEnabled. NOTE: due to the bug, server IDs must be 127 or less.
# Additionally, the maximum support TTL value is 1099511627775 which is smaller than what was allowed in 3.5.3 (1152921504606846975)
# 3.5.4,3.6.0中的新功能:由于ZOOKEEPER-2901,3.5.4 / 3.6.0版不支持在3.5.3版中创建的TTL节点。 但是,通过zookeeper.emulate353TTLNodes系统属性提供了一种解决方法。
# 如果您使用ZooKeeper 3.5.3中的TTL节点,并且除了zookeeper.extendedTypesEnabled之外,还需要将兼容性设置zookeeper.emulate353TTLNodes设置为“true”。
# 注意:由于该错误,服务器ID必须为127或更少。 此外,最大支持TTL值为1099511627775,小于3.5.3(1152921504606846975)中允许的值
zookeeper.emulate353TTLNodes=false
# -----------------------------------Cluster Options-------------------------
# The options in this section are designed for use with an ensemble of servers -- that is, when deploying clusters of servers.
# -------------------------下面是集群配置,单机不用配置也可------------------
# electionAlg
# (No Java system property)
# Election implementation to use. A value of "0" corresponds to the original UDP-based version, "1" corresponds to
# the non-authenticated UDP-based version of fast leader election, "2" corresponds to the authenticated UDP-based version of
# fast leader election, and "3" corresponds to TCP-based version of fast leader election. Currently, algorithm 3 is the default
# Note
# The implementations of leader election 0, 1, and 2 are now deprecated . We have the intention of removing them in the next release,
# at which point only the FastLeaderElection will be available.
# electionAlg
# (没有Java系统属性)
# 选举实施使用。 值“0”对应于原始的基于UDP的版本,“1”对应于未经认证的基于UDP的快速领导者选举版本,
# “2”对应于经认证的基于UDP的快速领导者选举版本,以及 “3”对应于基于TCP的快速领导者选举版本。 目前,算法3是默认值
# 注意
# 现在不推荐使用领导者选举0,1和2的实现。 我们打算在下一个版本中删除它们,此时只有FastLeaderElection可用。
electionAlg=3
# The number of ticks that the initial
# synchronization phase can take
# 此配置表示,允许follower(相对于Leaderer言的“客户端”)连接并同步到Leader的初始化连接时间,以tickTime为单位。
# 当初始化连接时间超过该值,则表示连接失败。
# initLimit
#(没有Java系统属性)
# 以滴答为单位的时间量(请参阅tickTime),以允许关注者连接并同步到领导者。 如果ZooKeeper管理的数据量很大,则根据需要增加此值。
initLimit=10
# The number of ticks that can pass between
# sending a request and getting an acknowledgement
# 此配置项表示Leader与Follower之间发送消息时,请求和应答时间长度。
# 如果follower在设置时间内不能与leader通信,那么此follower将会被丢弃。
# initLimit tickTime的个数,表示在leader选举结束后,followers与leader同步需要的时间,
# 如果followers比较多或者说leader的数据非常多时,同步时间相应可能会增加,那么这个值也需要相应增加。
# 当然,这个值也是follower和observer在开始同步leader的数据时的最大等待时间(setSoTimeout)
# syncLimit tickTime的个数,这时间容易和上面的时间混淆,它也表示follower和observer与leader交互时的最大等待时间,
# 只不过是在与leader同步完毕之后,进入正常请求转发或ping等消息交互时的超时时间。
syncLimit=5
# leaderServes
# (Java system property: zookeeper.leaderServes)
# Leader accepts client connections. Default value is "yes". The leader machine coordinates updates. For higher update throughput
# at thes slight expense of read throughput the leader can
# be configured to not accept clients and focus on coordination. The default to this option is yes, which means that a leader will accept client connections.
# Note
# Turning on leader selection is highly recommended when you have more than three ZooKeeper servers in an ensemble.
# leaderServes
# (Java系统属性:zookeeper.leaderServes)
# 领导者接受客户连接。 默认值为“是”。 领导者协调更新。 为了获得更高的更新吞吐量,读取吞吐量略有损失,可以将领导者配置为不接受客户端并专注于协调。
# 此选项的默认值为yes,这表示领导者将接受客户端连接。
# 注意
# 当您在一个集合中有三个以上的ZooKeeper服务器时,强烈建议启用领导者选择。
leaderServes=yes
# cnxTimeout
# (Java system property: zookeeper.cnxTimeout)
# Sets the timeout value for opening connections for leader election notifications. Only applicable if you are using electionAlg 3.
# Note
# Default value is 5 seconds.
# cnxTimeout
# (Java系统属性:zookeeper.cnxTimeout)
# 设置打开领导者选举通知连接的超时值。 仅适用于您使用electionAlg 3的情况。
# 注意
# 默认值为5秒。
cnxTimeout=5
# standaloneEnabled
# (No Java system property)
# New in 3.5.0: When set to false, a single server can be started in replicated mode, a lone participant can run with observers, and a cluster can reconfigure down to one node,
# and up from one node. The default is true for backwards compatibility. It can be set using QuorumPeerConfig's setStandaloneEnabled method or by adding "standaloneEnabled=false"
# or "standaloneEnabled=true" to a server's config file.
# standaloneEnabled
# (没有Java系统属性)
# 3.5.0中的新增功能:设置为false时,可以在复制模式下启动单个服务器,单个参与者可以与观察者一起运行,并且群集可以重新配置为一个节点,并从一个节点向上重新配置。 对于向后兼容性,
# 默认值为true。 可以使用QuorumPeerConfig的setStandaloneEnabled方法或通过将“standaloneEnabled = false”或“standaloneEnabled = true”添加到服务器的配置文件来设置它。
standaloneEnabled=true
# reconfigEnabled
# (No Java system property)
# New in 3.5.3: This controls the enabling or disabling of Dynamic Reconfiguration feature. When the feature is enabled, users can perform reconfigure operations through
# the ZooKeeper client API or through ZooKeeper command line tools assuming users are authorized to perform such operations. When the feature is disabled, no user,
# including the super user, can perform a reconfiguration. Any attempt to reconfigure will return an error. "reconfigEnabled" option can be set as "reconfigEnabled=false"
# or "reconfigEnabled=true" to a server's config file, or using QuorumPeerConfig's setReconfigEnabled method. The default value is false. If present, the value should be
# consistent across every server in the entire ensemble. Setting the value as true on some servers and false on other servers will cause inconsistent behavior depending
# on which server is elected as leader. If the leader has a setting of "reconfigEnabled=true", then the ensemble will have reconfig feature enabled. If the leader has a
# setting of "reconfigEnabled=false", then the ensemble will have reconfig feature disabled. It is thus recommended to have a consistent value for "reconfigEnabled" across
# servers in the ensemble.
# reconfigEnabled
# (没有Java系统属性)
# 3.5.3中的新增功能:它控制动态重新配置功能的启用或禁用。启用此功能后,用户可以通过ZooKeeper客户端API或ZooKeeper命令行工具执行重新配置操作,前提是用户有权执行此类操作。
# 禁用此功能后,任何用户(包括超级用户)都无法执行重新配置。任何重新配置的尝试都将返回错误。 “reconfigEnabled”选项可以设置为“reconfigEnabled = false”或“reconfigEnabled = true”
# 到服务器的配置文件,或使用QuorumPeerConfig的setReconfigEnabled方法。默认值为false。如果存在,则该值应该在整个集合中的每个服务器上保持一致。在某些服务器上将值设置为true而在
# 其他服务器上将其设置为false将导致不一致的行为,具体取决于哪个服务器被选为leader。如果领导者的设置为“reconfigEnabled = true”,则整体将启用重新配置功能。如果领导者的设置为
# “reconfigEnabled = false”,则整体将禁用重新配置功能。因此,建议在整体中的服务器之间具有“reconfigEnabled”的一致值。
reconfigEnabled=false
# 4lw.commands.whitelist
# (Java system property: zookeeper.4lw.commands.whitelist)
# New in 3.5.3: A list of comma separated Four Letter Words commands that user wants to use. A valid Four Letter Words command must be put in this list
# else ZooKeeper server will not enable the command. By default the whitelist only contains "srvr" command which zkServer.sh uses. The rest of four letter
# word commands are disabled by default.
# Here's an example of the configuration that enables stat, ruok, conf, and isro command while disabling the rest of Four Letter Words command:
# 4lw.commands.whitelist=stat, ruok, conf, isro
# If you really need enable all four letter word commands by default, you can use the asterisk option so you don't have to include every command
# one by one in the list. As an example, this will enable all four letter word commands:
# 4lw.commands.whitelist=*
# 4lw.commands.whitelist
# (Java系统属性:zookeeper.4lw.commands.whitelist)
# 3.5.3中的新功能:用户想要使用的逗号分隔的四字母单词命令列表。 必须在此列表中放入有效的Four Letter Words命令,否则ZooKeeper服务器将不启用该命令。
# 默认情况下,白名单仅包含zkServer.sh使用的“srvr”命令。 默认情况下,禁用其余的四个字母单词命令。
# 下面是一个配置示例,它启用stat,ruok,conf和isro命令,同时禁用其余的Four Letter Words命令:
# 4lw.commands.whitelist = stat,ruok,conf,isro
# 如果您确实需要默认启用所有四个字母的单词命令,则可以使用星号选项,这样就不必在列表中逐个包含每个命令。 例如,这将启用所有四个字母单词命令:
4lw.commands.whitelist=*
# tcpKeepAlive
# (Java system property: zookeeper.tcpKeepAlive)
# New in 3.5.4: Setting this to true sets the TCP keepAlive flag on the sockets used by quorum members to perform elections.
# This will allow for connections between quorum members to remain up when there is network infrastructure that may otherwise break them.
# Some NATs and firewalls may terminate or lose state for long running or idle connections.
# Enabling this option relies on OS level settings to work properly, check your operating system's options regarding TCP keepalive for more information. Defaults to false.
# TCPKEEPALIVE
# (Java系统属性:zookeeper.tcpKeepAlive)
# 3.5.4中的新增内容:将此设置为true会在仲裁成员用于执行选举的套接字上设置TCP keepAlive标志。 这将允许仲裁成员之间的连接在存在可能以其他方式破坏它们的网络基础结构时保持不变。
# 对于长时间运行或空闲连接,某些NAT和防火墙可能会终止或丢失状态。
# 启用此选项依赖于操作系统级别设置才能正常工作,请查看操作系统有关TCP keepalive的选项以获取更多信息。 默认为false。
tcpKeepAlive=false
# group.x=nnnnn[:nnnnn]
# (No Java system property)
# Enables a hierarchical quorum construction."x" is a group identifier and the numbers following the "=" sign correspond to server identifiers. The left-hand side of the
# assignment is a colon-separated list of server identifiers. Note that groups must be disjoint and the union of all groups must be the ZooKeeper ensemble.
# group.x= NNNNN[:NNNNN]
# (没有Java系统属性)
# 启用分层仲裁构造。“x”是组标识符,“=”符号后面的数字对应于服务器标识符。 赋值的左侧是以冒号分隔的服务器标识符列表。 请注意,组必须是不相交的,并且所有组的并集必须是ZooKeeper集合。
# weight.x=nnnnn
# (No Java system property)
# Used along with "group", it assigns a weight to a server when forming quorums. Such a value corresponds to the weight of a server when voting. There are a few parts of ZooKeeper
# that require voting such as leader election and the atomic broadcast protocol. By default the weight of server is 1. If the configuration defines groups, but not weights,
# then a value of 1 will be assigned to all servers.
# This document gives an example of how to use hierarchical quorums. The basic idea is very simple. First, we split servers into groups, and add a line for each
# group listing the servers that form this group. Next we have to assign a weight to each server.
# The following example shows how to configure a system with three groups of three servers each, and we assign a weight of 1 to each server:
# This document gives an example of how to use hierarchical quorums. The basic idea is very simple. First, we split servers into groups, and add a line for each group listing the
# servers that form this group. Next we have to assign a weight to each server.
# The following example shows how to configure a system with three groups of three servers each, and we assign a weight of 1 to each server:
# When running the system, we are able to form a quorum once we have a majority of votes from a majority of non-zero-weight groups. Groups that have zero
# weight are discarded and not considered when forming quorums. Looking at the example, we are able to form a quorum once we have votes from at least two servers from each of
# two different groups.
# 本文档提供了如何使用分层仲裁的示例。 基本思路很简单。 首先,我们将服务器分成组,并为列出构成该组的服务器的每个组添加一行。 接下来,我们必须为每个服务器分配权重。
# 以下示例显示如何使用三组三个服务器配置系统,并为每个服务器分配权重1:
# 在运行系统时,一旦我们获得大多数非零重量组的大多数投票,我们就能形成法定人数。 没有权重的组被丢弃,在形成法定人数时不予考虑。 看一下这个例子,
# 一旦我们从两个不同组中的每个服务器中获得至少两个服务器的投票,我们就可以形成一个法定人数。
# group.1=1:2:3
# group.2=4:5:6
# group.3=7:8:9
# weight.1=1
# weight.2=1
# weight.3=1
# weight.4=1
# weight.5=1
# weight.6=1
# weight.7=1
# weight.8=1
# weight.9=1
# server.x=[hostname]:nnnnn[:nnnnn], etc
# (No Java system property)
# servers making up the ZooKeeper ensemble. When the server starts up, it determines which server it is by looking for the file
# myid in the data directory. That file contains the server number, in ASCII, and it should match x in server.x in the left hand side of this setting.
# The list of servers that make up ZooKeeper servers that is used by the clients must match the list of ZooKeeper servers that each ZooKeeper server has.
# There are two port numbers nnnnn. The first followers use to connect to the leader, and the second is for leader election. The leader election port
# is only necessary if electionAlg is 1, 2, or 3 (default). If electionAlg is 0, then the second
# port is not necessary. If you want to test multiple servers on a single machine, then different ports can be used for each server.
# server.x = [hostname]:nnnnn [:nnnnn]等
# (没有Java系统属性)
# 构成ZooKeeper集合的服务器。 当服务器启动时,它通过在数据目录中查找文件myid来确定它是哪个服务器。 该文件包含服务器编号(ASCII),它应与此设置左侧server.x中的x匹配。
# 组成客户端使用的ZooKeeper服务器的服务器列表必须与每个ZooKeeper服务器所具有的ZooKeeper服务器列表相匹配。
# 有两个端口号nnnnn。 第一批追随者用于连接领导者,第二个追随者用于领导者选举。 仅当electionAlg为1,2或3(默认值)时,才需要领导者选举端口。 如果electionAlg为0,
# 则不需要第二个端口。 如果要在一台计算机上测试多个服务器,则可以为每个服务器使用不同的端口。
# 配置集群
server.1=127.0.0.1:2888:3888
server.2=127.0.0.1:2889:3889
server.3=127.0.0.1:2890:3890
2、进入\zookeeper-3.4.12\bin目录,发现如下命令(注意如果要在任何目录运行以下命令则要设置环境变量path):
a、zkServer.cmd:启动zookeeper服务器命令。
C:\WINDOWS\system32>zkServer.cmd
b、zkCli.cmd:启动客户端并连接到zookeeperfuwu服务器命令(2181为zoo.cfg中配置的客户端连接端口):
C:\WINDOWS\system32>zkCli -server 127.0.0.1:2181
c、zkEnv.cmd:此命令在以上两个命令中会用到,不用我们调用。
还有一个zkCleanup.sh,它用来清理事务日志和快照。但比较少用。
d、ls /:查看节点目录,如下:有[dubbo, zookeeper]两个节点。
[zk: localhost:2181(CONNECTED) 13] ls /
[dubbo, zookeeper]
e、create /zk_test my_data:这将创建一个新的znode,并将字符串“my_data”与节点相关联,如下:
[zk: localhost:2181(CONNECTED) 14] create /zk_test my_test_data
Created /zk_test
[zk: localhost:2181(CONNECTED) 15] ls /
[dubbo, zookeeper, zk_test]
f、get /zk_test:通过运行get命令验证数据是否与znode相关联,如下:
[zk: localhost:2181(CONNECTED) 23] get /zk_test
my_test_data
cZxid = 0x97
ctime = Tue Aug 14 23:30:22 GMT+08:00 2018
mZxid = 0x97
mtime = Tue Aug 14 23:30:22 GMT+08:00 2018
pZxid = 0x97
cversion = 0
dataVersion = 0
aclVersion = 0
ephemeralOwner = 0x0
dataLength = 12
numChildren = 0
g、set /zk_test set_data_test:我们可以通过发出set命令来更改与zk_test相关的数据,如:
[zk: localhost:2181(CONNECTED) 25] set /zk_test set_data_test
cZxid = 0x97
ctime = Tue Aug 14 23:30:22 GMT+08:00 2018
mZxid = 0x98
mtime = Tue Aug 14 23:40:40 GMT+08:00 2018
pZxid = 0x97
cversion = 0
dataVersion = 1
aclVersion = 0
ephemeralOwner = 0x0
dataLength = 13
numChildren = 0
[zk: localhost:2181(CONNECTED) 26] get /zk_test
set_data_test
cZxid = 0x97
ctime = Tue Aug 14 23:30:22 GMT+08:00 2018
mZxid = 0x98
mtime = Tue Aug 14 23:40:40 GMT+08:00 2018
pZxid = 0x97
cversion = 0
dataVersion = 1
aclVersion = 0
ephemeralOwner = 0x0
dataLength = 13
numChildren = 0
h、delete /zk_test:删除节点,如下:
[zk: localhost:2181(CONNECTED) 27] delete /zk_test
[zk: localhost:2181(CONNECTED) 28] ls /
[dubbo, zookeeper]
三、配置不同安装模式
1、复制模式(replicated model:使用多台服务器配置,每个服务器都有一个zookeeper服务)
在独立模式下运行ZooKeeper便于评估,开发和测试。 但在生产中,您应该以复制(replicated )模式运行ZooKeeper。 同一应用程序中的复制服务器组称为仲裁(quorum),在复制模式下,仲裁中的所有服务器都具有相同配置文件的副本。
对于复制(replicated )模式,至少需要三台服务器,强烈建议您使用奇数个服务器。 如果您只有两台服务器,那么您处于这样的情况:如果其中一台服务器出现故障,则没有足够的机器来构成多数仲裁。 两台服务器本质上不如单一服务器稳定,因为有两个单点故障。
复制模式所需的conf / zoo.cfg文件类似于独立模式中使用的文件,但有一些差异。 这是一个例子:
tickTime=2000
dataDir=/var/lib/zookeeper
clientPort=2181
initLimit=5
syncLimit=2
#1代表服务器编号,在myid文件中存储的就是这个值;
#zoo1代表复制仲裁模式下zookeeper服务所在服务器IP地址;
#2888代表ZooKeeper服务器使用此端口将关注者连接到领导者;
#3888进行领导者选举的端口
server.1=zoo1:2888:3888
server.2=zoo2:2888:3888
server.3=zoo3:2888:3888
新条目initLimit是暂停ZooKeeper用于限制仲裁中ZooKeeper服务器连接到领导者的时间长度。条目syncLimit限制服务器与领导者的过期时间。
使用这两个超时,您可以使用tickTime指定时间单位。在这个例子中,initLimit的超时是2000个milleseconds的5个滴答,或者是10秒。
表单server.X的条目列出了构成ZooKeeper服务的服务器。当服务器启动时,它通过在数据目录(data directory)中查找文件myid来知道它是哪个服务器。该文件包含服务器编号,ASCII格式。
最后,记下每个服务器名称后面的两个端口号:“2888”和“3888”。对等方使用以前的端口连接到其他对等方。这种连接是必要的,以便对等方可以进行通信,例如,就更新的顺序达成一致。更具体地说,ZooKeeper服务器使用此端口将关注者连接到领导者。当新的领导者出现时,跟随者使用此端口打开与领导者的TCP连接。由于默认的领导者选举也使用TCP,我们目前需要另一个端口进行领导者选举。这是服务器条目中的第二个端口。
server.A=B:C:D
A:其中 A 是一个数字,表示这个是服务器的编号;
B:是这个服务器的 ip 地址;
C:Zookeeper服务器之间的通信端口;
D:Leader选举的端口.
2、一台服务器部署多个zookeeper服务
如果要在一台计算机上测试多个服务器,请将servername指定为localhost,并为该服务器中的每个server.X指定唯一的仲裁和领导者选举端口(即上例中的2888:3888,2889:3889,2890:3890) 配置文件。 当然,单独的dataDirs和不同的clientPorts也是必需的(在上面复制的示例中,在单个localhost上运行,您仍然会有三个配置文件)。
请注意,在一台计算机上设置多台服务器不会产生任何冗余。 如果发生导致机器死机的事情,所有zookeeper服务器都将脱机。 完全冗余要求每台服务器都有自己的机器。 它必须是完全独立的物理服务器。 同一物理主机上的多个虚拟机仍然容易受到该主机的完全故障的影响。
还有一些其他配置参数可以大大提高性能:
要在更新时获得较低的延迟,请务必拥有专用的事务日志目录。 默认情况下,事务日志与数据快照和myid文件放在同一目录中。 dataLogDir参数指示用于事务日志的不同目录。
3、伪集群模式配置文件如下:
a、server1配置文件
# sending a request and getting an acknowledgement
# 此配置项表示Leader与Follower之间发送消息时,请求和应答时间长度。
# 如果follower在设置时间内不能与leader通信,那么此follower将会被丢弃。
syncLimit=5
# the directory where the snapshot is stored.
# do not use /tmp for storage, /tmp here is just
# example sakes.
# 存储内存数据库快照的位置,除非另有说明,否则指向数据库更新的事务日志。
# 放置事务日志的位置要小心。 专用的事务日志设备是始终如一的良好性能的关键。 将日志置于繁忙的设备上会对性能产生负面影响。
dataDir=F:\\ProgramTools\\zookeeper\\zookeeper-1\\dataDir\\data
#此选项将指示计算机将事务日志写入dataLogDir而不是dataDir。 这允许使用专用的日志设备,并有助于避免日志记录和快照之间的竞争。
# 拥有专用的日志设备会对吞吐量和稳定的延迟产生很大影响。 强烈建议专用日志设备并将dataLogDir设置为指向该设备上的目录,然后确保将dataDir指向不驻留在该设备上的目录。
dataLogDir=F:\\ProgramTools\\zookeeper\\zookeeper-1\\dataDir\\log
# 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
# 限制由IP地址标识的单个客户端可以对ZooKeeper集合的单个成员进行的并发连接数(在套接字级别)。
# 这用于防止某些类别的DoS攻击,包括文件描述符耗尽。 默认值为60.将此值设置为0将完全删除并发连接的限制。
#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
# 启用后,ZooKeeper自动清除功能会分别在dataDir和dataLogDir中保留autopurge.snapRetainCount最新快照和相应的事务日志,并删除其余日志。 默认为3.最小值为3。
#autopurge.snapRetainCount=3
# Purge task interval in hours
# Set to "0" to disable auto purge feature
# 必须触发清除任务的时间间隔(以小时为单位)。 设置为正整数(1和更高)以启用自动清除。 默认为0。
#autopurge.purgeInterval=1
#ZooKeeper使用事务日志和快照来持久化每个事务(注意是日志先写)。该配置项指定ZooKeeper在将内存数据库序列化为快照之前,需要先写多少次事务日志。
#也就是说,每写几次事务日志,就快照一次。默认值为100000。为了防止所有的ZooKeeper服务器节点同时生成快照(一般情况下,所有实例的配置文件是完全相同的),
#当某节点的先写事务数量在(snapCount/2+1,snapCount)范围内时(挑选一个随机值),这个值就是该节点拍快照的时机。snapCount最小为2,如果设置为1,则默认为2.
snapCount=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
b、server2配置文件:
# The number of milliseconds of each tick
# ZooKeeper使用的基本时间单位(以毫秒为单位)。 它用于做心跳,最小会话超时将是tickTime的两倍。
tickTime=2000
# The number of ticks that the initial
# synchronization phase can take
# 此配置表示,允许follower(相对于Leaderer言的“客户端”)连接并同步到Leader的初始化连接时间,以tickTime为单位。
# 当初始化连接时间超过该值,则表示连接失败。
initLimit=10
# The number of ticks that can pass between
# sending a request and getting an acknowledgement
# 此配置项表示Leader与Follower之间发送消息时,请求和应答时间长度。
# 如果follower在设置时间内不能与leader通信,那么此follower将会被丢弃。
syncLimit=5
# the directory where the snapshot is stored.
# do not use /tmp for storage, /tmp here is just
# example sakes.
# 存储内存数据库快照的位置,除非另有说明,否则指向数据库更新的事务日志。
# 放置事务日志的位置要小心。 专用的事务日志设备是始终如一的良好性能的关键。 将日志置于繁忙的设备上会对性能产生负面影响。
dataDir=F:\\ProgramTools\\zookeeper\\zookeeper-2\\dataDir\\data
#此选项将指示计算机将事务日志写入dataLogDir而不是dataDir。 这允许使用专用的日志设备,并有助于避免日志记录和快照之间的竞争。
# 拥有专用的日志设备会对吞吐量和稳定的延迟产生很大影响。 强烈建议专用日志设备并将dataLogDir设置为指向该设备上的目录,然后确保将dataDir指向不驻留在该设备上的目录。
dataLogDir=F:\\ProgramTools\\zookeeper\\zookeeper-2\\dataDir\\log
# the port at which the clients will connect
# 这个端口就是客户端连接 Zookeeper 服务器的端口,Zookeeper 会监听这个端口,接受客户端的访问请求。
clientPort=2182
# the maximum number of client connections.
# increase this if you need to handle more clients
# 限制由IP地址标识的单个客户端可以对ZooKeeper集合的单个成员进行的并发连接数(在套接字级别)。
# 这用于防止某些类别的DoS攻击,包括文件描述符耗尽。 默认值为60.将此值设置为0将完全删除并发连接的限制。
#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
# 启用后,ZooKeeper自动清除功能会分别在dataDir和dataLogDir中保留autopurge.snapRetainCount最新快照和相应的事务日志,并删除其余日志。 默认为3.最小值为3。
#autopurge.snapRetainCount=3
# Purge task interval in hours
# Set to "0" to disable auto purge feature
# 必须触发清除任务的时间间隔(以小时为单位)。 设置为正整数(1和更高)以启用自动清除。 默认为0。
#autopurge.purgeInterval=1
#ZooKeeper使用事务日志和快照来持久化每个事务(注意是日志先写)。该配置项指定ZooKeeper在将内存数据库序列化为快照之前,需要先写多少次事务日志。
#也就是说,每写几次事务日志,就快照一次。默认值为100000。为了防止所有的ZooKeeper服务器节点同时生成快照(一般情况下,所有实例的配置文件是完全相同的),
#当某节点的先写事务数量在(snapCount/2+1,snapCount)范围内时(挑选一个随机值),这个值就是该节点拍快照的时机。snapCount最小为2,如果设置为1,则默认为2.
snapCount=1
#配置集群
server.1=localhost:2888:3888
server.2=localhost:2889:3889
server.3=localhost:2890:3890
c、server3配置文件:
# The number of milliseconds of each tick
# ZooKeeper使用的基本时间单位(以毫秒为单位)。 它用于做心跳,最小会话超时将是tickTime的两倍。
tickTime=2000
# The number of ticks that the initial
# synchronization phase can take
# 此配置表示,允许follower(相对于Leaderer言的“客户端”)连接并同步到Leader的初始化连接时间,以tickTime为单位。
# 当初始化连接时间超过该值,则表示连接失败。
initLimit=10
# The number of ticks that can pass between
# sending a request and getting an acknowledgement
# 此配置项表示Leader与Follower之间发送消息时,请求和应答时间长度。
# 如果follower在设置时间内不能与leader通信,那么此follower将会被丢弃。
syncLimit=5
# the directory where the snapshot is stored.
# do not use /tmp for storage, /tmp here is just
# example sakes.
# 存储内存数据库快照的位置,除非另有说明,否则指向数据库更新的事务日志。
# 放置事务日志的位置要小心。 专用的事务日志设备是始终如一的良好性能的关键。 将日志置于繁忙的设备上会对性能产生负面影响。
dataDir=F:\\ProgramTools\\zookeeper\\zookeeper-3\\dataDir\\data
#此选项将指示计算机将事务日志写入dataLogDir而不是dataDir。 这允许使用专用的日志设备,并有助于避免日志记录和快照之间的竞争。
# 拥有专用的日志设备会对吞吐量和稳定的延迟产生很大影响。 强烈建议专用日志设备并将dataLogDir设置为指向该设备上的目录,然后确保将dataDir指向不驻留在该设备上的目录。
dataLogDir=F:\\ProgramTools\\zookeeper\\zookeeper-3\\dataDir\\log
# the port at which the clients will connect
# 这个端口就是客户端连接 Zookeeper 服务器的端口,Zookeeper 会监听这个端口,接受客户端的访问请求。
clientPort=2183
# the maximum number of client connections.
# increase this if you need to handle more clients
# 限制由IP地址标识的单个客户端可以对ZooKeeper集合的单个成员进行的并发连接数(在套接字级别)。
# 这用于防止某些类别的DoS攻击,包括文件描述符耗尽。 默认值为60.将此值设置为0将完全删除并发连接的限制。
#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
# 启用后,ZooKeeper自动清除功能会分别在dataDir和dataLogDir中保留autopurge.snapRetainCount最新快照和相应的事务日志,并删除其余日志。 默认为3.最小值为3。
#autopurge.snapRetainCount=3
# Purge task interval in hours
# Set to "0" to disable auto purge feature
# 必须触发清除任务的时间间隔(以小时为单位)。 设置为正整数(1和更高)以启用自动清除。 默认为0。
#autopurge.purgeInterval=1
#ZooKeeper使用事务日志和快照来持久化每个事务(注意是日志先写)。该配置项指定ZooKeeper在将内存数据库序列化为快照之前,需要先写多少次事务日志。
#也就是说,每写几次事务日志,就快照一次。默认值为100000。为了防止所有的ZooKeeper服务器节点同时生成快照(一般情况下,所有实例的配置文件是完全相同的),
#当某节点的先写事务数量在(snapCount/2+1,snapCount)范围内时(挑选一个随机值),这个值就是该节点拍快照的时机。snapCount最小为2,如果设置为1,则默认为2.
snapCount=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
d、myid配置文件
在每个zookeeper服务中dataDir=F:\\ProgramTools\\zookeeper\\zookeeper-3\\dataDir\\data目录下新建名为myid,内容为配置中server.x中的x,比如server3的为server.1,所以myid文件里面只存储一个1.
e、然后用zkServer.cmd命令打开zookeeper服务器就可以了,刚开始会报错,但是都启动了就不会报错了。