文章目录
概述
//奇数台解释,以及半数以上人同意才可进行一致性读写操作
举个通俗易懂的例子,就好比开会需要做决定,
如果有5个人参与讨论,只要有3个人同意,就可以做出决定。
但如果只有4个人参与讨论,那么如果有2个人持不同意见,就无法做出决定
因此,为了确保能够做出决定,最好让参与讨论的人数为奇数。这样就能够确保在发生分歧时,仍然能够做出决定。
架构图
Zookeeper内部数据结构
安装
Linux
//解压
mkdir /opt/module/ && tar -zxvf apache-zookeeper-3.9.1-bin.tar.gz -C /opt/module && cd /opt/module/
//修改目录名
mv apache-zookeeper-3.9.1-bin zookeeper-3.9.1
//创建zookeeper数据目录
cd zookeeper-3.9.1 && mkdir zkData
//修改配置文件 == 数据目录改为zkData
cp conf/zoo_sample.cfg conf/zoo.cfg
sed -i '/^dataDir=/c\dataDir=/opt/module/zookeeper-3.9.1/zkData' conf/zoo.cfg
//查看配置文件
more conf/zoo.cfg
//==============服务端======================
//启动zookeeper服务端 == 注意确保2181、39306、8080 这三个端口不会被占用,否则启动失败
// zookeeper服务端启动
cd /opt/module/zookeeper-3.9.1/bin && sh zkServer.sh start
// zookeeper服务端状态查看
cd /opt/module/zookeeper-3.9.1 && sh bin/zkServer.sh status
// zookeeper服务端状态停止
cd /opt/module/zookeeper-3.9.1 && sh bin/zkServer.sh stop
//查看zookeeper服务端是否启动成功 == 出现QuorumPeerMain说明启动成功
jps -l
//查看zookeeper服务端占用的端口号
netstat -anlp |grep $(jps -l | grep QuorumPeerMain | awk '{print $1}') | grep tcp
//==============客户端======================
cd /opt/module/zookeeper-3.9.1 && sh bin/zkCli.sh
//查看zookeeper客户端占用的端口号
netstat -anlp |grep $(jps -l | grep ZooKeeperMain | awk '{print $1}') | grep tcp
服务端相关
客户端相关
Windows
配置参数
//tickTime
默认2000
通信心跳时间,Zookeeper服务器与客户端心跳时间,单位毫秒
//initLimit
默认10
【建立连接】
Leader和Follower初始连接时能容忍的最多心跳数(tickTime的数量)
LF初始通信时限(tickTime * initLimit时间范围内未通信成功,则认为失败)、zookeeper集群Leader与Follower的通信时限
//syncLimit
默认5
【建立连接之后的通信】
Leader和Follower.之间通信时间如果超过syncLimit*tickTime,Leader认为Follwer死掉,从服务器列表中删除Follwer。
//dataDir
保存Zookeeper中的数据
//clientPort
客户端连接端口,通常不做修改。
集群
参考配置文件
# 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/module/zookeeper-3.9.1/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.
#
# https://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.httpHost=0.0.0.0
#metricsProvider.httpPort=7000
#metricsProvider.exportJvmInfo=true
# 集群配置
server.107=192.168.19.107:2888:3888
server.108=192.168.19.108:2888:3888
server.109=192.168.19.109:2888:3888
配置步骤流程
//===========================
// 在107里面运行下面的命令
//集群必须中每台zookeeper必须设置一个唯一的身份证号
echo 107 > /opt/module/zookeeper-3.9.1/zkData/myid
//将107里面的zookeeper同步到另外两台服务器上
rsync -avz /opt/module/ root@192.168.19.108:/opt/module
rsync -avz /opt/module/ root@192.168.19.109:/opt/module
// 在108里面运行下面的命令
echo 108 > /opt/module/zookeeper-3.9.1/zkData/myid
// 在109里面运行下面的命令
echo 109 > /opt/module/zookeeper-3.9.1/zkData/myid
//================================================
//=================
//================================================
//配置文件添加集群信息 == opt/module/zookeeper-3.9.1/conf/zoo.cfg
server.zookeeper的身份证号=zookeeper的ip:集群间的通信端口号:集群leader挂了重新选举新Leader的端口号
//配置文件添加的信息
# 集群配置
server.107=192.168.19.107:2888:3888
server.108=192.168.19.108:2888:3888
server.109=192.168.19.109:2888:3888
//同步文件过去
rsync -avz /opt/module/zookeeper-3.9.1/conf/zoo.cfg root@192.168.19.108:/opt/module/zookeeper-3.9.1/conf
rsync -avz /opt/module/zookeeper-3.9.1/conf/zoo.cfg root@192.168.19.109:/opt/module/zookeeper-3.9.1/conf
启动
107服务器
108服务器
107服务器
109服务器
刚兴趣的同行可以进群沟通交流,内置机器人供大家愉快