- 開始用zookeeper协调分布式程序
-
- 单例操作
- 管理zookeeper存储
- 连接zookeeper
- 执行zookeeper
- 以复制模式执行zookeeper
- 其他优化
文章主要是针对0基础想尝试使用zookeeper的开发人员,当中包括了一些简单的样例。仅用一台zookeeperserver,一些命令确认server正在执行,一个简单的程序样例。
文章最后,为了方便,也有一些内容考虑到一些相对复杂些的样例。列如,以复制模式部署,优化事务。
可是假设想运用到商业项目中。请參阅 .
这个时间好比是zookeeper的心跳时间,是最小会话单元的超时时间范围是这个时间的2倍。
假如进程执行失败,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]
[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
[zkshell: 9] create /zk_test my_dataCreated /zk_test
[zkshell: 11] ls /[zookeeper, 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
[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
[zkshell: 16] delete /zk_test[zkshell: 17] ls /[zookeeper][zkshell: 18]
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 for sample code using of the different APIs.
这个文件和上面介绍单例模式使用的配置文件和类似。仅仅是有一点小小的不同,例如以下:
tickTime=2000dataDir=/var/lib/zookeeperclientPort=2181initLimit=5syncLimit=2server.1=zoo1:2888:3888server.2=zoo2:2888:3888server.3=zoo3:2888:3888
当server启动的时候,通过寻找在数据文件夹的myid文件知道是哪台server。这个文件含有以
ASCII编码的server编号。尤其在zookeeperserver依次连接到leader时候。当一个新的leader诞生时,小弟们会通过这个port号利用tcp协议连接到leader。由于默认leader也用tcp协议。我们必需要求另外一个port用于选举leader。就是属性server的第二个port号。
(在这个复制模式执行的样例里,每一个执行在单一的机器都有一个配置文件)