/**************************************************************
技术博客
http://www.cnblogs.com/itdef/
 
技术交流群
群号码:324164944
 
欢迎c c++ windows驱动爱好者 服务器程序员沟通交流
**************************************************************/
ROUTER 与 REQ通讯
#include "stdafx.h"
#include "zhelpers.hpp"
#include <thread> void worker_thread(void *arg) {
zmq::context_t context();
zmq::socket_t worker(context, ZMQ_REQ); s_set_id(worker, (intptr_t)arg);
worker.connect("tcp://localhost:5671"); // "ipc" doesn't yet work on windows. int total = ;
while () {
// Tell the broker we're ready for work
s_send(worker, "Hi Boss"); // Get workload from broker, until finished
std::string workload = s_recv(worker);
if ("Fired!" == workload) {
std::cout << "Processed: " << total << " tasks" << std::endl;
break;
}
total++; // Do some random work
s_sleep(within() + );
}
return;
} int main() {
zmq::context_t context();
zmq::socket_t broker(context, ZMQ_ROUTER); broker.bind("tcp://*:5671"); // "ipc" doesn't yet work on windows. const int NBR_WORKERS = ;
std::thread workers[NBR_WORKERS];
for (int worker_nbr = ; worker_nbr < NBR_WORKERS; worker_nbr++) {
workers[worker_nbr]= std::thread( worker_thread, (void *)(intptr_t)worker_nbr);
} // Run for five seconds and then tell workers to end
int64_t end_time = s_clock() + ;
int workers_fired = ;
while () {
// Next message gives us least recently used worker
std::string identity = s_recv(broker);
s_recv(broker); // Envelope delimiter
s_recv(broker); // Response from worker s_sendmore(broker, identity);
s_sendmore(broker, "");
// Encourage workers until it's time to fire them
if (s_clock() < end_time)
s_send(broker, "Work harder");
else {
s_send(broker, "Fired!");
if (++workers_fired == NBR_WORKERS)
break;
}
} for (int worker_nbr = ; worker_nbr < NBR_WORKERS; worker_nbr++) {
workers[worker_nbr].join();
}
return ;
}

ROUTER 与 DEALER通讯

// rtdealer_cpp.cpp : 定义控制台应用程序的入口点。
// #include "stdafx.h"
//
// Custom routing Router to Dealer
//
// Olivier Chamoux <olivier.chamoux@fr.thalesgroup.com> #include "zhelpers.hpp"
#include <thread> static void *
worker_task(void *args)
{
zmq::context_t context();
zmq::socket_t worker(context, ZMQ_DEALER); #if (defined (WIN32))
s_set_id(worker, (intptr_t)args);
#else
s_set_id(worker); // Set a printable identity
#endif worker.connect("tcp://localhost:5671"); int total = ;
while () {
// Tell the broker we're ready for work
s_sendmore(worker, "");
s_send(worker, "Hi Boss"); // Get workload from broker, until finished
s_recv(worker); // Envelope delimiter
std::string workload = s_recv(worker);
// .skip
if ("Fired!" == workload) {
std::cout << "Completed: " << total << " tasks" << std::endl;
break;
}
total++; // Do some random work
s_sleep(within() + );
} return NULL;
} // .split main task
// While this example runs in a single process, that is just to make
// it easier to start and stop the example. Each thread has its own
// context and conceptually acts as a separate process.
int main() {
zmq::context_t context();
zmq::socket_t broker(context, ZMQ_ROUTER); broker.bind("tcp://*:5671");
srandom((unsigned)time(NULL)); const int NBR_WORKERS = ;
std::thread workers[NBR_WORKERS];
int worker_nbr = ;
for (; worker_nbr < NBR_WORKERS; ++worker_nbr) {
workers[worker_nbr] = std::thread( worker_task, (void *)(intptr_t)worker_nbr);
} // Run for five seconds and then tell workers to end
int64_t end_time = s_clock() + ;
int workers_fired = ;
while () {
// Next message gives us least recently used worker
std::string identity = s_recv(broker);
{
s_recv(broker); // Envelope delimiter
s_recv(broker); // Response from worker
} s_sendmore(broker, identity);
s_sendmore(broker, ""); // Encourage workers until it's time to fire them
if (s_clock() < end_time)
s_send(broker, "Work harder");
else {
s_send(broker, "Fired!");
if (++workers_fired == NBR_WORKERS)
break;
}
} for (int worker_nbr = ; worker_nbr < NBR_WORKERS; ++worker_nbr) {
workers[worker_nbr].join();
} return ;
}

