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. 資料轉換
在應用間交換不同格式的資訊
操作消息的負載内容,包括加密、壓縮和編碼轉換
在異構的傳輸協定的資料類型間格式化消息
基于消息内容和複雜規則路由消息
消息的過濾、聚合以及重新排列序号
暴露端點、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驗證,通過标準輸入傳輸字元串,将其原樣傳遞給标準輸出進行顯示。相應配置如下:
xml 代碼
<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"
</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
org.mule.lifecycle.abstractlifecyclemanager: starting:
hello, world!
其中info輸出是mule第一次初始化相應connector列印出來的,之後調用服務不會再次顯示。
異步方式适用于簡單的消息傳遞的場景。
5.2
請求-響應方式
請求-響應方式即請求方調用服務後,服務立即處理并傳回響應結果,不需将消息再次傳遞。
圖 request-response
請求-響應方式通過input
endpoint的exchange-pattern=”request-response”實作,相應配置如下:
xml
代碼
<strong>
<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>
class="demo.mule.umo.echo"
</component>
</service>
</model>
</strong>
</strong>
上邊是通過service配置的,通過flow配置如下:
<flow
name="echoflow">
<inbound-endpoint
exchange-pattern="request-response"
<cxf:jaxws-service
serviceclass="demo.mule.umo.echo"
<component>
<singleton-object
</component>
</flow>
在浏覽器中輸入“http://localhost:7007/services/echo/echo/text/hello,world”,浏覽器中會顯示“hello,world”的輸出資訊。
請求-響應方式适用于單次服務調用的場景。
5.3 同步方式
同步方式即請求方調用服務後,component将處理結果發送給另一個外部服務處理,并将處理結果反方向傳回。
圖 synchronous
同步方式通過inbound和outbound
name="echo">
address="http://localhost:7007/services/echo"
exchange-pattern="request-response"
/>
<component>
</component>
<vm:outbound-endpoint
path="vm"
</flow>
name="vm">
<vm:inbound-endpoint
class="demo.mule.umo.vm"
同步方式适用于通過mule調用遠端服務的場景。
5.4 異步請求-響應方式
異步請求-響應方式即請求方調用服務後不需要立即獲得傳回結果,component将請求發送給其他外圍系統處理(可能有多個),全部處理完畢後通過指定的異步應答router傳回給請求方。
圖
asynchronous request-response
異步請求-響應方式通過在outbound
endpoint中增加reply-to以及增加async-reply節點實作,響應配置如下:
異步請求-響應方式适用于請求需要被多個遠端服務并行處理,結果需要彙總處理後傳回的場景。
注:上述代碼未運作通過,queue1和queue2獲得了請求消息并正常處理,但傳回至async-reply時抛出異常,暫未定位到問題。
後将collection-async-reply-router改為single-async-reply-router未報異常,代碼示例如下:
<em><service
name="async req-rep">
<inbound>
<stdio:inbound-endpoint
ref="stdioinendpoint"
</inbound>
<component
<outbound>
<multicasting-router>
<vm:outbound-endpoint
path="async.queue1"
path="async.queue2"
<reply-to
address="vm://reply"
</multicasting-router>
</outbound>
<async-reply
timeout="5000"
failontimeout="true">
<vm:inbound-endpoint
path="reply"
<single-async-reply-router
</async-reply>
</service></em>
等有空看看collection-async-reply-router吧,或者自定義router。