支持异步同步的分布式CommandBus MSMQ实现:支持Session传递、多实例处理2014-06-22 cnblogs Aaron先上一张本文所描述的适用场景图

分布式场景,共3台server:前端ServerOrder App ServerWarehouse 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的处理)在App server中能读取前端Server的Session value(写Session还不支持)本文目标是用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>
<WebSessionSupport>true</WebSessionSupport>
<CommandQueue>PC-20130606HCVPprivate$Commands_{0} </CommandQueue>
<ResponseQueue>PC- 20130606HCVPprivate$CommandResponses</ResponseQueue>
<NetworkLocation>\PC- 20130606HCVP
etwork</NetworkLocation>
</CommandBusSetting>
Command的编写方式:
[QueueName("ACommand")]//这个可选,没有QueueName时,默认对应的msmq队列名为类名,此处为ACommandpublic class ACommand : BaseCommand//需要继承自BaseCommand{public string Tag { get; set; }//自定义的属性}
后端App Server的编写