DEALER REQ区别在于 REQ模式socket发送信息时候会自动在信息前添加“”空白信息作为隔离

DEALER模式socket需要手动添加 s_sendmore(socket, "");

而ROUTER 在接受信息前会受到来源socket的地址信息 而发送时会将地址信息添加到socket的信息之前 同时还要在在信息前添加“”空白信息作为隔离

s_sendmore(broker, identity);
s_sendmore(broker, "");

均衡负载 此段代码比较复杂 理解较难

我将代码设置为单线程 然后调试 纠正了一些我理解的router模式

稍后画图

// lbbroker_cpp.cpp : 定义控制台应用程序的入口点。
// #include "stdafx.h"
#include "zhelpers.hpp"
#include <thread>
#include <queue> void client_thread(void* arg) {
zmq::context_t context();
zmq::socket_t client(context, ZMQ_REQ); s_set_id(client, (intptr_t));
client.connect("tcp://localhost:5672"); s_send(client, "HELLO");
std::string reply = s_recv(client);
std::cout << "Client: " << reply << std::endl;
return;
} void worker_thread(void* arg){
zmq::context_t context();
zmq::socket_t worker(context, ZMQ_REQ); s_set_id(worker, (intptr_t));
worker.connect("tcp://localhost:5673"); s_send(worker, "READY");
while () {
std::string address = s_recv(worker);
{
std::string empty = s_recv(worker);
assert(empty.size() == );
}
std::string request = s_recv(worker);
std::cout << "Worker: " << request << std::endl; s_sendmore(worker, address);
s_sendmore(worker, "");
s_send(worker, "OK");
}
return;
} int main()
{
zmq::context_t context();
zmq::socket_t frontend(context, ZMQ_ROUTER);
zmq::socket_t backend(context, ZMQ_ROUTER); frontend.bind("tcp://*:5672");
backend.bind("tcp://*:5673"); int client_nbr;
std::thread client[];
for (client_nbr = ; client_nbr < ; client_nbr++)
{
client[client_nbr] = std::thread(client_thread,(void *)(intptr_t)client_nbr);
} int worker_nbr;
std::thread worker[];
for (worker_nbr = ; worker_nbr < ; worker_nbr++)
{
worker[worker_nbr] = std::thread(worker_thread, (void *)(intptr_t)worker_nbr);
} std::queue<std::string> worker_queue; while ()
{
zmq::pollitem_t items[] = {
{backend,,ZMQ_POLLIN,},
{frontend,,ZMQ_POLLIN,}
};
if (worker_queue.size())
zmq::poll(&items[], , -);
else
zmq::poll(&items[], , -); if (items[].revents & ZMQ_POLLIN) { // Queue worker address for LRU routing
worker_queue.push(s_recv(backend)); {
// Second frame is empty
std::string empty = s_recv(backend);
assert(empty.size() == );
} // Third frame is READY or else a client reply address
std::string client_addr = s_recv(backend); // If client reply, send rest back to frontend
if (client_addr.compare("READY") != ) { {
std::string empty = s_recv(backend);
assert(empty.size() == );
} std::string reply = s_recv(backend);
s_sendmore(frontend, client_addr);
s_sendmore(frontend, "");
s_send(frontend, reply); if (--client_nbr == )
break;
}
}
if (items[].revents & ZMQ_POLLIN) { // Now get next client request, route to LRU worker
// Client request is [address][empty][request]
std::string client_addr = s_recv(frontend); {
std::string empty = s_recv(frontend);
assert(empty.size() == );
} std::string request = s_recv(frontend); std::string worker_addr = worker_queue.front();//worker_queue [0];
worker_queue.pop(); s_sendmore(backend, worker_addr);
s_sendmore(backend, "");
s_sendmore(backend, client_addr);
s_sendmore(backend, "");
s_send(backend, request);
}
} for (client_nbr = ; client_nbr < ; client_nbr++)
{
client[client_nbr].join();
} for (worker_nbr = ; worker_nbr < ; worker_nbr++)
{
worker[worker_nbr].join();
} return ;
}

