一、概述與特點?
Zookeeper 是一個分佈式服務框架,是ApacheHadoop的一個子項目。從設計模式角度來看,它是基於觀察者模式設計的一個分佈式協調框架;它負責存儲大家關心的數據,觀察者訂閱關心的數據或結點,當發生變化時,它將通知觀察者以實現分佈協調。常稱分佈式協調指揮官!
特點:
- 一個領導者Leader,多個跟跟隨者Follower;
- 一個Zookeeper集羣,半數以上存活,則集羣可用;
- 全局數據一致:無論Client連接哪個Server,獲取得到的數據都一致的;
- 更新請求順序進行,來自同一個client的請求按順序執行;
- 數據更新原子性,一次數據更新要麼成功,要麼失敗;
- 實時性,一定時間能Client能讀到最新的數據;
1、數據結構
Zookeeper數據結構與Unix很相似,整體上可以看作是一個樹,每個結點稱作一個ZNode,每個Znode可以存儲1M數據,每個結點可以通過路徑唯一標識。
2、應用場景
- 統一命名服務
- 統一配置管理
- 統一集羣管理
- 集羣服務上下線通知
- 軟負載
二、安裝與部署
- 下載與安裝官網下載3.4.14:;
- tar -zxvf zookeeper-3.4.14.tar.gz -C /opt/module/;
- mv zoo_sample.cfg zoo.cfg;
- vi zoo.cfg,一般會更改zookeeper存儲數據的路徑:dataDir=/opt/module/zookeeper-3.4.14/zkData;
[root@vm-1154-0-2 conf]# vi zoo.cfg
#The number of milliseconds of each tick
#心跳時間
tickTime=2000
# The number of ticks that the initial
# synchronization phase can take
#初始化啓動時10*2000內Leader與Follower的通信期限,沒有通信上認爲掛了;
initLimit=10
# The number of ticks that can pass between
# sending a request and getting an acknowledgement
#啓動之後5*2000內Leader與Follower的通信期限,沒有通信上認爲掛了;
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.4.14/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.
#
# 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
- 啓動服務:zkServer.sh start
[root@vm-1154-0-2 zookeeper-3.4.14]# bin/zkServer.sh start
ZooKeeper JMX enabled by default
Using config: /opt/module/zookeeper-3.4.14/bin/../conf/zoo.cfg
Starting zookeeper ... STARTED
- 查看啓動狀態:zkServer.sh status
[root@vm-1154-0-2 zookeeper-3.4.14]# bin/zkServer.sh status
ZooKeeper JMX enabled by default
Using config: /opt/module/zookeeper-3.4.14/bin/../conf/zoo.cfg
Mode: standalone
- 根據需要啓動客戶端可以進行zkCli.sh
常用命令
- help 幫助命令;
- create 創建結點命令,創建的同事需要指定值;
- ls 查看結點;
- ls2 查看結點詳情;
- get 獲取結點值;
- delete 刪除結點;
- rmr 遞歸刪除結點;
- set 設置結點值;
以上爲本地模式
三、內部原理
1、選舉機制
半數以上機制:半數以上主機存活,集羣可用,因此Zookeeper適合安裝奇數臺服務器,否則機器存在浪費;
以5臺服務器爲例描述選舉過程:
- 第一臺主機啓動,它選擇自己一票,發出去的信息一直沒有人收到,此時一直是looking狀態;
- 第二臺主機啓動,它也選擇自己一票,與第一臺主機交換信息,都沒有超過半數,但自己的ID大,然後第一臺投第二臺一票,此時第二臺主機2票,仍然未超過半數;
- 第三臺服務器啓動,它也選擇自己一票,與其它主機交互信息,此時三臺主機的票數均未超過半數,主機間交換,第三臺服務器ID最大,於數第一臺與第二臺將票投給第三臺,此時第三臺有3票,超過半數,於是第三臺被選爲Leader;
- 第四臺,第五臺啓動,由於此時已經有Leader,不再發生變化,除非Leader掛掉;
2、結點類型
結點類型分爲是否持久、是否有序;
- 持久結點:客戶端與Zookeeper斷掉後結點不會刪除;
- 臨時結點:客戶端與Zookeeper斷開,結點會被刪除;創建結點時帶-e參數 create -e;
- 有序結點:生成結點會根據生成先後,生成對應的序號,序號單調增加,可以根據序號大寫判斷結點生成的時間先後,序號由父結點維護;創建結點時帶-s參數 create -s;
3、監聽機制
- 首先要有一個main線程;
- 在main線程中創建Zookeeper客戶端,此時會創建兩個線程,一個connect線程,一個listener線程;
- 通過connect線程建立連接併發送註冊監聽事件給Zookeeper服務;
- Zookeeper服務器將監聽路徑添加到監聽列表中;
- 當監聽的路徑發生數據或結點變化,Zookeeper會主動通過listener線程通知客戶端;
- 客戶端調用內部和理processor;
常用監聽
- 數據監聽 get path watch;
- 結點監聽 ls path watch;
注意:上面的命令每執行一次只監聽一次變化。
4、寫數據流程
以下以三個結點的Zookeeper集羣爲例介紹寫數據流程server1,leader,server2:
- client向server1發送寫數據請求;
- server1通知leader,leader寫入數據,並通知serverr1與server2寫入;
- server1與server2寫入成功後分別通知leader寫入成功;
- leader通知server1與server2本次寫入成功;
- server1通知client寫入成功;
四、企業面試真題
1、請描述Zookeeper的選舉機制?
2、Zookeeper的監聽原理?
3、Zookeeper的部署方式有幾種?集羣方式中有幾種角色?集羣至少需要幾臺機器?
4、Zookeeper常用命令有哪些?