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

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

服务端代码:

#include <zookeeper/zookeeper.h>
#include <zookeeper/zookeeper_log.h>
#include <iostream> using namespace std; zhandle_t* zkhandle = NULL;
const char* host = "10.7.18.31:2181,10.7.18.199:2181";
int timeout = ; 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;
zkhandle = zookeeper_init(host, InitWatch, timeout, , const_cast<char*>("TODO"), );
}
}
} 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[]) {
zoo_set_debug_level(ZOO_LOG_LEVEL_WARN);
zhandle_t* zkhandle = NULL;
zkhandle = zookeeper_init(host, InitWatch, timeout, , const_cast<char*>("TODO"), );
if (NULL == zkhandle) {
cout << "zookeeper init error" << endl;
return ;
}
while () {
int ret = zoo_acreate(zkhandle, "/cluster/index+endpoint", "", , &ZOO_OPEN_ACL_UNSAFE, ZOO_EPHEMERAL, CreateWatch, "create");
if (ret) {
cout << "zoo_acreate error :" << ret << endl;
}
sleep();
} zookeeper_close(zkhandle); return ;
}

client

#include <zookeeper/zookeeper.h>
#include <zookeeper/zookeeper_log.h>
#include <iostream> using namespace std; const char* host = "10.7.18.31:2181,10.7.18.199:2181";
const int timeout = ;
zhandle_t* zkhandle = NULL; 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;
zkhandle = zookeeper_init(host, InitWatch, timeout, , const_cast<char*>("TODO"), );
}
}
} void ChildWatch(zhandle_t* zh, int type, int state, const char* path, void* watcher) {
if (type == ZOO_CHILD_EVENT) {
struct String_vector str_vec;
int ret = zoo_wget_children(zh, "/cluster", ChildWatch, , &str_vec);
if (ret) {
cout << "zoo_wget_children error :" << ret << endl;
}
cout << "endpoint change: " << endl;
for (int i = ; i < str_vec.count; ++i) {
string endpoint = str_vec.data[i];
cout << endpoint << endl;
}
}
} int main(int argc, char* argv[]) {
zoo_set_debug_level(ZOO_LOG_LEVEL_WARN);
if (zkhandle) {
zookeeper_close(zkhandle);
}
zkhandle = zookeeper_init(host, InitWatch, timeout, , const_cast<char*>("TODO"), );
if (NULL == zkhandle) {
cout << "zookeeper init error" << endl;
return ;
}
while () {
struct String_vector str_vec;
int ret = zoo_wget_children(zkhandle, "/cluster", ChildWatch, const_cast<char*>(""), &str_vec);
if (ret) {
cout << "zoo_wget_children error :" << ret << endl;
}
for (int i = ; i < str_vec.count; ++i) {
string endpoint = str_vec.data[i];
cout << "init endpoint :" << endpoint << endl;
}
sleep();
} zookeeper_close(zkhandle);
}

