目录html
本篇主要介绍在Mule ESB中控制消息的流向。控制消息的流向有不少不一样的场景,数据的路由,数据的拆分和组合,数据的排序,数据的分发等。数据路由是ESB平台上最基本,也是最重要的功能之一,完整的ESB平台都会有相对应的功能。ESB基本的功能可分红消息路由,消息传输和消息转换等,后续几篇Mule ESB开发教程咱们会陆续讲解这些功能。git
Mule ESB一样具备不少的消息路由组件。做为开源ESB产品中很成熟的平台,Mule ESB的发展状态很是好,通过多年的发展,2017年在纽交所成功上市。Mule ESB拥有众多的企业案例,咱们做为MuleSoft的重要合做伙伴,使用Mule ESB企业版开发实施,或者Mule ESB社区版开发实施,帮助国内众多的行业标杆客户成功上线企业集成项目。github
Mule ESB的源代码托管在GitHub上,拥有很是详细的英文文档,Mule ESB的中文文档资料和开发教程却很是少,咱们使用8篇文章来写基础Mule ESB中文开发教程,一块儿为开源软件作贡献。算法
咱们来看一个常见的应用场景。咱们的集成应用能够接收客户端提交订单的请求,有多种不一样类型的客户端。手机客户端是一个App,咱们在App上使用的消息格式一般是轻量级的JSON格式。而PC客户端是一个Windows时代的产物,它仍然使用XML格式。咱们的集成应用在接收到订单请求后,会根据订单的类别分发到不一样的仓库,订单类别存储在订单内容的OrderType字段中。spring
咱们首先要区分订单的格式,由于JSON和XML的处理逻辑是彻底不一样的。在Mule中咱们可使用MEL的JSON Path来解决JSON文件的解析,XML Path来解决XML文件的解析。MEL的详细用法能够参考上一篇文章。订单的格式一般会放在消息头上传递,而Mule Message中对应消息头的部分就是Message Inboud Properties。express
{ "OrderType": "Book", "TotalAmount": 1000, "OrderLines": [{ "ProductName": "Learn English", "Qty": 3, "Price": 300 }, { "ProductName": "Lean Mule ESB", "Qty": 1, "Price": 100 }] }
使用Postman提交JSON,注意Content-Type设置为application/json。编程
经过Postman的Preview能够看到,实际上Http的Request头信息上设定了Content-Type字段为application/json。json
<?xml version="1.0" encoding="UTF-8"?> <OrderInfo> <OrderType>Book</OrderType> <TotalAmount>1000</TotalAmount> <OrderLines> <OrderLine> <ProductName>Learn English</ProductName> <Qty>3</Qty> <Price>300</Price> </OrderLine> <OrderLine> <ProductName>Learn Mule ESB</ProductName> <Qty>1</Qty> <Price>100</Price> </OrderLine> </OrderLines> </OrderInfo>
使用Postman提交XML,注意Content-Type设置为application/xml。服务器
经过Postman的Preview能够看到,实际上Http的Request头信息上设定了Content-Type字段为application/xml。app
Mule ESB最基本的路由组件就是Choice组件,组件相似于咱们在编程语言中的if/esle if/else。订单的格式是存放在Message Inbound Properties中的。这里就使用Choice组件来判断订单的格式。
Mule XML Config:
<?xml version="1.0" encoding="UTF-8"?> <mule 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" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="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 http://www.mulesoft.org/schema/mule/http http://www.mulesoft.org/schema/mule/http/current/mule-http.xsd"> <http:listener-config name="HTTP_Listener_Configuration" host="0.0.0.0" port="8081" doc:name="HTTP Listener Configuration"/> <flow name="flow-control"> <http:listener config-ref="HTTP_Listener_Configuration" path="/" doc:name="HTTP"/> <choice doc:name="Choice"> <when expression="#[message.inboundProperties['content-type'] == 'application/json']"> <logger message="JSON" level="INFO" doc:name="JSON"/> </when> <when expression="#[message.inboundProperties['content-type'] == 'application/xml']"> <logger message="XML" level="INFO" doc:name="XML"/> </when> <otherwise> <logger message="Error" level="INFO" doc:name="Error"/> </otherwise> </choice> </flow> </mule>
Mule UI Config:
经过Choice组件所支持的MEL语法,咱们就能经过判断mule message inboundProperties来肯定订单格式。须要注意的是,图上所示的Default是选择的最后一个路径,实际是if,else if,else,end if这个路径中的else路径。
经过消息头的判断,咱们已经能够区分JSON格式和XML格式订单,那么若是区分订单类型呢,这里就须要用到基于消息内容的路由,咱们对前面的代码作改进,同时引入消息队列。
Mule XML Config:
<?xml version="1.0" encoding="UTF-8"?> <mule xmlns:jms="http://www.mulesoft.org/schema/mule/jms" 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" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="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 http://www.mulesoft.org/schema/mule/http http://www.mulesoft.org/schema/mule/http/current/mule-http.xsd http://www.mulesoft.org/schema/mule/jms http://www.mulesoft.org/schema/mule/jms/current/mule-jms.xsd"> <http:listener-config name="HTTP_Listener_Configuration" host="0.0.0.0" port="8081" doc:name="HTTP Listener Configuration"/> <jms:activemq-connector name="Active_MQ" brokerURL="tcp://192.168.63.1:61616" validateConnections="true" doc:name="Active MQ"/> <flow name="flow-control"> <http:listener config-ref="HTTP_Listener_Configuration" path="/" doc:name="HTTP"/> <choice doc:name="Choice"> <when expression="#[message.inboundProperties['content-type'] == 'application/json']"> <set-variable doc:name="orderType" value="#[json:/OrderType]" variableName="orderType"/> <choice doc:name="Choice"> <when expression="#[flowVars.orderType == 'Book']"> <jms:outbound-endpoint queue="bookQ" connector-ref="Active_MQ" doc:name="bookQ"/> </when> <when expression="#[flowVars.orderType == 'Tool']"> <jms:outbound-endpoint queue="toolQ" connector-ref="Active_MQ" doc:name="toolQ"/> </when> <otherwise> <jms:outbound-endpoint queue="DLQ" connector-ref="Active_MQ" doc:name="DLQ"/> </otherwise> </choice> </when> <when expression="#[message.inboundProperties['content-type'] == 'application/xml']"> <set-variable doc:name="orderType" value="#[xpath://OrderInfo/OrderType]" variableName="orderType"/> <choice doc:name="Choice"> <when expression="#[flowVars.orderType == 'Book']"> <jms:outbound-endpoint queue="bookQ" connector-ref="Active_MQ" doc:name="bookQ"/> </when> <when expression="#[flowVars.orderType == 'Tool']"> <jms:outbound-endpoint queue="toolQ" connector-ref="Active_MQ" doc:name="toolQ"/> </when> <otherwise> <jms:outbound-endpoint queue="DLQ" connector-ref="Active_MQ" doc:name="DLQ"/> </otherwise> </choice> </when> <otherwise> <jms:outbound-endpoint queue="DLQ" connector-ref="Active_MQ" doc:name="DQL"/> </otherwise> </choice> </flow> </mule>
Mule UI Config:
除了使用Choice组件改变Flow执行路径之外,还要不少控制流向的组件。好比Round-Robin,Resequencer,Scatter-Gather,Splitter,Aggregator等组件。这些组件的用法各不相同,这里主要讲一下Round-Robin的用法。Round-Robin是一种轮询调度算法,在负载均衡的策略里面常常见到Round-Robin,轮询调度算法的原理是每一次把来自用户的请求轮流分配给内部中的服务器,从1开始,直到N(内部服务器个数),而后从新开始循环。
Mule的Round-Robin组件也是相似的思路,轮询执行Flow中的分支路径。举例以下,咱们接收到用户的检索请求,每次请求咱们使用不一样的执行路径,第一次使用Bing搜索,第二次使用Google,第三次使用Baidu。
Mule XML Config:
<?xml version="1.0" encoding="UTF-8"?> <mule 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" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="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 http://www.mulesoft.org/schema/mule/http http://www.mulesoft.org/schema/mule/http/current/mule-http.xsd"> <http:listener-config name="HTTP_Listener_Configuration" host="0.0.0.0" port="8081" doc:name="HTTP Listener Configuration"/> <flow name="round-robin-flow"> <http:listener config-ref="HTTP_Listener_Configuration" path="/" doc:name="HTTP"/> <round-robin doc:name="Round Robin"> <set-payload value="Baidu" doc:name="Baidu"/> <set-payload value="Google" doc:name="Google"/> <set-payload value="Bing" doc:name="Bing"/> </round-robin> </flow> </mule>
Mule UI Config:
本文同步发文于EnjoyingSoft Blogs ,CSDN,简书
访问EnjoyingSoft 网站,获取更多Mule ESB 社区版 实施帮助。
欢迎转载,但必须保留原文和此段声明,且在文章页面明显位置给出原文连接,不然保留追究法律责任的权利。