Mule ESB 介绍

1. 简介
Mule ESB是一个基于Java的轻量级企业服务总线和集成平台,允许开发人员快速便利地连接多个应用,并支持应用间的数据交换。Mule ESB支持集成现有系统而无论其底层采用何种技术,如JMS、Web Services、JDBC、HTTP以及其他技术。

2. 整体结构


[img]http://dl2.iteye.com/upload/attachment/0088/5035/c2030f74-0294-3bce-a463-01faca51b72e.jpg[/img]

图 整体结构

从上图可见,Mule通过Transports/Connectors与外围的异构系统连接,提供Routing(路由)、Transaction Management(事务管理)、Transformation(转换)、Message Broker(消息代理)、Transportation Management(传输管理)、Security(安全)等核心模块。Mule可以单独使用,也可以架设在常用的应用服务器上。


[img]http://dl2.iteye.com/upload/attachment/0088/5039/3ef2d5b3-bcc0-371c-955e-26eab495ed41.jpg[/img]

图 架构简图

外围系统的服务请求通过Mule ESB的Transport接入,Mule通过Transformer进行数据的格式转换,然后经过Inbound Router进行消息过滤(内部通过配置filter实现)后交给Mule的Component进行业务逻辑处理,处理后的结果通过Outbound Router确定传递给哪个接收方,然后通过Transformer进行数据格式转换,通过Transport连接至接收方,传递信息。

此图描述的是Mule中的一个典型场景的处理过程,涵盖了Mule中的各个关键组件。其中某些处理步骤不是必须的,如Inbound Router、Transformer。后续可以看到一些其他场景的处理。

3. 功能

a. 服务中介

将业务逻辑和消息发送分离
屏蔽服务的消息格式和协议
提供任意位置的服务调用
提供协议桥接
b. 数据转换

在应用间交换不同格式的信息
操作消息的负载内容,包括加密、压缩和编码转换
在异构的传输协议的数据类型间格式化消息
c. 消息路由
基于消息内容和复杂规则路由消息
消息的过滤、聚合以及重新排列序号
d. 服务创建和托管
暴露端点、EJB、Spring Bean以及POJO作为服务
作为轻量级的服务容器进行服务托管
Mule ESB中有一些基本的概念,理解这些基本概念后才能理解Mule的内部机制。从中也可以看到Mule解决问题的基本思路。



4. 基本概念

4.1 Model

Model表示托管各个服务的运行时环境。


[img]http://dl2.iteye.com/upload/attachment/0088/5041/ec673c12-cca3-33d3-b5b7-9b8bca68eed1.jpg[/img]

4.2 Service

Service是用来处理服务请求的基本单位,它调用各个组件进行服务请求的处理。


[img]http://dl2.iteye.com/upload/attachment/0088/5043/4971106d-b44e-368e-bad1-623b24c295c5.jpg[/img]


4.3 Transport

Transport管理消息的接收和发送,数据转换的过程也是在Transport中通过调用Transformer完成的。


[img]http://dl2.iteye.com/upload/attachment/0088/5045/35fbd612-7908-3143-8387-027f97d65399.jpg[/img]


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用于转换消息的内容。


[img]http://dl2.iteye.com/upload/attachment/0088/5050/83f2bc50-98fd-3150-afe1-40c6e6903da0.jpg[/img]


4.5 Router

Router使用Filter基于消息中的属性信息进行消息的分发。


[img]http://dl2.iteye.com/upload/attachment/0088/5054/3fa7f6ea-66d6-3124-9cf7-4c68b2675b7b.jpg[/img]


图 Router

Router在Service中的位置决定了Router的性质(inbound、outbound和response)和担任的角色(pass-through、aggregator等)。

4.6 Component

Component是Service的核心部件,是Service的业务逻辑的实现。


[img]http://dl2.iteye.com/upload/attachment/0088/5056/828e57d9-53b2-37f0-a71c-4100e607ec53.jpg[/img]


图 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)和多个消息处理器组成的处理器链。


[img]http://dl2.iteye.com/upload/attachment/0088/5058/50fc69be-7029-345e-8868-4d2b3bc0b13d.jpg[/img]


