目前的公司是使用的阿里內部的dubbo,也就是EDAS,里面用了阿里自己的EDAS服務,如果是使用過dubbo的老鐵,應該知道zookeeper,zookeeper在大數據和RPC通信上應用比較管飯。不管用過zookeeper沒有,這次主要是介紹下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/

image.png
源碼地址: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地址節點角色CPUMemoryHostnamecentos7192.168.69.100偽分布式22Gzookeeper-virtuaCentos7192.168.69.101真分布式-領導者22Gzookeeper-LeaderCentos7192.168.69.102真分布式-屬下122Gzookeeper-Follower1Centos7192.168.69.103真分布式-屬下222Gzookeeper-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_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)創建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 配置
參數意義tickTime2000syncLimitLeader 和 follower 之間的通訊時長 最長不能超過initLimt*ticktimeinitLimt接受客戶端鏈接 zk 初始化的最長等待心跳時長initLimt*ticktimedataDir數據目錄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使用。