支持异步同步的分布式CommandBus MSMQ实现
2013-09-05 10:07
183 查看
支持异步同步的分布式CommandBus MSMQ实现
先上一张本文所描述的适用场景图分布式场景,共3台server:
前端Server
Order App Server
Warehouse App Server
功能:
前端Server可以不停的发送Command到CommandBus,然后由CommandBus分配不同的Command到各自的app server去处理。
前端Server可以只发送Command而不必等待Response
前端Server可以同步等待Response返回
MSMQ消息超过3.5M会自动转为网络共享方式传输消息
对于同一Command的处理,可以通过增加App Server的方式来提高并发处理速度(比如:可以开2个app server instance来同时处理ACommand的处理)
本文目标是用msmq实现分布式CommandBus的应用(已经更新到A2D Framework中了)。
前端Server发送Command的方式(异步):
ACommand cmd = new ACommand() { Tag = "aaa" }; CommandBusDistributer<ACommand, ACommandResult> cmdDistributer = new CommandBusDistributer<ACommand, ACommandResult>(); cmdDistributer.ResultCatached += new CommandResultCatchedDelegate<ACommandResult>(cmdDistributer_ResultCatached); cmdDistributer.SendRequest(cmd);
同步方式:
ACommand cmd = new ACommand() { Tag = "aaa" }; CommandBusDistributer<ACommand, ACommandResult> cmdDistributer = new CommandBusDistributer<ACommand, ACommandResult>(); cmdDistributer.SendRequest(cmd); ACommandResult result=cmdDistributer.WaitResponse();
配置文件:
<?xml version="1.0" encoding="utf-8" ?> <CommandBusSetting> <AutoCreateIfNotExists>true</AutoCreateIfNotExists> <CommandQueue>PC-20130606HCVP\private$\Commands_{0}</CommandQueue> <ResponseQueue>PC-20130606HCVP\private$\CommandResponses</ResponseQueue> <NetworkLocation>\\PC-20130606HCVP\network</NetworkLocation> </CommandBusSetting>
Command的编写方式:
[QueueName("ACommand")]//这个可选,没有QueueName时,默认对应的msmq队列名为类名,此处为ACommand public class ACommand : BaseCommand //需要继承自BaseCommand { public string Tag { get; set; }//自定义的属性 }
后端App Server的编写
CommandHandlerHost1: Console程序,相当于App Server 1,会处理部分Command
static void Main(string[] args) { Thread.Sleep(2000); CommandHandlerListener listener = new CommandHandlerListener(); listener.AddHandler(new TestCommandHandlers()); listener.AddHandler(new Test2CommandHandlers()); listener.Start(); Console.ReadKey(); }
CommandHandlerHost2: Console程序,相当于App Server 2,会处理部分Command
static void Main(string[] args) { Thread.Sleep(2000); CommandHandlerListener listener = new CommandHandlerListener(); listener.AddHandler(new Test3CommandHandlers()); listener.Start(); Console.ReadKey(); }
CommandHandlers: 所有的Command处理函数都会在这个项目中实现
public class TestCommandHandlers : ICommandHandlers, ICommandHandler<ACommand, ACommandResult>, ICommandHandler<BCommand, BCommandResult> { public ACommandResult Handler(ACommand cmd) { Console.WriteLine("From [public ACommandResult Handler(ACommand cmd)]: " + cmd.Tag); ACommandResult result = new ACommandResult(); result.Result = "result from ACommand"; return result; } public BCommandResult Handler(BCommand cmd) { Console.WriteLine("From [public BCommandResult Handler(BCommand cmd)]: " + cmd.Tag); BCommandResult result = new BCommandResult(); result.Result = "result from BCommand"; return result; } }
下面是目前的性能测试:
下载代码
相关文章推荐
- 支持异步同步的分布式CommandBus MSMQ实现 - 支持Session传递、多实例处理
- 机器人的异步代码同步实现
- WebApi FormData+文件长传 异步+同步实现
- 使用AjaxPro来实现异步加载和同步加载
- 利用Spring框架封装的JavaMail实现同步或异步邮件发送 分享
- NSURLConnection和NSMutableURLRequest 实现同步、异步请求
- 半同步/半异步进程池实现服务端程序
- 基于bootstrap的上传插件fileinput实现ajax异步上传功能(支持多文件上传预览拖拽)
- 分享:一个支持并发, 支持异步/同步, 支持http/https, 支持续传的avhttp库
- JS分页的实现(同步与异步)
- 同步、异步、阻塞、非阻塞 I/O 及 异步I/O实现
- 关于ajax同步与异步的实现问题
- python实现简单redis客户端(3)使用asyncio添加异步支持
- GCD的同步异步串行并行、NSOperation和NSOperationQueue一级用dispatch_once实现单例
- 用delegate实现.NET应用程序的同步函数的异步调用-.NET多线程编程实践之一
- 为什么jsonp只支持get请求?JSONP是同步还是异步
- PHP协程(2):通过生成器栈实现异步的同步写法
- 线程同步和异步的区别?IOS中如何实现多线程的同步?
- 分享:一个支持并发, 支持异步/同步, 支持http/https, 支持续传的avhttp库
- 实现支持文件分块多点异步上传的 Web Services 及其客户端(非Web)应用程序调用相关异步执行的 Web Method