Mule30开发图解教程.docx
《Mule30开发图解教程.docx》由会员分享,可在线阅读,更多相关《Mule30开发图解教程.docx(23页珍藏版)》请在冰点文库上搜索。
Mule30开发图解教程
环境搭建和例子:
Downloadmulestandaloefromwebsite:
1.http:
//www.mulesoft.org/download-mule-esb-community-edition
2.Unziptoapath(suchas:
D:
\mulesl312)
InstallingMuleIDEinEclipse
3.StartEclipse,andsetupaworkspaceforyourinstallationofMuleifyouhaven'talready.(Makesureyourworkspacedoesnothaveaspaceinthedirectorypath)
4.Intheworkbenchview,chooseHelp>InstallNewSoftware.
5.PresstheAddbuttonnexttotheWorkwithtextbox,enterhttp:
//dist.muleforge.org/mule-ide/updates/3.4/andpressEnter.TheMuleIDEnowappearsinyourlistofavailablesoftware.
6.SelecttheMuleIDEcheckboxandclickNext,andafterEclipseprocessesforamoment,clickNextagain.
7.ReviewtheMuleIDElicense,selecttheoptiontoacceptthelicense,andthenclickFinish.
8.ClickYestorestartEclipse.
9.SetMuleDistributionsineclipse:
YouarenowreadytoconfiguretheMuledistributionasdescribedinthenextsection.
ConfigMULE_HOMEandaddbintopath:
10.MULE_HOME:
D:
\mulesl312
11.Path:
%Path%;%MULE_HOME%\bin
CreateanewMuleproject:
HowtoImportaexistingprojecttoMuleprojectandrunit:
thereissomeecamplesinexamplefolderofMULE,wecanimportthem
WeneeedtoaddMuleclasspathlibtotheproject
ThenwecanruntheprojectonMuleruntimelikeas:
Soserverstarts:
NFO2011-06-2211:
38:
01,125[main]org.mule.module.management.agent.JmxAgent:
RegisteredEndpointServicewithname:
Mule.04cd6ac8-9c81-11e0-95b7-8bd1ea0dc252:
type=Endpoint,service="HelloWorld",connector=connector.http.mule.default,name="endpoint.http.localhost.8888"
INFO2011-06-2211:
38:
01,125[main]org.mule.module.management.agent.JmxAgent:
RegisteredConnectorServicewithnameMule.04cd6ac8-9c81-11e0-95b7-8bd1ea0dc252:
type=Connector,name="connector.servlet.mule.default.1"
INFO2011-06-2211:
38:
01,125[main]org.mule.module.management.agent.JmxAgent:
RegisteredConnectorServicewithnameMule.04cd6ac8-9c81-11e0-95b7-8bd1ea0dc252:
type=Connector,name="connector.stdio.mule.default.1"
INFO2011-06-2211:
38:
01,125[main]org.mule.module.management.agent.JmxAgent:
RegisteredConnectorServicewithnameMule.04cd6ac8-9c81-11e0-95b7-8bd1ea0dc252:
type=Connector,name="connector.VM.mule.default.1"
INFO2011-06-2211:
38:
01,125[main]org.mule.module.management.agent.JmxAgent:
RegisteredConnectorServicewithnameMule.04cd6ac8-9c81-11e0-95b7-8bd1ea0dc252:
type=Connector,name="connector.http.mule.default.1"
INFO2011-06-2211:
38:
01,140[main]org.mule.DefaultMuleContext:
**********************************************************************
*MuleESBandIntegrationPlatform*
*Version:
3.1.2Build:
21975*
*MuleSoft,Inc.*
*Formoreinformationgotohttp:
//www.mulesoft.org*
**
*Serverstarted:
11-6-22上午11:
38*
*ServerID:
04cd6ac8-9c81-11e0-95b7-8bd1ea0dc252*
*JDK:
1.6.0_10-rc2(mixedmode,sharing)*
*OSencoding:
UTF-8,Muleencoding:
UTF-8*
*OS:
WindowsXP-ServicePack3(5.1,x86)*
*Host:
PC-201103071221(192.168.40.61)*
*Mode:
Development*
**
*AgentsRunning:
*
*JMXAgent*
**********************************************************************
访问URL
mule-config.xml:
//localhost:
8888"transformer-refs="HttpRequestToNameString"exchange-pattern="request-response">
//所有的转换器都在此,就我理解所有的转换过程其实都要自己写,可以自己定义通用的转换器重用并扩展。
//publicclassChatStringToStringextendsAbstractTransformer父类
理论和模型:
1.简介
MuleESB是一个基于Java的轻量级企业服务总线和集成平台,允许开发人员快速便利地连接多个应用,并支持应用间的数据交换。
MuleESB支持集成现有系统而无论其底层采用何种技术,如JMS、WebServices、JDBC、HTTP以及其他技术。
2.整体结构
图整体结构
从上图可见,Mule通过Transports/Connectors与外围的异构系统连接,提供Routing(路由)、TransactionManagement(事务管理)、Transformation(转换)、MessageBroker(消息代理)、TransportationManagement(传输管理)、Security(安全)等核心模块。
Mule可以单独使用,也可以架设在常用的应用服务器上。
图架构简图
外围系统的服务请求通过MuleESB的Transport接入,Mule通过Transformer进行数据的格式转换,然后经过InboundRouter进行消息过滤(内部通过配置filter实现)后交给Mule的Component进行业务逻辑处理,处理后的结果通过OutboundRouter确定传递给哪个接收方,然后通过Transformer进行数据格式转换,通过Transport连接至接收方,传递信息。
此图描述的是Mule中的一个典型场景的处理过程,涵盖了Mule中的各个关键组件。
其中某些处理步骤不是必须的,如InboundRouter、Transformer。
后续可以看到一些其他场景的处理。
3.功能
a.服务中介
∙将业务逻辑和消息发送分离
∙屏蔽服务的消息格式和协议
∙提供任意位置的服务调用
∙提供协议桥接
b.数据转换
∙在应用间交换不同格式的信息
∙操作消息的负载内容,包括加密、压缩和编码转换
∙在异构的传输协议的数据类型间格式化消息
c.消息路由
∙基于消息内容和复杂规则路由消息
∙消息的过滤、聚合以及重新排列序号
d.服务创建和托管
∙暴露端点、EJB、SpringBean以及POJO作为服务
∙作为轻量级的服务容器进行服务托管
MuleESB中有一些基本的概念,理解这些基本概念后才能理解Mule的内部机制。
从中也可以看到Mule解决问题的基本思路。
4.基本概念
4.1Model
Model表示托管各个服务的运行时环境。
图Model
4.2Service
Service是用来处理服务请求的基本单位,它调用各个组件进行服务请求的处理。
图Service
4.3Transport
Transport管理消息的接收和发送,数据转换的过程也是在Transport中通过调用Transformer完成的。
图Transport
4.3.1Connector
Connector用于管控特定协议的使用,如HTTPConnector、JMSConnector等。
4.3.2End-Point
Endpoint用于表示一种协议的特定使用方式,如listening/polling、从中读取、向指定地址写入等,定义了发送和接收消息的通道。
Endpoint控制的是底层的实体在Connector中如何被使用。
Endpoint定义于Inbound和OutboundRouter中。
4.4Transformer
Transformer用于转换消息的内容。
图Transformer
4.5Router
Router使用Filter基于消息中的属性信息进行消息的分发。
图Router
Router在Service中的位置决定了Router的性质(inbound、outbound和response)和担任的角色(pass-through、aggregator等)。
4.6Component
Component是Service的核心部件,是Service的业务逻辑的实现。
图Component:
implicitbridgecomponent
Component可以是JavaClass(POJO、SpringBean)、WebService、Script等。
Component可定义自己的生命周期:
initialise、start、stop、dispose,不过需要实现Mule的LifeCycle接口。
Mule3.0版本开始提供@PostConstruct和@PreDestroy的注解,对应生命周期的initialise和dispose阶段,不需要实现Mule的LifeCycle接口了。
4.7Flow(@since3.0)
Flow是Mule3.0新引入的,包含一个消息源(MessageSource)和多个消息处理器组成的处理器链。
图Flow
根据实际需求着重检查了一下MuleESB的消息传递方式。
Mule支持常用的几种消息传递方式,能够满足要求。
5.消息传递方式
5.1异步方式
异步方式是一种单向调用,调用者不需要获得响应。
图Asynchronous
异步方式通过inbound和outboundendpoint的exchange-pattern=”one-way”实现。
使用基本的StdioTransport验证,通过标准输入传输字符串,将其原样传递给标准输出进行显示。
相应配置如下:
xml 代码
1.
2.
3. inbound-endpoint system="IN" exchange-pattern="one-way" />
4.
5.
6.
7.
8.
9.
10.
11.
12. outbound-endpoint system="OUT" exchange-pattern="one-way" />
13.
14.
15.
运行服务,控制台显示结果如下:
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第一次初始化相应Connector打印出来的,之后调用服务不会再次显示。
异步方式适用于简单的消息传递的场景。
5.2请求-响应方式
请求-响应方式即请求方调用服务后,服务立即处理并返回响应结果,不需将消息再次传递。
图Request-Response
请求-响应方式通过inputendpoint的exchange-pattern=”request-response”实现,相应配置如下:
xml 代码
1.
2.
3.
4.
5.
6. //localhost:
7007/services/Echo"
7. exchange-pattern="request-response">
8. jaxws-service />
9.
10.
11.
12.
13.
14.
15.
16.
17.
上边是通过service配置的,通过flow配置如下:
xml 代码
1.
2. //localhost:
7007/services/Echo"
3. exchange-pattern="request-response" />
4. jaxws-service serviceClass="demo.mule.umo.Echo" />
5.
6.
7.
8.
在浏览器中输入“http:
//localhost:
7007/services/Echo/echo/text/hello,world”,浏览器中会显示“hello,world”的输出信息。
请求-响应方式适用于单次服务调用的场景。
5.3同步方式
同步方式即请求方调用服务后,component将处理结果发送给另一个外部服务处理,并将处理结果反方向返回。
图Synchronous
同步方式通过inbound和outboundendpoint的exchange-pattern=”request-response”实现,相应配置如下:
xml 代码
1.
2. //localhost:
7007/services/Echo"
3. exchange-pattern="request-response" />
4. jaxws-service serviceClass="demo.mule.umo.Echo" />
5.
6.
7.
8. outbound-endpoint path="vm" exchange-pattern="request-response" />
9.
10.
11. inbound-endpoint path="vm" exchange-pattern="request-response" />