MULE ESB学习笔记

简介: 写之前的内容时,Mule刚刚3.0.1版本,很多官方文档还没有更新(尤其示例代码),维持在V2的状态。经过了一年多的时间,Mule社区版发展至了3.2版本,并且推出了Mule Studio可视化开发工具(当前beta状态,支持Mule 3.1.2)。 将以前自己验证的示例代码在3.1.2版本上又跑了一遍(有些变化),在此做一下记录。 一. 服务调用 1. Mule实现并提供Web

写之前的内容时,Mule刚刚3.0.1版本,很多官方文档还没有更新(尤其示例代码),维持在V2的状态。经过了一年多的时间,Mule社区版发展至了3.2版本,并且推出了Mule Studio可视化开发工具(当前beta状态,支持Mule 3.1.2)。

将以前自己验证的示例代码在3.1.2版本上又跑了一遍(有些变化),在此做一下记录。


一. 服务调用

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&amp;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&amp;method=processXml" />

</when>

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

<core:outbound-endpoint

                address="wsdl-cxf:http://server2:5050/mule-business/webservice/CalcService?wsdl&amp;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;

publicclassString2EnvelopeTransformerextendsAbstractTransformer{

privatestaticLoglog=LogFactory.getLog(String2EnvelopeTransformer.class);

@Override

protectedObjectdoTransform(Objectsrc,Stringenc)throws TransformerException {

Envelopeenv=newEnvelope();

if(srcinstanceofString){

StringTokenizerst=newStringTokenizer((String)src,",");

Stringarea=st.nextToken();

Stringdata=st.nextToken();

env.create(area,data);

}

log.debug(env);

returnenv;

}

}
  • 说明
  • 自定义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”进行测试,观察控制台输出结果。
目录
相关文章
|
6月前
|
监控 应用服务中间件 nginx
Agentic 时代必备技能:手把手为 Dify 应用构建全链路可观测系统
本文讲述 Dify 平台在 Agentic 应用开发中面临的可观测性挑战,从开发者与运维方双重视角出发,系统分析了当前 Dify 可观测能力的现状、局限与改进方向。
1310 101
|
5月前
|
监控 前端开发 数据可视化
Entity Explorer:基于 UModel 的实体探索平台
阿里云 Entity Explorer 正式发布:基于 UModel 的智能实体探索平台,实现亿级实体秒级检索、关系拓扑自动构建、详情页动态渲染,让可观测性从“数据堆砌”迈向“业务洞察”。
501 80
|
6月前
|
存储 算法 AliSQL
AliSQL 向量技术解析(一):存储格式与算法实现
AliSQL基于MySQL 8.0原生扩展向量处理能力,支持高达16383维的向量存储与计算,集成余弦相似度、欧式距离等函数,并通过HNSW算法实现高效近似最近邻搜索。借助结构化辅助表与精度压缩技术,兼顾检索精度与性能,结合数据字典适配保障DDL原子性,为推荐系统、AI应用提供开箱即用的高维向量检索解决方案。
AliSQL 向量技术解析(一):存储格式与算法实现
|
5月前
|
人工智能 运维 安全
助力企业构建 AI 原生应用,函数计算FunctionAI 重塑模型服务与 Agent 全栈生态
在 AI 技术应用落地进程中,目前面临着五大核心挑战:开发/学习门槛过高,部署运维阶段复杂,AI 应用安全备受挑战,生态能力方面存在严重的割裂与锁定现象,同时资源成本高昂且利用率低下。这些挑战极大地阻碍了 AI 技术的广泛普及以及应用效率的有效提升。阿里云函数计算(FC)依托 Serverless AI 基础设施与全栈能力的创新突破,推出 Function AI(函数智能),精准攻克上述痛点问题,全面推动 AI 应用在开发至运维的全流程中实现降本增效。
|
5月前
|
传感器 搜索推荐 物联网
RFID打造宠物智能管理新模式
通过RFID技术为宠物建立独特的RFID电子"身份证",实现对宠物全生命周期的精细跟踪与详细记录,做到有据可查。RFID提供了精细的宠物数据跟踪能力,将宠物的生命历程、疫苗、健康状况等详细记录,使重要信息易于获取和管理。RFID技术通过为宠物建立唯一电子身份,实现精准识别与数据交互,RFID打造宠物智能管理新模式。
|
5月前
|
机器学习/深度学习 人工智能 算法
奥维:AI技术赋能水利工程 “人工智能+”展现巨大潜力
奥维数字科技凭借对AI技术的深耕与水利场景的深刻理解,打造出奥维水利算法云这一核心解决方案,将AI能力渗透到大坝安全、洪水预报、淹没分析等关键环节,以“精准、实时、可进化”的服务特性,为水利行业智能化升级提供了可落地的技术范式。奥维通过“AI+水利”的实践证明,人工智能并非简单的“技术叠加”,而是能从“数据处理、模型优化、决策支撑”三个核心环节重构水利工程的运行模式:它让大坝监测更精准、洪水预报更及时、应急响应更科学,也让水利决策从“经验驱动”转向“数据驱动”。
766 5
|
5月前
|
缓存 监控 安全
知识图谱与大模型:谁将引领未来发展?
本文对比了知识图谱与大模型的技术优劣。知识图谱逻辑清晰、可解释性强但构建繁琐;大模型灵活高效却存在黑盒与幻觉风险。实际工作中,二者并非对立,推荐采用RAG等融合架构,用图谱提供可靠支撑,用大模型快速生成,以兼顾系统可靠性与迭代效率。
|
安全 算法 网络协议
解析:HTTPS通过SSL/TLS证书加密的原理与逻辑
HTTPS通过SSL/TLS证书加密,结合对称与非对称加密及数字证书验证实现安全通信。首先,服务器发送含公钥的数字证书,客户端验证其合法性后生成随机数并用公钥加密发送给服务器,双方据此生成相同的对称密钥。后续通信使用对称加密确保高效性和安全性。同时,数字证书验证服务器身份,防止中间人攻击;哈希算法和数字签名确保数据完整性,防止篡改。整个流程保障了身份认证、数据加密和完整性保护。
|
5月前
|
人工智能 监控 测试技术
n8n+AI模型实现用例智能生成与脚本自维护
本文介绍如何在n8n中构建AI驱动的自动化工作流自维护系统。通过监控、AI生成测试用例与智能修复,让脚本具备“免疫系统”,降低维护成本,提升稳定性,实现人机协同的高效运维。
|
数据采集 机器学习/深度学习 人工智能
智能运维在IT管理中的实践与探索
【10月更文挑战第21天】 本文深入探讨了智能运维(AIOps)技术在现代IT管理中的应用,通过分析其核心组件、实施策略及面临的挑战,揭示了智能运维如何助力企业实现自动化监控、故障预测与快速响应,从而提升整体运维效率与系统稳定性。文章还结合具体案例,展示了智能运维在实际环境中的显著成效。
686 133