栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

Zookeeper集群配置(单机搭建)

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

Zookeeper集群配置(单机搭建)

Zookeep

目录

Zookeeper

特点

配置文件解析

单机集群搭建

选举机制


Zookeeper

        Zookeeper是一个分布式应用协调服务,本质就是文件系统加通知机制,Zookeeper以树形结构存储数据,每个节点成为一个ZNode,每个ZNode默认存储1MB数据

Zookeeper可以作为微服务的注册中心,它在CAP理论中保证的是CP,Eureka保证的是AP

特点

Zookeeper集群只有一个领导者leader,多个跟随者组成follower集群中只要有半数以上的节点存活,Zookeeper集群就能正常服务,所以Zookeeper适合奇数台服务器全局数据一致:每个follower里面的数据都是一致,保存的是数据副本,从leader更新数据来自同一个Client的更新请求按照其顺序依次执行,队列先进先出数据更新原子性,不可被分割,要么全部更新成功要么全部更新失败实时性,在一定的时间范围馁,Client能读到最新的数据,数据同步

配置文件解析
# The number of milliseconds of each tick
tickTime=2000		#服务器和客户端的心跳时间,单位毫秒

# The number of ticks that the initial 
# synchronization phase can take
initLimit=10		#Leader和Follower初始化的通信时间 10个心跳时间 即 10*2=20秒

# The number of ticks that can pass between 
# sending a request and getting an acknowledgement
syncLimit=5		#Leader和Follower同步通信时间,如果超过这个时间服务器认为Follower死亡并从列表中删除 5个心跳时间 即 5*2=10秒

# the directory where the snapshot is stored.
# do not use /tmp for storage, /tmp here is just 
# example sakes.
dataDir=/opt/zookeeper-3.5.7/data		#存储zookeeper中的数据,默认存储在tmp目录,会被定期删除,需要自己定义

# the port at which the clients will connect
clientPort=2181		#客户端连接服务器时用的端口

选举机制

服务器初始化选举:

假设有5台Zookeeper进行集群。分别为1,2,3,4,5号服务器,在选举的时候有半数以上的票数的follower成为leader

在Zookeeper第一次启动的时候,1号服务器启动,发起一次选举,投自己1票,此时1号服务器票数1票,不够半数以上(3票),选举无法完成,1号服务器状态保持为LOOKING

2号服务器启动时,在发起一次选举,先投自己一票,然后与1号服务器交换结果,由于2号服务器myid大于1号所以,1号服务器更改选票给2号服务器,此时2号服务器有2票,1号服务器0票

3号服务器,发起选举,此时1号和2号的都会把选票给3号服务器,此时3号服务器有3票已经超过集群总量的半数,选举成为leader,后面4号和5号服务器启动时,因为已经有leader产生,所以4号和5号成为follower

SID:服务器ID,用来标识一台Zookeeper集群中的机器,每台机器都不能重复和myid一致

zxid:事务id,用来标识一次服务器状态的变更

Epoch:每个Leader任期的代号,没有Leader时,同一轮通票过程中的逻辑时钟值是相同的

服务器非初始化选举:

服务器运行期间无法和Leader保持连接时,会重新选举

重新选举的时候先按照Epoch比较,Epoch最大的服务器直接当选Leader,如果Epoch一样则选择ZXID最大的当选Leader,以上2者都一样的话进行SID比较,SID最大的当选Leader

单机集群搭建

本实例在单台Linux服务器开启三个Zookeeper服务组成集群,分别是zookeeper-3.5.7,zookeeper02-3.5.7,zookeeper03-3.5.7

第一个Zookeeper服务配置文件,后面2个服务配置文件只需要修改clientPort端口号,不能重复,dataDir文件保存路径,不能重复否则会认为是同一个Zookeeper服务

因为是在一个Linux服务器上,所以2888和3888端口不能重复

# 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=/opt/zookeeper-3.5.7/data
# 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

#########################cluster###########################
server.1=127.0.0.1:2888:3888
server.2=127.0.0.1:2889:3889
server.3=127.0.0.1:2890:3890

分别启动三个Zookeeper服务

zookeeper-3.5.7 因为zookeeper的选举机制,第一台服务器启动报错,因为配置文件配置了集群,需要启动半数以上服务器才会形成集群关系

 zookeeper02-3.5.7 启动并查看状态已经和第一台服务器形成集群

zookeeper03-3.5.7

连接到Zookeeper

[root@lixiaofeng zookeeper-3.5.7]# bin/zkCli.sh -server localhost:2181		#第一台服务器

[root@lixiaofeng zookeeper02-3.5.7]# bin/zkCli.sh -server localhost:2191	#第二台服务器

[root@lixiaofeng zookeeper03-3.5.7]# bin/zkCli.sh -server localhost:2183	#第三台服务器

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/735260.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号