码迷,mamicode.com
首页 > 其他好文 > 详细

zookeeper 简单的代码demo

时间:2014-12-19 19:28:09      阅读:250      评论:0      收藏:0      [点我收藏+]

标签:

写了一个关于zookeepeer应用的简单demo

服务端定时的向zookeeper集群注册,客户端监听zookeeper服务节点变化,一旦变化,立刻响应,更新服务端列表

服务端代码:

#include <zookeeper/zookeeper.h>
#include <zookeeper/zookeeper_log.h>
#include <iostream>

using namespace std;

void InitWatch(zhandle_t* zh, int type, int state, const char* path, void* watcher) {
  if (type == ZOO_SESSION_EVENT) {
    if (state == ZOO_CONNECTED_STATE) {
      cout << "build connection ok" << endl;
    } else if (state == ZOO_EXPIRED_SESSION_STATE) {
      cout << "connection disconnect" << endl;
    }
  }
}

void CreateWatch(int rc, const char *name, const void *data) {
  if (rc == ZNODEEXISTS || rc == ZOK) {
    if (rc == ZOK) {
      cout << "registry ok" << endl;
    } else {
      cout << "node exist" << endl;
    }
  } else {
    cout << "registry error" << endl;
  }
}

int main(int argc, char* argv[]) {
  const char* host = "10.7.18.31:2181,10.7.18.199:2181";
  int timeout = 100000;
  zoo_set_debug_level(ZOO_LOG_LEVEL_WARN);
  zhandle_t* zkhandle = NULL;
  zkhandle = zookeeper_init(host, InitWatch, timeout, 0, const_cast<char*>("TODO"), 0);
 while (1) {
  if (!zkhandle) {
    zookeeper_close(zkhandle);
    zkhandle = zookeeper_init(host, InitWatch, timeout, 0, const_cast<char*>("TODO"), 0);
  }
  if (NULL == zkhandle) {
    cout << "zookeeper init error" << endl;
    sleep(1);
    continue;
  }
  int ret = zoo_acreate(zkhandle, "/cluster/index+endpoint", "", 8, &ZOO_OPEN_ACL_UNSAFE, ZOO_EPHEMERAL, CreateWatch, "create");
  if (ret) {
    cout << "zoo_acreate error :" << ret << endl;
  }
  sleep(5);
 }

 zookeeper_close(zkhandle);
}

客户端代码:

#include <zookeeper/zookeeper.h>
#include <zookeeper/zookeeper_log.h>
#include <iostream>

using namespace std;

void InitWatch(zhandle_t* zh, int type, int state, const char* path, void* watcher) {
  if (type == ZOO_SESSION_EVENT) {
    if (state == ZOO_CONNECTED_STATE) {
      cout << "build connection ok" << endl;
    } else if (state == ZOO_EXPIRED_SESSION_STATE) {
      cout << "connection disconnect" << endl;
    }
  }
}

void CreateWatch(int rc, const char *name, const void *data) {
  if (rc == ZNODEEXISTS || rc == ZOK) {
    if (rc == ZOK) {
      cout << "registry ok" << endl;
    } else {
      cout << "node exist" << endl;
    }
  } else {
    cout << "registry error" << endl;
  }
}

int main(int argc, char* argv[]) {
  const char* host = "10.7.18.31:2181,10.7.18.199:2181";
  int timeout = 100000;
  zoo_set_debug_level(ZOO_LOG_LEVEL_WARN);
  zhandle_t* zkhandle = NULL;
  zkhandle = zookeeper_init(host, InitWatch, timeout, 0, const_cast<char*>("TODO"), 0);
 while (1) {
  if (!zkhandle) {
    zookeeper_close(zkhandle);
    zkhandle = zookeeper_init(host, InitWatch, timeout, 0, const_cast<char*>("TODO"), 0);
  }
  if (NULL == zkhandle) {
    cout << "zookeeper init error" << endl;
    sleep(1);
    continue;
  }
  int ret = zoo_acreate(zkhandle, "/cluster/index+endpoint", "", 8, &ZOO_OPEN_ACL_UNSAFE, ZOO_EPHEMERAL, CreateWatch, "create");
  if (ret) {
    cout << "zoo_acreate error :" << ret << endl;
  }
  sleep(5);
 }

 zookeeper_close(zkhandle);
}


zookeeper 简单的代码demo

标签:

原文地址:http://my.oschina.net/hejiula/blog/358097

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!