此处图仅为个人领悟 不能保证完全正确 请谨慎参考

开始看官方的图示 我以为上述代码是如官方所示

但是对于router来说

connect到router的客户端或者工作者(client worker)

与router直接send到router的信息处理上是有区别的

下图用直线和箭头来区分

我将代码设置为单线程 然后调试 得出流程如图:

由于新建线程的随机性

步骤1 2 是的先后次序是随机的

1 client 发送信息到frontend router

信息格式为

"0001"

""

"HELLO"

2 worker发送信息到backend router

信息格式为

"0009"

""

"READY"

3函数主体进入POLL循环

根据代码

if (worker_queue.size())
zmq::poll(&items[0], 2, -1);
else
zmq::poll(&items[0], 1, -1);

由于空闲worker队列中暂时无记录

所以仅仅对backend 进行poll轮询

首先接受信息记录的通讯ID 为9 并push进worker队列

worker_queue.push(s_recv(backend));

然后接受空字节分隔符 在接受发送的正文“READY”

接收信息格式如下:

"0009"

""

"READY"

由于此次接受的正文是“READY” 根据代码不进入发送流程

if (client_addr.compare("READY") != 0)

步骤3结束

4 主体代码进入第二次POLL轮询

frontend 接收信息如下

信息格式为

"0001"

""

"HELLO"

弹出之前接收空闲worker的ID

std::string worker_addr = worker_queue.front();

然后向backend发送信息格式如下:

"0009"

""

"0001"

""

"HELLO"

5 backend接收信息后直接路由到ID为0009的worker

6 worker接收到信息格式为

"0001"

""

"HELLO"

然后发送信息格式:

"0001"

""

"OK"

7 backend接收到步骤6的信息:

将步骤6的worker的id 0009 push仅worker队列

worker_queue.push(s_recv(backend));

此次接受的信息格式为

"0009"

""

"001"

""

"OK"

由于此次接受正文不是"READY"

进入发送模式 发送信息到frontend

发送信息格式为

"0001"

""

"OK"

8 frontend直接路由此信息到ID 0001的client

最后client接收到的信息格式为

"OK"

