DDD:DDD+CQRS+高伸缩性的分布式架构

物理架构

物理架构优势

  • WEB服务器可以单独做负载平衡(独立伸缩)。
  • 应用服务可以单击做负载平衡(独立伸缩)。
  • 容易引入“后台任务服务器”(正在做这方面的支持)。
  • 支持混合部署(一部分业务逻辑运行在WEB服务器,一部分业务逻辑运行在应用服务器),部署方式对开发人员几乎透明。

如何选择部署模型

  • 当用户数少(自己测试)的时候可以不用应用服务器,只做WEB负责平衡。
  • 当用户数多(自己测试)的时候,将频繁执行的业务逻辑分离部署到应用服务器上。
  • 对于那些长时间(自己测试)执行的任务,将它们部署到后台任务服务器上。

示例代码

项目结构

WEB服务器代码

 1 using System;
 2 using System.Collections.Generic;
 3 using System.Linq;
 4 using System.Web;
 5 using System.Web.Mvc;
 6 
 7 using System.Diagnostics;
 8 
 9 using Microsoft.Practices.ServiceLocation;
10 
11 using Happy.Commands;
12 using Happy.WCF.Demo.Commands;
13 using Happy.WCF.Commands;
14 
15 namespace Happy.WCF.Demo.Mvc.Controllers
16 {
17     public class DefaultController : Controller
18     {
19         public ActionResult Index()
20         {
21             var watch = Stopwatch.StartNew();
22 
23             var localBus = ServiceLocator.Current.GetInstance<ICommandBus>();
24             var localCommand = new TestCommand { X = 5, Y = 5 };
25             localBus.Send(localCommand);
26 
27             watch.Stop();
28 
29             var localMessage = string.Format("本地命令:{0} + {1} = {2},执行时间:{3}", localCommand.X, localCommand.Y, localCommand.Result, watch.Elapsed);
30 
31 
32             watch = Stopwatch.StartNew();
33 
34             var remoteBus = ServiceLocator.Current.GetInstance<ICommandBus>("Proxy");
35             var remoteCommand = new TestCommand { X = 5, Y = 5 };
36             remoteBus.Send(remoteCommand);
37 
38             watch.Stop();
39 
40             var remoteMessage = string.Format("远程命令:{0} + {1} = {2},执行时间:{3}", remoteCommand.X, remoteCommand.Y, remoteCommand.Result, watch.Elapsed);
41 
42 
43             return this.Content(
44                 localMessage
45                 +
46                 "<br/>"
47                 +
48                 remoteMessage
49             );
50         }
51     }
52 }

代码下载

下载地址

为什么标题为“DDD+CQRS+高伸缩性的分布式架构”

这个分布式部署模式的Demo是用HappyFramework这个开源框架开发的,而HappyFramework的目的就是为了支持DDD+CQRS。

posted on 2013-04-19 14:26  幸福框架  阅读(6138)  评论(14编辑  收藏  举报

导航

我要啦免费统计