zl程序教程

您现在的位置是:首页 >  大数据

当前栏目

Zookeeper场景实践:(5)分布式通知/协调

zookeeper分布式分布式 实践 场景 通知 协调
2023-09-14 09:01:04 时间
系统调度模式:操作人员发送通知实际是通过控制台改变某个节点的状态,然后Zookeeper将这些变化发送给注册了这个节点的Watcher的所有客户端。 工作汇报模式:这个情况是每个工作进程都在某个目录下创建一个临时节点,并携带工作的进度数据。这样汇总的进程可以监控目录子节点的变化获得工作进度的实时的全局情况。 总的来说,利用Zookeeper的watcher注册和异步通知功能,通知的发送者创建一个节点,并将通知的数据写入的该节点;通知的接受者则对该节点注册watch,当节点变化时,就算作通知的到来。

通过上面的说明,其实实现还是非常容易的。看下关键的几个地方:


void zktest_watcher_g(zhandle_t* zh, int type, int state, const char* path, void* watcherCtx) 

 //监控节点数据变化

 if(type == ZOO_CHANGED_EVENT 

 state == ZOO_CONNECTED_STATE 

 g_monitor_child == 0){

 show_notify(zh,g_path);

 //监控子节点个数变化

 }else if(type == ZOO_CHILD_EVENT 

 state == ZOO_CONNECTED_STATE 

 g_monitor_child == 1){

 show_list(zh,g_path);

 //监控子节点数据变化

 }else if(type == ZOO_CHANGED_EVENT 

 state == ZOO_CONNECTED_STATE 

 g_monitor_child == 1){

 show_list(zh,g_path);

}


下面是完整的代码:

#include stdio.h 

#include string.h 

#include unistd.h 

#include"zookeeper.h" 

#include"zookeeper_log.h" 

char g_host[512]= "172.17.0.36:2181"; 

char g_path[512]= "/Notify";

int g_monitor_child = 0;

//watch function when child list changed

void zktest_watcher_g(zhandle_t* zh, int type, int state, const char* path, void* watcherCtx);

void show_notify(zhandle_t *zkhandle,const char *path);

//show all process ip:pid

void show_list(zhandle_t *zkhandle,const char *path);

void print_usage();

void get_option(int argc,const char* argv[]);

/**********unitl*********************/ 

void print_usage()

 printf("Usage : [notify] [-h] [-c] [-p path][-s ip:port] \n");

 printf(" -h Show help\n");

 printf(" -p path\n");

 printf(" -c monitor the child nodes\n");

 printf(" -s zookeeper server ip:port\n");

 printf("For example:\n");

 printf("notify -s172.17.0.36:2181 -p /Notify\n");

void get_option(int argc,const char* argv[])

 extern char *optarg;

 int optch;

 int dem = 1;

 const char optstring[] = "hcp:s:";