图 Flow

根据实际需求着重检查了一下Mule ESB的消息传递方式。Mule支持常用的几种消息传递方式,能够满足要求。


5. 消息传递方式

5.1 异步方式

异步方式是一种单向调用,调用者不需要获得响应。


[img]http://dl2.iteye.com/upload/attachment/0088/5064/8821fea5-7a63-3718-8b89-81450bd88863.png[/img]


图 Asynchronous

异步方式通过inbound和outbound endpoint的exchange-pattern=”one-way”实现。

使用基本的Stdio Transport验证,通过标准输入传输字符串,将其原样传递给标准输出进行显示。相应配置如下:


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>
运行服务,控制台显示结果如下:


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 请求-响应方式

请求-响应方式即请求方调用服务后,服务立即处理并返回响应结果,不需将消息再次传递。


[img]http://dl2.iteye.com/upload/attachment/0088/5066/dcc780c8-53ae-3e2a-bdaa-16a74159e7bb.png[/img]


请求-响应方式通过input endpoint的exchange-pattern=”request-response”实现,相应配置如下:


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>
上边是通过service配置的,通过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”,浏览器中会显示“hello,world”的输出信息。

请求-响应方式适用于单次服务调用的场景。

5.3 同步方式

同步方式即请求方调用服务后,component将处理结果发送给另一个外部服务处理,并将处理结果反方向返回。


[img]http://dl2.iteye.com/upload/attachment/0088/5068/ede7103b-61f3-3438-a1c7-c26e76bc97b2.png[/img]

图 Synchronous

同步方式通过inbound和outbound endpoint的exchange-pattern=”request-response”实现,相应配置如下:


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>
同步方式适用于通过Mule调用远程服务的场景。

5.4 异步请求-响应方式

异步请求-响应方式即请求方调用服务后不需要立即获得返回结果,component将请求发送给其他外围系统处理(可能有多个),全部处理完毕后通过指定的异步应答Router返回给请求方。


[img]http://dl2.iteye.com/upload/attachment/0088/5072/5e986e1e-084d-3263-8076-10f1f1b6f1c9.png[/img]


图 Asynchronous Request-Response

异步请求-响应方式通过在OutBound Endpoint中增加reply-to以及增加async-reply节点实现,响应配置如下:


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>
异步请求-响应方式适用于请求需要被多个远程服务并行处理,结果需要汇总处理后返回的场景。

注:上述代码未运行通过,queue1和queue2获得了请求消息并正常处理,但返回至async-reply时抛出异常,暂未定位到问题。

后将collection-async-reply-router改为single-async-reply-router未报异常,代码示例如下:



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>

6. 配置模式

Mule 3.0版本提供了“pattern”的机制。Pattern总结了实际使用过程中的常见场景,以简化的服务配置方式提供。

6.1 简单服务模式(simple service pattern)

简单服务模式用于简化同步服务调用的配置,对应消息传递方式中的请求-响应方式。


[img]http://dl2.iteye.com/upload/attachment/0088/5092/ed040030-2cfd-3179-86af-f2ad4f7b5f8c.png[/img]

简单服务模式通过simple-service 元素配置,主要的元素属性包括:

属性 说明
address 服务监听的地址,如vm:in
component-class Component的实现类
type direct: 默认;

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针对服务请求接入可以做额外的处理,比如增加Transformer配置进行数据转换。

6.2 桥接模式(bridge pattern)

桥接模式用于在inbound endpoint和outbound endpoint之间建立直接连接,不需要component提供业务逻辑。


[img]http://dl2.iteye.com/upload/attachment/0088/5094/69d8eeae-2cdf-3f4f-9305-873a06767f0b.png[/img]

桥接模式通过bridge元素配置,主要属性包括:

属性 说明
inboundAddress 服务请求接入地址
outboundAddress 服务接出的实际地址
exchange-pattern request-response: 默认,返回处理结果;

one-way: 单向

transacted true: 在向outbound endpoint分发时使用事务;

false: 不使用事务


