文章目錄
- 概述
- 安裝
- Linux
- Windows
- 配置參數
- 集群
- 參考配置文件
- 配置步驟流程
- 啟動
概述
Zookeeper: 為分布式框架組件提供協調服務的中間件 == 【類似:文件系統+通知機制】== 負責存儲上下層應用關系的數據以及接收觀察者注冊監聽,一旦觀察查關心的數據發生變化,則Zookeeper會將數據變化推送給觀察者
?
官網: https://zookeeper.apache.org/
?
組成: Zookeeper由一個領導者(Leader),多個跟隨者(Follower)組成的集群 == 半數以上的節點存活,集群才能正常工作,并且建議安裝奇數臺服務器集群
?
全局數據一致: 每個Server保存一份相同的數據副本,Client無論連接到哪個Server,數據都是一致的。
?
實時性: 客戶端A更新信息到服務端A,則客戶端B能在一定很快的時間范圍內也同步回最新服務端A數據
?
Zookeeper內部數據結構: 樹結構,每個節點最多存儲1MB,并且每個數據節點有節點唯一標識進行識別區分
?
Zookeeper集群同步邏輯: 客戶端向ZooKeeper集群發送寫請求時,請求 首先會發送給Leader節點 ,Leader節點會將該請求廣播給所有Follower節點,只有當大多數節點(包括Leader節點)都寫入成功后,寫操作才會被確認為成功(并通知客戶端,客戶端收到寫入成功響應無需等待集群中所有機器都寫入成功,只需過半機器寫入成功即可,后續慢慢會執行同步,最終達到zxid一致) ,從而保證數據的一致性
//奇數臺解釋,以及半數以上人同意才可進行一致性讀寫操作
舉個通俗易懂的例子,就好比開會需要做決定,
如果有5個人參與討論,只要有3個人同意,就可以做出決定。
但如果只有4個人參與討論,那么如果有2個人持不同意見,就無法做出決定
因此,為了確保能夠做出決定,最好讓參與討論的人數為奇數。這樣就能夠確保在發生分歧時,仍然能夠做出決定。
?
架構圖
Zookeeper內部數據結構
安裝
Linux
下載地址: https://archive.apache.org/dist/zookeeper/
//解壓
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
配置參數
配置文件路徑: /opt/module/zookeeper-3.9.1/conf/zoo.cfg
//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、108、109
//===========================
// 在107里面運行下面的命令//集群必須中每臺zookeeper必須設置一個唯一的身份證號echo 107 > /opt/module/zookeeper-3.9.1/zkData/myid //將107里面的zookeeper同步到另外兩臺服務器上rsync -avz /opt/module/ root@192.168.19.108:/opt/modulersync -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服務器