Ubuntu下zookeeper C client安装

1 安装zookeeper
    (a)下载    zookeeper-x.y.z.tar.gz  
    (b)安装jdk:
    下载jdk:jdk-7u25-linux-i586.tar.gz
    tar zxvf jdk-7u25-linux-i586.tar.gz
    安装zookeeper:
    tar zxvf zookeeper-3.3.5.tar.gz
           cd /zookeeper-3.3.5
    (c)环境变量设置:
    vim /etc/profile
    在最后添加下面两条语句
    export JAVA_HOME=/usr/local/src/jdk1.7.0_25
    export JRE_HOME=/usr/local/src/jdk1.7.0_25/jre
    export ZOOKEEPER_INSTALL=/usr/local/src/zookeeper-3.3.5
           export PATH=$PATH:$ZOOKEEPER_INSTALL/bin:$JAVA_HOME/bin:$JRE_HOME/bin
    (d)配置文件设置:
    运行zookeeper之前,需要创建配置文件,在./conf/zoo.cfg,单机运行的只需要指定dataDir即可,在集群中还要指定服务器的配置
    单机配置:
         cd conf
         cp zoo_sample.cfg zoo.cfg
         vim zoo.cfg
    之后修改zoo.cfg中的dataDir为存储快照的目录即可
2 安装 C client:
  Zookeeper C client 的实现在 src/c 目录下,进入到该目录安装 Zookeeper C client,步骤如下:
$ cd ./src/c
$ ./configure $ make $ sudo make install
3 启动:
   zkServer.sh start
  正常启动后,发送四字命令可以测试是否正常运行,比如发送
    echo ruok|nc 127.0.0.1 2181
  会收到 imok 的提示,表示zookeeper启动成功!
 

 当启动 ZooKeeper 服务成功之后,输入下述命令,连接到 ZooKeeper 服务:

zkCli.sh –server 10.77.20.23:2181

 
4 C 开发示例:
编译的时候引入所在的头文件和动态库 gcc -o zookeeper_test zookeeper_test.c -I/usr/local/include/c-client-src -L/usr/local/lib -lzookeeper_mt
具体示例:
/*
 * =============================================================================
 *
 *       Filename:  zktest.c
 *
 *       Description:  zookeeper api testcase.
 *
 *       Created:  02/15/2013 08:48:49 PM
 *
 *       Author:  Fu Haiping (forhappy), haipingf@gmail.com
 *       Company:  ICT ( Institute Of Computing Technology, CAS )
 *       link:http://www.cnblogs.com/haippy/archive/2013/02/21/2920426.html
 * =============================================================================
 */
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <zookeeper/zookeeper.h>
#include <zookeeper/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("state: %d\n", state);
    printf("path: %s\n", path);
    printf("watcherCtx: %s\n", (char *)watcherCtx);
}

void zktest_dump_stat(const struct Stat *stat)
{
    char tctimes[40];
    char tmtimes[40];
    time_t tctime;
    time_t tmtime;

    if (!stat) {
        fprintf(stderr,"null\n");
        return;
    }
    tctime = stat->ctime/1000;
    tmtime = stat->mtime/1000;
       
    ctime_r(&tmtime, tmtimes);
    ctime_r(&tctime, tctimes);
       
    fprintf(stderr, "\tctime = %s\tczxid=%llx\n"
    "\tmtime=%s\tmzxid=%llx\n"
    "\tversion=%x\taversion=%x\n"
    "\tephemeralOwner = %llx\n",
     tctimes, stat->czxid,
     tmtimes, stat->mzxid,
    (unsigned int)stat->version, (unsigned int)stat->aversion,
    stat->ephemeralOwner);
}

void zktest_stat_completion(int rc, const struct Stat *stat, const void *data)
{
    fprintf(stderr, "%s: rc = %d Stat:\n", (char*)data, rc);
    zktest_dump_stat(stat);
}

void zktest_void_completion(int rc, const void *data)
{
    fprintf(stderr, "[%s]: rc = %d\n", (char*)(data==0?"null":data), rc);
}

void zktest_string_completion(int rc, const char *name, const void *data)
{
    fprintf(stderr, "[%s]: rc = %d\n", (char*)(data==0?"null":data), rc);
    if (!rc) {
        fprintf(stderr, "\tname = %s\n", name);
    }
}

int main(int argc, const char *argv[])
{
    const char* host = "127.0.0.1:2181,127.0.0.1:2182,"
        "127.0.0.1:2183,127.0.0.1:2184,127.0.0.1:2185";
    int timeout = 30000;
    
    zoo_set_debug_level(ZOO_LOG_LEVEL_WARN);
    zhandle_t* zkhandle = zookeeper_init(host,
            zktest_watcher_g, timeout, 0, "hello zookeeper.", 0);
    if (zkhandle == NULL) {
        fprintf(stderr, "Error when connecting to zookeeper servers...\n");
        exit(EXIT_FAILURE);
    }

    // struct ACL ALL_ACL[] = {{ZOO_PERM_ALL, ZOO_ANYONE_ID_UNSAFE}};
    // struct ACL_vector ALL_PERMS = {1, ALL_ACL};
    int ret = zoo_acreate(zkhandle, "/xyz", "hello", 5,
           &ZOO_OPEN_ACL_UNSAFE, 0 /* ZOO_SEQUENCE */,
           zktest_string_completion, "acreate");
    if (ret) {
        fprintf(stderr, "Error %d for %s\n", ret, "acreate");
        exit(EXIT_FAILURE);
    }
    ret = 0;

    ret = zoo_aexists(zkhandle, "/xyz", 1, zktest_stat_completion, "aexists");
    if (ret) {
        fprintf(stderr, "Error %d for %s\n", ret, "aexists");
        exit(EXIT_FAILURE);
    }
    ret = 0;
    // Wait for asynchronous zookeeper call done.
    getchar();

    ret = zoo_adelete(zkhandle, "/xyz", -1, zktest_void_completion, "adelete");
    if (ret) {
        fprintf(stderr, "Error %d for %s\n", ret, "adelete");
        exit(EXIT_FAILURE);
    }
    // Wait for asynchronous zookeeper call done.
    getchar();

    zookeeper_close(zkhandle);
}
编译命令:
     gcc -o zookeeper_test zookeeper_test.c -I/usr/local/include/c-client-src -L/usr/local/lib -lzookeeper_mt

 
5 参考:
(1)http://hi.baidu.com/raybin_yang/item/52fb4315ed99de05d0d66da2   configure --prefix 命令  默认安装位置
(2)http://blog.chinaunix.net/uid-16989284-id-2303676.html  关于引入头文件,库
 
posted @ 2013-06-15 11:38  lpshou  阅读(4769)  评论(0编辑  收藏  举报