我正在为Zookeeper使用C绑定,并尝试仅连接到本地主机服务器。请注意,我可以在同一端口上使用zkCli.sh
进行连接,并列出或创建znodes。
即使zookeeper_init
方法成功返回,观察者回调也不会被调用。这是最小的示例:
#include <stdio.h>
#include <unistd.h>
#include <errno.h>
#include "zookeeper.h"
static zhandle_t *zh;
void main_watcher (zhandle_t *zkh,
int type,
int state,
const char *path,
void* context)
{
printf("Callback called !\n"); // NEVER GETS CALLED
}
int main()
{
zoo_set_debug_level(ZOO_LOG_LEVEL_DEBUG);
zh = zookeeper_init("127.0.0.1:2181", main_watcher, 10000, 0, NULL, 0);
if (!zh) {
printf("ERROR init!");
return errno;
}
sleep(20);
zookeeper_close(zh);
}
这些是我在运行代码时获得的日志,不存在回调printf:
2018-08-16 10:34:26,952:25258:ZOO_INFO@log_env@753: Client environment:zookeeper.version=zookeeper C client 3.4.12
2018-08-16 10:34:26,952:25258:ZOO_INFO@log_env@757: Client environment:host.name=TEST
2018-08-16 10:34:26,952:25258:ZOO_INFO@log_env@764: Client environment:os.name=Linux
2018-08-16 10:34:26,952:25258:ZOO_INFO@log_env@765: Client environment:os.arch=4.13.0-43-generic
2018-08-16 10:34:26,952:25258:ZOO_INFO@log_env@766: Client environment:os.version=#48~16.04.1-Ubuntu SMP Thu May 17 12:56:46 UTC 2018
2018-08-16 10:34:26,953:25258:ZOO_INFO@log_env@774: Client environment:user.name=test
2018-08-16 10:34:26,953:25258:ZOO_INFO@log_env@782: Client environment:user.home=/home/test
2018-08-16 10:34:26,953:25258:ZOO_INFO@log_env@794: Client environment:user.dir=/home/test
2018-08-16 10:34:26,953:25258:ZOO_INFO@zookeeper_init@827: Initiating client connection, host=127.0.0.1:2181 sessionTimeout=10000 watcher=0x4007a6 sessionId=0 sessionPasswd=<null> context=(nil) flags=0
2018-08-16 10:34:46,954:25258:ZOO_INFO@zookeeper_close@2581: Freeing zookeeper resources for sessionId=0
您知道为什么不调用观察者回调吗?