Zookeeper


title: “Zookeeper”
createTime: 2022-01-05T11:36:51+08:00
updateTime: 2022-01-05T11:36:51+08:00
draft: false
author: “name”
tags: [“zookeeper”]
categories: [“install”]
description: “测试的”

zookeeper集群搭建

一、下载zookeeper

  • 进入将要安装的目录
  • cd /home/htga/zookeeper
  • wget https://mirrors.tuna.tsinghua.edu.cn/apache/zookeeper/zookeeper-3.4.14/zookeeper-3.4.14.tar.gz

二、解压

  • tar -zxvf zookeeper-3.4.14.tar.gz

三、复制配置文件名称

  • cd zookeeper-3.4.14/conf/
  • cp zoo_sample.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=/zookeeper/data/ #修改数据存放位置
# the port at which the clients will connect
dataLogDir=/zookeeper/logs #修改日志存放位置
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
#增加集群信息
server.1=192.168.200.128:2888:3888
server.2=192.168.200.129:2888:3888
server.3=192.168.200.130:2888:3888

五、设置每个节点的id

  • 在zoo.cfg中对应的 dataDir 目录下执行
  • echo 1 > myid 注意myid要一摸一样 1这个对应 zoo.cfg 中 集群信息 server.1

六、复制整个software目录到其他节点上

  • scp -r zookeeper/ 192.168.200.129:/home/htga/
  • scp -r zookeeper/ 192.168.200.130:/home/htga/
  • 依次修改129和130机器上的myid
  • echo 2 > myid
  • echo 3 > myid

七、关闭防火墙

  • systemctl stop firewalld.service #停止firewall
  • systemctl disable firewalld.service #禁止firewall开机启动
  • firewall-cmd --state #查看默认防火墙状态(关闭后显示notrunning,开启后显示running)

八、把zookeeper目录添加到环境变量

  • export ZOOKEEPER_HOME=/zookeeper/zookeeper-3.4.14/
  • export PATH= Z O O K E E P E R H O M E / b i n : ZOOKEEPER_HOME/bin: ZOOKEEPERHOME/bin:PATH
  • export PATH

九、启动zk集群

  • zookeeper-3.4.14至少启动3台节点,集群才能正常运行,一般zk集群搭建奇数节点数
  • ./bin/zkServer.sh start

十、查看zookeeper运行状态

    [root@node2 zookeeper]# zkServer.sh status
    ZooKeeper JMX enabled by default
    Using config: //software/zookeeper-3.4.14/bin/../conf/zoo.cfg
    Mode: leader

集群测试

  • 参考文章末尾的地址

相关命令

#启动ZK服务: 
bin/zkServer.sh start
#停止ZK服务: 
bin/zkServer.sh stop
#重启ZK服务: 
bin/zkServer.sh restart
#查看ZK服务状态: 
bin/zkServer.sh status

zoo.cfg配置参数解读

Server.1=192.168.200.128:2888:3888。

1 是一个数字,表示这个是第几号服务器;

192.168.200.128 是这个服务器的ip地址;

2888 是这个服务器与集群中的Leader服务器交换信息的端口;

3888 是万一集群中的Leader服务器挂了,需要一个端口来重新进行选举,选出一个新的Leader,而这个端口就是用来执行选举时服务器相互通信的端口。

集群模式下配置一个文件myid,这个文件在dataDir目录下,这个文件里面有一个数据就是A的值,Zookeeper启动时读取此文件,拿到里面的数据与zoo.cfg里面的配置信息比较从而判断到底是哪个server。

1)tickTime=2000:通信心跳数

tickTime:通信心跳数,Zookeeper服务器心跳时间,单位毫秒

Zookeeper使用的基本时间,服务器之间或客户端与服务器之间维持心跳的时间间隔,也就是每个tickTime时间就会发送一个心跳,时间单位为毫秒。

它用于心跳机制,并且设置最小的session超时时间为两倍心跳时间。(session的最小超时时间是2*tickTime)

2)initLimit=10:LF初始通信时限

集群中的follower跟随者服务器(F)与leader领导者服务器(L)之间初始连接时能容忍的最多心跳数(tickTime的数量),用它来限定集群中的Zookeeper服务器连接到Leader的时限。

投票选举新leader的初始化时间

Follower在启动过程中,会从Leader同步所有最新数据,然后确定自己能够对外服务的起始状态。

Leader允许F在initLimit时间内完成这个工作。

3)syncLimit=5:LF同步通信时限

集群中Leader与Follower之间的最大响应时间单位,假如响应超过syncLimit * tickTime,

Leader认为Follwer死掉,从服务器列表中删除Follwer。

在运行过程中,Leader负责与ZK集群中所有机器进行通信,例如通过一些心跳检测机制,来检测机器的存活状态。

如果L发出心跳包在syncLimit之后,还没有从F那收到响应,那么就认为这个F已经不在线了。

4)dataDir:数据文件目录+数据持久化路径

保存内存数据库快照信息的位置,如果没有其他说明,更新的事务日志也保存到数据库。

5)clientPort=2181:客户端连接端口

监听客户端连接的端口

注意事项

1. 启动第一台的时候如果查看状态显示报错,这是因为另外两台没有启动,启动第一台的时候一直在尝试连接另外两台的原因
2. 三台都启动后查看状态,如果一台状态为 Mode: leader,另外两台为Mode: follower,说明集群搭建成功
3. 如果查看状态为Error contacting service. It is probably not running
4.请查看同目录下的日志文件zookeeper.out日志,根据具体原因去解决问题
5.Caused by: java.lang.IllegalArgumentException: /zookeeper-3.4.14/data/myid file is missing
6.Datadir路径配置错误

参考网址

zookeeper集群搭建

zookeeper集群客户端测试

zookeeper集群搭建 & 数据同步性测试

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值