Zookeeper C API 指南一(准备工作)
2013-02-21 11:16 Haippy 阅读(34995) 评论(2) 编辑 收藏 举报以前自己的博客中转载、翻译或写过(不过自己才疏学浅,写的不好)一些 Zookeeper 方面的文章,但是都没有涉及到 Zookeeper C API 的内容,今天的这篇博客是我农历新年的第一篇技术博客,我想详细讲讲 Zookeeper C API 的使用规则和示例,算是把以前的旧帐还上吧 :-)
Zookeeper 官方页面上提供了一些编程指南 和 API 文档,不过大部分都是 Java 示例,涉及 C API 的部分很少,只有在 ZooKeeper Programmer's Guide 中 ACL Permissions 一节讲了 Zookeeper C API 中设置 ACL 应该注意的事项,正是由于缺少Zookeeper C API 相关的资料,大部分 Zookeeper C/C++ 开发者只能通过阅读 Zookeeper C API 的源码来了解 C API 的使用方法,本文希望在此方面给大家提供一些便利,减少 Zookeeper 新手使用 C API 的困难和恐惧,当然我自己也是一枚新手啦 :-)。
废话不多说了,先从最基本的开始吧!
Zookeeper 伪分布式安装
首先是 Zookeeper 的安装,如果你没有足够的机器,建议在单机上通过伪分布式安装方法来模拟 Zookeeper 集群,我在这里提供给大家一个简单的安装包来降低伪分布式 Zookeeper 的安装难度,该安装包可以模拟一个 5 Zookeeper 实例的集群。下载 zookeeper-3.4.0-pseudoclusters.tar.bz2,解压至 /tmp/ 目录(注意,安装包里面 dataDir 和 dataLogDir 配置已经预设为 /tmp/ 目录,如果你只是自己在 Zookeeper 上做一些小实验,建议不要更改该配置,这样在你做完了实验以后删除 /tmp/zookeeper/ 目录即可),操作方法如下:
forhappy@haiping-ict:/tmp$ pwd /tmp forhappy@haiping-ict:/tmp$ tar xvf zookeeper-3.4.0-pseudoclusters.tar.bz2 forhappy@haiping-ict:/tmp$ cd zookeeper/ forhappy@haiping-ict:/tmp/zookeeper$ ls server001 server003 server005 stopZKCluster.sh server002 server004 startZKCluster.sh
然后在 /tmp/zookeeper 目录下执行 startZKCluster.sh 启动 Zookeeper 服务,或者执行 stopZKCluster.sh 停掉 Zookeeper 服务。
启动 Zookeeper 服务:
forhappy@haiping-ict:/tmp/zookeeper$ ./startZKCluster.sh . starting zookeeper instance server001... JMX enabled by default Using config: /tmp/zookeeper/server001/zookeeper-3.4.0/bin/../conf/zoo.cfg Starting zookeeper ... STARTED starting zookeeper instance server002... JMX enabled by default Using config: /tmp/zookeeper/server002/zookeeper-3.4.0/bin/../conf/zoo.cfg Starting zookeeper ... STARTED starting zookeeper instance server003... JMX enabled by default Using config: /tmp/zookeeper/server003/zookeeper-3.4.0/bin/../conf/zoo.cfg Starting zookeeper ... STARTED starting zookeeper instance server004... JMX enabled by default Using config: /tmp/zookeeper/server004/zookeeper-3.4.0/bin/../conf/zoo.cfg Starting zookeeper ... STARTED starting zookeeper instance server005... JMX enabled by default Using config: /tmp/zookeeper/server005/zookeeper-3.4.0/bin/../conf/zoo.cfg Starting zookeeper ... STARTED
停掉 Zookeeper 服务:
forhappy@haiping-ict:/tmp/zookeeper$ ./stopZKCluster.sh . stopping zookeeper instance server001... JMX enabled by default Using config: /tmp/zookeeper/server001/zookeeper-3.4.0/bin/../conf/zoo.cfg Stopping zookeeper ... STOPPED stopping zookeeper instance server002... JMX enabled by default Using config: /tmp/zookeeper/server002/zookeeper-3.4.0/bin/../conf/zoo.cfg Stopping zookeeper ... STOPPED stopping zookeeper instance server003... JMX enabled by default Using config: /tmp/zookeeper/server003/zookeeper-3.4.0/bin/../conf/zoo.cfg Stopping zookeeper ... STOPPED stopping zookeeper instance server004... JMX enabled by default Using config: /tmp/zookeeper/server004/zookeeper-3.4.0/bin/../conf/zoo.cfg Stopping zookeeper ... STOPPED stopping zookeeper instance server005... JMX enabled by default Using config: /tmp/zookeeper/server005/zookeeper-3.4.0/bin/../conf/zoo.cfg Stopping zookeeper ... STOPPED
Zookeeper C API 安装
Zookeeper C client 的实现在 src/c 目录下,进入到该目录安装 Zookeeper C client,步骤如下:
$ ./configure $ make $ sudo make install
至此,基本的准备工作已经完成,你可以通过 Zookeeper 本身提供的 Shell 来操作 Zookeeper,操作方法如下,进入 /tmp/zookeeper/server001/zookeeper-3.4.0 目录,执行bin/zkCli.sh -server 127.0.0.1:2181 进入 Zookeeper shell:
forhappy@haiping-ict:/tmp/zookeeper/server001/zookeeper-3.4.0$ bin/zkCli.sh -server 127.0.0.1:2181
[zk: 127.0.0.1:2181(CONNECTED) 2] 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
在 shell 中你可以完成基本的操作,如创建、获取、删除、设置某一节点,设置节点 ACL等,可以 zookeeper shell 中通过 help 获取相关命令的用法。
如果你按照上面的步骤完成了 Zookeeper 伪分布式的安装,并且想继续了解 Zookeeper C API 的使用方法,请继续阅读《Zookeeper C API 指南二(监视(Wathes), 基本常量和结构体介绍)》