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

歡迎訪問 生活随笔!

生活随笔

當(dāng)前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

Mule ESB 学习笔记

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

寫之前的內(nèi)容時,Mule剛剛3.0.1版本,很多官方文檔還沒有更新(尤其示例代碼),維持在V2的狀態(tài)。經(jīng)過了一年多的時間,Mule社區(qū)版發(fā)展至了3.2版本,并且推出了Mule Studio可視化開發(fā)工具(當(dāng)前beta狀態(tài),支持Mule 3.1.2)。

將以前自己驗證的示例代碼在3.1.2版本上又跑了一遍(有些變化),在此做一下記錄。


一. 服務(wù)調(diào)用

1. Mule實現(xiàn)并提供Web Service

??? 在Mule上開發(fā)并發(fā)布一個Web Service供客戶端調(diào)用。

  • 示例配置

<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”,回車后瀏覽器中將顯示返回結(jié)果信息。地址中的“echo”是服務(wù)的方法,“text”是方法的參數(shù),“hello”是參數(shù)的值。

2. Web Service Proxy

??? Web Service Proxy用來將客戶端的WS請求直接轉(zhuǎn)發(fā)至相應(yīng)的遠(yuǎn)程WS服務(wù)端處理,并返回處理結(jié)果。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參數(shù)中的表達(dá)式。
  • 測試方法
  • 瀏覽器中通過“http://localhost:65000/webservice/EchoService?wsdl”(將內(nèi)容復(fù)制,保存為*.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為這種常見的場景提供了現(xiàn)成的模式,以簡化配置。
  • 測試方法
  • 通過“http://localhost:65082/services/Echo2?wsdl”獲取wsdl文件,然后使用SoapUI測試。

3. Web Service to Web Service

??? Web Service To Web Service用于在Mule中提供Web Service供客戶端調(diào)用,Mule接收請求后調(diào)用遠(yuǎn)端的Web Service進(jìn)行處理,并返回結(jié)果。

  • 示例配置

<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參數(shù)的配置方式,使用了wsdl-cxf前綴表示此web service是由cxf提供的。
  • 測試方法
  • 在瀏覽器中輸入“http://localhost:65082/services/Echo8/echo/text/hello”進(jìn)行測試。

4. Socket to Socket

??? Socket To Socket用于將客戶端的Socket請求轉(zhuǎn)發(fā)至遠(yuǎn)程的Socket服務(wù)端處理,并返回處理結(jié)果。

  • 示例配置

<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參數(shù),表明服務(wù)地址。
  • 測試方法
  • 通過?? SimpleServer和SimpleClient測試類,首先啟動SimpleServer,然后啟動SimpleClient,發(fā)送請求并接收處理結(jié)果。

5. JMS Topic

??? 客戶端發(fā)送Web Service請求,Mule將請求消息發(fā)送至遠(yuǎn)程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”發(fā)送請求,Mule控制臺上輸出訂閱者的處理結(jié)果(上述示例中通過Mule配置了一個JMS的訂閱者)。也可以通過ActiveMQ的控制臺,查看到Topic中增加了一條發(fā)布的消息。


二. 基于消息內(nèi)容的路由

??? Mule提供基于消息內(nèi)容的路由機(jī)制,根據(jù)消息中的指定信息,將消息發(fā)送至不同的服務(wù)端進(jìn)行處理。

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指明表達(dá)式的解析方式,使用expression描述消息內(nèi)容的判斷條件。
  • 測試方法
  • 同Socket To Socket測試,消息內(nèi)容分別為<req><area>bj</area></req>、<req><area>sh</area></req>,查看發(fā)送至不同服務(wù)器的輸出。

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發(fā)送請求,查看返回結(jié)果。修改消息內(nèi)容,查看結(jié)果的變化。

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>]]>”數(shù)據(jù)進(jìn)行測試。


三. 數(shù)據(jù)轉(zhuǎn)換

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[]進(jìn)行壓縮處理,因此對于字符串類型的消息,首先需要通過string-to-byte-array-transformer進(jìn)行轉(zhuǎn)換。
  • 測試方法
  • 在控制臺的提示信息后輸入測試字符串,完成后控制臺輸出同樣的信息。

2. 加密解密

??? 加密、解密是一種特定的數(shù)據(jù)轉(zhuǎn)換方式,因此通過自定義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是自定義的壓縮轉(zhuǎn)換器,DesDecryptTransformer是對應(yīng)的解壓轉(zhuǎn)換器。
  • 測試方法
  • 在瀏覽器地址欄中輸入“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類,實現(xiàn)其doTransform方法。該方法接收兩個參數(shù),一個是消息對象,一個是消息的Encoding,方法拋出TransformerException,返回轉(zhuǎn)換后的消息對象。
  • 實例配置

<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”進(jìn)行測試,觀察控制臺輸出結(jié)果。

總結(jié)

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

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯,歡迎將生活随笔推薦給好友。