1. 程式人生 > >Apache ZooKeeper Getting Started Guide 翻譯

Apache ZooKeeper Getting Started Guide 翻譯

tail var directory storage 我們 刪除 multi 包括 initial

技術分享
ZooKeeper 開始向導
  • 開始: 用zookeeper協調分布式程序
    • 單例操作
    • 管理zookeeper存儲
    • 連接zookeeper
    • 執行zookeeper
    • 以復制模式執行zookeeper
    • 其他優化
Getting Started:通過zookeeper協調分布式程序 這份文檔包括了讓你高速開始使用zookeeper的幫助信息。

文章主要是針對0基礎想嘗試使用zookeeper的開發人員,當中包括了一些簡單的樣例。僅用一臺zookeeperserver,一些命令確認server正在執行,一個簡單的程序樣例。

文章最後,為了方便,也有一些內容考慮到一些相對復雜些的樣例。列如,以復制模式部署,優化事務。

可是假設想運用到商業項目中。請參閱 ZooKeeper Administrator‘s Guide.


單例操作 以單例模式啟動zookeeperserver是簡單的。zookeeper服務包括一個jar文件和一些配置。 啟動zookeeper你首先須要一份配置文件,在下載的文件文件夾conf/zoo.cfg: tickTime=2000 dataDir=/var/lib/zookeeper clientPort=2181 你能夠任意命名這個文件的名字,可是為了描寫敘述的清楚我們就叫它 conf/zoo.cfg. 假如已經存在dataDir文件夾請改變dataDir的值。以下是對每個字段意義的介紹 tickTime
zookeeper用到的時間單位是毫秒。

這個時間好比是zookeeper的心跳時間,是最小會話單元的超時時間範圍是這個時間的2倍。

dataDir 這個文件夾是存儲內存中數據快照的位置,除非你特別說明,更新數據事物的log也在這個位置。 clientPort client連接監聽port。你須要創建一個配置文件。啟動ZooKeeper:bin/zkServer.sh start ZooKeeper 的日誌記錄是通過log4j — 很多其它關於日誌的解說請看log4j官網。這裏介紹的啟動zookeeper是以單例模式。

假如進程執行失敗,zookeeper服務就會掛掉。單例模式啟動對於開發環境來說是最好的。假設想已復制模式啟動請看Running Replicated ZooKeeper.

Managing ZooKeeper Storage 對於長時間執行在生產環境的zookeeper服務,存儲必須被額外的管理(dataDir and logs),關於這些請看maintenance Connecting to ZooKeeper 假設zookeeper已經執行,你就能夠通過以下幾種選擇進行連接
  • Java: Use
    
    bin/zkCli.sh -server 127.0.0.1:2181
    
    This lets you perform simple, file-like operations.
  • C: compile cli_mt (multi-threaded) or cli_st (single-threaded) by running make cli_mt or make cli_st in the src/c subdirectory in the ZooKeeper sources. See the README contained within src/c for full details.

    You can run the program from src/c using:

    
    LD_LIBRARY_PATH=. cli_mt 127.0.0.1:2181
    

    or

    
    LD_LIBRARY_PATH=. cli_st 127.0.0.1:2181
    

    This will give you a simple shell to execute file system like operations on ZooKeeper.

Once you have connected, you should see something like:


Connecting to localhost:2181
log4j:WARN No appenders could be found for logger (org.apache.zookeeper.ZooKeeper).
log4j:WARN Please initialize the log4j system properly.
Welcome to ZooKeeper!
JLine support is enabled
[zkshell: 0]
        

假設在shell中執行。你能夠子啊連接後鍵入 help ,這將返回client能夠執行的命令列表 例如以下:

[zkshell: 0] help
ZooKeeper host:port cmd args
        get path [watch]
        ls path [watch]
        set path data [version]
        delquota [-n|-b] path
        quit
        printwatches on|off
        create path data acl
        stat path [watch]
        listquota path
        history
        setAcl path acl
        getAcl path
        sync path
        redo cmdno
        addauth scheme auth
        delete path [version]
        deleteall path
        setquota -n|-b val path

        

你能夠嘗試一些簡單的命令來了解這個簡單的命令行. First, start by issuing the list command, as in ls, yielding: [zkshell: 8] ls /[zookeeper] 接下來。創建一個咋弄的通過執行create /zk_test my_data.這個會創建一個新的anode和與這個anode關聯的字符串數據”my_data” ,你能夠看到以下的執行結果:

