hadoop學習筆記:zookeeper學習(上)

出處:http://www.cnblogs.com/sharpxiajun/p/3294581.html


大型網站架構交流QQ羣:466097527 每週技術分享 經典電子書分享 歡迎加羣


在前面的文章裏我多次提到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。


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章