写了一个关于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. UIDynamic-吸附-重力-碰撞-物理仿真动画

    现实生活中: 运动场==物理仿真器 跑步==物理仿真行为 人==仿真元素 创建步骤: 1.创建物理仿真器,并且指定仿真范围 2.创建物理仿真行为,并且指定仿真元素 3.将物理仿真行为添加到仿真器中 D ...

  2. TinyMCE:下载、安装、配置

    第一步:下载 官网下载:https://www.tiny.cloud/download/ TinyMCE从4.0开始,不再支持直接下载,而是直接使用提供免费的CDN,让用户免除安装过程,可以在网站中使 ...

  3. 使用docker搭建laravel记叙

    第一步,先从dockerhub上pull一个docker镜 docker pull laraedit/laraedit 这个docker镜像已经安装了 nginx.laravel和mysql,所以不需 ...

  4. JavaSE思维导图

    Java基础知识:  面向对象:  集合:  多线程.网络编程.反射.设计模式:  常用API:  转载 https://blog.csdn.net/qq_34983808/article/detai ...

  5. Java String源码解析

    public final class String implements java.io.Serializable, Comparable<String>, CharSequence { ...

  6. dubbo之监控中心(monitor)

    一.monitor是dubbo框架中的一个监控中心.这个只是针对于消费者和提供者进行一个数据记录,不参与业务和使用.当然当monitor挂掉之后,也不会影响服务的正常运行. 二.在阿里的dubbo中也 ...

  7. nodejs HTTP服务

    nodejs中的HTTP服务   nodejs最重要的方面之一是具有非常迅速的实现HTTP和HTTPS服务器和服务的能力.http服务是相当低层次的,你可能要用到不同的模块,如express来实现完整 ...

  8. python 函数定义顺序

    #!/usr/bin/python # Hello World def order(): print("haha") print('Hello World!') order()

  9. Selenium(Python)驱动Chrome浏览器

    Chrome浏览器与chromedriver.exe驱动可以是官网上最新的, 随意! Chrome.py: from selenium import webdriverfrom selenium.we ...

  10. APP功能性测试-1

    疑难点 根据软件说明()或用户需求()验证App的各个功能实现 根据需求,提炼App的用户使用场景,验证功能 根据测试指标,验证功能 根据被测试功能点的特性采用特定的方法进行测试(场景,边界值,,,) ...