Zookeeper 初體驗之——偽分布式安裝


簡介

Apache Zookeeper 是由 Apache Hadoop 的 Zookeeper 子項目發展而來,現在已經成為了 Apache 的頂級項目。Zookeeper 為分布式系統提供了高效可靠且易於使用的協同服務,它可以為分布式應用提供相當多的服務,諸如統一命名服務,配置管理,狀態同步和組服務等。Zookeeper 接口簡單,開發人員不必過多地糾結在分布式系統編程難於處理的同步和一致性問題上,你可以使用 Zookeeper 提供的現成(off-the-shelf)服務來實現分布式系統的配置管理,組管理,Leader 選舉等功能。

Zookeeper 維護了大規模分布式系統中的常用對象,比如配置信息,層次化命名空間等,本文將從開發者的角度詳細介紹 Zookeeper 的配置信息的意義以及 Zookeeper 的典型應用場景(配置文件的管理、集群管理、分布式隊列、同步鎖、Leader 選舉、隊列管理等)。

Zookeeper 安裝與配置

本文采用 Zookeeper-3.4.0 以基礎介紹它的安裝步驟以及配置信息,最新的代碼可以到 Zookeeper 的官網http://zookeeper.apache.org/下載。Zookeeper功能強大,但是安裝卻十分簡單,下面重點以偽分布式模式來介紹 Zookeeper 的安裝。

偽分布式模式安裝