代码示例:



<bridge name="queue-to-topic" transacted="true" inboundAddress="jms://myQueue"
outboundAddress="jms://topic:myTopic" />

Mule在接入、接出的过程中可以做额外的处理,比如增加Transformer配置进行数据转换。如果使用事务控制,对于异构的协议之间的事务需要有支持XA的事务控制器。

6.3 校验器模式(validator pattern)

校验器模式通过定义一个校验过滤器过滤服务请求,并同步返回ACK(ACKnowledge)或NACK(Not Acknowledge)结果。通过校验的服务请求被异步分发给处理方。


[img]http://dl2.iteye.com/upload/attachment/0088/5100/0df0274b-951e-3d88-8b56-50337f39ad00.bmp[/img]

校验器模式通过validator元素配置,主要属性包括:

属性 说明
inboundAddress 服务请求接入地址
outboundAddress 服务接出地址
ackExpression 表达式,用于构建服务请求被接收时的信息
nackExpression 表达式,用于构建服务请求被拒绝时的信息
errorExpression @since 3.0.1

表达式,用于构建在服务请求分发出错时的信息
validationFilter-ref 过滤器的引用,也可以使用子元素指定

用于确定服务请求是否被接收

代码示例:


<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的表达式后续补充。

6.4 web服务代理模式(web service proxy pattern)

Web服务代理模式用于将Web Service请求直接转发至远程目标Web Service服务端,Mule本身不提供实际的Web Service。


[img]http://dl2.iteye.com/upload/attachment/0088/5102/0ef8c2f3-79e1-30f3-9e09-0d1920a119b9.bmp[/img]

Web服务代理模式通过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在转发的过程中可以做额外的处理,比如增加Transformer配置进行数据转换。

7. 总结

一. 服务调用

1. Mule实现并提供Web Service

在Mule上开发并发布一个Web Service供客户端调用。

•示例配置

<flow name="local-ws">

<core:inbound-endpoint address="http://localhost:65082/services/Echo1"

exchange-pattern="request-response" doc:name="Generic" doc:description="Generic endpoint specified by address URI" />

<cxf:jaxws-service serviceClass="demo.mule.component.Echo" doc:name="SOAP"

doc:description="Make a web service available via CXF" />

<component doc:name="Component" doc:description="Invoke a Java component">

<singleton-object class="demo.mule.component.Echo" />

</component>

</ flow >

•测试方法

在浏览器地址栏中输入“ http://localhost:65082/services/Echo1/echo/text/hello ”,回车后浏览器中将显示返回结果信息。地址中的“ echo ”是服务的方法,“ text ”是方法的参数,“ hello ”是参数的值。


2. Web Service Proxy

Web Service Proxy用来将客户端的WS请求直接转发至相应的远程WS服务端处理,并返回处理结果。Mule本身不做任何处理。

2.1 配置方式1

•示例配置

<flow name="local2remote-ws">

<http:inbound-endpoint keep-alive="false" address="http://localhost:65000"

encoding="UTF-8" disableTransportTransformer="false" exchange-pattern="request-response" doc:name="HTTP"

doc:description="" />

<http:outbound-endpoint method="GET" keep-alive="false"

address="http://localhost:5050#[header:INBOUND:http.request]" responseTimeout="10000" encoding="UTF-8"

disableTransportTransformer="false" followRedirects="false" exchange-pattern="request-response"

doc:name="HTTP" doc:description="" />

</ flow >

•说明

注意 outbound-endpoint 中 address 参数中的表达式。

•测试方法

浏览器中通过“ http://localhost:65000/webservice/EchoService?wsdl ”(将内容复制,保存为 *.wsdl ),然后使用 SoapUI 测试。

2.2 配置方式2

•示例配置

<pattern:web-service-proxy name="ws-proxy" inboundAddress="http://localhost:65082/services/Echo2"

outboundAddress="http://localhost:65082/services/Echo1?method=echo">

</pattern:web-service-proxy>

•说明

Mule 为这种常见的场景提供了现成的模式,以简化配置。

•测试方法

