MULE ESB环境搭建和例子(通过装插件的方式)
環(huán)境搭建和例子:
Download mule standaloe from web site:
1.????http://www.mulesoft.org/download-mule-esb-community-edition
2.????Unzip to a path(such as: D:\mulesl312)
?
Installing Mule IDE in Eclipse
3.????Start Eclipse, and set up a workspace foryour installation of Mule if you haven't already. (Make sure your workspacedoes not have a space in the directory path)
4.????In the workbench view, choose Help >Install New Software.
5.????Press the Add button next to the Work withtext box, enter http://dist.muleforge.org/mule-ide/updates/3.4/ and press Enter. The Mule IDE now appears in your list ofavailable software.
6.????Select the Mule IDE check box and click Next,and after Eclipse processes for a moment, click Next again.
7.????Review the Mule IDE license, select theoption to accept the license, and then click Finish.
8.????Click Yes to restart Eclipse.
9.????Set Mule Distributions in eclipse:
?
?
?
?
You are now ready to configure the Muledistribution as described in the next section.
Config MULE_HOME and add bin to path:
10.? MULE_HOME: D:\mulesl312
11.? Path:%Path%; %MULE_HOME%\bin
?
Create a new Mule project:
?
?
How to Import? a? existing project to Mule project and run it:
there is some ecamples in example folder of MULE,wecanimport? them
We neeed to add Mule classpath lib to the project
?
Then we can run the project on Muleruntime like as:
?
So server starts:
NFO? 2011-06-22 11:38:01,125 [main]org.mule.module.management.agent.JmxAgent: Registered Endpoint Service withname:Mule.04cd6ac8-9c81-11e0-95b7-8bd1ea0dc252:type=Endpoint,service="HelloWorld",connector=connector.http.mule.default,name="endpoint.http.localhost.8888"
INFO? 2011-06-22 11:38:01,125 [main]org.mule.module.management.agent.JmxAgent: Registered Connector Service withnameMule.04cd6ac8-9c81-11e0-95b7-8bd1ea0dc252:type=Connector,name="connector.servlet.mule.default.1"
INFO? 2011-06-22 11:38:01,125 [main]org.mule.module.management.agent.JmxAgent: Registered Connector Service withnameMule.04cd6ac8-9c81-11e0-95b7-8bd1ea0dc252:type=Connector,name="connector.stdio.mule.default.1"
INFO? 2011-06-22 11:38:01,125 [main] org.mule.module.management.agent.JmxAgent:Registered Connector Service with nameMule.04cd6ac8-9c81-11e0-95b7-8bd1ea0dc252:type=Connector,name="connector.VM.mule.default.1"
INFO? 2011-06-22 11:38:01,125 [main]org.mule.module.management.agent.JmxAgent: Registered Connector Service withnameMule.04cd6ac8-9c81-11e0-95b7-8bd1ea0dc252:type=Connector,name="connector.http.mule.default.1"
INFO? 2011-06-22 11:38:01,140 [main]org.mule.DefaultMuleContext:
**********************************************************************
* Mule ESB and IntegrationPlatform?????????????????????????????????*
* Version: 3.1.2 Build:21975???????????????????????????????????????*
* MuleSoft, Inc.????????????????????????????????????????????????????*
* For more information go tohttp://www.mulesoft.org???????????????? *
*???????????????????????????????????????????????????????????????????*
* Server started: 11-6-22 上午11:38??????????????????????????????? *
* Server ID:04cd6ac8-9c81-11e0-95b7-8bd1ea0dc252??????????????????? *
* JDK: 1.6.0_10-rc2 (mixedmode, sharing)???????????????????????????*
* OS encoding: UTF-8, Muleencoding: UTF-8??????????????????????????*
* OS: Windows XP - Service Pack3 (5.1, x86)???????????????????????? *
* Host: PC-201103071221(192.168.40.61)?????????????????? ???????????*
* Mode: Development?????????????????????????????????????????????????*
*???????????????????????????????????????????????????????????????????*
* Agents Running:???????????????????????????????????????????????????*
*?? JMX Agent???????????????? ???????????????????????????????????????*
**********************************************************************
| 訪問(wèn)URL |
mule-config.xml:
?<inbound-endpoint address="http://localhost:8888" transformer-refs="HttpRequestToNameString"exchange-pattern="request-response">
? <not-filter>
? <wildcard-filter pattern="/favicon.ico"/>
??????? </not-filter>
?? </inbound-endpoint>
?
//所有的轉(zhuǎn)換器都在此,就我理解所有的轉(zhuǎn)換過(guò)程其實(shí)都要自己寫(xiě),可以自己定義通用的轉(zhuǎn)換器重用并擴(kuò)展。
//public class ChatStringToString extends AbstractTransformer 父類
??? <custom-transformer name="StringToNameString" class="org.mule.example.hello.StringToNameString"/>
??? <custom-transformer name="NameStringToChatString" class="org.mule.example.hello.NameStringToChatString"/>
??? <custom-transformer name="ChatStringToString" class="org.mule.example.hello.ChatStringToString"/>
??? <custom-transformer name="HttpRequestToNameString" class="org.mule.example.hello.HttpRequestToNameString"/>
??? <custom-transformer name="ExceptionToString" class="org.mule.example.hello.ExceptionToString"/>
??? <custom-transformer name="HttpRequestToParameter" class="org.mule.transport.servlet.transformers.HttpRequestToParameter"/>
??? <custom-transformer name="ObjectToString" class="org.mule.transformer.simple.ObjectToString"/>
?
?
?
?
?
?
?
?
理論和模型:
?
1. 簡(jiǎn)介
Mule ESB是一個(gè)基于Java的輕量級(jí)企業(yè)服務(wù)總線和集成平臺(tái),允許開(kāi)發(fā)人員快速便利地連接多個(gè)應(yīng)用,并支持應(yīng)用間的數(shù)據(jù)交換。Mule ESB支持集成現(xiàn)有系統(tǒng)而無(wú)論其底層采用何種技術(shù),如JMS、Web Services、JDBC、HTTP以及其他技術(shù)。
2. 整體結(jié)構(gòu)
圖整體結(jié)構(gòu)
從上圖可見(jiàn),Mule通過(guò)Transports/Connectors與外圍的異構(gòu)系統(tǒng)連接,提供Routing(路由)、Transaction Management(事務(wù)管理)、Transformation(轉(zhuǎn)換)、Message Broker(消息代理)、Transportation Management(傳輸管理)、Security(安全)等核心模塊。Mule可以單獨(dú)使用,也可以架設(shè)在常用的應(yīng)用服務(wù)器上。
圖架構(gòu)簡(jiǎn)圖
外圍系統(tǒng)的服務(wù)請(qǐng)求通過(guò)Mule ESB的Transport接入,Mule通過(guò)Transformer進(jìn)行數(shù)據(jù)的格式轉(zhuǎn)換,然后經(jīng)過(guò)Inbound Router進(jìn)行消息過(guò)濾(內(nèi)部通過(guò)配置filter實(shí)現(xiàn))后交給Mule的Component進(jìn)行業(yè)務(wù)邏輯處理,處理后的結(jié)果通過(guò)Outbound Router確定傳遞給哪個(gè)接收方,然后通過(guò)Transformer進(jìn)行數(shù)據(jù)格式轉(zhuǎn)換,通過(guò)Transport連接至接收方,傳遞信息。
此圖描述的是Mule中的一個(gè)典型場(chǎng)景的處理過(guò)程,涵蓋了Mule中的各個(gè)關(guān)鍵組件。其中某些處理步驟不是必須的,如Inbound Router、Transformer。后續(xù)可以看到一些其他場(chǎng)景的處理。
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ī)則路由消息
·????????消息的過(guò)濾、聚合以及重新排列序號(hào)
d. 服務(wù)創(chuàng)建和托管
·????????暴露端點(diǎn)、EJB、Spring Bean以及POJO作為服務(wù)
·????????作為輕量級(jí)的服務(wù)容器進(jìn)行服務(wù)托管
Mule ESB中有一些基本的概念,理解這些基本概念后才能理解Mule的內(nèi)部機(jī)制。從中也可以看到Mule解決問(wèn)題的基本思路。
?
4. 基本概念
4.1 Model
Model表示托管各個(gè)服務(wù)的運(yùn)行時(shí)環(huán)境。
圖 Model
4.2 Service
Service是用來(lái)處理服務(wù)請(qǐng)求的基本單位,它調(diào)用各個(gè)組件進(jìn)行服務(wù)請(qǐng)求的處理。
圖 Service
4.3 Transport
Transport管理消息的接收和發(fā)送,數(shù)據(jù)轉(zhuǎn)換的過(guò)程也是在Transport中通過(guò)調(diào)用Transformer完成的。
圖 Transport
4.3.1 Connector
Connector用于管控特定協(xié)議的使用,如HTTP Connector、JMS Connector等。
4.3.2 End-Point
Endpoint用于表示一種協(xié)議的特定使用方式,如listening/polling、從中讀取、向指定地址寫(xiě)入等,定義了發(fā)送和接收消息的通道。Endpoint控制的是底層的實(shí)體在Connector中如何被使用。
Endpoint定義于Inbound和Outbound Router中。
4.4 Transformer
Transformer用于轉(zhuǎn)換消息的內(nèi)容。
圖 Transformer
4.5 Router
Router使用Filter基于消息中的屬性信息進(jìn)行消息的分發(fā)。
圖 Router
Router在Service中的位置決定了Router的性質(zhì)(inbound、outbound和response)和擔(dān)任的角色(pass-through、aggregator等)。
4.6 Component
Component是Service的核心部件,是Service的業(yè)務(wù)邏輯的實(shí)現(xiàn)。
圖 Component: implicit bridge component
Component可以是Java Class(POJO、Spring Bean)、Web Service、Script等。
Component可定義自己的生命周期:initialise、start、stop、dispose,不過(guò)需要實(shí)現(xiàn)Mule的LifeCycle接口。Mule 3.0版本開(kāi)始提供@PostConstruct和@PreDestroy的注解,對(duì)應(yīng)生命周期的initialise和dispose階段,不需要實(shí)現(xiàn)Mule的LifeCycle接口了。
4.7 Flow(@since 3.0)
Flow是Mule 3.0新引入的,包含一個(gè)消息源(Message Source)和多個(gè)消息處理器組成的處理器鏈。
圖 Flow
?
根據(jù)實(shí)際需求著重檢查了一下Mule ESB的消息傳遞方式。Mule支持常用的幾種消息傳遞方式,能夠滿足要求。
?
5. 消息傳遞方式
5.1 異步方式
異步方式是一種單向調(diào)用,調(diào)用者不需要獲得響應(yīng)。
圖 Asynchronous
異步方式通過(guò)inbound和outbound endpoint的exchange-pattern=”one-way”實(shí)現(xiàn)。
使用基本的Stdio Transport驗(yàn)證,通過(guò)標(biāo)準(zhǔn)輸入傳輸字符串,將其原樣傳遞給標(biāo)準(zhǔn)輸出進(jìn)行顯示。相應(yīng)配置如下:
?
xml?代碼
1.? <service?name="echo">????
2.? ????<inbound>????
3.? ????????<stdio:inbound-endpoint?system="IN"?exchange-pattern="one-way"?/>????
4.? ????</inbound>????
5.? ????????
6.? ????<component>????
7.? ????????<singleton-object?class="demo.mule.umo.StdIo"?/>????
8.? ????</component>????
9.? ????????
10. ????<outbound>????
11. ????????<pass-through-router>????
12. ????????????<stdio:outbound-endpoint?system="OUT"?exchange-pattern="one-way"?/>????
13. ????????</pass-through-router>????
14. ????</outbound>????
15. </service>?? ?
運(yùn)行服務(wù),控制臺(tái)顯示結(jié)果如下:
?
1.? Please?enter:?Hello,?world!????
2.? INFO??2010-12-07?19:21:18,877?[ConsoleConnector.dispatcher.1]????
3.? ????org.mule.lifecycle.AbstractLifecycleManager:?Initialising:????
4.? ????'ConsoleConnector.dispatcher.23255376'.?Object?is:?StdioMessageDispatcher????
5.? INFO??2010-12-07?19:21:18,877?[ConsoleConnector.dispatcher.1]????
6.? ????org.mule.lifecycle.AbstractLifecycleManager:?Starting:????
7.? ????'ConsoleConnector.dispatcher.23255376'.?Object?is:?StdioMessageDispatcher????
8.? Hello,?world! ? ?
其中INFO輸出是Mule第一次初始化相應(yīng)Connector打印出來(lái)的,之后調(diào)用服務(wù)不會(huì)再次顯示。
異步方式適用于簡(jiǎn)單的消息傳遞的場(chǎng)景。
5.2 請(qǐng)求-響應(yīng)方式
請(qǐng)求-響應(yīng)方式即請(qǐng)求方調(diào)用服務(wù)后,服務(wù)立即處理并返回響應(yīng)結(jié)果,不需將消息再次傳遞。
圖 Request-Response
請(qǐng)求-響應(yīng)方式通過(guò)input endpoint的exchange-pattern=”request-response”實(shí)現(xiàn),相應(yīng)配置如下:
?
xml?代碼
1.? <strong>??
2.? ????<strong>??
3.? ????????<model?name="services">????????
4.? ????????????<service?name="echoService">????????
5.? ????????????????<inbound>????????
6.? ????????????????????<inbound-endpoint?address="http://localhost:7007/services/Echo"????????
7.? ????????????????????????exchange-pattern="request-response">????????
8.? ????????????????????????<cxf:jaxws-service?/>????????
9.? ????????????????????</inbound-endpoint>????????
10. ????????????????</inbound>????????
11. ????????????????<component>????????
12. ????????????????????<singleton-object?class="demo.mule.umo.Echo"?/>????????
13. ????????????????</component>????????
14. ????????????</service>????????
15. ????????</model>??
16. ????</strong>??
17. </strong>???
上邊是通過(guò)service配置的,通過(guò)flow配置如下:
?
xml?代碼
1.? <flow?name="EchoFlow">????????
2.? ????<inbound-endpoint?address="http://localhost:7007/services/Echo"????????
3.? ????????exchange-pattern="request-response"?/>????????
4.? ????<cxf:jaxws-service?serviceClass="demo.mule.umo.Echo"?/>????????
5.? ????<component>????????
6.? ????????<singleton-object?class="demo.mule.umo.Echo"?/>????????
7.? ????</component>????????
8.? </flow>?? ?
在瀏覽器中輸入“http://localhost:7007/services/Echo/echo/text/hello,world”,瀏覽器中會(huì)顯示“hello,world”的輸出信息。
請(qǐng)求-響應(yīng)方式適用于單次服務(wù)調(diào)用的場(chǎng)景。
5.3 同步方式
同步方式即請(qǐng)求方調(diào)用服務(wù)后,component將處理結(jié)果發(fā)送給另一個(gè)外部服務(wù)處理,并將處理結(jié)果反方向返回。
圖 Synchronous
同步方式通過(guò)inbound和outbound endpoint的exchange-pattern=”request-response”實(shí)現(xiàn),相應(yīng)配置如下:
?
xml?代碼
1.? <flow?name="echo">??????
2.? ????<inbound-endpoint?address="http://localhost:7007/services/Echo"??????
3.? ????????exchange-pattern="request-response"?/>??????
4.? ????<cxf:jaxws-service?serviceClass="demo.mule.umo.Echo"?/>??????
5.? ????<component>??????
6.? ????????<singleton-object?class="demo.mule.umo.StdIo"?/>??????
7.? ????</component>??????
8.? ????<vm:outbound-endpoint?path="vm"?exchange-pattern="request-response"?/>??????
9.? </flow>??????
10. <flow?name="vm">??????
11. ????<vm:inbound-endpoint?path="vm"?exchange-pattern="request-response"?/>??????
12. ????<component>??????
13. ????????<singleton-object?class="demo.mule.umo.Vm"?/>??????
14. ????</component>??????
15. ????<stdio:outbound-endpoint?system="OUT"?exchange-pattern="one-way"?/>??????
16. </flow>?? ?
同步方式適用于通過(guò)Mule調(diào)用遠(yuǎn)程服務(wù)的場(chǎng)景。
5.4 異步請(qǐng)求-響應(yīng)方式
異步請(qǐng)求-響應(yīng)方式即請(qǐng)求方調(diào)用服務(wù)后不需要立即獲得返回結(jié)果,component將請(qǐng)求發(fā)送給其他外圍系統(tǒng)處理(可能有多個(gè)),全部處理完畢后通過(guò)指定的異步應(yīng)答Router返回給請(qǐng)求方。
圖?Asynchronous Request-Response
異步請(qǐng)求-響應(yīng)方式通過(guò)在OutBound Endpoint中增加reply-to以及增加async-reply節(jié)點(diǎn)實(shí)現(xiàn),響應(yīng)配置如下:
?
xml?代碼
1.? <flow?name="echo">??????
2.? ????<inbound-endpoint?address="http://localhost:7007/services/Echo"??????
3.? ????????exchange-pattern="request-response"?/>??????
4.? ????<cxf:jaxws-service?serviceClass="demo.mule.umo.Echo"?/>??????
5.? ????<component>??????
6.? ????????<singleton-object?class="demo.mule.umo.StdIo"?/>??????
7.? ????</component>??????
8.? ????<vm:outbound-endpoint?path="vm"?exchange-pattern="request-response"?/>??????
9.? </flow>??????
10. <flow?name="vm">??????
11. ????<vm:inbound-endpoint?path="vm"?exchange-pattern="request-response"?/>??????
12. ????<component>??????
13. ????????<singleton-object?class="demo.mule.umo.Vm"?/>??????
14. ????</component>??????
15. ????<stdio:outbound-endpoint?system="OUT"?exchange-pattern="one-way"?/>??????
16. </flow>?? ?
異步請(qǐng)求-響應(yīng)方式適用于請(qǐng)求需要被多個(gè)遠(yuǎn)程服務(wù)并行處理,結(jié)果需要匯總處理后返回的場(chǎng)景。
注:上述代碼未運(yùn)行通過(guò),queue1和queue2獲得了請(qǐng)求消息并正常處理,但返回至async-reply時(shí)拋出異常,暫未定位到問(wèn)題。
后將collection-async-reply-router改為single-async-reply-router未報(bào)異常,代碼示例如下:
?
xml?代碼
1.? <em><service?name="async?req-rep">??????
2.? ????<inbound>??????
3.? ????????<stdio:inbound-endpoint?ref="stdioInEndpoint"?/>??????
4.? ????</inbound>??????
5.? ????<component?class="demo.mule.umo.Echo"?/>??????
6.? ????<outbound>??????
7.? ????????<multicasting-router>??????
8.? ????????????<vm:outbound-endpoint?path="async.queue1"?exchange-pattern="one-way"?/>??????
9.? ????????????<vm:outbound-endpoint?path="async.queue2"?exchange-pattern="one-way"?/>??????
10. ????????????<reply-to?address="vm://reply"?/>??????
11. ????????</multicasting-router>??????
12. ????</outbound>??????
13. ????<async-reply?timeout="5000"?failOnTimeout="true">??????
14. ????????<vm:inbound-endpoint?path="reply"?exchange-pattern="one-way"?/>??????
15. ????????<single-async-reply-router?/>??????
16. ????</async-reply>??????
17. </service></em>??
?
?
附加參考圖:
?
?
?
?
?
?
總結(jié)
以上是生活随笔為你收集整理的MULE ESB环境搭建和例子(通过装插件的方式)的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
- 上一篇: 什么是p2p理财平台
- 下一篇: WebService入门篇