写了一个关于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. Filter(过滤器)与Interceptor(拦截器)的区别

    Filter能够对请求和响应资源进行拦截: Interceptor只针对请求进行拦截 在 Struts2中: (1)拦截器是基于java反射机制的,而过滤器是基于函数回调的. (2)过滤器依赖与ser ...

  2. sass的嵌套

    sass的嵌套包括两种: 1.选择器的嵌套.(最常用到) 指的是在一个选择器中嵌套另一个选择器来实现继承,从而增强了sass文件的结构性和可读性. 在选择器嵌套中,可以使用&表示父元素选择器 ...

  3. git 完善使用中

    GIT 版本库控制: 第一步:Git 的账号注册 url :https://github.com/ 这是git的官网如果第一次打开会这样 中间红色圈内是注册 内容, 第一项是用户名 第二项是邮箱 第三 ...

  4. Macbook下安装memcached

    参考文献: https://blog.csdn.net/weixin_41827162/article/details/82049520 感谢大佬 安装memcached需要Homebrew 注意点: ...

  5. For-each Loop,Index++ Loop , Iterator 那个效率更高

    平时在写Java/C# 程序的时候,会写很多的Loop 语句,for() 及 Iterator loop 及Java 8 的foreach Loop, 这些Loop 那种效率最高呢?写个小程序测试一下 ...

  6. opencv3 学习四 - 图像减色

    程序如下 #include "opencv2/opencv.hpp" using namespace cv; int main() { // 灰度图 Mat original = ...

  7. Qt :undefined reference to vtable for "xxx::xxx"

    现象: 类加上宏 Q_OBJECT 就会报错 :undefined reference to vtable for "xxx::xxx" 解决方法: 重新 qmake 其他情况,查 ...

  8. Python3爬虫(十四) 验证码处理

    Infi-chu: http://www.cnblogs.com/Infi-chu/ 一.图形验证码识别1.使用tesserocr import tesserocr from PIL import I ...

  9. Go 入门 - 方法和接口

    方法和接口 方法的接受者 Go中没有类,取而代之的是在结构体上定义的方法 为了将方法(函数)绑定在某一类结构体上,我们在定义函数(方法)时引入"接受者"的概念. 方法接受者在它自己 ...

  10. 北京Uber优步司机奖励政策(1月1日)

    滴快车单单2.5倍,注册地址:http://www.udache.com/ 如何注册Uber司机(全国版最新最详细注册流程)/月入2万/不用抢单:http://www.cnblogs.com/mfry ...