目前的公司是使用的阿里內(nèi)部的dubbo,也就是EDAS,里面用了阿里自己的EDAS服務(wù),如果是使用過(guò)dubbo的老鐵,應(yīng)該知道zookeeper,zookeeper在大數(shù)據(jù)和RPC通信上應(yīng)用比較管飯。不管用過(guò)zookeeper沒(méi)有,這次主要是介紹下zookeeper和集群的部署。這個(gè)必須要實(shí)際操作下,才能理解的深刻。
(一)介紹zookeeper
- 歷史
Zookeeper 最早起源于雅虎研究院的一個(gè)研究小組。在當(dāng)時(shí),研究人員發(fā)現(xiàn),在雅虎內(nèi)部很多大型系統(tǒng)基本都需要依賴一個(gè)類似的系統(tǒng)來(lái)進(jìn)行分布式協(xié)調(diào),但是這些系統(tǒng)往往都存在分布式單點(diǎn)問(wèn)題。所以,雅虎的開(kāi)發(fā)人員就試圖開(kāi)發(fā)一個(gè)通用的無(wú)單點(diǎn)問(wèn)題的分布式協(xié)調(diào)框架,以便讓開(kāi)發(fā)人員將精力集中在處理業(yè)務(wù)邏輯上。
關(guān)于“ZooKeeper”這個(gè)項(xiàng)目的名字,其實(shí)也有一段趣聞。在立項(xiàng)初期,考慮到之前內(nèi)部很多項(xiàng)目都是使用動(dòng)物的名字來(lái)命名的(例如著名的Pig項(xiàng)目),雅虎的工程師希望給這個(gè)項(xiàng)目也取一個(gè)動(dòng)物的名字。時(shí)任研究院的首席科學(xué)家 RaghuRamakrishnan 開(kāi)玩笑地說(shuō):“在這樣下去,我們這兒就變成動(dòng)物園了!”此話一出,大家紛紛表示就叫動(dòng)物園管理員吧一一一因?yàn)楦鱾€(gè)以動(dòng)物命名的分布式組件放在一起,雅虎的整個(gè)分布式系統(tǒng)看上去就像一個(gè)大型的動(dòng)物園了,而 Zookeeper 正好要用來(lái)進(jìn)行分布式環(huán)境的協(xié)調(diào)一一于是,Zookeeper 的名字也就由此誕生了。
- 為什么zookeeper會(huì)火
20世紀(jì)60年代,大型機(jī)被發(fā)明了出來(lái),憑借自身的超強(qiáng)計(jì)算和I/O處理能力以及穩(wěn)定、安全性,成為了世界的主流。但是大型機(jī)也有一些致命的問(wèn)題,比如造價(jià)昂貴、操作復(fù)雜、單點(diǎn)問(wèn)題等。特別是對(duì)大型機(jī)的人才的培養(yǎng)成本非常之高。隨著這些問(wèn)題的出現(xiàn),不斷影響了大型機(jī)的發(fā)展。而另一方面PC機(jī)性能的不斷提升和網(wǎng)絡(luò)普及,大家開(kāi)始轉(zhuǎn)向了使用小型機(jī)和普通的PC服務(wù)器來(lái)搭建分布式的計(jì)算機(jī)系統(tǒng),來(lái)降級(jí)成本。而后分布式便不斷火熱起來(lái)。
- zookeeper的官網(wǎng)
https://zookeeper.Apache.org/
下載地址: https://www-eu.apache.org/dist/zookeeper/
image.png
源碼地址:https://github.com/apache/zookeeper
(二)集群部署
集群分為2類,一種是分布式集群,一種是偽分布式集群。
分布式:每個(gè)應(yīng)用在單獨(dú)的獨(dú)立主機(jī)上,端口都是一致的。
偽分布式:多個(gè)應(yīng)用在一臺(tái)主機(jī)上,端口進(jìn)行區(qū)別。偽分布式在實(shí)際生產(chǎn)環(huán)境很少。
對(duì)于操作來(lái)說(shuō)偽分布式集群更難一些。
mac 安裝vgarant :https://idig8.com/2018/07/29/Docker-zhongji-07/
window安裝vgaranthttps://idig8.com/2018/07/29/docker-zhongji-08/
系統(tǒng)類型IP地址節(jié)點(diǎn)角色CPUMemoryHostnamecentos7192.168.69.100偽分布式22Gzookeeper-virtuaCentos7192.168.69.101真分布式-領(lǐng)導(dǎo)者22Gzookeeper-LeaderCentos7192.168.69.102真分布式-屬下122Gzookeeper-Follower1Centos7192.168.69.103真分布式-屬下222Gzookeeper-Follower2
src的小技巧,這樣就有顏色了,之前一直忽略了,看的眼疼,現(xiàn)在顏色分明了好多了。
- (2.1)偽環(huán)境配置
還是用vagrant來(lái),自從熟悉了vagrant 我基本沒(méi)手動(dòng)創(chuàng)建過(guò)虛擬機(jī)。
(2.1.1)基礎(chǔ)設(shè)置
su #密碼 vagrant cd ~ vi /etc/ssh/sshd_config sudo systemctl restart sshd vi /etc/resolv.conf # 設(shè)置成8.8.8.8 service network restart
(2.1.2)jdk安裝
腳本在我的源碼里面
vi pro.sh sh pro.sh
(2.1.3)zookeeper下載
下載工具千萬(wàn)切記用最新的已經(jīng)出到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)進(jìn)入zk中的conf目錄下拷貝3個(gè)文件
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個(gè)文件zoo1.cfg,zoo2.cfg,zoo3.cfg
(2.1.6.1)編輯zoo1.cfg
vi zoo1.cfg
dataDir=/Apps/servers/data/d_1
dataLogDir=/apps/servers/logs/logs_1
clientPort=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_2
dataLogDir=/apps/servers/logs/logs_2
clientPort=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_3
dataLogDir=/apps/servers/logs/logs_3
clientPort=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)創(chuàng)建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)進(jìn)入bin目錄下輸入命令 分別進(jìn)行啟動(dòng)
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)進(jìn)入每一個(gè)看看效果
source /etc/profile sh zkCli.sh -server localhost:2181 sh zkCli.sh -server localhost:2182 sh zkCli.sh -server localhost:2183
偽分布式其實(shí)就是這樣就搭建完畢了。重點(diǎn)還是分布式的往下看。
- (1.2)分布式環(huán)境配置
(1.2.1)基礎(chǔ)設(shè)置(三臺(tái)機(jī)器都需要設(shè)置)
su #密碼 vagrant cd ~ vi /etc/ssh/sshd_config sudo systemctl restart sshd vi /etc/resolv.conf # 設(shè)置成8.8.8.8 service network restart
(1.2.2)jdk安裝(三臺(tái)機(jī)器都需要設(shè)置)
腳本在我的源碼里面
vi pro.sh sh pro.sh
(1.2.3)zookeeper下載(三臺(tái)機(jī)器都需要設(shè)置)
下載工具千萬(wàn)切記用最新的已經(jīng)出到3.5.4,我還是用3.4.10
為什么是三個(gè),因?yàn)閆ookeeper喜歡奇數(shù)不喜歡偶數(shù)。
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文件(三臺(tái)機(jī)器都需要設(shè)置)
cd ~ cd zookeeper-3.4.10/ cd conf cp zoo_sample.cfg zoo.cfg
(1.2.5)配置cfg文件,其實(shí)這3個(gè)機(jī)器的配置文件是一樣的,我就不重復(fù)寫(xiě)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
啟動(dòng)命令運(yùn)行3臺(tái)虛擬機(jī)下的zookeeper
cd ~/zookeeper-3.4.10/bin sh zkServer.sh start
(三) 概念梳理
- (3.1)Zoo.cfg 配置
參數(shù)意義tickTime2000syncLimitLeader 和 follower 之間的通訊時(shí)長(zhǎng) 最長(zhǎng)不能超過(guò)initLimt*ticktimeinitLimt接受客戶端鏈接 zk 初始化的最長(zhǎng)等待心跳時(shí)長(zhǎng)initLimt*ticktimedataDir數(shù)據(jù)目錄dataLogDir日志文件clientPort客戶端鏈接服務(wù)端端口號(hào)Server.A=B:C:D A:第幾號(hào)服務(wù)器 B 服務(wù) IP C 代表 Leader 和 follower 通訊端口 D 備用選 leader 端口
- (3.2)角色
Leader:
Leader 作為整個(gè) ZooKeeper 集群的主節(jié)點(diǎn),負(fù)責(zé)響應(yīng)所有對(duì) ZooKeeper 狀態(tài)變更的請(qǐng)求。它會(huì)將每個(gè)狀態(tài)更新請(qǐng)求進(jìn)行排序和編號(hào),以便保證整個(gè)集群內(nèi)部消息處理的 FIFO,寫(xiě)操作都走 leader
Follower :
Follower 的邏輯就比較簡(jiǎn)單了。除了響應(yīng)本服務(wù)器上的讀請(qǐng)求外,follower 還要處理leader 的提議,并在 leader 提交該提議時(shí)在本地也進(jìn)行提交。 另外需要注意的是,leader 和 follower 構(gòu)成 ZooKeeper 集群的法定人數(shù),也就是說(shuō),只有他們才參與新 leader的選舉、響應(yīng) leader 的提議。
Observer :
如果 ZooKeeper 集群的讀取負(fù)載很高,或者客戶端多到跨機(jī)房,可以設(shè)置一些 observer服務(wù)器,以提高讀取的吞吐量。Observer 和 Follower 比較相似,只有一些小區(qū)別:首先observer 不屬于法定人數(shù),即不參加選舉也不響應(yīng)提議;其次是 observer 不需要將事務(wù)持
久化到磁盤(pán),一旦 observer 被重啟,需要從 leader 重新同步整個(gè)名字空間。
- (3.3)Zookeeper 特性
Zookeeper 是一個(gè)由多個(gè) server 組成的
1.集群一個(gè) leader,多個(gè) follower
2.每個(gè) server 保存一份數(shù)據(jù)副本
3.全局?jǐn)?shù)據(jù)一致分布式讀follower,寫(xiě)由 leader 實(shí)施更新請(qǐng)求轉(zhuǎn)發(fā),由 leader 實(shí)施更新請(qǐng)求順序進(jìn)行,來(lái)自同一個(gè) client 的更新請(qǐng)求按其發(fā)送順序依次執(zhí)行數(shù)據(jù)更新原子性,
4.一次數(shù)據(jù)更新要么成功,要么失敗全局唯一數(shù)據(jù)視圖,
5.client 無(wú)論連接到哪個(gè) server,數(shù)據(jù)視圖都是一致的實(shí)時(shí)性,在一定事件范圍內(nèi),client 能讀到最新數(shù)據(jù)
PS:本次主要說(shuō)說(shuō)zookeeper的原理和集群部署,沒(méi)有太詳細(xì)的介紹里面的細(xì)節(jié),下次說(shuō)說(shuō)zookeeper使用。