消息中间件利用高效可靠的消息传递机制进行平台无关的数据交流,并基于数据通信来进行分布式系统的集成。通过提供消息传递和消息排队模型,它可以在分布式环境下扩展进程间的通信。SpringBird 基础平台架构之消息中间件(Mom,Message Oriented Middleware)基于通用的三层架构,数据访问层采用了无Sql注入风险的IBatis.net,表现层采用了微软最新的Asp.net mvc3 Razor模板解析引擎和轻量级的Jquery easyui,服务层采用了接口编程,整体使用成熟可靠的Ioc、Aop框架Spring.net进行服务层、数据访问层和表现层之间的整合。讨论qq:2262366309

  • 应用场景

    消息异步传输

    Esb异步通信

    聊天信息的存储与转发

  • 消息传输模式

    点对点模式(P2P),需要建立连接通道

    队列模式(Queue),不用建立连接通道,双方是1对1的关系

    发布/订阅模式(Pub/Sub),不用建立连接通道,空间非耦合(不用互相了解),时间非耦合(不用同时在线),数据非耦合(采取异步方式),双方是多对多的关系

  • 数据库ER图

    Topic:主题,Subscriber:订阅者,SubscriberTop:订阅者主题关联表,Message:消息,MessageSubscriber:消息订阅者

  • 消息中间件管理端

    以成熟可靠的SpringBird Erp系统快速开发平台为基础,使用利器SpringBird代码生成工具生成代码,代码既规范又高效,基础信息管理包括机构管理(部门管理、用户管理)和权限管理(应用管理、角色管理、资源管理、菜单管理)

    主题、订阅者及之间的关系维护界面如下:

  • 消息中间件服务端

    发布消息方法的参数采用了Json格式的字符串,便于其他语言调用,尤其是php等没有DateTime类型的语言

    订阅端获取消息采用了拉模式,优点是服务端负载轻,不用管消息的当前发送状态,缺点是不够及时,可以通过服务端接收到消息后可以主动推送一个消息通知订阅端来拉消息来解决

 1 namespace SpringBird.Mom
2 {
3 /// <summary>
4 /// 消息服务
5 /// </summary>
6 public interface IMessageService
7 {
8 /// <summary>
9 /// 确认消息
10 /// </summary>
11 /// <param name="id">编码</param>
12 /// <returns>结果</returns>
13 bool AcknowledgeMessage(string id);
14
15 /// <summary>
16 /// 发布消息
17 /// </summary>
18 /// <param name="message">消息</param>
19 /// <returns>结果</returns>
20 bool PublishMessage(string message);
21
22 /// <summary>
23 /// 拉消息集合
24 /// </summary>
25 /// <param name="subscriber">订阅者</param>
26 /// <param name="count">数量</param>
27 /// <returns>消息集合</returns>
28 string PullMessages(string subscriber, int count);
29 }
30 }

    使用Spring的WebServiceExporter动态生成WebService代码,配置如下

 1 <?xml version="1.0" encoding="utf-8" ?>
2 <objects xmlns="http://www.springframework.net" default-autowire="byName">
3 <!--Web服务集合-->
4 <object id="MessageWebService" type="Spring.Web.Services.WebServiceExporter, Spring.Web">
5 <property name="TargetName" value="MessageService"/>
6 <property name="Namespace" value="http://springbird.com/"/>
7 <property name="Description" value="消息Web服务"/>
8 <property name="MemberAttributes">
9 <dictionary>
10 <entry key="AcknowledgeMessage">
11 <object type="System.Web.Services.WebMethodAttribute, System.Web.Services">
12 <property name="MessageName" value="AcknowledgeMessage"/>
13 <property name="Description" value="确认消息"/>
14 </object>
15 </entry>
16 <entry key="PublishMessage">
17 <object type="System.Web.Services.WebMethodAttribute, System.Web.Services">
18 <property name="MessageName" value="PublishMessage"/>
19 <property name="Description" value="发布消息"/>
20 </object>
21 </entry>
22 <entry key="PullMessages">
23 <object type="System.Web.Services.WebMethodAttribute, System.Web.Services">
24 <property name="MessageName" value="PullMessages"/>
25 <property name="Description" value="拉消息集合"/>
26 </object>
27 </entry>
28 </dictionary>
29 </property>
30 </object>
31 </objects>
  • 消息中间件客户端

    为了使用方便,定了消息客户端接口,由它将Message转换成Json格式的消息

  1 using System.Collections.Generic;