ZooKeeper server &&client的更多相关文章

  1. zookeeper中client命令实践

    Welcome to ZooKeeper! 2016-09-14 16:06:04,528 [myid:] - INFO [main-SendThread(master:2181):ClientCnx ...

  2. NetMQ(ZeroMQ)Client => Server => Client 模式的实现

    ØMQ (也拼写作ZeroMQ,0MQ或ZMQ)是一个为可伸缩的分布式或并发应用程序设计的高性能异步消息库.它提供一个消息队列, 但是与面向消息的中间件不同,ZeroMQ的运行不需要专门的消息代理(m ...

  3. docker报Error response from daemon: client is newer than server (client API version: 1.24, server API version: 1.19)

    docker version Client: Version: 17.05.0-ce API version: 1.24 (downgraded from 1.29) Go version: go1. ...

  4. Zookeeper之Zookeeper的Client的分析【转】

    Zookeeper之Zookeeper的Client的分析 1)几个重要概念 ZooKeeper:客户端入口 Watcher:客户端注册的callback ZooKeeper.SendThread:  ...

  5. org.I0Itec.zkclient.exception.ZkTimeoutException: Unable to connect to zookeeper server within

    org.I0Itec.zkclient.exception.ZkTimeoutException: Unable to connect to zookeeper server within timeo ...

  6. Unable to connect to zookeeper server within timeout: 5000

    错误 严重: StandardWrapper.Throwable org.springframework.beans.factory.BeanCreationException: Error crea ...

  7. 线上zk节点报org.apache.zookeeper.server.NIOServerCnxnFactory.run(NIOServerCnxnFactory.java:187) at java.lang.Thread.run(libgcj.so.10)

    线上zk做配置管理,最近突然发现两个节点一直在刷下边 java.nio.channels.CancelledKeyException    at gnu.java.nio.SelectionKeyIm ...

  8. Redis2.2.2源码学习——Server&Client链接的建立以及相关Event

    Redis中Server和User建立链接(图中的client是服务器端用于描述与客户端的链接相关的信息) Redis Server&Client链接的建立时相关Event的建立(图中的cli ...

  9. Socket编程--基础(基本server/client实现)

    IPv4套接口地址结构 IPv4套接口地址结构通常也称为“网际套接字地址结构”,它以“sockaddr_in”命名,定义在头文件中 LINUX结构下的常用结构,一般创建套接字的时候都要将这个结构里面的 ...

随机推荐

  1. thinkphp5配置讲解

    一.thinkphp配置类型有哪些? 1.在thinkphp中,有6种配置.即惯例配置,应用配置.扩展配置.模块配置.场景配置.动态配置. 2.惯例配置就是系统默认的配置. 3.应用配置就是我们自己开 ...

  2. python打印九九乘法表

    每种编程语言都可能会遇到编写“九九乘法表”的问题,用Python来处理也是很简单的,具体代码如下(基于Python3)): i = 1 while i <= 9: j = 1 while j & ...

  3. day 24 内置模块re

    1.正则表达式,匹配字符串 正则表达式是对字符串操作的一种逻辑公式.我们一般使用正则表达式对字符串镜子那个匹配和过滤,使用正则的优缺点: 优点: 灵活,功能性强,逻辑性强 缺点: 上手难.一旦上手,会 ...

  4. 大数据学习--day11(抽象类、接口、equals、compareTo)

    抽象类.接口.equals.compareTo 什么是抽象方法  ?     区分于正常的方法       1.使用了 abstract 修饰符          该修饰符修饰方法 则该方法就是抽象方 ...

  5. 《JQuery常用插件教程》系列分享专栏

    <JQuery常用插件教程>已整理成PDF文档,点击可直接下载至本地查阅https://www.webfalse.com/read/201719.html 文章 使用jquery插件实现图 ...

  6. Shuffling Machine

    7-43 Shuffling Machine(20 分) Shuffling is a procedure used to randomize a deck of playing cards. Bec ...

  7. 如何理解 UL94HB , UL94-V0 , UL94-V1 , UL94-V2

    塑料阻燃等级由HB,V-2,V-1向V-0逐级递增: UL94V0,V1,V2是不同的阻燃等级,其等级不同,耐燃的测试方法亦不同,测试判定的标准也不同. V0的测试方法是将测试物倾斜45度,用酒精灯点 ...

  8. P2934 [USACO09JAN]安全出行Safe Travel

    P2934 [USACO09JAN]安全出行Safe Travel https://www.luogu.org/problemnew/show/P2934 分析: 建出最短路树,然后考虑一条非树边u, ...

  9. 4456: [Zjoi2016]旅行者

    4456: [Zjoi2016]旅行者 https://www.lydsy.com/JudgeOnline/problem.php?id=4456 分析: 每次对当前矩阵按长边化一条分治线,然后在对分 ...

  10. JS学习 用 arguments 对象模拟函数重载

    用 arguments 对象判断传递给函数的参数个数,即可模拟函数重载: function doAdd() { if(arguments.length == 1) { alert(arguments[ ...