在前面的文章裏我屢次提到zookeeper對於分佈式系統開發的重要性,所以對zookeeper的學習是很是必要的。本篇博文主要是講解zookeeper的安裝和zookeeper的一些基本的應用,同時我還會教你們如何安裝僞分佈式,僞分佈式不能在windows下實現,只能在linux下實現,個人僞分佈式是經過電腦的虛擬機完成了,好了,不廢話了,具體內容以下:html
首先咱們要下載一個zookeeper,下載地址是:java
http://www.apache.org/dyn/closer.cgi/zookeeper/node
通常咱們會選擇一個stable版(穩定版)進行下載,我下載的版本是zookeeper-3.4.5。linux
我筆記本的操做系統是windows7,windows操做系統能夠做爲zookeeper的開發平臺,可是不能做爲zookeeper的生產平臺,首先咱們在windows下安裝一個單機版的zookeeper。web
咱們先解壓zookeeper的安裝包,解壓後的zookeeper安裝包我放置的路徑是:數據庫
E:\zookeeper\zookeeper-3.4.5apache
下圖是zookeeper的目錄結構:windows
咱們進入conf包,將zoo_sample.cfg文件複製一份,並將複製好的文件更名爲zoo.cfg。打開新建的zoo.cfg文件,將裏面的內容進行修改,修改後的文件內容以下:服務器
#initLimit=10 #syncLimit=5 tickTime=2000 dataDir=E:/zookeeper/zookeeper-3.4.5/data clientPort=2181
下面我來解釋下配置文件裏的各個參數:session
initLimit和syncLimit是針對集羣的參數,在我後面講解僞分佈式安裝時候我會再講解。
tickTime:該參數用來定義心跳的間隔時間,zookeeper的客戶端和服務端之間也有和web開發裏相似的session的概念,而zookeeper裏最小的session過時時間就是tickTime的兩倍。
dataDir:英文註釋能夠翻譯爲存儲在內存中的數據庫快照功能,咱們能夠看看運行後dataDir所指向的文件存儲了什麼樣的數據,以下圖所示:
看來dataDir裏還存儲了日誌信息,dataDir不能存放在命名爲tmp的文件裏。
clientPort:是監聽客戶端鏈接的端口號。
接下來咱們要將zookeeper的安裝信息配置到windows的環境變量裏,咱們在「個人電腦」上點擊右鍵,選擇屬性,再點擊高級系統設置,點擊環境變量按鈕,在系統變量這一欄,點擊新建,添加:
變量名:ZOOKEEPER_HOME 變量值:E:\zookeeper\zookeeper-3.4.5
仍是在系統變量這一欄,找到path,點擊編輯path,在變量值裏添加:% ZOOKEEPER_HOME %\bin; % ZOOKEEPER_HOME %\conf;
Zookeeper使用java編寫的,所以安裝zookeeper以前必定要先安裝好jdk,而且jdk的版本要大於或等於1.6。
這樣單機版的zookeeper就安裝好了,下面咱們將運行zookeeper。
首先咱們打開windows的命令行工具,將文件夾轉到zookeeper安裝目錄的下的bin目錄,而後輸入zkServer命令,回車執行,那麼zookeeper服務就啓動成功了。
下面咱們用客戶端鏈接zookeeper的服務端,咱們再打開一個命令行工具,輸入命令:
zkCli -server localhost:2181
下面是相關測試,以下圖所示:
僞分佈式的安裝,zookeeper和hadoop同樣也能夠進行僞分佈式的安裝,下面我就講解如何進行僞分佈式安裝。
我開始嘗試在windows下安裝僞分佈式,可是沒有成功,最後是在linux操做系統下才安裝好僞分佈式,咱們首先下載好zookeeper的安裝程序,而後新建三個配置文件分別是:
zoo1.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=E:/zookeeper/zookeeper-3.4.5/d_1 # the port at which the clients will connect clientPort=2181 # # 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 dataLogDir=E:/zookeeper/zookeeper-3.4.5/log1_2 server.1=localhost:2887:3887 server.2=localhost:2888:3888 server.3=localhost:2889:3889
zoo2.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=E:/zookeeper/zookeeper-3.4.5/d_2 # the port at which the clients will connect clientPort=2182 # # 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 dataLogDir=E:/zookeeper/zookeeper-3.4.5/logs_2 server.1=localhost:2887:3887 server.2=localhost:2888:3888 server.3=localhost:2889:3889
zoo3.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=E:/zookeeper/zookeeper-3.4.5/d_3 # the port at which the clients will connect clientPort=2183 # # 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 dataLogDir=E:/zookeeper/zookeeper-3.4.5/logs_3 server.1=localhost:2887:3887 server.2=localhost:2888:3888 server.3=localhost:2889:3889
這裏咱們把每一個配置文件裏的clientPort作了必定修改,讓每一個文件之間的clientPort不同,dataDir屬性也作了一樣的調整,同時還添加了新配置內容,以下所示:
server.1=localhost:2887:3887 server.2=localhost:2888:3888 server.3=localhost:2889:3889
這裏localhost指的是組成zookeeper服務的機器IP的地址,2887是用於進行leader選舉的端口,3887是zookeeper集羣裏各個機器之間的通訊接口。
initLimit:是指follower鏈接並同步到leader的初始化鏈接,它是經過tickTime的倍數表示,例如咱們上面的配置就是10倍的tickTime,當初始化鏈接時間超過設置的倍數時候則鏈接失敗。
syncLimit:是指follower和leader之間發送消息時請求和應答的時間長度,若是follower在設置的時間範圍內不能喝leader通訊,那麼該follower將會被丟棄,它也是按tickTime的倍數進行設置的。
dataLogDir:這個配置是指zookeeper運行的相關日誌寫入的目錄,設定了配置,那麼dataLog裏日誌的目錄將無效,專門的日誌存放路徑,對zookeeper的性能和穩定性有好處。
這裏每個配置文件都表明一個zookeeper服務器,下面咱們啓動僞分佈式的zookeeper集羣。
zkServer.sh start zoo1.cfg zkServer.sh start zoo2.cfg zkServer.sh start zoo3.cfg
下面我寫一個java程序,該程序做爲客戶端調用zookeeper的服務,代碼以下:
package cn.com.test; import java.io.IOException; import org.apache.zookeeper.CreateMode; import org.apache.zookeeper.KeeperException; import org.apache.zookeeper.WatchedEvent; import org.apache.zookeeper.Watcher; import org.apache.zookeeper.ZooDefs.Ids; import org.apache.zookeeper.ZooKeeper; public class zkClient { public static void main(String[] args) throws Exception{ Watcher wh = new Watcher(){ @Override public void process(WatchedEvent event) { System.out.println(event.toString()); } }; ZooKeeper zk = new ZooKeeper("localhost:2181",30000,wh); System.out.println("=========建立節點==========="); zk.create("/sharpxiajun", "znode1".getBytes(), Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT); System.err.println("=============查看節點是否安裝成功==============="); System.out.println(new String(zk.getData("/sharpxiajun", false, null))); System.out.println("=========修改節點的數據=========="); zk.setData("/sharpxiajun", "sharpxiajun130901".getBytes(), -1); System.out.println("========查看修改的節點是否成功========="); System.out.println(new String(zk.getData("/sharpxiajun", false, null))); System.out.println("=======刪除節點=========="); zk.delete("/sharpxiajun", -1); System.out.println("==========查看節點是否被刪除============"); System.out.println("節點狀態:" + zk.exists("/sharpxiajun", false)); zk.close(); } }
執行結果以下:
log4j:WARN No appenders could be found for logger (org.apache.zookeeper.ZooKeeper). log4j:WARN Please initialize the log4j system properly. =========建立節點=========== WatchedEvent state:SyncConnected type:None path:null =============查看節點是否安裝成功=============== znode1 =========修改節點的數據========== ========查看修改的節點是否成功========= sharpxiajun130901 =======刪除節點========== ==========查看節點是否被刪除============ 節點狀態:null
程序我今天不講解了,只是給大夥展現下使用zookeeper的方式,本文可能沒啥新穎的東西,可是本文是一個基礎,有了這個基礎咱們才能真正操做zookeeper。