1. 簡介
Mule ESB是一個基於Java的輕量級企業服務總線和集成平臺,允許開發人員快速便利地連接多個應用,並支持應用間的數據交換。Mule ESB支持集成現有系統而無論其底層採用何種技術,如JMS、Web Services、JDBC、HTTP以及其他技術。
2. 整體結構
圖 整體結構
從上圖可見,Mule通過Transports/Connectors與外圍的異構系統連接,提供Routing(路由)、Transaction Management(事務管理)、Transformation(轉換)、Message Broker(消息代理)、Transportation Management(傳輸管理)、Security(安全)等核心模塊。Mule可以單獨使用,也可以架設在常用的應用服務器上。
圖 架構簡圖
外圍系統的服務請求通過Mule ESB的Transport接入,Mule通過Transformer進行數據的格式轉換,然後經過Inbound Router進行消息過濾(內部通過配置filter實現)後交給Mule的Component進行業務邏輯處理,處理後的結果通過Outbound Router確定傳遞給哪個接收方,然後通過Transformer進行數據格式轉換,通過Transport連接至接收方,傳遞信息。
此圖描述的是Mule中的一個典型場景的處理過程,涵蓋了Mule中的各個關鍵組件。其中某些處理步驟不是必須的,如Inbound Router、Transformer。後續可以看到一些其他場景的處理。
3. 功能
a. 服務中介
- 將業務邏輯和消息發送分離
- 屏蔽服務的消息格式和協議
- 提供任意位置的服務調用
- 提供協議橋接
b. 數據轉換
- 在應用間交換不同格式的信息
- 操作消息的負載內容,包括加密、壓縮和編碼轉換
- 在異構的傳輸協議的數據類型間格式化消息
c. 消息路由
- 基於消息內容和複雜規則路由消息
- 消息的過濾、聚合以及重新排列序號
d. 服務創建和託管
- 暴露端點、EJB、Spring Bean以及POJO作爲服務
- 作爲輕量級的服務容器進行服務託管
Mule ESB中有一些基本的概念,理解這些基本概念後才能理解Mule的內部機制。從中也可以看到Mule解決問題的基本思路。
4. 基本概念
4.1 Model
Model表示託管各個服務的運行時環境。
圖 Model
4.2 Service
Service是用來處理服務請求的基本單位,它調用各個組件進行服務請求的處理。
圖 Service
4.3 Transport
Transport管理消息的接收和發送,數據轉換的過程也是在Transport中通過調用Transformer完成的。
圖 Transport
4.3.1 Connector
Connector用於管控特定協議的使用,如HTTP Connector、JMS Connector等。
4.3.2 End-Point
Endpoint用於表示一種協議的特定使用方式,如listening/polling、從中讀取、向指定地址寫入等,定義了發送和接收消息的通道。Endpoint控制的是底層的實體在Connector中如何被使用。
Endpoint定義於Inbound和Outbound Router中。
4.4 Transformer
Transformer用於轉換消息的內容。
圖 Transformer
4.5 Router
Router使用Filter基於消息中的屬性信息進行消息的分發。
圖 Router
Router在Service中的位置決定了Router的性質(inbound、outbound和response)和擔任的角色(pass-through、aggregator等)。
4.6 Component
Component是Service的核心部件,是Service的業務邏輯的實現。
圖 Component: implicit bridge component
Component可以是Java Class(POJO、Spring Bean)、Web Service、Script等。
Component可定義自己的生命週期:initialise、start、stop、dispose,不過需要實現Mule的LifeCycle接口。Mule 3.0版本開始提供@PostConstruct和@PreDestroy的註解,對應生命週期的initialise和dispose階段,不需要實現Mule的LifeCycle接口了。
4.7 Flow(@since 3.0)
Flow是Mule 3.0新引入的,包含一個消息源(Message Source)和多個消息處理器組成的處理器鏈。
圖 Flow
根據實際需求着重檢查了一下Mule ESB的消息傳遞方式。Mule支持常用的幾種消息傳遞方式,能夠滿足要求。5. 消息傳遞方式
5.1 異步方式
異步方式是一種單向調用,調用者不需要獲得響應。
圖 Asynchronous
異步方式通過inbound和outbound endpoint的exchange-pattern=”one-way”實現。
使用基本的Stdio Transport驗證,通過標準輸入傳輸字符串,將其原樣傳遞給標準輸出進行顯示。相應配置如下:
<service name="echo">
<inbound>
<stdio:inbound-endpoint system="IN" exchange-pattern="one-way" />
</inbound>
<component>
<singleton-object class="demo.mule.umo.StdIo" />
</component>
<outbound>
<pass-through-router>
<stdio:outbound-endpoint system="OUT" exchange-pattern="one-way" />
</pass-through-router>
</outbound>
</service>
運行服務,控制檯顯示結果如下:
Please enter: Hello, world!
INFO 2010-12-07 19:21:18,877 [ConsoleConnector.dispatcher.1]
org.mule.lifecycle.AbstractLifecycleManager: Initialising:
'ConsoleConnector.dispatcher.23255376'. Object is: StdioMessageDispatcher
INFO 2010-12-07 19:21:18,877 [ConsoleConnector.dispatcher.1]
org.mule.lifecycle.AbstractLifecycleManager: Starting:
'ConsoleConnector.dispatcher.23255376'. Object is: StdioMessageDispatcher
Hello, world!
其中INFO輸出是Mule第一次初始化相應Connector打印出來的,之後調用服務不會再次顯示。
異步方式適用於簡單的消息傳遞的場景。
5.2 請求-響應方式
請求-響應方式即請求方調用服務後,服務立即處理並返回響應結果,不需將消息再次傳遞。
圖 Request-Response
請求-響應方式通過input endpoint的exchange-pattern=”request-response”實現,相應配置如下:
<model name="services">
<service name="echoService">
<inbound>
<inbound-endpoint address="http://localhost:7007/services/Echo"
exchange-pattern="request-response">
<cxf:jaxws-service />
</inbound-endpoint>
</inbound>
<component>
<singleton-object class="demo.mule.umo.Echo" />
</component>
</service>
</model>
上邊是通過service配置的,通過flow配置如下:
<flow name="EchoFlow">
<inbound-endpoint address="http://localhost:7007/services/Echo"
exchange-pattern="request-response" />
<cxf:jaxws-service serviceClass="demo.mule.umo.Echo" />
<component>
<singleton-object class="demo.mule.umo.Echo" />
</component>
</flow>
在瀏覽器中輸入“http://localhost:7007/services/Echo/echo/text/hello,world”,瀏覽器中會顯示“hello,world”的輸出信息。
請求-響應方式適用於單次服務調用的場景。
5.3 同步方式
同步方式即請求方調用服務後,component將處理結果發送給另一個外部服務處理,並將處理結果反方向返回。
圖 Synchronous
同步方式通過inbound和outbound endpoint的exchange-pattern=”request-response”實現,相應配置如下:
<flow name="echo">
<inbound-endpoint address="http://localhost:7007/services/Echo"
exchange-pattern="request-response" />
<cxf:jaxws-service serviceClass="demo.mule.umo.Echo" />
<component>
<singleton-object class="demo.mule.umo.StdIo" />
</component>
<vm:outbound-endpoint path="vm" exchange-pattern="request-response" />
</flow>
<flow name="vm">
<vm:inbound-endpoint path="vm" exchange-pattern="request-response" />
<component>
<singleton-object class="demo.mule.umo.Vm" />
</component>
<stdio:outbound-endpoint system="OUT" exchange-pattern="one-way" />
</flow>
同步方式適用於通過Mule調用遠程服務的場景。
5.4 異步請求-響應方式
異步請求-響應方式即請求方調用服務後不需要立即獲得返回結果,component將請求發送給其他外圍系統處理(可能有多個),全部處理完畢後通過指定的異步應答Router返回給請求方。
圖 Asynchronous Request-Response
異步請求-響應方式通過在OutBound Endpoint中增加reply-to以及增加async-reply節點實現,響應配置如下:
<flow name="echo">
<inbound-endpoint address="http://localhost:7007/services/Echo"
exchange-pattern="request-response" />
<cxf:jaxws-service serviceClass="demo.mule.umo.Echo" />
<component>
<singleton-object class="demo.mule.umo.StdIo" />
</component>
<vm:outbound-endpoint path="vm" exchange-pattern="request-response" />
</flow>
<flow name="vm">
<vm:inbound-endpoint path="vm" exchange-pattern="request-response" />
<component>
<singleton-object class="demo.mule.umo.Vm" />
</component>
<stdio:outbound-endpoint system="OUT" exchange-pattern="one-way" />
</flow>
異步請求-響應方式適用於請求需要被多個遠程服務並行處理,結果需要彙總處理後返回的場景。
注:上述代碼未運行通過,queue1和queue2獲得了請求消息並正常處理,但返回至async-reply時拋出異常,暫未定位到問題。
後將collection-async-reply-router改爲single-async-reply-router未報異常,代碼示例如下:
<service name="async req-rep">
<inbound>
<stdio:inbound-endpoint ref="stdioInEndpoint" />
</inbound>
<component class="demo.mule.umo.Echo" />
<outbound>
<multicasting-router>
<vm:outbound-endpoint path="async.queue1" exchange-pattern="one-way" />
<vm:outbound-endpoint path="async.queue2" exchange-pattern="one-way" />
<reply-to address="vm://reply" />
</multicasting-router>
</outbound>
<async-reply timeout="5000" failOnTimeout="true">
<vm:inbound-endpoint path="reply" exchange-pattern="one-way" />
<single-async-reply-router />
</async-reply>
</service>
等有空看看collection-async-reply-router吧,或者自定義router。
to be continued...