/*
Asynchronous request-reply single-threaded server in Python
that spawns a request handler each time a request is received
This is different from other examples because the number of request handler threads is not defined ahead of time.
Request:
Client DEALER --> Server ROUTER --> Request handler (spawned)
1. Clients send requests via a DEALER socket on port 5570
2. Server receives requests via a ROUTER socket on port 5570
3. Server passes both the request and the client identity directly to request handlers when they are spawned
Reply:
Client DEALER <-- Server ROUTER <-- Server DEALER <-- Request handler DEALER
1. Request handler returns the reply to the Server via a DEALER socket on inproc
2. Server receives the reply from the request handler via a DEALER socket on inproc
3. Server sends the reply to the client via a ROUTER socket on port 5570
4. Client receives the reply via a DEALER socket on port 5570
*/ using System;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using NetMQ;
using NetMQ.Sockets; namespace NetmqSample
{
public class ZmqClient
{
public void Request(string input)
{
var socket = new DealerSocket();
socket.Options.Identity = Encoding.UTF8.GetBytes(Guid.NewGuid().ToString());
socket.Connect("tcp://127.0.0.1:5570"); socket.SendFrame(input);
Console.WriteLine($"client send: {input} : {DateTime.Now:T}"); var answer = socket.ReceiveFrameString();
Console.WriteLine($"client received: {answer} : {DateTime.Now:T}"); socket.Dispose();
}
} public class ZmqServer
{
private DealerSocket _backend;
private RouterSocket _frontend; public void Run()
{
_frontend = new RouterSocket();
_frontend.Bind("tcp://*:5570");
_frontend.ReceiveReady += Frontend_ReceiveReady; _backend = new DealerSocket();
_backend.Bind("inproc://backend");
_backend.ReceiveReady += Backend_ReceiveReady; var poller = new NetMQPoller { _frontend, _backend };
poller.RunAsync(); Console.WriteLine("server started");
} private void Backend_ReceiveReady(object sender, NetMQSocketEventArgs e)
{
var id = e.Socket.ReceiveFrameString();
var msg = e.Socket.ReceiveFrameString(); Console.WriteLine($"server backend response: {id} : {msg}");
_frontend.SendFrame(id, true);
_frontend.SendFrame(msg);
} private void Frontend_ReceiveReady(object sender, NetMQSocketEventArgs e)
{
var id = e.Socket.ReceiveFrameString();
var msg = e.Socket.ReceiveFrameString(); //Console.WriteLine($"server frontend received: {id} : {msg} : {DateTime.Now:T}");
var task = new Task(() => new RequestHandler().Run(id, msg), TaskCreationOptions.LongRunning);
task.Start();
}
} public class RequestHandler
{
public void Run(string id, string msg)
{
var worker = new DealerSocket("inproc://backend"); // Simulate a long-running operation
Thread.Sleep(); worker.SendFrame(id, true);
worker.SendFrame(msg + " : " + DateTime.Now.ToLongTimeString());
worker.Dispose();
}
}
}
    class Program
{
static void Main(string[] args)
{
var server = new ZmqServer();
server.Run(); Enumerable.Range(, ).ToList().ForEach(x =>
{
Task.Factory.StartNew(() => new ZmqClient().Request(x.ToString("")), TaskCreationOptions.LongRunning);
}); Console.ReadLine();
}
}