通过“ http://localhost:65082/services/Echo2?wsdl ”获取 wsdl 文件,然后使用 SoapUI 测试。


3. Web Service to Web Service

Web Service To Web Service用于在Mule中提供Web Service供客户端调用,Mule接收请求后调用远端的Web Service进行处理,并返回结果。

•示例配置


<flow name="local-ws2remote-ws">

<core:inbound-endpoint address="http://localhost:65082/services/Echo8"

disableTransportTransformer="false" exchange-pattern="request-response" doc:name="Generic"

doc:description="Generic endpoint specified by address URI" />

<cxf:jaxws-service serviceClass="demo.mule.component.Echo" doc:name="SOAP"

doc:description="Make a web service available via CXF" />

<core:outbound-endpoint

address="wsdl-cxf:http://server1:5050/mule-business/webservice/EchoService?wsdl&method=Echo" />

</ flow >

•说明

注意outbound-endpoint中address参数的配置方式,使用了wsdl-cxf前缀表示此web service是由cxf提供的。

•测试方法

在浏览器中输入“ http://localhost:65082/services/Echo8/echo/text/hello ”进行测试。


4. Socket to Socket

Socket To Socket用于将客户端的Socket请求转发至远程的Socket服务端处理,并返回处理结果。

•示例配置

<flow name="tcp2tcp">

<tcp:inbound-endpoint host="localhost" port="7100" responseTimeout="10000"

encoding="UTF-8" disableTransportTransformer="false" exchange-pattern="request-response" doc:name="TCP"

doc:description="The TCP transport enables events to be sent and received over TCP sockets." />

<tcp:outbound-endpoint host="localhost" port="7000" responseTimeout="10000"

encoding="UTF-8" disableTransportTransformer="false" exchange-pattern="request-response" doc:name="TCP"

doc:description="The TCP transport enables events to be sent and received over TCP sockets." />

</ flow >

•说明

主要配置 host 、 port 参数,表明服务地址。

•测试方法

通过 SimpleServer 和 SimpleClient 测试类,首先启动 SimpleServer ,然后启动 SimpleClient ,发送请求并接收处理结果。


5. JMS Topic

客户端发送Web Service请求,Mule将请求消息发送至远程JMS的Topic中。

•示例配置

<flow name="local-ws2jms-topic">

<core:inbound-endpoint address="http://localhost:65082/services/Echo3"

responseTimeout="10000" encoding="UTF-8" disableTransportTransformer="false" mimeType="text/plain"

exchange-pattern="one-way" doc:name="Generic" doc:description="Generic endpoint specified by address URI" />

<cxf:jaxws-service serviceClass="demo.mule.component.Echo" doc:name="SOAP"

doc:description="Make a web service available via CXF" />

<jms:outbound-endpoint topic="topic1" responseTimeout="10000" encoding="UTF-8"

disableTransportTransformer="false" disableTemporaryReplyToDestinations="false" exchange-pattern="one-way"

connector-ref="activemqConnector" doc:name="JMS" doc:description="Send or receive messages from a JMS queue" />

</flow>
<flow name="jms-topic2echo">

<jms:inbound-endpoint topic="topic1" responseTimeout="10000" encoding="UTF-8"

disableTransportTransformer="false" disableTemporaryReplyToDestinations="false" exchange-pattern="one-way"

connector-ref="activemqConnector" doc:name="JMS" doc:description="Send or receive messages from a JMS queue" />

<echo-component doc:name="Echo" doc:description="Echoes message payload." />

</ flow >

•说明

JMS endpoint 是单向的,不需要返回值。通过 topic 属性指定 JMS Server 的 Topic 名称, connector-ref 指明了使用的 JMS 连接。

•测试方法

在浏览器地址栏中输入“ http://localhost:65082/services/Echo3/echo/text/hello ”发送请求, Mule 控制台上输出订阅者的处理结果(上述示例中通过 Mule 配置了一个 JMS 的订阅者)。也可以通过 ActiveMQ 的控制台,查看到 Topic 中增加了一条发布的消息。


二. 基于消息内容的路由