void zktest_watcher_g(zhandle_t* zh, int type, int state, const char* path, void* watcherCtx) printf("watcher event\n"); printf("type: %d\n", type); printf("state: %d\n", state); printf("path: %s\n", path); printf("watcherCtx: %s\n", (char *)watcherCtx); if(type == ZOO_CHANGED_EVENT state == ZOO_CONNECTED_STATE g_monitor_child == 0){ show_notify(zh,g_path); }else if(type == ZOO_CHILD_EVENT state == ZOO_CONNECTED_STATE g_monitor_child == 1){ show_list(zh,g_path); }else if(type == ZOO_CHANGED_EVENT state == ZOO_CONNECTED_STATE g_monitor_child == 1){ show_list(zh,g_path); void show_notify(zhandle_t *zkhandle,const char *path) char notify_buffer[1024]={0}; int notify_len = sizeof(notify_buffer); int ret = zoo_get(zkhandle,g_path,1,notify_buffer,¬ify_len,NULL); if(ret != ZOK){ fprintf(stderr,"failed to get the data of path %s!\n",g_path); }else{ printf("Notice:%s\n",notify_buffer); void show_list(zhandle_t *zkhandle,const char *path) struct String_vector children; int i = 0; int ret = zoo_get_children(zkhandle,path,1, children); if(ret == ZOK){ char child_path[512] ={0}; char notify_buffer[1024] = {0}; int notify_len = sizeof(notify_buffer); printf("--------------\n"); for(i = 0; i children.count; ++i){ sprintf(child_path,"%s/%s",g_path,children.data[i]); ret = zoo_get(zkhandle,child_path,1,notify_buffer,¬ify_len,NULL); if(ret != ZOK){ fprintf(stderr,"failed to get the data of path %s!\n",child_path); }else{ printf("%s:%s\n",children.data[i],notify_buffer); }else{ fprintf(stderr,"failed to get the children of path %s!\n",path); for(i = 0; i children.count; ++i){ free(children.data[i]); children.data[i] = NULL; int main(int argc, const char *argv[]) int timeout = 30000; char path_buffer[512]; int bufferlen=sizeof(path_buffer); zoo_set_debug_level(ZOO_LOG_LEVEL_WARN); //设置日志级别,避免出现一些其他信息 get_option(argc,argv); zhandle_t* zkhandle = zookeeper_init(g_host,zktest_watcher_g, timeout, 0, (char *)"Notify Test", 0); if (zkhandle ==NULL) fprintf(stderr, "Error when connecting to zookeeper servers...\n"); exit(EXIT_FAILURE); int ret = zoo_exists(zkhandle,g_path,0,NULL); if(ret != ZOK){ ret = zoo_create(zkhandle,g_path,"1.0",strlen("1.0"), ZOO_OPEN_ACL_UNSAFE,0, path_buffer,bufferlen); if(ret != ZOK){ fprintf(stderr,"failed to create the path %s!\n",g_path); }else{ printf("create path %s successfully!\n",g_path); if(ret == ZOK g_monitor_child == 0){ show_notify(zkhandle,g_path); }else if(ret == ZOK g_monitor_child == 1){ show_list(zkhandle,g_path); getchar(); zookeeper_close(zkhandle); return 0; }


程序由3个参数选项


notify -s172.17.0.36:2181 -p /Notify
当你在客户端修改数据的时候,程序就能收到对应的通知了。


ZooKeeper(分布式协调服务)使用介绍 ZooKeeper 是一个开源的分布式协调服务,目前由 Apache 进行维护。ZooKeeper 可以用于实现分布式系统中常见的发布/订阅、负载均衡、命令服务、分布式协调/通知、集群管理、Master 选举、分布式锁和分布式队列等功能。
你造吗?zookeeper原来是用 贿赂算法 来完成分布式服务协调的 大家好,我是指北君。今天,我想学习下zookeeper,毕竟微服务这么火,立志不当最菜程序员的我也不能掉队。 但是,这个zookeepr我也没用过,只能从这个理论基础学起了,最终目的就是对它这个源码也得有个大概的了解。啥,你问我为啥要学习源码?平时也不用!! 那你要看下为啥现在刚毕业00后的都张口闭口了解各种源码了,不卷能行么,不到35就得退休了。 今天就给大家聊下,对于这个zk理论的学习。
Zookeeper详细使用解析!分布式架构中的协调服务框架最佳选型实践 本文主要介绍了Zookeeper实现分布式协调服务,解决分布式环境中服务的协调和管理问题。分析了Zookeeper实现分布式锁的方式,详细介绍Zookeeper中的数据模型和特点,Zookeeper中一致性的实现方式。通过使用Docker安装Zookeeper,说明了Zookeeper进行分布式协调服务时的三种工作模式以及三种使用的端口。
Zookeeper基本原理与运用场景 Zookeeper基本原理与运用场景一、什么是Zookeeper? zookeeper是一个分布式的一致性协调服务。 换句话说,也可以把zookeeper看成一个小型的分布式文件系统。但是和FastDFS不同,zookeeper只适合用来存储一些小型的数据或者配置信息。
zookeeper在分布式系统中作为协调员的角色,可应用于Leader选举、分布式锁、配置管理等服务的实现。