[zkshell: 9] create /zk_test my_data
Created /zk_test
      

通過執行 ls / 命令會看到展現當前的文件夾情況:

[zkshell: 11] ls /
[zookeeper, zk_test]

        

註意。這個zk_test文件夾已經被創建。

你能夠確認下和這個節點關聯的數據通過執行get命令。例如以下:


[zkshell: 12] get /zk_test
my_data
cZxid = 5
ctime = Fri Jun 05 13:57:06 PDT 2009
mZxid = 5
mtime = Fri Jun 05 13:57:06 PDT 2009
pZxid = 5
cversion = 0
dataVersion = 0
aclVersion = 0
ephemeralOwner = 0
dataLength = 7
numChildren = 0
        

我們也能夠改變和zk_test關聯的數據通過set命令,例如以下:

[zkshell: 14] set /zk_test junk
cZxid = 5
ctime = Fri Jun 05 13:57:06 PDT 2009
mZxid = 6
mtime = Fri Jun 05 14:01:52 PDT 2009
pZxid = 5
cversion = 0
dataVersion = 1
aclVersion = 0
ephemeralOwner = 0
dataLength = 4
numChildren = 0
[zkshell: 15] get /zk_test
junk
cZxid = 5
ctime = Fri Jun 05 13:57:06 PDT 2009
mZxid = 6
mtime = Fri Jun 05 14:01:52 PDT 2009
pZxid = 5
cversion = 0
dataVersion = 1
aclVersion = 0
ephemeralOwner = 0
dataLength = 4
numChildren = 0
      

(註意我們在設置zk_test的數據後又執行了get 命令,數據的確改變了)。

最後通過delete 刪除zk_test這個節點:

[zkshell: 16] delete /zk_test
[zkshell: 17] ls /
[zookeeper]
[zkshell: 18]


獲取很多其他其他內容。請看Programmer‘s Guide.

Programming to ZooKeeper

ZooKeeper has a Java bindings and C bindings. They are functionally equivalent. The C bindings exist in two variants: single threaded and multi-threaded. These differ only in how the messaging loop is done. For more information, see the Programming Examples in the ZooKeeper Programmer‘s Guide for sample code using of the different APIs.

復制模式執行zookeeper 執行zookeeper以單例模式對於評估測試開發是非常方便。可是在正式環境你應該以復制模式執行。每一個zookeeperserver都有一份相同的配置文件。

這個文件和上面介紹單例模式使用的配置文件和類似。僅僅是有一點小小的不同,例如以下:


tickTime=2000
dataDir=/var/lib/zookeeper
clientPort=2181
initLimit=5
syncLimit=2
server.1=zoo1:2888:3888
server.2=zoo2:2888:3888
server.3=zoo3:2888:3888


這個新的屬性, initLimit是用來定義zookeeper連接到leader的超時時間,屬性syncLimit 限制一個leader過時時間。對於這兩種超時時間,你也能夠指定用tickTime的時間單位計量。比如。initLimit是5 ticks,每一個tick是2000毫秒,也就是10秒。 屬性server.X 列出了zookeeper服務的組成。

當server啟動的時候,通過尋找在數據文件夾的myid文件知道是哪臺server。這個文件含有以ASCII編碼的server編號。

最後,註意在每一個server名後的兩個port號: " 2888" and "3888.通過這些port能夠彼此連接。比如,一個彼此連接是必的當按順序更新數據時。

尤其在zookeeperserver依次連接到leader時候。當一個新的leader誕生時,小弟們會通過這個port號利用tcp協議連接到leader。由於默認leader也用tcp協議。我們必需要求另外一個port用於選舉leader。就是屬性server的第二個port號。


註意 假如你想在一臺機器上進行多個zookeeper服務測試。須要指出唯一的集群名localhost和那些leader選舉port(比如2888:3888, 2889:3889, 2890:3890 )。隔離各個dataDir文件夾和不同的port號也是必須的。

(在這個復制模式執行的樣例裏,每一個執行在單一的機器都有一個配置文件)


其他優化 有其他的配置參數能夠提高性能:There are a couple of other configuration parameters that can greatly increase performance: 為了降低等待和高速更新,它是重要的有個事物log文件夾。默認事物log文件是和數據快照和myid文件放在一起。屬性dataLogDir能夠指名別的地方
Last Published: 08/07/2014 04:18:26 Copyright ? 2008-2013 The Apache Software Foundation.

Apache ZooKeeper Getting Started Guide 翻譯