Mule提供基于消息内容的路由机制,根据消息中的指定信息,将消息发送至不同的服务端进行处理。

1. Socket to Socket 路由

•示例配置

<flow name="tcp2tcp-router">

<tcp:inbound-endpoint host="localhost" port="7101" responseTimeout="10000"

encoding="UTF-8" disableTransportTransformer="false" exchange-pattern="request-response" doc:name="TCP"

doc:description="The TCP transport enables events to be sent and received over TCP sockets." />

<choice>
<when evaluator="jxpath" expression="(req/area)='bj'">

<tcp:outbound-endpoint host="server1" port="7101"

responseTimeout="10000" encoding="UTF-8" disableTransportTransformer="false" exchange-pattern="request-response"

doc:name="TCP" doc:description="The TCP transport enables events to be sent and received over TCP sockets." />

</when>
<when evaluator="jxpath" expression="(req/area)='sh'">

<tcp:outbound-endpoint host="server1" port="7102"

responseTimeout="10000" encoding="UTF-8" disableTransportTransformer="false" exchange-pattern="request-response"

doc:name="TCP" doc:description="The TCP transport enables events to be sent and received over TCP sockets." />

</when>
</choice>
</ flow >

•说明

路由使用了 <choice> 、 <when> 元素,表示路由分支。 When 元素使用 evaluator 指明表达式的解析方式,使用 expression 描述消息内容的判断条件。

•测试方法

同 Socket To Socket 测试,消息内容分别为 <req><area>bj</area></req> 、 <req><area>sh</area></req> ,查看发送至不同服务器的输出。


2. Web Service to JMS Topic 路由

•示例配置

<flow name="local-ws2jms-topic-router">

<core:inbound-endpoint address="http://localhost:65082/services/Echo7"

disableTransportTransformer="false" exchange-pattern="request-response" doc:name="Generic"

doc:description="Generic endpoint specified by address URI" />

<cxf:jaxws-service serviceClass="demo.mule.component.Echo" doc:name="SOAP"

doc:description="Make a web service available via CXF" />

<choice>
<when evaluator="jxpath" expression="(req/area)='bj'">

<jms:outbound-endpoint topic="topic1" responseTimeout="10000" encoding="UTF-8"

disableTransportTransformer="false" disableTemporaryReplyToDestinations="false"

exchange-pattern="one-way" connector-ref="activemqConnector" doc:name="JMS"

doc:description="Send or receive messages from a JMS queue" />

</when>
<when evaluator="jxpath" expression="(req/area)='sh'">

<jms:outbound-endpoint topic="topic2" responseTimeout="10000" encoding="UTF-8"

disableTransportTransformer="false" disableTemporaryReplyToDestinations="false"

exchange-pattern="one-way" connector-ref="activemqConnector" doc:name="JMS"

doc:description="Send or receive messages from a JMS queue" />

</when>
</choice>
</ flow >

•测试方法

通过“ http://localhost:65082/services/Echo7?wsdl ”获取 wsdl 文件,然后通过 SoapUI 发送请求,查看返回结果。修改消息内容,查看结果的变化。


3. Web Service to Web Service 路由

•示例配置

<flow name="local-ws2jms-topic-router">

<core:inbound-endpoint address="http://localhost:65082/services/Echo9"

disableTransportTransformer="false" exchange-pattern="request-response" doc:name="Generic"

doc:description="Generic endpoint specified by address URI" />

<cxf:jaxws-service serviceClass="demo.mule.component.Echo" doc:name="SOAP"

doc:description="Make a web service available via CXF" />

<choice>
<when evaluator="jxpath" expression="(req/area)='bj'">

<core:outbound-endpoint
address="wsdl-cxf:http://server1:5050/mule-business/webservice/CalcService?wsdl&method=processXml" />

</when>
<when evaluator="jxpath" expression="(req/area)='sh'">

<core:outbound-endpoint
address="wsdl-cxf:http://server2:5050/mule-business/webservice/CalcService?wsdl&method=processXml" />

</when>
</choice>

</ flow >

•测试方法