2
3 namespace SpringBird.Mom.Client
4 {
5 /// <summary>
6 /// 消息客户端
7 /// </summary>
8 public interface IMessageClient
9 {
10 /// <summary>
11 /// 确认消息
12 /// </summary>
13 /// <param name="id">编码</param>
14 /// <returns>结果</returns>
15 bool AcknowledgeMessage(string id);
16
17 /// <summary>
18 /// 发布消息
19 /// </summary>
20 /// <param name="message">消息</param>
21 /// <returns>结果</returns>
22 bool PublishMessage(Message message);
23
24 /// <summary>
25 /// 拉消息集合
26 /// </summary>
27 /// <param name="subscriber">订阅者</param>
28 /// <param name="count">数量</param>
29 /// <returns>消息集合</returns>
30 IList<Message> PullMessages(string subscriber, int count);
31 }
32 }
33
34 using System;
35 using System.Collections.Generic;
36
37 namespace SpringBird.Mom
38 {
39 /// <summary>
40 /// 消息
41 /// </summary>
42 [Serializable]
43 public class Message
44 {
45 private DateTime created = DateTime.Now;
46 private IDictionary<string, string> extensions = new Dictionary<string, string>();
47 private string id = Guid.NewGuid().ToString();
48
49 /// <summary>
50 /// 正文
51 /// </summary>
52 public string Body
53 {
54 get;
55 set;
56 }
57
58 /// <summary>
59 /// 创建时间
60 /// </summary>
61 public DateTime Created
62 {
63 get
64 {
65 return this.created;
66 }
67 set
68 {
69 this.created = value;
70 }
71 }
72
73 /// <summary>
74 /// 扩展集合
75 /// </summary>
76 public IDictionary<string, string> Extensions
77 {
78 get
79 {
80 return this.extensions;
81 }
82 set
83 {
84 this.extensions = value;
85 }
86 }
87
88 /// <summary>
89 /// 编码
90 /// </summary>
91 public string Id
92 {
93 get
94 {
95 return this.id;
96 }
97 set
98 {
99 this.id = value;
100 }
101 }
102
103 /// <summary>
104 /// 优先级
105 /// </summary>
106 public int Priority
107 {
108 get;
109 set;
110 }
111
112 /// <summary>
113 /// 主题
114 /// </summary>
115 public string Topic
116 {
117 get;
118 set;
119 }
120
121 /// <summary>
122 /// 版本
123 /// </summary>
124 public string Version
125 {
126 get
127 {
128 return "1.0.0.4";
129 }
130 }
131 }
132 }
133
134 using System.Collections.Generic;
135 using SpringBird.Core.Serialization;
136
137 namespace SpringBird.Mom.Client
138 {
139 public class MessageClient : IMessageClient
140 {
141 /// <summary>
142 /// 消息服务
143 /// </summary>
144 public IMessageService MessageService
145 {
146 get;
147 set;
148 }
149
150 /// <summary>
151 /// 序列化器
152 /// </summary>
153 public ISerializer Serializer
154 {
155 get;
156 set;
157 }
158
159 public bool AcknowledgeMessage(string id)
160 {
161 return this.MessageService.AcknowledgeMessage(id);
162 }
163
164 public bool PublishMessage(Message message)
165 {
166 return this.MessageService.PublishMessage(this.Serializer.Serialize(message));
167 }
168
169 public IList<Message> PullMessages(string subscriber, int count)
170 {
171 return this.Serializer.Deserialize<IList<Message>>(this.MessageService.PullMessages(subscriber, count));
172 }
173 }
174 }
175
176 namespace SpringBird.Mom
177 {
178 /// <summary>
179 /// 消息服务
180 /// </summary>
181 public interface IMessageService
182 {
183 /// <summary>
184 /// 确认消息
185 /// </summary>
186 /// <param name="id">编码</param>
187 /// <returns>结果</returns>
188 bool AcknowledgeMessage(string id);
189
190 /// <summary>
191 /// 发布消息
192 /// </summary>
193 /// <param name="message">消息</param>
194 /// <returns>结果</returns>
195 bool PublishMessage(string message);
196
197 /// <summary>
198 /// 拉消息集合
199 /// </summary>
200 /// <param name="subscriber">订阅者</param>
201 /// <param name="count">数量</param>
202 /// <returns>消息集合</returns>
203 string PullMessages(string subscriber, int count);
204 }
205 }

    使用Spring的WebServiceProxyFactory动态生成WebService代理代码,配置如下

1 <?xml version="1.0" encoding="utf-8" ?>
2 <objects xmlns="http://www.springframework.net" default-autowire="byName">
3 <!--Web服务集合-->
4 <object id="MessageService" type="Spring.Web.Services.WebServiceProxyFactory, Spring.Services">
5 <property name="ServiceInterface" value="SpringBird.Mom.IMessageService, SpringBird.Mom.Client"/>
6 <property name="ServiceUri" value="http://localhost/MomWs/MessageWebService.asmx"/>
7 </object>
8 </objects>

作者: SpringBird Erp系统快速开发平台 发表于 2011-08-09 13:15 原文链接

推荐.NET配套的通用数据层ORM框架:CYQ.Data 通用数据层框架