Zeromq基本模型-程序员宅基地

技术标签: 网络  

Zeromq基本模型

 

http://www.searchtb.com/2012/08/zeromq-primer.html

http://www.kuqin.com/algorithm/20120813/328769.html

1.Request-reply模式

 

说明:步调一致的同步请求应答模式,发送request之后必须等待reply才能继续发送request;

Client端 一般先send request 后 receive reply;

server端 一般先 receive request后 send reply。

该模式的简单使用例程 流程如下:

Start Start

 

 

Client send the request

Server receive the request

Client receive the answer

Server send the answer

 

 

 

 

 

 

 

End End

 

 

 

a) 服务端和客户端无论谁先启动,效果是相同的,这点不同于Socket。

b) 在服务端收到信息以前,程序是阻塞的,会一直等待客户端连接上来。

c) 服务端收到信息以后,会send一个“World”给客户端。值得注意的是一定是client连接上来以后,send消息给Server,然后Server再rev然后响应client,这种一问一答式的。如果Server先send,client先rev是会报错的。

 

// Hello World server

// Binds REP socket to tcp://*:5555

// Expects "Hello" from client, replies with "World"

//

#include

#include

#include

#include

 

int main (void)

{

void *context = zmq_ctx_new ();

 

// Socket to talk to clients

void *responder = zmq_socket (context, ZMQ_REP);

zmq_bind (responder, "tcp://*:5555");

 

while (true) {

// Wait for next request from client,

//do the zmq_msg_recv before zmq_msg_send

zmq_msg_t request;

zmq_msg_init (&request);

//Receiving "Hello" from client through the responder

zmq_msg_recv (&request, responder, 0);

zmq_msg_close (&request);

 

// Do some 'work'

sleep (1);

 

// Send reply back to client

zmq_msg_t reply;

zmq_msg_init_size (&reply, 5);

memcpy (zmq_msg_data (&reply), "World", 5);

// Send "world" to the client through responder

zmq_msg_send (&reply, responder, 0);

zmq_msg_close (&reply);

}

// We never get here but if we did, this would be how we end

zmq_close (responder);

zmq_ctx_destroy (context);

return 0;

}

 

 

//

// Hello World client

// Connects REQ socket to tcp://localhost:5555

// Sends "Hello" to server, expects "World" back

//

#include

#include

#include

#include

int main (void)

{

void *context = zmq_ctx_new ();

 

// Socket to talk to server

printf ("Connecting to hello world server…\n");

void *requester = zmq_socket (context, ZMQ_REQ);

zmq_connect (requester, "tcp://localhost:5555");

 

//only send one rquest an one reply in each process

zmq_msg_t request;

zmq_msg_init_size (&request, 5);

memcpy (zmq_msg_data (&request), "Hello", 5);

printf ("Sending Hello %d…\n", request_nbr);

zmq_msg_send (&request, requester, 0);

zmq_msg_close (&request);

 

zmq_msg_t reply;

zmq_msg_init (&reply);

zmq_msg_recv (&reply, requester, 0);

printf ("Received World %d\n", request_nbr);

zmq_msg_close (&reply);

 

zmq_close (requester);

zmq_ctx_destroy (context);

return 0;

}

 

 

 

 

 

 

 

 

2.Publish-subscribe模式

我们可以想象一下天气预报的订阅模式,由一个节点提供信息源,由其他的节点,接受信息源的信息.

 

 

 

说明:pub-sub是一种异步模型;

publisher向所有的subscriber push所有消息;

subscriber可以订阅多种消息,可以收到任何匹配的消息,可以过滤消息,可以向多个publisher订阅消息。

如果一个publisher没有相关的subscriber,则它只会简单的丢弃消息。

这个模型里,发布端是单向只发送数据的,且不关心是否把全部的信息都发送给订阅端。如果发布端开始发布信息的时候,订阅端尚未连接上来,这些信息直接丢弃。不过一旦订阅端连接上来,中间会保证没有信息丢失。同样,订阅端则只负责接收,而不能反馈。如果发布端和订阅端需要交互(比如要确认订阅者是否已经连接上),则使用额外的 socket 采用请求回应模型满足这个需求

 

 

 

 

 

 

 

 

 

 

 

 

// // Weather update server // Binds PUB socket to tcp://*:5556 // Publishes random weather updates // #include "zhelpers.h"

