ESB平台将维护一套路由表,根据请求中的路由信息来做处理。这个路由信息就是指在路由表中能唯一识别真实服务信息(数据)的相关数据(key),利用Mule Message我们可以在以下几个地方做功课。
1. 利用Http Header 来传送路由信息
在 Http Header 中添加自定义信息,ESB平台根据Header的数据进行处理。
这种方式实施比较简单,接入方需要修改一点代码即再调用HttpRequest之前需要按照约定封装Header。
OAuth 认证也是通过Header携带Auth信息来做的。
2. 利用URL来传送路由信息
通过 QueryString(或者是rest风格的URL) 传递信息,这种方式实施更简单,接入方甚至无须直接修改代码改改配置文件的URL即可。
但通过QueryString传递数据比较暴露而且有长度限制。
3. 利用Http Body 来传送路由信息
将路由信息存放在 Http Body里,这种方式实施比较麻烦,接入方需要修改不少代码而且极不灵活。
需要整体规划,将报文格式全部统一下来。
接下来举个栗子,看看 Mule 3.x 如何实现消息路由的(方式2)。
实现方式2——根据URL路由信息处理,核心很简单,通过一个groovy 脚本来处理(Mule 自带Choose组件,但还是自己写脚本灵活)
import org.apache.log4j.*; Logger logger = LogManager.getLogger("router"); class Router implements Serializable { def serviceId, address, contentType } List routers = [ new Router(serviceId:"svc001", address:"localhost:11788/Service1.svc"), new Router(serviceId:"svc002", address:"localhost:11789/Service1.svc"), new Router(serviceId:"svc003", address:"localhost:11790/Service1.svc"), new Router(serviceId:"svc004", address:"localhost:11791/Service1.svc"), ]; def requestSvcId = message.getInboundProperty("http.relative.path"); logger.info "======== request serviceId: ${requestSvcId}" def requestContentType = message.getInboundProperty("Content-Type"); logger.info "======== request contentType: ${requestContentType}" message.setOutboundProperty("SOAPAction", message.getInboundProperty("SOAPAction")); message.setOutboundProperty("Content-Type", requestContentType); def currentRouter = routers.find { r -> r.serviceId == requestSvcId; } logger.info "======== service address: ${currentRouter.address}" if (currentRouter != null) { if (requestContentType != '') { currentRouter.contentType = requestContentType } message.setSessionProperty("router-info", currentRouter) } message
<?xml version="1.0" encoding="UTF-8"?> <mule xmlns:scripting="http://www.mulesoft.org/schema/mule/scripting" xmlns:http="http://www.mulesoft.org/schema/mule/http" xmlns="http://www.mulesoft.org/schema/mule/core" xmlns:doc="http://www.mulesoft.org/schema/mule/documentation" xmlns:spring="http://www.springframework.org/schema/beans" version="CE-3.3.1" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation=" http://www.mulesoft.org/schema/mule/http http://www.mulesoft.org/schema/mule/http/current/mule-http.xsd http://www.mulesoft.org/schema/mule/scripting http://www.mulesoft.org/schema/mule/scripting/current/mule-scripting.xsd http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-current.xsd http://www.mulesoft.org/schema/mule/core http://www.mulesoft.org/schema/mule/core/current/mule.xsd "> <flow name="mule-esb-route-sampleFlow1" doc:name="mule-esb-route-sampleFlow1"> <http:inbound-endpoint exchange-pattern="request-response" address="http://localhost:9999/esb" doc:name="HTTP" /> <logger message="---------- #[groovy:message.toString()]" level="INFO" doc:name="log request"/> <scripting:component doc:name="Groovy"> <scripting:script engine="Groovy" file="router.groovy"/> </scripting:component> <http:outbound-endpoint exchange-pattern="request-response" address="http://#[groovy:message.getSessionProperty('router-info').address]" doc:name="HTTP"/> </flow> </mule>
http://www.infoq.com/cn/presentations/electricity-supplier-in-the-enterprise-service-bus