/*
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. 使用strings查看二进制文件中的字符串

    使用strings查看二进制文件中的字符串 今天介绍的这个小工具叫做strings,它实现功能很简单,就是找出文件内容中的可打印字符串.所谓可打印字符串的涵义是,它的组成部分都是可打印字符,并且以nu ...

  2. nginx_mysql_redis配置

    #Nginx所用用户和组,window下不指定 #user nobody; #工作的子进程数量(通常等于CPU数量或者2倍于CPU) worker_processes 2; #错误日志存放路径 #er ...

  3. 分页型Memory LCD显存管理与emWin移植

    上一篇随笔整理了一下逐行扫描型Memory LCD的显存管理与emWin移植,这篇就整理一下分页型Memory LCD显存管理与emWin移植. //此处以SSD1306作为实例 //OLED的显存/ ...

  4. IntelliJ IDEA mac 快捷键

    cmd+O 查找类alt+cmd+O 符号shift+cmd+O 查找文件^+space 补全alt + F7 查找符号的使用情况F1 查看文档cmd+b 跳转定义,或者 鼠标+ctrlcmd+F12 ...

  5. jquery 返回顶部 兼容web移动

    返回顶部图片 http://hovertree.com/texiao/mobile/6/tophovertree.gif 具体实现代码 <span id="tophovertree&q ...

  6. vcpu

    qemu_kvm_start_vcpu --> qemu_init_vcpu --> x86_cpu_realizefn -->  x86_cpu_common_class_init ...

  7. NOSDK--关于android傻瓜式的分包设想

    一直以来,我总是以“够用就好”为理由,很少再维护过自己的一键打包的项目.最近接触了棱镜的sdk,感觉将apk包上传到棱镜服务器,后台来进行分包这种简单的方式很招人待见. 原理似乎不难,apk即zip压 ...

  8. mysql基于“时间”的盲注

    无需页面报错,根据页面响应时间做判断! mysql基于时间的盲注 =================================================================== ...

  9. 用swing也可以做出好看的界面

    用Swing做出的例子:JavaFX做出的界面:后来又做出了自己编写的一套基于Synth的L&F,其与直接在代码中重绘某个组件不同,最大优点是具有可插拔性,即在不改变原有程序代码的情况下,用户 ...

  10. [转]MySQL日志——Undo | Redo

    本文是介绍MySQL数据库InnoDB存储引擎重做日志漫游 00 – Undo LogUndo Log 是为了实现事务的原子性,在MySQL数据库InnoDB存储引擎中,还用Undo Log来实现多版 ...