之前一直用zookeeper的java API,最近要用C开发,所以看了C的API及实现
使用zookeeper C API 的步骤:
(1)安装zookeeper,直接下载,解压,配置文件即可
(2)安装C API
进入./zookeeper/src/c目录
./configure
make
make install
(3)编写调用函数,可以参考下面的例子
(4)编译的时候注意引入所在的头文件和动态库,默认安装时头文件在/usr/local/include/c-client-src中,动态库在/usr/local/lib中,使用多线程时注意-DTHREAD
具体的编译:gcc a.c -DTHREAD -I/usr/local/include/c-client-src /usr/local/lib/libzookeeper_mt.so -o a
#include<stdio.h>
#include<string.h>
#include"zookeeper.h"
#include"zookeeper_log.h"
void zktest_watcher_g(zhandle_t* zh, int type, int state, const char* path, void* watcherCtx)
{
printf("Something happened.\n");
printf("type: %d\n", type);
printf