Mule ESB 學習筆記(8)

寫之前的內容時,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-endpointaddress參數的配置方式,使用了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將請求消息發送至遠程JMSTopic中。

  • 示例配置

<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;

 

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文檔閱讀及試用的總結。

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

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章