zookeeper的搭建和简单的使用

一、什么是zookeeper,有什么用

  ZooKeeper是一个分布式的,开放源码的分布式应用程序协调服务,是Google的Chubby一个开源的实现,它是集群的管理者,监视着集群中各个节点的状态根据节点提交的反馈进行下一步合理操作。最终,将简单易用的接口和性能高效、功能稳定的系统提供给用户(来自百度百科)。

  其主要的功能有

    1.命名服务   2.配置管理   3.集群管理   4.分布式锁  5.队列管理

二、zookeeper的单机部署

  1.下载并解压 apache-zookeeper-3.5.8-bin.tar.gz  

  2.将conf目录下zoo_sample.cfg配置文件修改为zoo.cfg。

  3.修改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
=/tmp/zookeeper
#日志存放的位置(新加的配置,默认在zookeeper的bin目录下的zookeeper.out)
dataLogDir=/tmp/zookeeper/logs # 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 ~

三、zookeeper集群搭建

  1.在三台服务器上分别解压zookeeper,并配置

  2.分别将conf目录下zoo_sample.cfg配置文件修改为zoo.cfg,并修改zoo.conf配置文件

# 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=/tmp/zookeeper
# the port at which the clients will connect
clientPort=2180
# 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.0=172.18.12.55:2288:3388
server.1=172.18.12.56:2288:3388
server.2=172.18.12.57:2288:3388

修改的内容:

  ①、tickTime:基本事件单元,这个时间是作为Zookeeper服务器之间或客户端与服务器之间维持心跳的时间间隔,每隔tickTime时间就会发送一个心跳;最小 的session过期时间为2倍tickTime

  ②、dataDir:存储内存中数据库快照的位置,除非另有说明,否则指向数据库更新的事务日志。注意:应该谨慎的选择日志存放的位置,使用专用的日志存储设备能够大大提高系统的性能,如果将日志存储在比较繁忙的存储设备上,那么将会很大程度上影像系统性能。

  ③、client:监听客户端连接的端口。

  ④、initLimit:允许follower连接并同步到Leader的初始化连接时间,以tickTime为单位。当初始化连接时间超过该值,则表示连接失败。

  ⑤、syncLimit:表示Leader与Follower之间发送消息时,请求和应答时间长度。如果follower在设置时间内不能与leader通信,那么此follower将会被丢弃。

  ⑥、server.A=B:C:D

    A:其中 A 是一个数字,表示这个是服务器的编号;

    B:是这个服务器的 ip 地址;

    C:Leader选举的端口;

    D:Zookeeper服务器之间的通信端口。

  我们需要修改的第一个是 dataDir ,在指定的位置处创建好目录。

  第二个需要新增的是 server.A=B:C:D 配置,其中 A 对应下面我们即将介绍的myid 文件。B是集群的各个IP地址,C:D 是端口配置。

四、在dataDir目录里创建myid文件,文件内容为配置文件里server.A=B:C:D的A

五、配置环境变量

 为了能够在任意目录启动zookeeper集群,我们需要配置环境变量。

  ps:你也可以不配,这不是搭建集群的必要操作,只不过如果你不配置环境变量,那么每次启动zookeeper需要到安装文件的 bin 目录下去启动。

  首先打开 /etc/profile,添加相应的配置信息:

export ZK_HOME=/usr/local/software/apache-zookeeper-3.5.8-bin
export PATH=$PATH:$ZK_HOME/bin

然后通过如下命令使得环境变量生效:

source /etc/profle

六、启动zookeeper服务

启动命令:

zkServer.sh start

  停止命令:

zkServer.sh stop

  重启命令:

zkServer.sh restart

  查看集群节点状态:

zkServer.sh status

 我们分别对集群三台机器执行启动命令。执行完毕后,分别查看集群节点状态

 

 

 三台机器,kafka1 成功的通过了选举称为了leader,而剩下的两台成为了 follower。这时候,如果你将kafka1关掉,会发现剩下两台又会有一台变成了 leader节点。

七、注意事项

1.关闭防火墙

systemctl stop firewalld
systemctl disable firewalld

2.创建dataDir 配置的目录

原文地址:https://www.cnblogs.com/navysummer/p/13697099.html