EnjoyingSoft之Mule ESB开发教程第五篇:控制消息的流向-数据路由

本篇主要介绍在Mule ESB中控制消息的流向。控制消息的流向有很多不同的场景,数据的路由,数据的拆分和组合,数据的排序,数据的分发等。数据路由是ESB平台上最基本,也是最重要的功能之一,完整的ESB平台都会有相对应的功能。ESB基本的功能可分成消息路由,消息传输和消息转换等,后续几篇Mule ESB开发教程我们会陆续讲解这些功能。

Mule ESB同样具有很多的消息路由组件。作为开源ESB产品中很成熟的平台,Mule ESB的发展状态非常好,经过多年的发展,2017年在纽交所成功上市。Mule ESB拥有众多的企业案例,我们作为MuleSoft的重要合作伙伴,使用Mule ESB企业版开发实施,或者Mule ESB社区版开发实施,帮助国内众多的行业标杆客户成功上线企业集成项目。

专注Mule ESB社区版实施,分享开源中间件。我们的网站: http://www.enjoyingsoft.com,或者邮件联系我们Contact@EnjoyingSoft.com

Mule ESB的源代码托管在GitHub上,拥有非常详细的英文文档,Mule ESB的中文文档资料和开发教程却非常少,我们使用8篇文章来写基础Mule ESB中文开发教程,一起为开源软件做贡献。

1. 使用场景

我们来看一个常见的应用场景。我们的集成应用可以接收客户端提交订单的请求,有多种不同类型的客户端。手机客户端是一个App,我们在App上使用的消息格式通常是轻量级的JSON格式。而PC客户端是一个Windows时代的产物,它仍然使用XML格式。我们的集成应用在接收到订单请求后,会根据订单的类别分发到不同的仓库,订单类别存储在订单内容的OrderType字段中。

2. 基于消息头的路由

我们首先要区分订单的格式,因为JSON和XML的处理逻辑是完全不同的。在Mule中我们可以使用MEL的JSON Path来解决JSON文件的解析,XML Path来解决XML文件的解析。MEL的详细用法可以参考上一篇文章。订单的格式通常会放在消息头上传递,而Mule Message中对应消息头的部分就是Message Inboud Properties。

2.1 使用JSON提交订单的消息

{
    "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。

json-post.png

通过Postman的Preview可以看到,实际上Http的Request头信息上设定了Content-Type字段为application/json。

json-post-preview.png

2.2 使用XML提交订单的消息

<?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。

xml-post.png

通过Postman的Preview可以看到,实际上Http的Request头信息上设定了Content-Type字段为application/xml。

xml-post-preview.png

2.3 使用Choice组件判断订单格式

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:

flow-control.png

通过Choice组件所支持的MEL语法,我们就能通过判断mule message inboundProperties来确定订单格式。需要注意的是,图上所示的Default是选择的最后一个路径,实际是if,else if,else,end if这个路径中的else路径。

3. 基于消息内容的路由

通过消息头的判断,我们已经可以区分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:

message-ui.png

4. 其他控制流向的组件

除了使用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:

round.png

本文同步发文于EnjoyingSoft Blogs博客园CSDN

访问EnjoyingSoft 网站,获取更多Mule ESB 社区版 实施帮助。

欢迎转载,但必须保留原文和此段声明,且在文章页面明显位置给出原文链接,否则保留追究法律责任的权利。

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 160,108评论 4 364
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 67,699评论 1 296
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 109,812评论 0 244
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 44,236评论 0 213
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 52,583评论 3 288
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 40,739评论 1 222
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 31,957评论 2 315
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 30,704评论 0 204
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 34,447评论 1 246
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 30,643评论 2 249
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 32,133评论 1 261
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 28,486评论 3 256
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 33,151评论 3 238
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 26,108评论 0 8
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 26,889评论 0 197
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 35,782评论 2 277
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 35,681评论 2 272