使用“ <![CDATA[<req><seq>1</seq><area>bj</area><price>123.45</price><count>10</count></req>]]> ”数据进行测试。


三. 数据转换


1. 压缩解压

Mule原生提供了gzip压缩方式的Transformer。


•示例配置

<flow name="gzip">

<stdio:inbound-endpoint ref="stdioInEndpoint" />

<core:string-to-byte-array-transformer encoding="UTF-8" />

<component class="demo.mule.component.Passthrough" />

<core:gzip-compress-transformer encoding="UTF-8" />

<component class="demo.mule.component.Passthrough" />

<core:gzip-uncompress-transformer encoding="UTF-8" />

<component class="demo.mule.component.Passthrough" />

<core:byte-array-to-string-transformer encoding="UTF-8" />

<stdio:outbound-endpoint ref="stdioOutEndpoint" />

</ flow >

•说明

gzip-compress-transformer 针对 byte[] 进行压缩处理,因此对于字符串类型的消息,首先需要通过 string-to-byte-array-transformer 进行转换。

•测试方法

在控制台的提示信息后输入测试字符串,完成后控制台输出同样的信息。


2. 加密解密

加密、解密是一种特定的数据转换方式,因此通过自定义Transformer的形式支持。

•示例配置


<flow name="encrypt">

<core:inbound-endpoint address="http://localhost:65082/services/Echo11"

responseTimeout="10000" encoding="UTF-8" disableTransportTransformer="false" mimeType="text/plain"

exchange-pattern="one-way" />

<cxf:jaxws-service serviceClass="demo.mule.component.Echo" />

<component>
<singleton-object class="demo.mule.component.Echo" />

</component>
<core:custom-transformer class="demo.mule.transformer.DesEncryptTransformer" encoding="UTF-8" />

<component class="demo.mule.component.Passthrough" />

<core:custom-transformer class="demo.mule.transformer.DesDecryptTransformer" encoding="UTF-8" />

<stdio:outbound-endpoint ref="stdioOutEndpoint" />

</ flow >

•说明

DesEncryptTransformer 是自定义的压缩转换器, DesDecryptTransformer 是对应的解压转换器。

•测试方法

在浏览器地址栏中输入“ http://localhost:65082/services/Echo11/echo/text/ 测试字符串”,在控制台中可见加密后的字符串和最终解密后与原串相同的字符串。


3. 自定义Transformer

•示例代码

import demo.mule.dto.Envelope;


public class String2EnvelopeTransformer extends AbstractTransformer {
private static Log log = LogFactory.getLog(String2EnvelopeTransformer.class);


@Override
protected Object doTransform(Object src, String enc) throws TransformerException {
Envelope env = new Envelope();
if (src instanceof String) {
StringTokenizer st = new StringTokenizer((String) src, ",");
String area = st.nextToken();
String data = st.nextToken();
env.create(area, data);
}
log.debug(env);
return env;
}
}

•说明

自定义 Transformer 需要继承 AbstractTransformer 类,实现其 doTransform 方法。该方法接收两个参数,一个是消息对象,一个是消息的 Encoding ,方法抛出 TransformerException ,返回转换后的消息对象。

•实例配置

<flow name="local-ws">

<core:inbound-endpoint address="http://localhost:65082/services/Echo1"

exchange-pattern="request-response" doc:name="Generic" doc:description="Generic endpoint specified by address URI" />

<cxf:jaxws-service serviceClass="demo.mule.component.Echo" doc:name="SOAP"

doc:description="Make a web service available via CXF" />

<component doc:name="Component" doc:description="Invoke a Java component">

<singleton-object class="demo.mule.component.Echo" />

</component>
<core:custom-transformer class="demo.mule.transformer.String2EnvelopeTransformer"></core:custom-transformer>

<core:outbound-endpoint address="stdio://System.out" exchange-pattern="one-way" />

</ flow >


•测试方法

