C语言连接zookeeper客户端(不能完全参考官网教程)

准备过程

1.通过VStudio 远程连接linux的开发环境;
2.g++环境,通过MingW安装;
3.必须要安装好pthread.h的环境,不管是windows端(linux 可视化端开发就不管这个)还是linux端;
4.需要准备zookeeper源码,放在linux端,进入C客户端,利用C编译安装在linux端,找到Include和lib复制到windows项目;
5.项目的链接器的命令行需要添加-pthread,不然会报错

创建节点,C代码示例

//下面三个是来源于编译好的zookeeper
//注意:不要直接在项目中使用zookeeper源码
#include "zookeeper.h"
#include "zookeeper_log.h"
#include "zookeeper.jute.h"

#include 
#include 

const char* host_list = "10.8.16.19:2181,10.8.16.18:2181,10.8.16.17:2181";
static void zk_watcher(zhandle_t* zh, int type, int state, const char* path, void* watcherCtx)
{
    printf("zk_watcher: [type=%d] [state=%d] [path=%s] [watcher_ctx=%p]\n", type, state, path, watcherCtx);
}
int main()
{
    int time_out = 20000;
    zoo_set_debug_level(ZOO_LOG_LEVEL_DEBUG);

    zhandle_t* zkhandle = zookeeper_init(host_list, zk_watcher, time_out, 0, 0, 0);
    if (!zkhandle) {
        printf("main: 连接失败\n");
        return -1;
    }
  

    //struct ACL CREATE_ONLY_ACL[] = { {ZOO_PERM_CREATE, ZOO_AUTH_IDS} }; //可以通过数组的方式
    struct ACL CREATE_ONLY_ACL = { ZOO_PERM_CREATE, ZOO_AUTH_IDS} ;

    struct ACL_vector CREATE_ONLY = { 1, &CREATE_ONLY_ACL };

	//没有下面的zoo_get_acl后面创建/test/xyz不成功,
    struct Stat stat;
    int uu=zoo_get_acl(zkhandle, "/test", &CREATE_ONLY, &stat);
    printf("get_acl:%d\n", uu);


    int rc = zoo_create(zkhandle, "/test/xyz", "5569", 6, &CREATE_ONLY, ZOO_SEQUENCE, NULL,0); //ZOO_SEQUENCE 才能查看到内容

    printf("create:%d\n", rc); //返回0表示操作成功

  
    zookeeper_close(zkhandle);


    return 0;
}

你可能感兴趣的:(c语言,zookeeper,windows)