1. 簡介
Mule ESB是一個基于Java的輕量級企業(yè)服務(wù)總線和集成平臺,允許開發(fā)人員快速便利地連接多個應(yīng)用,并支持應(yīng)用間的數(shù)據(jù)交換。Mule ESB支持集成現(xiàn)有系統(tǒng)而無論其底層采用何種技術(shù),如JMS、Web Services、JDBC、HTTP以及其他技術(shù)。
2. 整體結(jié)構(gòu)
圖 整體結(jié)構(gòu)
從上圖可見,Mule通過Transports/Connectors與外圍的異構(gòu)系統(tǒng)連接,提供Routing(路由)、Transaction Management(事務(wù)管理)、Transformation(轉(zhuǎn)換)、Message Broker(消息代理)、Transportation Management(傳輸管理)、Security(安全)等核心模塊。Mule可以單獨使用,也可以架設(shè)在常用的應(yīng)用服務(wù)器上。
圖 架構(gòu)簡圖
外圍系統(tǒng)的服務(wù)請求通過Mule ESB的Transport接入,Mule通過Transformer進行數(shù)據(jù)的格式轉(zhuǎn)換,然后經(jīng)過Inbound Router進行消息過濾(內(nèi)部通過配置filter實現(xiàn))后交給Mule的Component進行業(yè)務(wù)邏輯處理,處理后的結(jié)果通過Outbound Router確定傳遞給哪個接收方,然后通過Transformer進行數(shù)據(jù)格式轉(zhuǎn)換,通過Transport連接至接收方,傳遞信息。
此圖描述的是Mule中的一個典型場景的處理過程,涵蓋了Mule中的各個關(guān)鍵組件。其中某些處理步驟不是必須的,如Inbound Router、Transformer。后續(xù)可以看到一些其他場景的處理。
3. 功能
a. 服務(wù)中介
-
將業(yè)務(wù)邏輯和消息發(fā)送分離
- 屏蔽服務(wù)的消息格式和協(xié)議
- 提供任意位置的服務(wù)調(diào)用
- 提供協(xié)議橋接
b. 數(shù)據(jù)轉(zhuǎn)換
- 在應(yīng)用間交換不同格式的信息
- 操作消息的負載內(nèi)容,包括加密、壓縮和編碼轉(zhuǎn)換
- 在異構(gòu)的傳輸協(xié)議的數(shù)據(jù)類型間格式化消息
c. 消息路由
- 基于消息內(nèi)容和復(fù)雜規(guī)則路由消息
- 消息的過濾、聚合以及重新排列序號
d. 服務(wù)創(chuàng)建和托管
- 暴露端點、EJB、Spring Bean以及POJO作為服務(wù)
- 作為輕量級的服務(wù)容器進行服務(wù)托管
Mule ESB中有一些基本的概念,理解這些基本概念后才能理解Mule的內(nèi)部機制。從中也可以看到Mule解決問題的基本思路。
4. 基本概念
4.1 Model
Model表示托管各個服務(wù)的運行時環(huán)境。
圖 Model
4.2 Service
Service是用來處理服務(wù)請求的基本單位,它調(diào)用各個組件進行服務(wù)請求的處理。
圖 Service
4.3 Transport
Transport管理消息的接收和發(fā)送,數(shù)據(jù)轉(zhuǎn)換的過程也是在Transport中通過調(diào)用Transformer完成的。
圖 Transport
4.3.1 Connector
Connector用于管控特定協(xié)議的使用,如HTTP Connector、JMS Connector等。
4.3.2 End-Point
Endpoint用于表示一種協(xié)議的特定使用方式,如listening/polling、從中讀取、向指定地址寫入等,定義了發(fā)送和接收消息的通道。Endpoint控制的是底層的實體在Connector中如何被使用。
Endpoint定義于Inbound和Outbound Router中。
4.4 Transformer
Transformer用于轉(zhuǎn)換消息的內(nèi)容。
圖 Transformer
4.5 Router
Router使用Filter基于消息中的屬性信息進行消息的分發(fā)。
圖 Router
Router在Service中的位置決定了Router的性質(zhì)(inbound、outbound和response)和擔任的角色(pass-through、aggregator等)。
4.6 Component
Component是Service的核心部件,是Service的業(yè)務(wù)邏輯的實現(xiàn)。
圖 Component: implicit bridge component
Component可以是Java Class(POJO、Spring Bean)、Web Service、Script等。
Component可定義自己的生命周期:initialise、start、stop、dispose,不過需要實現(xiàn)Mule的LifeCycle接口。Mule 3.0版本開始提供@PostConstruct和@PreDestroy的注解,對應(yīng)生命周期的initialise和dispose階段,不需要實現(xiàn)Mule的LifeCycle接口了。
4.7 Flow(@since 3.0)
Flow是Mule 3.0新引入的,包含一個消息源(Message Source)和多個消息處理器組成的處理器鏈。
圖 Flow
根據(jù)實際需求著重檢查了一下Mule ESB的消息傳遞方式。Mule支持常用的幾種消息傳遞方式,能夠滿足要求。
5. 消息傳遞方式
5.1 異步方式
異步方式是一種單向調(diào)用,調(diào)用者不需要獲得響應(yīng)。
圖 Asynchronous
異步方式通過inbound和outbound endpoint的exchange-pattern=”one-way”實現(xiàn)。
使用基本的Stdio Transport驗證,通過標準輸入傳輸字符串,將其原樣傳遞給標準輸出進行顯示。相應(yīng)配置如下:
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" exchange-pattern="one-way" />??? ??????? </pass-through-router>??? ??? </outbound>??? </service>??? 運行服務(wù),控制臺顯示結(jié)果如下: 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第一次初始化相應(yīng)Connector打印出來的,之后調(diào)用服務(wù)不會再次顯示。 異步方式適用于簡單的消息傳遞的場景。
5.2 請求-響應(yīng)方式
請求-響應(yīng)方式即請求方調(diào)用服務(wù)后,服務(wù)立即處理并返回響應(yīng)結(jié)果,不需將消息再次傳遞。
圖 Request-Response
請求-響應(yīng)方式通過input endpoint的exchange-pattern=”request-response”實現(xiàn),相應(yīng)配置如下:
xml 代碼 <strong>? ??? <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>??????? ??????????????????? <singleton-object class="demo.mule.umo.Echo" />??????? ??????????????? </component>??????? ??????????? </service>??????? ??????? </model>? ??? </strong>? </strong>??? 上邊是通過service配置的,通過flow配置如下:
xml 代碼
<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”的輸出信息。
請求-響應(yīng)方式適用于單次服務(wù)調(diào)用的場景。
5.3 同步方式
同步方式即請求方調(diào)用服務(wù)后,component將處理結(jié)果發(fā)送給另一個外部服務(wù)處理,并將處理結(jié)果反方向返回。
圖 Synchronous
同步方式通過inbound和outbound endpoint的exchange-pattern=”request-response”實現(xiàn),相應(yīng)配置如下:
xml 代碼 <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調(diào)用遠程服務(wù)的場景。
5.4 異步請求-響應(yīng)方式
異步請求-響應(yīng)方式即請求方調(diào)用服務(wù)后不需要立即獲得返回結(jié)果,component將請求發(fā)送給其他外圍系統(tǒng)處理(可能有多個),全部處理完畢后通過指定的異步應(yīng)答Router返回給請求方。
圖 Asynchronous Request-Response
異步請求-響應(yīng)方式通過在OutBound Endpoint中增加reply-to以及增加async-reply節(jié)點實現(xiàn),響應(yīng)配置如下:
xml 代碼 <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>??? 異步請求-響應(yīng)方式適用于請求需要被多個遠程服務(wù)并行處理,結(jié)果需要匯總處理后返回的場景。
注:上述代碼未運行通過,queue1和queue2獲得了請求消息并正常處理,但返回至async-reply時拋出異常,暫未定位到問題。
后將collection-async-reply-router改為single-async-reply-router未報異常,代碼示例如下:
xml 代碼
<em><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></em>? 等有空看看collection-async-reply-router吧,或者自定義router。
總結(jié)
以上是生活随笔為你收集整理的关于MULE ESB相关的介绍的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
如果覺得生活随笔網(wǎng)站內(nèi)容還不錯,歡迎將生活随笔推薦給好友。