【Zookeeper】01 概述 & 基礎部署
背景:
隨著網際網路技術的發展,企業對計算機系統的計算,儲存能力要求越來越高,各大IT企業都在追求高併發,海量儲存的極致,
在這樣的背景下,單純依靠少量高效能單機來完成計算機,雲端計算的任務已經無法滿足需求,企業的IT架構逐漸由集中式往分散式過渡。
所謂的分散式是指:把一個計算任務分解成若干個計算單元,並分派到不同的計算機中去執行,最終彙總計算結果的過程。
什麼是Zookeeper?
Zookeeper是原始碼開放的分散式協調服務,是一個高效能的分散式資料一致性的解決方案,
它將那些複雜的,容易出錯的分散式一致性服務封裝起來。
使用者可以通過呼叫Zookeeper提供的介面來解決一些分散式應用中的實際問題。
應用場景
(1)資料釋出/訂閱
資料的釋出與訂閱,顧名思義就是一方把資料釋出出來,另一方通過某種手段獲取。
通常資料釋出與訂閱有兩種模式:推模式和拉模式,
推模式一般是伺服器主動往客戶端推送資訊,
拉模式是客戶端主動去服務端請求目標資料(通常採用定時輪詢的方式)
Zookeeper採用兩種方式互相結合:釋出者將資料釋出到Zookeeper叢集節點上,
訂閱者通過一定的方法告訴Zookeeper伺服器,自己對哪個節點的資料感興趣,
那麼在服務端資料發生變化時,就會通知客戶端去獲取這些資訊。
(2)負載均衡
首先在服務端啟動的時候,把自己在zookeeper伺服器上註冊成一個臨時節點。
zookeeper擁有兩種形式的節點,一種是臨時節點,一種是永久節點。
這兩種節點後面的會有較為詳細的介紹。
註冊成臨時節點後,再服務端出問題時,節點會自動的從zookeeper上刪除,如此zookeeper伺服器上的列表就是最新的可用的列表。
客戶端在需要訪問伺服器的時候首先會去Zookeeper獲得所有可用的服務端的連線資訊。
客戶端通過一定的策略(如隨機)選擇一個與之建立連線。
當客戶端發現連線不可用時,會再次從zookeeper上獲取可用的服務端連線,並同時刪除之前獲取的連線列表。
(3)命名服務
提供名稱的服務。如一般使用較多的有兩種id,一種是資料庫自增長id,一種是UUID,
兩種id都有侷限,自增長id僅適合在單表單庫中使用,uuid適合在分散式系統中使用但由於id沒有規律難以理解。
而ZK提供了一定的介面可以用來獲取一個順序增長的,可以在叢集環境下使用的id。
(4)分散式協調,通知,心跳服務
在分散式服務系統中,我們常常需要知道哪個服務是可用的,哪個服務是不可用的,
傳統的方式是通過ping主機來實現的,ping得200的結果說明說明該服務是OK的。
而在使用 zookeeper時,可以將所有的服務都註冊成一個臨時節點,我們判斷一個服務是否可用,
只需要判斷這個節點是否在zookeeper叢集中存在就可以了,不需要直接去連線和ping服務所在主機,減少系統的複雜度和對服務主機的壓力。
優勢
(1)原始碼開放
(2)高效能,易用穩定,該優勢已在眾多分散式系統中得到驗證
(3)有著廣泛的應用,並且與眾多大資料相關技術能實現良好的融合開發。
下載 & 安裝:
https://apache.org/dist/zookeeper/stable http://mirror.bit.edu.cn/apache/zookeeper/stable/
Linux作業系統環境下最佳。但是注意,需要Java執行環境的支援
解壓Zookeeper壓縮包
tar -zxvf apache-zookeeper-3.5.8-bin.tar.gz
賦值解藥的包目錄到usr | local |下面,重新命名zookeeper
cp -r apache-zookeeper-3.5.8-bin /usr/local/zookeeper
zookeeper的目錄結構:
[root@VM-0-7-centos zookeeper]# ll total 40 drwxr-xr-x 2 root root 4096 Aug 6 20:26 bin drwxr-xr-x 2 root root 4096 Aug 6 20:26 conf drwxr-xr-x 5 root root 4096 Aug 6 20:26 docs drwxr-xr-x 2 root root 4096 Aug 6 20:26 lib -rw-r--r-- 1 root root 11358 Aug 6 20:26 LICENSE.txt -rw-r--r-- 1 root root 432 Aug 6 20:26 NOTICE.txt -rw-r--r-- 1 root root 1560 Aug 6 20:26 README.md -rw-r--r-- 1 root root 1347 Aug 6 20:26 README_packaging.txt
我們需要對zookeeper進行一定的配置才能啟動使用。
檢視conf配置目錄:
[root@VM-0-7-centos zookeeper]# ll conf total 12 -rw-r--r-- 1 root root 535 Aug 6 20:26 configuration.xsl -rw-r--r-- 1 root root 2712 Aug 6 20:26 log4j.properties -rw-r--r-- 1 root root 922 Aug 6 20:26 zoo_sample.cfg
我們備份這個樣板配置檔案,複製一份並且更改配置:
cp -r zoo_sample.cfg 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
# 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
zookeeper的執行程式在bin目錄下:
啟動zookeeper
./zkServer.sh start
顯示:
/usr/bin/java
ZooKeeper JMX enabled by default
Using config: /usr/local/zookeeper/bin/../conf/zoo.cfg
Starting zookeeper ... STARTED
關閉zookeeper:
./zkServer.sh stop
顯示:
/usr/bin/java
ZooKeeper JMX enabled by default
Using config: /usr/local/zookeeper/bin/../conf/zoo.cfg
Stopping zookeeper ... ./zkServer.sh: line 213: kill: (19512) - No such process
STOPPED
檢視zookeeper執行狀態:
./zkServer.sh status
顯示:
/usr/bin/java
ZooKeeper JMX enabled by default
Using config: /usr/local/zookeeper/bin/../conf/zoo.cfg
Client port found: 2181. Client address: localhost.
Error contacting service. It is probably not running.