zoukankan      html  css  js  c++  java
  • zookeeper c api 安装 & 连接 zookeeper

    一、安装c api

      以此方式下载的zookeeper

    wget "http://mirrors.hust.edu.cn/apache/zookeeper/zookeeper-3.4.14/zookeeper-3.4.14.tar.gz"

      c api源码位于zookeeper-3.4.14/zookeeper-client/zookeeper-client-c

    cd zookeeper-3.4.14/zookeeper-client/zookeeper-client-c
    ./configure
    make
    sudo make install

      安装后也可以在/usr/local/include/zookeeper中找到头文件,当然也可以把它拷走至自己的指定目录。

    二、使用c api连接zookeeper

    zookeeper_test.c

     1 #include <zookeeper.h>
     2 #include <zookeeper_log.h>
     3 #include <zookeeper.jute.h>
     4 #include <stdio.h>
     5 
     6 //伪分布式部署 host list最好以配置文件形式,此处为测试程序,暂时写死
     7 const char * host_list = "127.0.0.1:12181,127.0.0.1:12182,127.0.0.1:12183";
     8 void zk_watcher(zhandle_t *zh, int type, int state, const char *path,void *watcherCtx)
     9 {
    10     printf("zk_watcher: [type=%d] [state=%d] [path=%s] [watcher_ctx=%p]
    ", type, state, path, watcherCtx);
    11 }
    12 
    13 void zk_exists(zhandle_t *zkhandle, const char *path)
    14 {
    15     struct Stat stat;
    16     printf("zk_exists: ==========BEGIN============
    ");
    17     int ret_code = zoo_exists(zkhandle, path, 0, &stat);
    18     printf("zk_exists: [ret=%d]
    ", ret_code);
    19     if(ZOK == ret_code)
    20     {
    21         printf("zk_exists: [path=%s] [czxid=%ld] [mzxid=%ld] [version=%d] [cversion=%d] [child_num=%d]
    ",
    22                path, stat.czxid, stat.mzxid, stat.version, stat);
    23     }
    24     printf("zk_exists: ==========END============
    ");
    25 }
    26 
    27 int main()
    28 {
    29     int time_out = 50000;
    30     zoo_set_debug_level(ZOO_LOG_LEVEL_DEBUG);
    31     zhandle_t * zkhandle = zookeeper_init(host_list, zk_watcher, time_out, 0, NULL, 0);
    32     if (NULL == zkhandle)
    33     {
    34         printf("main: connect to zk fail.
    ");
    35         return -1;
    36     }
    37     zk_exists(zkhandle, "/");
    38 
    39     zookeeper_close(zkhandle);
    40 
    41     return 0;
    42 }

    Makefile

     1 ZK_CAPI_PATH := /usr/local
     2 
     3 INC_PATH := ${ZK_CAPI_PATH}/include/zookeeper
     4 SRC_PATH := ./*.c
     5 LIB_PATH := ${ZK_CAPI_PATH}/lib
     6 
     7 LIB_NAME := zookeeper_mt
     8 
     9 EXE_NAME := zookeeper_test.exe
    10 
    11 main:
    12         gcc -g -DTHREADED ${SRC_PATH} -I ${INC_PATH} -o ${EXE_NAME} -L ${LIB_PATH} -l ${LIB_NAME}
    13 clean:
    14         rm ${EXE_NAME}

    编译后运行

    root@iZwz9hextk0ee6gik32377Z:~/zookeeper/c_test# make
    gcc -g -DTHREADED ./*.c -I /usr/local/include/zookeeper -o zookeeper_test.exe -L /usr/local/lib -l zookeeper_mt
    root@iZwz9hextk0ee6gik32377Z:~/zookeeper/c_test# ./zookeeper_test.exe 
    2020-04-11 11:30:04,628:26341(0x7fd9a61b3740):ZOO_INFO@log_env@753: Client environment:zookeeper.version=zookeeper C client 3.4.14
    2020-04-11 11:30:04,628:26341(0x7fd9a61b3740):ZOO_INFO@log_env@757: Client environment:host.name=iZwz9hextk0ee6gik32377Z
    2020-04-11 11:30:04,628:26341(0x7fd9a61b3740):ZOO_INFO@log_env@764: Client environment:os.name=Linux
    2020-04-11 11:30:04,628:26341(0x7fd9a61b3740):ZOO_INFO@log_env@765: Client environment:os.arch=4.15.0-88-generic
    2020-04-11 11:30:04,628:26341(0x7fd9a61b3740):ZOO_INFO@log_env@766: Client environment:os.version=#88-Ubuntu SMP Tue Feb 11 20:11:34 UTC 2020
    2020-04-11 11:30:04,628:26341(0x7fd9a61b3740):ZOO_INFO@log_env@774: Client environment:user.name=root
    2020-04-11 11:30:04,628:26341(0x7fd9a61b3740):ZOO_INFO@log_env@782: Client environment:user.home=/root
    2020-04-11 11:30:04,628:26341(0x7fd9a61b3740):ZOO_INFO@log_env@794: Client environment:user.dir=/root/zookeeper/c_test
    2020-04-11 11:30:04,628:26341(0x7fd9a61b3740):ZOO_INFO@zookeeper_init@827: Initiating client connection, host=127.0.0.1:12181,127.0.0.1:12182,127.0.0.1:12183 sessionTimeout=50000 watcher=0x559328a3b86a sessionId=0 sessionPasswd=<null> context=(nil) flags=0
    2020-04-11 11:30:04,629:26341(0x7fd9a61b3740):ZOO_DEBUG@start_threads@221: starting threads...
    2020-04-11 11:30:04,629:26341(0x7fd9a4f30700):ZOO_DEBUG@do_io@367: started IO thread
    zk_exists: ==========BEGIN============
    2020-04-11 11:30:04,629:26341(0x7fd9a472f700):ZOO_DEBUG@do_completion@458: started completion thread
    2020-04-11 11:30:04,629:26341(0x7fd9a61b3740):ZOO_DEBUG@zoo_awexists@2894: Sending request xid=0x5e9139bd for path [/] to 127.0.0.1:12183
    2020-04-11 11:30:04,633:26341(0x7fd9a4f30700):ZOO_INFO@check_events@1764: initiated connection to server [127.0.0.1:12183]
    2020-04-11 11:30:04,635:26341(0x7fd9a4f30700):ZOO_INFO@check_events@1811: session establishment complete on server [127.0.0.1:12183], sessionId=0x30000314d590004, negotiated timeout=40000
    2020-04-11 11:30:04,635:26341(0x7fd9a4f30700):ZOO_DEBUG@check_events@1817: Calling a watcher for a ZOO_SESSION_EVENT and the state=ZOO_CONNECTED_STATE
    2020-04-11 11:30:04,635:26341(0x7fd9a472f700):ZOO_DEBUG@process_completions@2169: Calling a watcher for node [], type = -1 event=ZOO_SESSION_EVENT
    zk_watcher: [type=-1] [state=3] [path=] [watcher_ctx=(nil)]
    2020-04-11 11:30:04,636:26341(0x7fd9a4f30700):ZOO_DEBUG@process_sync_completion@1929: Processing sync_completion with type=1 xid=0x5e9139bd rc=0
    zk_exists: [ret=0]
    zk_exists: [path=/] [czxid=0] [mzxid=0] [version=0] [cversion=8] [child_num=2]
    zk_exists: ==========END============
    2020-04-11 11:30:04,636:26341(0x7fd9a4f30700):ZOO_DEBUG@do_io@445: IO thread terminated
    2020-04-11 11:30:04,636:26341(0x7fd9a472f700):ZOO_DEBUG@do_completion@468: completion thread terminated
    2020-04-11 11:30:04,636:26341(0x7fd9a61b3740):ZOO_INFO@zookeeper_close@2564: Closing zookeeper sessionId=0x30000314d590004 to [127.0.0.1:12183]

    可以看到取到的“/”目录的信息:

    zk_exists: [path=/] [czxid=0] [mzxid=0] [version=0] [cversion=8] [child_num=2]

    实际从zookeeper client中得到的:

    [zk: 127.0.0.1:12181(CONNECTED) 0] get /
    
    cZxid = 0x0
    ctime = Thu Jan 01 08:00:00 CST 1970
    mZxid = 0x0
    mtime = Thu Jan 01 08:00:00 CST 1970
    pZxid = 0x400000018
    cversion = 8
    dataVersion = 0
    aclVersion = 0
    ephemeralOwner = 0x0
    dataLength = 0
    numChildren = 2

    参考链接:

      https://www.cnblogs.com/haippy/archive/2013/02/21/2920280.html

  • 相关阅读:
    jsp页面input输入框限制输入内容
    sql计算两个日期之间的相差天数
    sql根据一个字段日期加减7天存入另一字段中
    ajax请求捕获异常
    跨网段和局域网的SQL SERVER发布订阅配置图解和常见问题
    一次得到多个数据集
    SQL Server 2008语句大全完整版
    关于已开票已收款未发货的账务处理
    高格-一些特点话题【7】
    高格-塑料管业中厂内工单,满足随时换料的处理【6】
  • 原文地址:https://www.cnblogs.com/chinxi/p/12678457.html
Copyright © 2011-2022 走看看