zl程序教程

您现在的位置是:首页 >  其他

当前栏目

zookeeper配置文件

2023-04-18 16:26:18 时间


zookeeper配置文件

# 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=/var/zkdata
# 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

## Metrics Providers
#
# https://prometheus.io Metrics Exporter
#metricsProvider.className=org.apache.zookeeper.metrics.prometheus.PrometheusMetricsProvider
#metricsProvider.httpPort=7000
#metricsProvider.exportJvmInfo=true

zookeeper的默认配置文件为 zookeeper/conf/zoo_sample.cfg ,需要将其修改为zoo.cfg

其中各配置项含义如下:

tickTime

(tickTime=2000):Client-Server通信心跳时间

zookeeper服务器之间或客户端与服务器之间维持心跳的时间间隔,也就是每个tickTime时间就会发送一个心跳。

tickTime以毫秒为单位

initLimit

(initLimit=10):Leader - Follower 初始通信时限。

用来配置 Zookeeper 接受客户端(这里所说的客户端不是用户连接 Zookeeper 服务器的客户端,而是 Zookeeper 服务器集群中连接到 Leader 的 Follower 服务器)初始化连接时最长能忍受多少个心跳时间间隔数。当已经超过 5 个心跳的时间(也就是 tickTime)长度后 Zookeeper 服务器还没有收到客户端的返回信息,那么表明这个客户端连接失败。总的时间长度就是 5*2000 = 10 秒。

syncLimit

(syncLimit=5):Leader- Follower 同步通信时限。

这个配置项标识 Leader 与 Follower 之间发送消息,请求和应答时间长度,最长不能超过多少个 tickTime 的时间长度,总的时间长度就是 5*2000 = 10 秒。

dataDir

(dataDir=c:apache-zookeeper-3.6.1-bindata):数据文件目录。

Zookeeper 保存数据的目录,默认情况下,Zookeeper 将写数据的日志文件也保存在这个目录里。

clientPort

(clientPort=2181):客户端连接端口。

​ 客户端连接 Zookeeper 服务器的端口,Zookeeper 会监听这个端口,接受客户端的访问请求。

maxClientCnxns

(maxClientCnxns=60):对于一个客户端的连接数最大限制。

默认是 60,这在大部分时候是足够了。但是在我们实际使用中发现,在测试环境经常超过这个数,经过调查发现有的团队将几十个应用全部部署到一台机器上,以方便测试,于是这个数字就超过了。

autopurge.snapRetainCount、autopurge.purgeInterval

设置清除时间和保留个数。

客户端在与 zookeeper 交互过程中会产生非常多的日志,而且 zookeeper 也会将内存中的数据作为 snapshot 保存下来,这些数据是不会被自动删除的,这样磁盘中这样的数据就会越来越多。不过可以通过这两个参数来设置,让 zookeeper 自动删除数据。

  • autopurge.purgeInterval 就是设置多少小时清理一次。

  • autopurge.snapRetainCount 是设置保留多少个 snapshot ,之前的则删除。

服务器名称和地址

服务器名称与地址(服务器编号,服务器地址,LF 通信端口,选举端口),这个配置项的书写格式比较特殊。

server.A= B:C:D 
  • A:其中 A 是一个数字,表示这个是第几号服务器
  • B:B 是这个服务器的 ip 地址
  • C:C 表示的是这个服务器与集群中的 Leader 服务器交换信息的端口
  • D:D 表示的是万一集群中的 Leader 服务器挂了,需要一个端口来重新进行选举,选出一个新的 Leader,而这个端口就是用来执行选举时服务器相互通信的端口

如果是伪集群的配置方式,由于 B 都是一样,所以不同的 Zookeeper 实例通信端口号不能一样,所以要给它们分配不同的端口号。

server.1 = itcast05:2888:3888
 
server.2 = itcast06:2888:3888
 
server.3 = itcast07:2888:3888   

myid 的值是 zoo.cfg 文件里定义的 server.AA 的值,Zookeeper 启动时会读取这个文件,拿到里面的数据与 zoo.cfg 里面的配置信息比较从而判断到底是那个 server,只是一个标识作用。