在前面的文章里我多次提到zookeeper對于分布式系統開發的重要性,因此對zookeeper的學習是非常必要的。本篇博文主要是講解zookeeper的安裝和zookeeper的一些基本的應用,同時我還會教大家如何安裝偽分布式,偽分布式不能在windows下實現,只能在linux下實現,我的偽分布式是通過電腦的虛擬機完成了,好了,不廢話了,具體內容如下:
首先我們要下載一個zookeeper,下載地址是:
http://www.apache.org/dyn/closer.cgi/zookeeper/
一般我們會選擇一個stable版(穩定版)進行下載,我下載的版本是zookeeper-3.4.5。
我筆記本的操作系統是windows7,windows操作系統可以作為zookeeper的開發平臺,但是不能作為zookeeper的生產平臺,首先我們在windows下安裝一個單機版的zookeeper。
我們先解壓zookeeper的安裝包,解壓后的zookeeper安裝包我放置的路徑是:
E:\zookeeper\zookeeper-3.4.5
下圖是zookeeper的目錄結構:
我們進入conf包,將zoo_sample.cfg文件復制一份,并將復制好的文件改名為zoo.cfg。打開新建的zoo.cfg文件,將里面的內容進行修改,修改后的文件內容如下:
#initLimit=10 #syncLimit=5 tickTime=2000 dataDir=E:/zookeeper/zookeeper-3.4.5/data clientPort=2181
下面我來解釋下配置文件里的各個參數:
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。
文章列表