Mule ESB 學習筆記(3)

寫之前的內容時,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文檔閱讀及試用的總結。

PS:程序代碼插件有時有點問題,前幾段代碼字體是CN,後邊就突然變成一種非等寬字體了。而且XML的格式不是很直觀,因此直接粘貼了。。

轉載自:http://my.oschina.net/moon/blog/40125

發佈了64 篇原創文章 · 獲贊 28 · 訪問量 49萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章