int main (void) { // Prepare our context and publisher void *context = zmq_ctx_new (); void *publisher = zmq_socket (context, ZMQ_PUB); zmq_bind (publisher, "tcp://*:5556"); zmq_bind (publisher, "ipc://weather.ipc");

// Initialize random number generator srandom ((unsigned) time (NULL)); while (true) { // Get values that will fool the boss int zipcode, temperature, relhumidity; zipcode = randof (100000); temperature = randof (215) - 80; relhumidity = randof (50) + 10;

// Send message to all subscribers char update [20]; sprintf (update, "d %d %d", zipcode, temperature, relhumidity); s_send (publisher, update); } zmq_close (publisher); zmq_ctx_destroy (context); return 0; }

// Weather update client // Connects SUB socket to tcp://localhost:5556 // Collects weather updates and finds avg temp in zipcode #include "zhelpers.h"

int main (int argc, char *argv []) { void *context = zmq_ctx_new ();

// Socket to talk to server printf ("Collecting updates from weather server…\n"); void *subscriber = zmq_socket (context, ZMQ_SUB); zmq_connect (subscriber, "tcp://localhost:5556");

// Subscribe to zipcode, default is NYC, 10001 char *filter = (argc > 1)? argv [1]: "10001 "; zmq_setsockopt (subscriber, ZMQ_SUBSCRIBE, filter, strlen (filter)); char *string = s_recv (subscriber);

int zipcode, temperature, relhumidity; sscanf (string, "%d %d %d",&zipcode, &temperature, &relhumidity);
printf ("the imformation for zipcode '%s' was %d %d %d \n", filter,zipcode,temperature,relhumidity);

zmq_close (subscriber); zmq_ctx_destroy (context); return 0; }

a) 客户端需要zmq_setsockopt (subscriber, ZMQ_SUBSCRIBE, filter, strlen (filter));设置一个过滤值,相当于设定一个订阅频道,否则什么信息也收不到。

b) 服务器端一直不断的广播中,如果中途有Subscriber端退出,并不影响他继续的广播,当Subscriber再连接上来的时候,收到的就是后来发送的新的信息了。这对比较晚加入的,或者是中途离开的订阅者,必然会丢失掉一部分信息.

c) 但是,如果Publisher中途离开,所有的Subscriber会hold住,等待Publisher再上线的时候,会继续接受信息。

3.Pipeline push-pull模式

想象一下这样的场景,如果需要统计各个机器的日志,我们需要将统计任务分发到各个节点机器上,最后收集统计结果,做一个汇总。PipeLine比较适合于这种场景.

 

 

 

说明:这个模型里,管道是单向的,从 PUSH 端单向的向 PULL 端单向的推送数据流;

Ventilator将任务分配给worker实现任务的并行处理;

Push-pull模式是单向的,很适合消费者能力不足的情况,可以提供多个消费者。一条消息如果被A消费,B将不会再消费这条消息。

 

可以看到,task ventilator使用的是SOCKET_PUSH,将任务分发到Worker节点上。而Worker节点上,使用SOCKET_PULL从上游接受任务,并使用SOCKET_PUSH将结果汇集到Slink。值得注意的是,任务的分发的时候也同样有一个负载均衡的路由功能,worker可以随时自由加入,task ventilator可以均衡将任务分发出去

转载于:https://www.cnblogs.com/hust2012/archive/2012/12/26/2834667.html

版权声明:本文为博主原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。
本文链接:https://blog.csdn.net/dianding7125/article/details/102130975

智能推荐

linux 路由跟踪表满错误 nf_conntrack: table full, dropping packet 原理解决方法_nf_conntrack:table full-程序员宅基地

文章浏览阅读5.2k次。netfilter/conntrack 相关内核参数往往是用 Linux 服务器的互联网小公司业务量上去之后遇到的第 3 个“新手怪”。(第 1 位:进程可用的 FD 不足,第 2 位:IP 临时端口不足 + TIME_WAIT 状态的连接过多导致无法建立新连接)很多人以为 Linux 经过这么多年优化,默认参数应该“足够好”,其实不是。默认参数面向“通用”服务器,不适用于连接数和访问量比较多..._nf_conntrack:table full

linux下gcc编译优化选项的大体操作是怎么样的?_linux gcc 性能优化 编译参数调优-程序员宅基地

文章浏览阅读1.3k次。起因: 目前项目使用nios IDE作为开发平台,其使用的编译器为gcc的交叉编译器。在设定编译条件时,在debug模式下生成的程序正常,但是在release模式下会出现LCD显示的开端显示不全,缺少一个字节或字的状况。为了了解具体为什么造成该问题,对两种模式下的配置做了对比,编译器皆为nios2-elf-gcc交叉编译器,debug模式编译器参数为:-DALT_DEBUG -O0 ..._linux gcc 性能优化 编译参数调优

各种免费好用的api,含天气查询、IP查询、物流查询等-程序员宅基地

