『網際網路架構』軟體架構-zookeeper叢集部署與快速入門(32)
目前的公司是使用的阿里內部的dubbo,也就是EDAS,裡面用了阿里自己的EDAS服務,如果是使用過dubbo的老鐵,應該知道zookeeper,zookeeper在大資料和RPC通訊上應用比較管飯。不管用過zookeeper沒有,這次主要是介紹下zookeeper和叢集的部署。這個必須要實際操作下,才能理解的深刻。
原始碼:https://github.com/limingios/netFuture/ 【zookeeper】
(一)介紹zookeeper
- 歷史
Zookeeper 最早起源於雅虎研究院的一個研究小組。在當時,研究人員發現,在雅虎內部很多大型系統基本都需要依賴一個類似的系統來進行分散式協調,但是這些系統往往都存在分散式單點問題。所以,雅虎的開發人員就試圖開發一個通用的無單點問題的分散式協調框架,以便讓開發人員將精力集中在處理業務邏輯上。
關於“ZooKeeper”這個專案的名字,其實也有一段趣聞。在立項初期,考慮到之前內部很多專案都是使用動物的名字來命名的(例如著名的Pig專案),雅虎的工程師希望給這個專案也取一個動物的名字。時任研究院的首席科學家 RaghuRamakrishnan 開玩笑地說:“在這樣下去,我們這兒就變成動物園了!”此話一出,大家紛紛表示就叫動物園管理員吧一一一因為各個以動物命名的分散式元件放在一起,雅虎的整個分散式系統看上去就像一個大型的動物園了,而 Zookeeper 正好要用來進行分散式環境的協調一一於是,Zookeeper 的名字也就由此誕生了。
- 為什麼zookeeper會火
20世紀60年代,大型機被髮明瞭出來,憑藉自身的超強計算和I/O處理能力以及穩定、安全性,成為了世界的主流。但是大型機也有一些致命的問題,比如造價昂貴、操作複雜、單點問題等。特別是對大型機的人才的培養成本非常之高。隨著這些問題的出現,不斷影響了大型機的發展。而另一方面PC機效能的不斷提升和網路普及,大家開始轉向了使用小型機和普通的PC伺服器來搭建分散式的計算機系統,來降級成本。而後分散式便不斷火熱起來。
- zookeeper的官網
https://zookeeper.apache.org/
下載地址: https://www-eu.apache.org/dist/zookeeper/
原始碼地址:https://github.com/apache/zookeeper
(二)叢集部署
叢集分為2類,一種是分散式叢集,一種是偽分散式叢集。
分散式:每個應用在單獨的獨立主機上,埠都是一致的。
偽分散式:多個應用在一臺主機上,埠進行區別。偽分散式在實際生產環境很少。
對於操作來說偽分散式叢集更難一些。
mac 安裝vgarant :https://idig8.com/2018/07/29/docker-zhongji-07/
window安裝vgaranthttps://idig8.com/2018/07/29/docker-zhongji-08/
系統型別 | IP地址 | 節點角色 | CPU | Memory | Hostname |
---|---|---|---|---|---|
Centos7 | 192.168.69.100 | 偽分散式 | 2 | 2G | zookeeper-virtua |
Centos7 | 192.168.69.101 | 真分散式-領導者 | 2 | 2G | zookeeper-Leader |
Centos7 | 192.168.69.102 | 真分散式-屬下1 | 2 | 2G | zookeeper-Follower1 |
Centos7 | 192.168.69.103 | 真分散式-屬下2 | 2 | 2G | zookeeper-Follower2 |
src的小技巧,這樣就有顏色了,之前一直忽略了,看的眼疼,現在顏色分明瞭好多了。
-
(2.1)偽環境配置
>還是用vagrant來,自從熟悉了vagrant 我基本沒手動建立過虛擬機器。
(2.1.1)基礎設定
su #密碼 vagrant cd ~ vi /etc/ssh/sshd_config sudo systemctl restart sshd vi /etc/resolv.conf # 設定成8.8.8.8 service network restart
(2.1.2)jdk安裝
指令碼在我的原始碼裡面
vi pro.sh sh pro.sh
(2.1.3)zookeeper下載
下載工具千萬切記用最新的已經出到3.5.4,我還是用3.4.10
wget https://www-eu.apache.org/dist/zookeeper/zookeeper-3.4.10/zookeeper-3.4.10.tar.gz
(2.1.4)解壓zookeeper
tar zxvf zookeeper-3.4.10.tar.gz
(2.1.5)進入zk中的conf目錄下拷貝3個檔案
cd /root/zookeeper-3.4.10/conf cp zoo_sample.cfg zoo1.cfg cp zoo_sample.cfg zoo2.cfg cp zoo_sample.cfg zoo3.cfg
(2.1.6) 編輯這3個檔案zoo1.cfg,zoo2.cfg,zoo3.cfg
(2.1.6.1)編輯zoo1.cfg
vi zoo1.cfg
dataDir=/apps/servers/data/d_1dataLogDir=/apps/servers/logs/logs_1clientPort=2181
# 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=/apps/servers/data/d_1 dataLogDir=/apps/servers/logs/logs_1 # 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 server.1=localhost:2187:2887 server.2=localhost:2188:2888 server.3=localhost:2189:2889
(2.1.6.2)編輯zoo2.cfg
vi zoo2.cfg
dataDir=/apps/servers/data/d_2dataLogDir=/apps/servers/logs/logs_2clientPort=2182
# 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=/apps/servers/data/d_2 dataLogDir=/apps/servers/logs/logs_2 # the port at which the clients will connect clientPort=2182 # 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=localhost:2187:2887 server.2=localhost:2188:2888 server.3=localhost:2189:2889
(2.1.6.3)編輯zoo3.cfg
vi zoo3.cfg
dataDir=/apps/servers/data/d_3dataLogDir=/apps/servers/logs/logs_3clientPort=2183
# 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=/apps/servers/data/d_3 dataLogDir=/apps/servers/logs/logs_3 # the port at which the clients will connect clientPort=2183 # 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=localhost:2187:2887 server.2=localhost:2188:2888 server.3=localhost:2189:2889
(2.1.7)建立data目錄和日誌目錄
mkdir -p /apps/servers/data/d_1 mkdir -p /apps/servers/data/d_2 mkdir -p /apps/servers/data/d_3 mkdir -p /apps/servers/logs/logs_1 mkdir -p /apps/servers/logs/logs_2 mkdir -p /apps/servers/logs/logs_3 echo "1" >/apps/servers/data/d_1/myid echo "2" >/apps/servers/data/d_2/myid echo "3" >/apps/servers/data/d_3/myid
(2.1.8)進入bin目錄下輸入命令 分別進行啟動
cd /root/zookeeper-3.4.10/bin sh zkServer.sh start ../conf/zoo1.cfg sh zkServer.sh start ../conf/zoo2.cfg sh zkServer.sh start ../conf/zoo3.cfg
(2.1.9)進入每一個看看效果
source /etc/profile sh zkCli.sh -server localhost:2181 sh zkCli.sh -server localhost:2182 sh zkCli.sh -server localhost:2183
偽分散式其實就是這樣就搭建完畢了。重點還是分散式的往下看。
- (1.2)分散式環境配置
(1.2.1)基礎設定(三臺機器都需要設定)
su #密碼 vagrant cd ~ vi /etc/ssh/sshd_config sudo systemctl restart sshd vi /etc/resolv.conf # 設定成8.8.8.8 service network restart
(1.2.2)jdk安裝(三臺機器都需要設定)
指令碼在我的原始碼裡面
vi pro.sh sh pro.sh
(1.2.3)zookeeper下載(三臺機器都需要設定)
下載工具千萬切記用最新的已經出到3.5.4,我還是用3.4.10
為什麼是三個,因為Zookeeper喜歡奇數不喜歡偶數。
wget https://www-eu.apache.org/dist/zookeeper/zookeeper-3.4.10/zookeeper-3.4.10.tar.gz
(1.2.4)解壓zookeeper
tar zxvf zookeeper-3.4.10.tar.gz
(1.2.4)配置cfg檔案(三臺機器都需要設定)
cd ~ cd zookeeper-3.4.10/ cd conf cp zoo_sample.cfg zoo.cfg
(1.2.5)配置cfg檔案,其實這3個機器的配置檔案是一樣的,我就不重複寫cfg了,就直接截圖了
vi 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 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 server.0=192.168.69.101:2888:3888 server.1=192.168.69.102:2888:3888 server.2=192.168.69.103:2888:3888
(1.2.6)配置myid
需要配置到上邊dataDir=/tmp/zookeeper的目錄下。
cd / cd tmp mkdir zookeeper cd zookeeper
(1.2.6.1)192.168.69.101配置myid
echo '0'>myid cat myid
(1.2.6.2)192.168.69.102配置myid
echo '1'>myid cat myid
(1.2.6.3)192.168.69.103配置myid
echo '2'>myid cat myid
啟動命令執行3臺虛擬機器下的zookeeper
cd ~/zookeeper-3.4.10/bin sh zkServer.sh start
####(三) 概念梳理
- (3.1)Zoo.cfg 配置
引數 | 意義 |
---|---|
tickTime | 2000 |
syncLimit | Leader 和 follower 之間的通訊時長 最長不能超過initLimt*ticktime |
initLimt | 接受客戶端連結 zk 初始化的最長等待心跳時長initLimt*ticktime |
dataDir | 資料目錄 |
dataLogDir | 日誌檔案 |
clientPort | 客戶端連結服務端埠號Server.A=B:C:D A:第幾號伺服器 B 服務 IP C 代表 Leader 和 follower 通訊埠 D 備用選 leader 埠 |
- (3.2)角色
Leader:
Leader 作為整個 ZooKeeper 叢集的主節點,負責響應所有對 ZooKeeper 狀態變更的請求。它會將每個狀態更新請求進行排序和編號,以便保證整個叢集內部訊息處理的 FIFO,寫操作都走 leader
Follower :
Follower 的邏輯就比較簡單了。除了響應本伺服器上的讀請求外,follower 還要處理leader 的提議,並在 leader 提交該提議時在本地也進行提交。 另外需要注意的是,leader 和 follower 構成 ZooKeeper 叢集的法定人數,也就是說,只有他們才參與新 leader的選舉、響應 leader 的提議。
Observer :
如果 ZooKeeper 叢集的讀取負載很高,或者客戶端多到跨機房,可以設定一些 observer伺服器,以提高讀取的吞吐量。Observer 和 Follower 比較相似,只有一些小區別:首先observer 不屬於法定人數,即不參加選舉也不響應提議;其次是 observer 不需要將事務持
久化到磁碟,一旦 observer 被重啟,需要從 leader 重新同步整個名字空間。
-
(3.3)Zookeeper 特性
>Zookeeper 是一個由多個 server 組成的
1.叢集一個 leader,多個 follower
2.每個 server 儲存一份資料副本
3.全域性資料一致分散式讀follower,寫由 leader 實施更新請求轉發,由 leader 實施更新請求順序進行,來自同一個 client 的更新請求按其傳送順序依次執行資料更新原子性,
4.一次資料更新要麼成功,要麼失敗全域性唯一資料檢視,
5.client 無論連線到哪個 server,資料檢視都是一致的實時性,在一定事件範圍內,client 能讀到最新資料
PS:本次主要說說zookeeper的原理和叢集部署,沒有太詳細的介紹裡面的細節,下次說說zookeeper使用。
>>原創文章,歡迎轉載。轉載請註明:轉載自IT人故事會,謝謝!
>>原文連結地址:上一篇:已是最新文章