一. 服务调用
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 ”进行测试,观察控制台输出结果。
仅以上述示例代码作为这一段时间Mule文档阅读及试用的总结。
相关推荐
Mule ESB(Enterprise Service Bus,企业服务总线)是一款开源的集成平台,旨在简化企业级应用之间的数据交互。本文将围绕Mule ESB的源码进行深入探讨,揭示其核心设计理念与工作原理。 首先,`logging.conf`是日志...
### ESB原理及Mule ESB实践 ...综上所述,ESB和Mule ESB是现代IT架构中不可或缺的部分,它们为企业提供了一种灵活、高效的服务集成方案。无论是理论层面还是实际应用,掌握ESB原理及Mule ESB实践都是非常有价值的。
在本篇“Mule ESB 学习笔记(13)CSV数据文件到数据库”中,我们将探讨如何使用Mule ESB(Enterprise Service Bus,企业服务总线)处理CSV(Comma Separated Values,逗号分隔值)数据,并将其有效地导入到数据库中...
根据提供的文件内容,以下是关于Mule ESB手册-中文版的知识点: 1. Mule ESB简介 ...通过这些知识点的学习,可以加深对Mule ESB的使用方法的理解,并通过实例加深对ESB概念的理解,对新手来说非常有帮助。
Mule ESB 是一个基于 Java 的轻量级企业服务总线和集成平台,允许开发人员快速便利地连接多个应用,并支持应用间的数据交换。Mule ESB 支持集成现有系统而无论其底层采用何种技术,如 JMS、Web Services、JDBC、...
MULE ESB(Mule Enterprise Service Bus)是Anypoint Platform的核心组件,它是一个强大的、全面集成的企业服务总线(ESB),专为构建、部署和管理API和集成解决方案而设计。MULE ESB-4.1是MuleSoft公司推出的企业版...
Mule ESB 是一个轻量级的基于java的企业服务总线和集成平台, 使得开发人员可以快速,简单的连接多个应用, 使得它们可以交换数据。 Mule ESB 容易集成现有异构系统,包括:JMS, Web Services, JDBC, HTTP, 等. ESB...
五、学习和使用Mule ESB "MuleESB3"这个文件名可能指的是Mule ESB的第三个主要版本。在该版本中,用户可以期待更完善的特性和改进。对于初学者,建议首先通过官方文档了解Mule ESB的基本概念和工作原理,然后使用Any...
Mule ESB(Enterprise Service Bus,企业服务总线)是一款强大的开源集成平台,专为构建可扩展、灵活和可靠的分布式应用程序而设计。Mule ESB 3.0是该平台的一个重要版本,提供了许多改进和新特性,使得它在处理企业...
《Mule ESB 开发手册》是一份详尽的指南,专为希望深入了解并掌握 Mule ESB(Enterprise Service Bus)技术的...通过深入学习和实践,开发者可以充分利用 Mule ESB 的强大功能,实现高效、可靠的企业级集成解决方案。
- **定位**:Mule ESB 3.0是一款轻量级的消息框架和整合平台,旨在帮助企业轻松地集成不同的系统和服务。 - **核心特性**:基于EIP(Enterprise Integration Patterns)原则构建,支持多种传输协议(如file, FTP, ...
Mule ESB(Enterprise Service Bus,企业服务总线)是一款强大的开源集成平台,它帮助企业将不同的系统、应用程序和服务连接在一起,实现数据的高效流转。本教程将带您入门Mule ESB项目,通过实例学习其核心概念和...
《Mule ESB Cookbook随书源码》是一个与Mule ESB相关的实践指南,它包含了大量实例代码,旨在帮助读者深入理解和应用Mule ESB这一开源企业服务总线(Enterprise Service Bus)。Mule ESB是业界广泛采用的ESB解决方案...
Mule ESB,全称Mule Enterprise Service Bus,是一个开源的企业服务总线系统,旨在促进不同应用程序和服务之间的数据交换和集成。Mule的核心设计是基于轻量级的Java平台,尤其是J2EE 1.4标准,使得它能够在各种企业...
MuleESB是一个基于Java的轻量级企业服务总线和集成平台,允许开发人员快速便利地连接多个应用,并支持应用间的数据交换。MuleESB支持集成现有系统而无论其底层采用何种技术,如JMS、WebServices、JDBC、HTTP以及其他...
描述:本手册旨在为用户提供对Mule ESB 3的基础使用指导,强调了Mule ESB作为一个社区成熟且文档丰富的开源企业服务总线(ESB)的使用方法。 知识点说明: 1. Mule ESB概述: Mule ESB是一个开源的中间件平台,...
Mule ESB(Enterprise Service Bus,企业服务总线)是一种开源的集成平台,由Mulesoft公司提供,它主要用于构建和管理API及企业内部系统的集成。Mule ESB的核心特性是轻量级、高性能和易用性,它允许开发者快速地...
MULE ESB(Message Broker Enterprise Service Bus)是一款强大的企业级服务总线,由Mulesoft公司开发,它提供了一个集成平台,用于连接各种应用程序和服务,实现数据的高效传输。MULE ESB-4.1社区版是Mulesoft为...