日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

MULE ESB学习笔记

發布時間:2024/9/27 编程问答 30 豆豆
生活随笔 收集整理的這篇文章主要介紹了 MULE ESB学习笔记 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

寫之前的內容時,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”進行測試,觀察控制臺輸出結果。

總結

以上是生活随笔為你收集整理的MULE ESB学习笔记的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。