文章浏览阅读706次,点赞7次,收藏9次。各种免费好用的api,含天气查询、IP查询、物流查询等

Qt工作笔记-Windows上界面滑动效果_qt 界面滑动-程序员宅基地

文章浏览阅读5.5k次,点赞7次,收藏20次。运行截图如下:源码如下:widget.h#ifndef WIDGET_H#define WIDGET_H#include <QWidget>QT_BEGIN_NAMESPACEclass QMouseEvent;class QPropertyAnimation;QT_END_NAMESPACEnamespace Ui {class Widget;..._qt 界面滑动

Qt工作笔记-使用Qt中QProcess与iostream中system调用外部进程_qt system-程序员宅基地

文章浏览阅读5.8k次,点赞9次,收藏22次。目录 基础概念演示 基础概念Qt中的QProcess类可以调用外部程序!同时iostream有个函数system也可以调用外部程序!这个system也是Windows,Linux上通用的 演示如下代码!这是一个外部程序,是用vs2012写的#include <fstream>#include <iostream>usin..._qt system

python︱matplotlib使用(读入、显示、写出、opencv混用、格式转换...)_<matplotlib.image.axesimage object at 0x7f1d27d0c2-程序员宅基地

文章浏览阅读1.3w次,点赞2次,收藏18次。opencv和matplotlib是比较常用的图像分析模块。在Ipython里面,opencv不能直接显示出来,所以有些时候会借助matplotlib来显示。%matplotlib inline .1、matplotlib的读入与显示import matplotlib.pyplot as plt import numpy as np from PIL import ..._

随便推点

关于java的equals和hashcode方法_java使用equals肯定会先调用hashcode吗-程序员宅基地

文章浏览阅读993次。大牛的博客http://blog.csdn.net/lifetragedy, 里面有一系列面试题. 本篇就写关于equals和hashcode的.首先,我们提出问题.1.equals和hashcode有没有什么样的规范呢?2.Object类的equals和hashcode方法是什么样的?3.什么情况下需要重写equals和hashcode方法?重写equals的话必须重写hashc_java使用equals肯定会先调用hashcode吗

快排递归非递归python_用Python递归思考-程序员宅基地

文章浏览阅读611次。快排递归非递归pythonOf all ideas I have introduced to children, recursion stands out as the one idea that is particularly able to evoke an excited response.Seymour Papert, Mindstorms 在我介绍给孩子们的所有想法中,递归特别引人注目...

MATLAB函数句柄(@),feval与内连函数(inline)_f=inline(a)内联函数-程序员宅基地

文章浏览阅读5.3k次,点赞5次,收藏9次。转载地址:http://www.cnblogs.com/begtostudy/archive/2012/06/27/2565920.htmlMATLAB函数句柄函数句柄(Function handle)是MATLAB的一种数据类型。包含了函数的路径、函数名、类型以及可能存在的重载方法;引入函数句柄是为了使feval及借助于它的泛函指令工作更可靠;使“函数调用”像“变_f=inline(a)内联函数

Android资源代码 源码 整理 Github开源项目下载地址_com.github.chenglei1986:datepicker:-snapshot aar依赖-程序员宅基地

文章浏览阅读5.4k次。第一部分 个性化控件(View)主要介绍那些不错个性化的View,包括ListView、ActionBar、Menu、ViewPager、Gallery、GridView、ImageView、ProgressBar、TextView、ScrollView、TimeView、TipView、FlipView、ColorPickView、GraphView、UI Style等等。一、L_com.github.chenglei1986:datepicker:-snapshot aar依赖下载

解决Cannot find module ‘element-ui‘_cannot find module './plugins/element.js' at webpa-程序员宅基地

文章浏览阅读3.6k次。**报错:Cannot find module ‘element-ui’**解决办法:步骤1.npm install element-ui --save步骤2.在main.js中import ElementUI from 'element-ui' import 'element-ui/lib/theme-chalk/index.css' Vue.use(ElementUI);芜湖!!_cannot find module './plugins/element.js' at webpackmissingmodule

S5P4418开发板 A9四核开发板 推荐 最新产品_cortex a9开发板推荐-程序员宅基地

文章浏览阅读565次。推荐理由:1、三星中国S5P4418开发系统国内唯一旗舰商;2、全球绝对独家发布,独家支持3G/4G电话和短信功能;支持休眠唤醒;3、真正的A9四核高端开发板; 4、严格意义上的企业产品级开发板; 5、强悍、稳定的硬件设计和军工级别的质量;6、免费送WIFI蓝牙二合一模块和超级电池盒 2015年5月10号,经过润尔信息(原广州华天正)技术研发团队历时_cortex a9开发板推荐