Zookeeper 安裝模式包括:單機模式,偽分布式模式和完全的集群模式。單機模式最簡單,本文將跳過單機模式安裝(單機模式安裝步驟參見 Zeekeeper 官方文檔http://zookeeper.apache.org/doc/current/zookeeperStarted.html),偽分布式模式與集群模式配置差別不大,由於手頭機器有限,所以本文采用了在單台機器上偽分布式安裝。

本文在Ubuntu 12.04上操作,Java 環境為 OpenJDK 1.7。安裝 Zookeeper 前首先下載你需要的版本,暫時解壓到指定目錄(本文解壓至~/zookeeper/目錄下),並修改配置(可能需要多次修改配置文件),本次偽分布式模擬 5 個 Zookeeper 節點,事先在/tmpzookeeper目錄下建立5個文件夾,分別命名為:server001,server002,server003,server004,server005,然后在每個server00#文件夾下面新建 data 和 logs 子文件夾。

Zookeeper 的配置文件主要在 conf 目錄,包括zoo.cfg (zoo_sample.cfg)和log4j.properties,修改 zoo_sample.cfg,重命名為zoo.cgf,打開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
#
# 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

將內容修改為(server001節點的配置文件):

# 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/server001/data
dataLogDir=/tmp/zookeeper/server001/logs
# 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
server.1=127.0.0.1:8881:7771
server.2=127.0.0.1:8882:7772
server.3=127.0.0.1:8883:7773
server.4=127.0.0.1:8884:7774
server.5=127.0.0.1:8885:7775
  • tickTime:這個時間是作為 Zookeeper 服務器之間或客戶端與服務器之間維持心跳的時間間隔,也就是每個 tickTime 時間就會發送一個心跳。
  • dataDir:顧名思義就是 Zookeeper 保存數據的目錄,默認情況下,Zookeeper 將寫數據的日志文件也保存在這個目錄里。
  • clientPort:這個端口就是客戶端連接 Zookeeper 服務器的端口,Zookeeper 會監聽這個端口,接受客戶端的訪問請求。
  • initLimit:這個配置項是用來配置 Zookeeper 接受客戶端(這里所說的客戶端不是用戶連接 Zookeeper 服務器的客戶端,而是 Zookeeper 服務器集群中連接到 Leader 的 Follower 服務器)初始化連接時最長能忍受多少個心跳時間間隔數。當已經超過 5個心跳的時間(也就是 tickTime)長度后 Zookeeper 服務器還沒有收到客戶端的返回信息,那么表明這個客戶端連接失敗。總的時間長度就是 5*2000=10 秒
  • syncLimit:這個配置項標識 Leader 與 Follower 之間發送消息,請求和應答時間長度,最長不能超過多少個 tickTime 的時間長度,總的時間長度就是 2*2000=4 秒
  • server.A=B:C:D:其中 A 是一個數字,表示這個是第幾號服務器;B 是這個服務器的 ip 地址;C 表示的是這個服務器與集群中的 Leader 服務器交換信息的端口;D 表示的是萬一集群中的 Leader 服務器掛了,需要一個端口來重新進行選舉,選出一個新的 Leader,而這個端口就是用來執行選舉時服務器相互通信的端口。如果是偽集群的配置方式,由於 B 都是一樣,所以不同的 Zookeeper 實例通信端口號不能一樣,所以要給它們分配不同的端口號。

然后將此 zookeeper 包拷貝至 /tmp/zookeeper/server001/ 目錄下,並在 /tmp/zookeeper/server001/data/ 下建立一個myid文件,文件內容為 1,

echo "1" >> /tmp/zookeeper/server001/data/myid


繼續修改~/zookeeper/目錄中的zookeeper配置文件文件(server002的配置文件,注意 clientPort=2182,與 server001 中的 clientPort=2181 不同,后續修改配置均需設置不同的 clientPort),內容如下:

# 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/server002/data
dataLogDir=/tmp/zookeeper/server002/logs
# 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
server.1=127.0.0.1:8881:7771
server.2=127.0.0.1:8882:7772
server.3=127.0.0.1:8883:7773
server.4=127.0.0.1:8884:7774
server.5=127.0.0.1:8885:7775

然后將此 zookeeper 包拷貝至 /tmp/zookeeper/server002/ 目錄下,並在 /tmp/zookeeper/server002/data/ 下建立一個myid文件,文件內容為 2,

echo "2" >> /tmp/zookeeper/server001/data/myid

依次修改配置文件,建立 server003,server004,server005節點文件夾,完成上述步驟后/tmp/zookeeper目錄結構如下:

forhappy@forhappy-lenovo:/tmp/zookeeper$ tree -d -L 2
.
├── server001
│   ├── data
│   ├── logs
│   └── zookeeper-3.4.0
├── server002
│   ├── data
│   ├── logs
│   └── zookeeper-3.4.0
├── server003
│   ├── data
│   ├── logs
│   └── zookeeper-3.4.0
├── server004
│   ├── data
│   ├── logs
│   └── zookeeper-3.4.0
└── server005
    ├── data
    ├── logs
    └── zookeeper-3.4.0

然后依次進入每個文件夾節點的zookeeper目錄中,啟動zookeeper服務,

$ bin/zkServer.sh start

如果一切順利,Zookeeper 偽分布式模式安裝成功,下面驗證 Zookeeper 安裝的正確性。

進入任意一個文件夾節點的zookeeper包所在的目錄,執行一下命令:

$ bin/zkCli.sh  -server 127.0.0.1:2181

執行成功后:

forhappy@forhappy-lenovo:/tmp/zookeeper/server001/zookeeper-3.4.0$ bin/zkCli.sh  -server 127.0.0.1:2181
Connecting to 127.0.0.1:2181

Welcome to ZooKeeper!

WATCHER::

WatchedEvent state:SyncConnected type:None path:null

[zk: 127.0.0.1:2181(CONNECTED) 0]

help 幫助:

[zk: 127.0.0.1:2181(CONNECTED) 0] help
ZooKeeper -server host:port cmd args
    connect host:port
    get path [watch]
    ls path [watch]
    set path data [version]
    rmr path
    delquota [-n|-b] path
    quit 
    printwatches on|off
    create [-s] [-e] path data acl
    stat path [watch]
    close 
    ls2 path [watch]
    history 
    listquota path
    setAcl path acl
    getAcl path
    sync path
    redo cmdno
    addauth scheme auth
    delete path [version]
    setquota -n|-b val path

至此,Zookeeper 安裝完成,下一篇博客將介紹 Zookeeper Java API,並給出 Zookeeper 典型的應用場景。

參考資料:

http://zookeeper.apache.org/

http://zookeeper.apache.org/doc/current/zookeeperStarted.html

http://zookeeper.apache.org/doc/current/index.html

http://www.ibm.com/developerworks/cn/opensource/os-cn-zookeeper/

http://blog.csdn.net/franklysun/article/details/6424582


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM