用C语言实现apache向zookeeper注册

代码

Java API虽然简单方便,但是还是太臃肿了部署比较麻烦,这里试了一下用C语言编写了一个小程序,在程序启动时向zookeeper注册节点,随后监视apache进程,当进程不存在时,程序退出。
Zookeeper C API请参考Zookeeper C API 指南

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
#include <stdbool.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <zookeeper/zookeeper.h>
#include <zookeeper/zookeeper_log.h>
void My_watcher_g(zhandle_t* zh, int type, int state,
const char* path, void* watcherCtx)
{
}
void My_string_completion(int rc, const char *name, const void *data)
{
fprintf(stderr,"successfully register node %s\n",name);
}
void monitor()
{
while(! system("ps -ef | grep apache2 | grep -q -v grep"))
sleep(2);
exit(1);
}
int main(int argc, const char *argv[])
{
const char* host = "127.0.0.1:2181";
int timeout = 2000;
int flag=0;
flag |= ZOO_EPHEMERAL;
flag |= ZOO_SEQUENCE;
zoo_set_debug_level(ZOO_LOG_LEVEL_WARN);
zhandle_t* zkhandle = zookeeper_init(host,
My_watcher_g, timeout, 0, "hello zookeeper.", 0);
if (zkhandle == NULL) {
fprintf(stderr, "Error when connecting to zookeeper servers...\n");
exit(EXIT_FAILURE);
}
int ret = zoo_acreate(zkhandle, "/node", "alive", 5,
&ZOO_OPEN_ACL_UNSAFE, flag,
My_string_completion, "zoo_acreate");
if (ret) {
fprintf(stderr, "Error %d for %s\n", ret, "acreate");
exit(EXIT_FAILURE);
}
monitor();
zookeeper_close(zkhandle);
}

会话超时由timeout来设定,单位为ms,监控的粒度由timeout(会话断开过多久zookeeper删除节点)和sleep()的时长来决定。用system(“ps”)比较简单,更好的方式可以判断apache的PID file或者pid。

主要由2个函数,zookeeper_init()创建连接和zoo_acreate()异步创建节点构成。

My_watcher_g()设置监视器,My_string_completion即返回字符串的回调函数,当zoo_acreate()调用结束时将会触发 completion 回调函数的调用。

程序启动可以集成在apache的init脚本中。

编译

将zookeeper的源码下载下来后,需要

1
2
3
4
cd src/c
./configure --prefix=/usr/local/zookeeper
make
make install

编译完成后会在/usr/local/zookeeper中产生如下文件:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
/usr/local/zookeeper/
├── bin
│   ├── cli_mt
│   ├── cli_st
│   └── load_gen
├── include
│   └── zookeeper
│   ├── proto.h
│   ├── recordio.h
│   ├── zookeeper.h
│   ├── zookeeper.jute.h
│   ├── zookeeper_log.h
│   └── zookeeper_version.h
└── lib
├── libzookeeper_mt.a
├── libzookeeper_mt.la
├── libzookeeper_mt.so -> libzookeeper_mt.so.2.0.0
├── libzookeeper_mt.so.2 -> libzookeeper_mt.so.2.0.0
├── libzookeeper_mt.so.2.0.0
├── libzookeeper_st.a
├── libzookeeper_st.la
├── libzookeeper_st.so -> libzookeeper_st.so.2.0.0
├── libzookeeper_st.so.2 -> libzookeeper_st.so.2.0.0
└── libzookeeper_st.so.2.0.0

编译上述程序register.c

编译:
gcc regiter.c -o reigter -DTHREAD -I /usr/local/zookeeper/include/ /usr/local/zookeeper/lib/libzookeeper_mt.so

如果编译报错

In file included from new.c:6:
/usr/local/zookeeper/include/zookeeper/zookeeper_log.h:22:23: error: zookeeper.h: No such file or directory

则需要修改一个文件,
/usr/local/zookeeper/include/zookeeper_log.h
中的
#include <zookeeper.h>
修改为
#include <zookeeper/zookeeper.h

运行时提示我们:

./a.out: error while loading shared libraries: libzookeeper_st.so.2: cannot open shared object file: No such file or directory

这是因为系统没有找到动态链接库,需要将libzookeeper_mt.so.2复制到/usr/lib64
执行 cp /usr/local/zookeeper/lib/libzookeeper_st.so.2 /usr/lib64

如果您觉得这篇文章对您有帮助,不妨支持我一下!