1. 程式人生 > >zookeeper從安裝到學習到初步總結及相關問題解決方案

zookeeper從安裝到學習到初步總結及相關問題解決方案

一 zookeeper個人理解

這是官方的中文講解:什麼是zookeeper

其實簡單來講就是:在使用叢集伺服器時,有一個管理者身份的叢集管理框架。負責任務管理分發,各項任務的監聽,警報及自動處理請求等。舉個例子來講,三臺伺服器,某一天突然有一臺掛了,這時候zookeeper會報警,並把掛掉的這臺伺服器的業務操作分配到正常的兩條伺服器上保持專案不被整體掛掉而影響整體功能使用。這僅僅是個人的簡單理解,喜歡的可以自行深入瞭解。

二 下載及安裝

下載地址:各版本列表  本人JDK為1.7 下載的是最新的3.5.3   也可以下載穩定版的3.4

安裝很簡單,就是解壓到指定目錄,開啟進入目錄:D:\zookeeper-3.5.3-beta\conf (注:我放到了D盤)

找到zoo_sample.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=D:\\zookeeper\\data  
dataLogDir=D:\\zookeeper\\log  
# 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

zoo_sample.cfg配置檔案簡單解析 1、tickTime:這個時間是作為 Zookeeper 伺服器之間或客戶端與伺服器之間維持心跳的時間間隔,也就是每個 tickTime 時間就會發送一個心跳。
2、dataDir:顧名思義就是 Zookeeper 儲存資料的目錄,預設情況下,Zookeeper 將寫資料的日誌檔案也儲存在這個目錄裡。
3、dataLogDir:顧名思義就是 Zookeeper 儲存日誌檔案的目錄

4、clientPort:這個埠就是客戶端連線 Zookeeper 伺服器的埠,Zookeeper 會監聽這個埠,接受客戶端的訪問請求。

儲存後,將zoo_sample.cfg檔案改名為zoo.cfg。(注:若不想改名,複製一份zoo_sample.cfg檔案再改也可以,其實就是啟動時讀取的是zoo.cfg檔案

修改好配置檔案以後進入目錄:D:\zookeeper-3.5.3-beta\bin  找到zkServer.cmd檔案,使用編輯器開啟,在內容最下方新增pause  

為什麼要這麼做呢?因為,若不新增,雙擊會閃退。當然你也可以不新增,使用cmd進入到該目錄啟動zkServer.cmd檔案也是一樣的效果。

雙擊zkServer.cmd檔案可以看到啟動成功的資訊,如圖:


若啟動錯誤,請檢查是否在D:\zookeeper-3.5.3-beta\conf目錄下存在zoo.cfg配置檔案。

三 zookeeper的三種安裝模式

在第二步的安裝中其實是屬於單機安裝模式。zookeeper的重要作用其實是叢集。所以,還有兩種安裝模式,分別是叢集安裝模式和偽叢集安裝模式。

所謂叢集,就是多個伺服器安裝,偽叢集就是單個伺服器,但是有多個zookeeper。具體我就不多說了,這裡推薦一個文章,寫的非常好,建議喜歡的同學可以看一看:zookeeper三種安裝模式

四 zookeeper的優缺點

有人說zookeeper是叢集的救星,他解決了叢集管理複雜性,操控性,管理性等各種問題。而又的人又說,其許可權控制,效能,資料一致性存在弊端。其實個人覺得zookeeper還是很好的,特別是針對中小型專案來說,在訪問量,資料量不是非常大,並且對資料精度要求不是非常高的情況下,zookeeper的作用是很大的。下面推薦一篇文章,講解的非常好,也可以當故事來讀,也可以給以後使用的人又點醒和借鑑作用,不多說了請看文章:zookeeper優缺點