在浏览器中输入“ http://localhost:65082/services/Echo1/echo/text/bj,hello ”进行测试,观察控制台输出结果。
蛋白质是生物体中普遍存在的一类重要生物大分子,由天然氨基酸通过肽键连接而成。它具有复杂的分子结构和特定的生物功能,是表达生物遗传性状的一类主要物质。 蛋白质的结构可分为四级:一级结构是组成蛋白质多肽链的线性氨基酸序列;二级结构是依靠不同氨基酸之间的C=O和N-H基团间的氢键形成的稳定结构,主要为α螺旋和β折叠;三级结构是通过多个二级结构元素在三维空间的排列所形成的一个蛋白质分子的三维结构;四级结构用于描述由不同多肽链(亚基)间相互作用形成具有功能的蛋白质复合物分子。 蛋白质在生物体内具有多种功能,包括提供能量、维持电解质平衡、信息交流、构成人的身体以及免疫等。例如,蛋白质分解可以为人体提供能量,每克蛋白质能产生4千卡的热能;血液里的蛋白质能帮助维持体内的酸碱平衡和血液的渗透压;蛋白质是组成人体器官组织的重要物质,可以修复受损的器官功能,以及维持细胞的生长和更新;蛋白质也是构成多种生理活性的物质,如免疫球蛋白,具有维持机体正常免疫功能的作用。 蛋白质的合成是指生物按照从脱氧核糖核酸(DNA)转录得到的信使核糖核酸(mRNA)上的遗传信息合成蛋白质的过程。这个过程包括氨基酸的活化、多肽链合成的起始、肽链的延长、肽链的终止和释放以及蛋白质合成后的加工修饰等步骤。 蛋白质降解是指食物中的蛋白质经过蛋白质降解酶的作用降解为多肽和氨基酸然后被人体吸收的过程。这个过程在细胞的生理活动中发挥着极其重要的作用,例如将蛋白质降解后成为小分子的氨基酸,并被循环利用;处理错误折叠的蛋白质以及多余组分,使之降解,以防机体产生错误应答。 总的来说,蛋白质是生物体内不可或缺的一类重要物质,对于维持生物体的正常生理功能具有至关重要的作用。
ESB和综合前置都是企业集成的解决方案,它们各自具有一些优势和劣势。以下是它们的主要优劣势: ESB的优势: 1. 灵活性:ESB提供了灵活的集成平台和中间件,可以根据需求进行定制和扩展,以满足不同的集成需求。 2. 可扩展性:ESB可以支持大规模的系统集成,通过添加插件或扩展组件来满足不同的集成需求。 3. 解耦性:ESB通过消息传递和服务调用来实现应用程序之间的解耦和互操作性,降低了系统间的耦合度,提高了系统的灵活性和可维护性。 4. 安全性:ESB提供了对数据传输和访问的安全管理功能,可以实现加密、身份认证、访问控制等安全机制。 ESB的劣势: 1. 复杂性:ESB作为一个综合的集成解决方案,其配置和管理可能相对复杂,需要专业的技术人员进行实施和维护。 2. 成本:由于ESB的复杂性和可扩展性,其实施和维护的成本可能较高,特别是对于规模较小的企业而言。 3. 性能:ESB可能对系统的性能产生一定的影响,特别是在高并发和大数据量的情况下,需要进行相应的优化和调整。 综合前置的优势: 1. 简单性:综合前置通常是一个独立的集成解决方案,其配置和管理相对简单,易于实施和维护。 2. 效率:综合前置通过集中管理和转换数据,可以提高系统间数据交换的效率和速度。 3. 一致性:综合前置可以处理不同系统之间的数据格式和协议差异,确保数据的一致性和准确性。 综合前置的劣势: 1. 有限的功能:综合前置通常只提供数据传输和转换功能,相对于ESB而言,功能扩展性较弱。 2. 依赖性:综合前置需要将多个系统集成到一个中心化的前置服务器上,系统间的依赖性较强,一旦前置服务器出现故障,可能影响整个系统的运行。 总体来说,选择ESB还是综合前置应该根据具体的业务需求、系统规模和集成复杂度来决定。ESB适用于复杂的系统集成场景,提供更全面和灵活的功能;而综合前置适用于简单的系统集成需求,具有简单性和高效性的优势。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值