Apache ZooKeeper Getting Started Guide 翻譯

來源:互聯網
上載者:User

標籤:zoo.cfg   started   情況   隔離   sync   int   width   ast   下載   


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的值。以下是對每個欄位意義的介紹 tickTimezookeeper用到的時間單位是毫秒。

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

dataDir這個檔案夾是儲存記憶體中資料快照的位置,除非你特別說明,更新資料事物的log也在這個位置。 clientPortclient串連監聽port。你須要建立一個設定檔。啟動ZooKeeper:bin/zkServer.sh startZooKeeper 的日誌記錄是通過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:2181log4j: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] helpZooKeeper 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_dataCreated /zk_test      

通過執行 ls / 命令會看到展現當前的檔案夾情況:
[zkshell: 11] ls /[zookeeper, zk_test]        

注意。這個zk_test檔案夾已經被建立。

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

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

我們也能夠改變和zk_test關聯的資料通過set命令,例如以下:
[zkshell: 14] set /zk_test junkcZxid = 5ctime = Fri Jun 05 13:57:06 PDT 2009mZxid = 6mtime = Fri Jun 05 14:01:52 PDT 2009pZxid = 5cversion = 0dataVersion = 1aclVersion = 0ephemeralOwner = 0dataLength = 4numChildren = 0[zkshell: 15] get /zk_testjunkcZxid = 5ctime = Fri Jun 05 13:57:06 PDT 2009mZxid = 6mtime = Fri Jun 05 14:01:52 PDT 2009pZxid = 5cversion = 0dataVersion = 1aclVersion = 0ephemeralOwner = 0dataLength = 4numChildren = 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=2000dataDir=/var/lib/zookeeperclientPort=2181initLimit=5syncLimit=2server.1=zoo1:2888:3888server.2=zoo2:2888:3888server.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:26Copyright ? 2008-2013 The Apache Software Foundation.

Apache ZooKeeper Getting Started Guide 翻譯

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.