文章主要是針對0基礎想嘗試使用zookeeper的開發人員,當中包括了一些簡單的樣例。僅用一臺zookeeperserver,一些命令確認server正在執行,一個簡單的程序樣例。html
文章最後,爲了方便,也有一些內容考慮到一些相對複雜些的樣例。列如,以複製模式部署,優化事務。node
但是假設想運用到商業項目中。請參閱 ZooKeeper Administrator's Guide.shell
這個時間比如是zookeeper的心跳時間,是最小會話單元的超時時間範圍是這個時間的2倍。apache
假如進程執行失敗,zookeeper服務就會掛掉。單例模式啓動對於開發環境來講是最好的。假設想已複製模式啓動請看Running Replicated ZooKeeper.app
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.tcp
You can run the program from src/c using:分佈式
LD_LIBRARY_PATH=. cli_mt 127.0.0.1:2181
oride
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.oop
Once you have connected, you should see something like:post
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]
[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
[zkshell: 9] create /zk_test my_data Created /zk_test
[zkshell: 11] ls / [zookeeper, 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
[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
[zkshell: 16] delete /zk_test [zkshell: 17] ls / [zookeeper] [zkshell: 18]
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.
這個文件和上面介紹單例模式使用的配置文件和相似。僅僅是有一點小小的不一樣,例如如下:
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
當server啓動的時候,經過尋找在數據文件夾的myid文件知道是哪臺server。這個文件含有以ASCII編碼的server編號。
尤爲在zookeeperserver依次鏈接到leader時候。當一個新的leader誕生時,小弟們會經過這個port號利用tcp協議鏈接到leader。因爲默認leader也用tcp協議。咱們必須要求另一個port用於選舉leader。就是屬性server的第二個port號。
(在這個複製模式執行的樣例裏,每個執行在單一的機器都有一個配置文件)