发现几篇写的比较好的博客,转载来看看
转载地址:https://www.cnblogs.com/haippy/archive/2013/02/21/2919365.html
目录
- Zookeeper C API 指南一(准备工作)
- Zookeeper C API 指南二(监视(Wathes), 基本常量和结构体介绍)
- Zookeeper C API 指南三(回调函数)
- Zookeeper C API 指南四(C API 概览)
- Zookeeper C API 指南五(同步 API 介绍)
- Zookeeper C API 指南六(异步 API 介绍)
- Zookeeper C API 指南七(Zookeeper 辅助 API 介绍)
- Zookeeper C API 指南八(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 伪分布式安装
首先是 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 获取相关命令的用法。