ESB知识点介绍
Mule ESB 學(xué)習(xí)筆記??
2013-06-19 13:55:08|??分類:?程序?|??標(biāo)簽:mule3??esb??|字號?訂閱
附-學(xué)習(xí)資料:
Mule in Action,by David Dossot & John D'Emic
Mule 官方文檔
DZone Refcardz #40: Core Mule
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)用間交換不同格式的信息?
操作消息的負(fù)載內(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)和擔(dān)任的角色(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驗證,通過標(biāo)準(zhǔn)輸入傳輸字符串,將其原樣傳遞給標(biāo)準(zhǔn)輸出進行顯示。相應(yīng)配置如下:
<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ù)不會再次顯示。
異步方式適用于簡單的消息傳遞的場景。
因為本站博客編輯時的問題,含有代碼的內(nèi)容只好單獨發(fā)布了,否則編輯時所有插入的代碼都丟失,修改起來太麻煩了。。。希望此問題能夠解決。
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)配置如下:
<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”的輸出信息。
請求-響應(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)配置如下:
<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)配置如下:
<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未報異常,代碼示例如下:
<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。
?
6. 配置模式
Mule 3.0版本提供了“pattern”的機制。Pattern總結(jié)了實際使用過程中的常見場景,以簡化的服務(wù)配置方式提供。
6.1 簡單服務(wù)模式(simple service pattern)
簡單服務(wù)模式用于簡化同步服務(wù)調(diào)用的配置,對應(yīng)消息傳遞方式中的請求-響應(yīng)方式。
圖 簡單服務(wù)模式
簡單服務(wù)模式通過simple-service?元素配置,主要的元素屬性包括:
| 屬性 | 說明 |
| address | 服務(wù)監(jiān)聽的地址,如vm:in |
| component-class | Component的實現(xiàn)類 |
| type | direct: 默認(rèn); jax-ws: 將component暴露為soap式的web service(component必須基于jax-ws的注解),address一般為Http Transport; jax-rs: 將component暴露為rest式的web service(component必須基于@Path的注解),address一般為Http或Servlet Transport |
代碼示例:
<simple-service name="simple-service" address="vm://simple.in" component-class="demo.mule.umo.Echo" />Mule針對服務(wù)請求接入可以做額外的處理,比如增加Transformer配置進行數(shù)據(jù)轉(zhuǎn)換。
6.2 橋接模式(bridge pattern)
橋接模式用于在inbound endpoint和outbound endpoint之間建立直接連接,不需要component提供業(yè)務(wù)邏輯。
圖 橋接模式
橋接模式通過bridge元素配置,主要屬性包括:
| 屬性 | 說明 |
| inboundAddress | 服務(wù)請求接入地址 |
| outboundAddress | 服務(wù)接出的實際地址 |
| exchange-pattern | request-response:?默認(rèn),返回處理結(jié)果; one-way:?單向 |
| transacted | true:?在向outbound endpoint分發(fā)時使用事務(wù); false:?不使用事務(wù) |
代碼示例:
?
<bridge name="queue-to-topic" transacted="true" inboundAddress="jms://myQueue" outboundAddress="jms://topic:myTopic" />?
Mule在接入、接出的過程中可以做額外的處理,比如增加Transformer配置進行數(shù)據(jù)轉(zhuǎn)換。如果使用事務(wù)控制,對于異構(gòu)的協(xié)議之間的事務(wù)需要有支持XA的事務(wù)控制器。
6.3 校驗器模式(validator pattern)
校驗器模式通過定義一個校驗過濾器過濾服務(wù)請求,并同步返回ACK(ACKnowledge)或NACK(Not Acknowledge)結(jié)果。通過校驗的服務(wù)請求被異步分發(fā)給處理方。
圖 校驗器模式
校驗器模式通過validator元素配置,主要屬性包括:
| 屬性 | 說明 |
| inboundAddress | 服務(wù)請求接入地址 |
| outboundAddress | 服務(wù)接出地址 |
| ackExpression | 表達式,用于構(gòu)建服務(wù)請求被接收時的信息 |
| nackExpression | 表達式,用于構(gòu)建服務(wù)請求被拒絕時的信息 |
| errorExpression | @since 3.0.1 表達式,用于構(gòu)建在服務(wù)請求分發(fā)出錯時的信息 |
| validationFilter-ref | 過濾器的引用,也可以使用子元素指定 用于確定服務(wù)請求是否被接收 |
代碼示例:
<validator name="integer-validator" inboundAddress="vm://validator.in" ackExpression="#[string:GOOD:#[message:payload]@#[context:serviceName]]" nackExpression="#[string:BAD:#[message:payload]@#[context:serviceName]]" outboundAddress="vm://test-service.in"> <payload-type-filter expectedType="java.lang.Integer" /> </validator>注:Mule的表達式后續(xù)補充。
6.4 web服務(wù)代理模式(web service proxy pattern)
Web服務(wù)代理模式用于將Web Service請求直接轉(zhuǎn)發(fā)至遠程目標(biāo)Web Service服務(wù)端,Mule本身不提供實際的Web Service。
圖 web服務(wù)代理模式
Web服務(wù)代理模式通過ws-proxy元素配置,主要屬性包括:
| 屬性 | 說明 |
| inboundAddress | Mule對外提供的地址 |
| outboundAddress | Web Service的實際地址 |
代碼示例:
<ws:proxy name="ws-proxy" inboundAddress="http://localhost:7006/services/Echo" outboundAddress="http://localhost:8000/services/Echo?method=echo"> </ws:proxy>Mule在轉(zhuǎn)發(fā)的過程中可以做額外的處理,比如增加Transformer配置進行數(shù)據(jù)轉(zhuǎn)換。
總結(jié)
- 上一篇: 如何体现对军队文职的热爱
- 下一篇: Web Service 一些对外公开的网