zeromq rpc原型的更多相关文章

  1. 简单的RPC原型与实现原理

    存在的问题 客户端硬编码服务端的地址 引入注册中心,方便服务的注册与发现 注册中心记录的信息:服务地址列表&服务节点权重 Zookeeper节点类型 临时节点:客户端.. 服务注销: tomc ...

  2. 使用go reflect实现一套简易的rpc框架

    go jsonrpc 在实际项目中,我们经常会碰到服务之间交互的情况,如何方便的与远端服务进行交互,就是一个需要我们考虑的问题. 通常,我们可以采用restful的编程方式,各个服务提供相应的web接 ...

  3. RPC框架原理简述:从实现一个简易RPCFramework说起(转)

    摘要: 本文阐述了RPC框架与远程调用的产生背景,介绍了RPC的基本概念和使用背景,之后手动实现了简易的RPC框架并佐以实例进行演示,以便让各位看官对RPC有一个感性.清晰和完整的认识,最后讨论了RP ...

  4. linux 下 rpc python 实例之使用XML-RPC进行远程文件共享

    这是个不错的练习,使用python开发P2P程序,或许通过这个我们可以自己搞出来一个P2P下载工具,类似于迅雷.XML-RPC是一个远程过程调用(remote procedure call,RPC)的 ...

  5. 使用XML-RPC进行远程文件共享

    这是个不错的练习,使用python开发P2P程序,或许通过这个我们可以自己搞出来一个P2P下载工具,类似于迅雷.XML-RPC是一个远程过程调用(remote procedure call,RPC)的 ...

  6. Redola.Rpc 的一个小目标

    Redola.Rpc 的一个小目标 Redola.Rpc 的一个小目标:20000 tps. Concurrency level: 8 threads Complete requests: 20000 ...

  7. ZeroMQ:云时代极速消息通信库

    ZeroMQ:云时代极速消息通信库(大规模|可扩展|低成本|高效率解决之道,大规模分布式|多线程应用程序|消息传递架构构建利器) [美]Pieter Hintjens(皮特.亨特金斯)著   卢涛 李 ...

  8. 以ZeroMQ谈消息中间件的设计【译文】

    本文主要是探究学习比较流行的一款消息层是如何设计与实现的 ØMQ是一种消息传递系统,或者乐意的话可以称它为"面向消息的中间件".它在金融服务,游戏开发,嵌入式系统,学术研究和航空航 ...

  9. NetMQ(一):zeromq简介

    ZeroMQ系列 之NetMQ 一:zeromq简介 二:NetMQ 请求响应模式 Request-Reply 三:NetMQ 发布订阅模式 Publisher-Subscriber 四:NetMQ ...

随机推荐

  1. java -- 容易放错的误区

    1.按值传递 和 引用传递 (基本类型包括基本类型的包装类 或者 字符串类型 传递的是 副本 并不会改变原来的值)||  如果是引用类型 传递的是地址,会改变原来的值. public class T ...

  2. 练习用基础SQL语句

    http://www.cnblogs.com/zxlovenet/p/3728842.html 本文语句大部分SQL语句来自<数据库系统概论>(第四版)王珊&萨师煊 ,是我们上课用 ...

  3. 初识exception

    一.exception的分类 根据此exception(异常)是否可以打断正在执行的指令,可以将exception分为 asynchronous exception 和 synchronous exc ...

  4. mysql 数据类型

    1.整型 MySQL数据类型 含义(有符号) tinyint(m) 1个字节  范围(-128~127) smallint(m) 2个字节  范围(-32768~32767) mediumint(m) ...

  5. TCPIP、Http、Socket的协议~ 写得挺形象,赞

    这篇文章写得挺形象,对TCPIP.HTTP.Socket的作用有一个整体层次的理解. 转载文章内容如下: 网络由下往上分为物理层.数据链路层.网络层.传输层.会话层.表示层和应用层.   通过初步的了 ...

  6. 【Sorting Collection】

    排序集锦 各种排序算法,总结一下,一直在遗忘...... [冒泡排序] 就是下面这个鬼啦: c实现代码(升序): #include<stdio.h> void BubbleSort(int ...

  7. 小明的密码-初级DP解法

    #include #include #include using namespace std; int visited[5][20][9009];// 访问情况 int dp[5][20][9009] ...

  8. 软件设计Tips

    模块-----角色----用户----部门 | |    ------权限 | 系统管理-------业务管理-------报表展示 | |         --------------------待 ...

  9. 小米4 miui专用 Xposed安装器86版

    转载自 http://www.52pojie.cn/thread-516435-1-1.html 写在前面:各位用xp受到不同限制,有些机型还找不到框架包,又要刷第三方rec又要谨慎选择框架版本.官方 ...

  10. Xcode7下模拟器输入文本无法显示系统键盘的解决办法

    xcode7下的ios模拟器输入内容无法系统键盘,只能用电脑键盘输入内容,这样可能会对调试带来麻烦. 其实xcode7下的ios模拟器默认只能使用一种,要么是模拟器系统键盘,要么就是是电脑键盘.设置方 ...