zeromq学习记录(八)负载均衡 附ZMQ_ROUTER的流程分析的更多相关文章

  1. SpringCloud全家桶学习之Feign负载均衡----Feign(四)

    一.Feign概述 (1)Feign是什么? 官网地址:https://projects.spring.io/spring-cloud/spring-cloud.html#spring-cloud-f ...

  2. 架构师成长之路6.5 DNS服务器搭建(添加记录、负载均衡、DNS视图)

    点击返回架构师成长之路 架构师成长之路6.5 DNS服务器搭建(添加记录.负载均衡.DNS视图)  部署主DNS : 点击 部署从DNS : 点击 1.添加A记录.CNAME记录.MX记录.PTR记录 ...

  3. Nginx记录-nginx 负载均衡5种配置方式(转载)

    nginx 负载均衡5种配置方式 1.轮询(默认)   每个请求按时间顺序逐一分配到不同的后端服务器,如果后端服务器down掉,能自动剔除.  2.weight 指定轮询几率,weight和访问比率成 ...

  4. zeromq学习记录(一)最初的简单示例使用ZMQ_REQ ZMQ_REP

    阅读zeromq guide的一些学习记录 zeromq官方例子 在VC下运行会有些跨平台的错误 我这里有做修改 稍后会发布出来 相关的代码与库  http://download.zeromq.org ...

  5. Dubbo -- 系统学习 笔记 -- 示例 -- 负载均衡

    Dubbo -- 系统学习 笔记 -- 目录 示例 想完整的运行起来,请参见:快速启动,这里只列出各种场景的配置方式 负载均衡 在集群负载均衡时,Dubbo提供了多种均衡策略,缺省为random随机调 ...

  6. [记录]HAproxy负载均衡配置教程

    HAproxy负载均衡配置教程 一.简介 haproxy是一个开源的高性能负载均衡软件:支持双机热备.虚拟主机和图形化的管理界面,自带强大的对RS健康检查功能:支持TCP(四层).HTTP(七层)应用 ...

  7. Dubbo源码(八) - 负载均衡

    前言 本文基于Dubbo2.6.x版本,中文注释版源码已上传github:xiaoguyu/dubbo 负载均衡,英文名称为Load Balance,其含义就是指将负载(工作任务)进行平衡.分摊到多个 ...

  8. 学习下nginx负载均衡--深入理解nginx

    作为代理服务器,一般都需要向上游服务器转发请求.这里的负载均衡是指通过一种策略尽量把请求平均的分发都上游服务器 1.upstream 语法 upstream name {} 配置快: http 栗子( ...

  9. SpringCloud学习笔记:负载均衡Ribbon(3)

    1. RestTemplate简介 RestTemplate是Spring Resource中一个访问第三方RESTful API接口的网络请求框架. RestTemplate是用来消费REST服务的 ...

随机推荐

  1. koa2+log4js+sequelize搭建的nodejs服务

    主要参考http://www.jianshu.com/p/6b816c609669这篇文章 npm安装使用国内taobao镜像,速度更快些 npm --registry https://registr ...

  2. java File处理

    /**************************************************************************************工具类********** ...

  3. bzoj5049: 导航系统

    Description 小Q来到了一个随机的国度.这个国度由n座城市和m条双向道路构成.因为这个国度崇尚随机,因此m条边是用随机 选择两端点的方式生成的.充满好奇的小Q想在这里进行k次随机的旅行,每次 ...

  4. I18nUtils

    import java.io.File; import java.io.FileInputStream; import java.io.InputStream; import java.util.*; ...

  5. mysql数据库优化(四)-项目实战

    在flask项目中,防止随着时间的流逝,数据库数据越来越多,导致接口访问数据库速度变慢.所以自己填充数据进行测试及 mysql优化 1.插入数据: 通过脚本,使用多进程,每100次提交数据 impor ...

  6. 深入理解Tomcat

    简介 tomcat是一个web服务器,运行jsp和servlet,使用HTTP与客户端(通常是浏览器)进行通信. 构成 下图是tomcat的架构,可以看出:核心内容是Connector和Contain ...

  7. JVM 符号引用与直接引用

       Java类从加载到虚拟机内存中开始,到卸载出内存为止,它的整个生命周期包括,加载 ,验证 , 准备 , 解析 , 初始化 ,卸载 ,总共七个阶段.其中验证 ,准备 , 解析 统称为连接.     ...

  8. py库:把python打包成exe文件(pyinstaller)

    http://blog.csdn.net/be_quiet_endeavor/article/details/73929077 用Pyinstaller把Python3.4程序打包成可执行文件exe ...

  9. (14)其他Linux命令

    *****拷贝文件cp somefile.1 /home/hadoop/      以原文件名进行拷贝 cp somefile.1 /home/hadoop/somefile.2    以新文件名进行 ...

  10. Keil中 Program Size: Code RO-data RW-data ZI-data

    一般 MCU 包含的存储空间有:片内 Flash 与片内 RAM,RAM 相当于内存,Flash 相当于硬盘. 现在我们就一个STM32的工程为例子 linking... Program Size: ...