目錄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 社區版 實施幫助。
歡迎轉載,但必須保留原文和此段聲明,且在文章頁面明顯位置給出原文連接,不然保留追究法律責任的權利。