Mule ESB 學習筆記

jopen 10年前發布 | 166K 次閱讀 Mule ESB 企業服務總線ESB

1. 簡介

Mule ESB是一個基于Java的輕量級企業服務總線和集成平臺,允許開發人員快速便利地連接多個應用,并支持應用間的數據交換。Mule ESB支持集成現有系統而無論其底層采用何種技術,如JMSWeb ServicesJDBCHTTP以及其他技術。

2. 整體結構

Mule ESB 學習筆記

圖 整體結構

從上圖可見,Mule通過Transports/Connectors與外圍的異構系統連接,提供Routing(路由)、Transaction Management(事務管理)、Transformation(轉換)、Message Broker(消息代理)、Transportation Management(傳輸管理)、Security(安全)等核心模塊。Mule可以單獨使用,也可以架設在常用的應用服務器上。

Mule ESB 學習筆記

圖 架構簡圖

外圍系統的服務請求通過Mule ESBTransport接入,Mule通過Transformer進行數據的格式轉換,然后經過Inbound Router進行消息過濾(內部通過配置filter實現)后交給MuleComponent進行業務邏輯處理,處理后的結果通過Outbound Router確定傳遞給哪個接收方,然后通過Transformer進行數據格式轉換,通過Transport連接至接收方,傳遞信息。

此圖描述的是Mule中的一個典型場景的處理過程,涵蓋了Mule中的各個關鍵組件。其中某些處理步驟不是必須的,如Inbound Router、Transformer。后續可以看到一些其他場景的處理。

3. 功能

a. 服務中介

  • 將業務邏輯和消息發送分離

    </li>

  • 屏蔽服務的消息格式和協議
  • 提供任意位置的服務調用
  • 提供協議橋接
  • </ul>

    b. 數據轉換

    • 在應用間交換不同格式的信息 
    • 操作消息的負載內容,包括加密、壓縮和編碼轉換
    • 在異構的傳輸協議的數據類型間格式化消息
    • </ul>

      c. 消息路由

      • 基于消息內容和復雜規則路由消息
      • 消息的過濾、聚合以及重新排列序號
      • </ul>

        d. 服務創建和托管

        • 暴露端點、EJBSpring Bean以及POJO作為服務
        • 作為輕量級的服務容器進行服務托管
        • </ul>

          Mule ESB中有一些基本的概念,理解這些基本概念后才能理解Mule的內部機制。從中也可以看到Mule解決問題的基本思路。

           

          4. 基本概念

          4.1 Model

          Model表示托管各個服務的運行時環境。

          Mule ESB 學習筆記

          圖 Model

          4.2 Service

          Service是用來處理服務請求的基本單位,它調用各個組件進行服務請求的處理。

          Mule ESB 學習筆記

          圖 Service

          4.3 Transport

          Transport管理消息的接收和發送,數據轉換的過程也是在Transport中通過調用Transformer完成的。

          Mule ESB 學習筆記

          圖 Transport

          4.3.1 Connector

          Connector用于管控特定協議的使用,如HTTP ConnectorJMS Connector等。

          4.3.2 End-Point

          Endpoint用于表示一種協議的特定使用方式,如listening/polling、從中讀取、向指定地址寫入等,定義了發送和接收消息的通道。Endpoint控制的是底層的實體在Connector中如何被使用。

          Endpoint定義于InboundOutbound Router中。

          4.4 Transformer

          Transformer用于轉換消息的內容。

          Mule ESB 學習筆記

          圖 Transformer

          4.5 Router

          Router使用Filter基于消息中的屬性信息進行消息的分發。

          Mule ESB 學習筆記

          圖 Router

          RouterService中的位置決定了Router的性質(inboundoutboundresponse)和擔任的角色(pass-throughaggregator等)。

          4.6 Component

          ComponentService的核心部件,是Service的業務邏輯的實現。

          Mule ESB 學習筆記

          圖 Component: implicit bridge component

          Component可以是Java Class(POJO、Spring Bean)、Web Service、Script等。

          Component可定義自己的生命周期:initialisestartstopdispose,不過需要實現MuleLifeCycle接口。Mule 3.0版本開始提供@PostConstruct@PreDestroy的注解,對應生命周期的initialisedispose階段,不需要實現MuleLifeCycle接口了。

          4.7 Flow(@since 3.0)

          FlowMule 3.0新引入的,包含一個消息源(Message Source)和多個消息處理器組成的處理器鏈。

          Mule ESB 學習筆記

          圖 Flow

           

          根據實際需求著重檢查了一下Mule ESB的消息傳遞方式。Mule支持常用的幾種消息傳遞方式,能夠滿足要求。

           

          5. 消息傳遞方式

          5.1 異步方式

          異步方式是一種單向調用,調用者不需要獲得響應。

          Mule ESB 學習筆記

          圖 Asynchronous

          異步方式通過inboundoutbound endpointexchange-pattern=”one-way”實現。

          使用基本的Stdio Transport驗證,通過標準輸入傳輸字符串,將其原樣傳遞給標準輸出進行顯示。相應配置如下:


               <service name="echo">    
                  <inbound>    
                      <stdio:inbound-endpoint system="IN" exchange-pattern="one-way" />    
                  </inbound>    
          
                  <component>    
                      <singleton-object class="demo.mule.umo.StdIo" />    
                  </component>    
          
                  <outbound>    
                      <pass-through-router>    
                          <stdio:outbound-endpoint system="OUT" exchange-pattern="one-way" />    
                      </pass-through-router>    
                  </outbound>    
              </service>    

          運行服務,控制臺顯示結果如下:
           
               Please enter: Hello, world!    
              INFO  2010-12-07 19:21:18,877 [ConsoleConnector.dispatcher.1]    
                  org.mule.lifecycle.AbstractLifecycleManager: Initialising:    
                  'ConsoleConnector.dispatcher.23255376'. Object is: StdioMessageDispatcher    
              INFO  2010-12-07 19:21:18,877 [ConsoleConnector.dispatcher.1]    
                  org.mule.lifecycle.AbstractLifecycleManager: Starting:    
                  'ConsoleConnector.dispatcher.23255376'. Object is: StdioMessageDispatcher    
              Hello, world!    

          其中 INFO輸出是Mule第一次初始化相應Connector打印出來的,之后調用服務不會再次顯示。

          異步方式適用于簡單的消息傳遞的場景。

          5.2 請求-響應方式

          請求-響應方式即請求方調用服務后,服務立即處理并返回響應結果,不需將消息再次傳遞。

          Mule ESB 學習筆記

          圖 Request-Response

          請求-響應方式通過input endpointexchange-pattern=”request-response”實現,相應配置如下:

               <strong>  
                  <strong>  
                      <model name="services">        
                          <service name="echoService">        
                              <inbound>        
                                  <inbound-endpoint address="http://localhost:7007/services/Echo"        
                                      exchange-pattern="request-response">        
                                      <cxf:jaxws-service />        
                                  </inbound-endpoint>        
                              </inbound>        
                              <component>        
                                  <singleton-object class="demo.mule.umo.Echo" />        
                              </component>        
                          </service>        
                      </model>  
                  </strong>  
              </strong>    

          邊是通過service配置的,通過flow配置如下:

           

               <flow name="EchoFlow">        
                  <inbound-endpoint address="http://localhost:7007/services/Echo"        
                      exchange-pattern="request-response" />        
                  <cxf:jaxws-service serviceClass="demo.mule.umo.Echo" />        
                  <component>        
                      <singleton-object class="demo.mule.umo.Echo" />        
                  </component>        
              </flow>    

          在瀏覽器中輸入“http://localhost:7007/services/Echo/echo/text/hello,world”,瀏覽器中會顯示“hello,world”的輸出信息。

          請求-響應方式適用于單次服務調用的場景。

          5.3 同步方式

          同步方式即請求方調用服務后,component將處理結果發送給另一個外部服務處理,并將處理結果反方向返回。

          Mule ESB 學習筆記

          圖 Synchronous

          同步方式通過inboundoutbound endpointexchange-pattern=”request-response”實現,相應配置如下:

           <flow name="echo">      
              <inbound-endpoint address="http://localhost:7007/services/Echo"      
                  exchange-pattern="request-response" />      
              <cxf:jaxws-service serviceClass="demo.mule.umo.Echo" />      
              <component>      
                  <singleton-object class="demo.mule.umo.StdIo" />      
              </component>      
              <vm:outbound-endpoint path="vm" exchange-pattern="request-response" />      
          </flow>      
          <flow name="vm">      
              <vm:inbound-endpoint path="vm" exchange-pattern="request-response" />      
              <component>      
                  <singleton-object class="demo.mule.umo.Vm" />      
              </component>      
              <stdio:outbound-endpoint system="OUT" exchange-pattern="one-way" />      
          </flow> 

          同步方式適用于通過Mule調用遠程服務的場景。

          5.4 異步請求-響應方式

          異步請求-響應方式即請求方調用服務后不需要立即獲得返回結果,component將請求發送給其他外圍系統處理(可能有多個),全部處理完畢后通過指定的異步應答Router返回給請求方。
          Mule ESB 學習筆記

          圖 Asynchronous Request-Response

          異步請求-響應方式通過在OutBound Endpoint中增加reply-to以及增加async-reply節點實現,響應配置如下:


               <flow name="echo">      
                  <inbound-endpoint address="http://localhost:7007/services/Echo"      
                      exchange-pattern="request-response" />      
                  <cxf:jaxws-service serviceClass="demo.mule.umo.Echo" />      
                  <component>      
                      <singleton-object class="demo.mule.umo.StdIo" />      
                  </component>      
                  <vm:outbound-endpoint path="vm" exchange-pattern="request-response" />      
              </flow>      
              <flow name="vm">      
                  <vm:inbound-endpoint path="vm" exchange-pattern="request-response" />      
                  <component>      
                      <singleton-object class="demo.mule.umo.Vm" />      
                  </component>      
                  <stdio:outbound-endpoint system="OUT" exchange-pattern="one-way" />      
              </flow>    

          異步請求-響應方式適用于請求需要被多個遠程服務并行處理,結果需要匯總處理后返回的場景。

          注:上述代碼未運行通過,queue1和queue2獲得了請求消息并正常處理,但返回至async-reply時拋出異常,暫未定位到問題。

          后將collection-async-reply-router改為single-async-reply-router未報異常,代碼示例如下:

           

          <service name="async req-rep">      
              <inbound>      
                  <stdio:inbound-endpoint ref="stdioInEndpoint" />      
              </inbound>      
              <component class="demo.mule.umo.Echo" />      
              <outbound>      
                  <multicasting-router>      
                      <vm:outbound-endpoint path="async.queue1" exchange-pattern="one-way" />      
                      <vm:outbound-endpoint path="async.queue2" exchange-pattern="one-way" />      
                      <reply-to address="vm://reply" />      
                  </multicasting-router>      
              </outbound>      
              <async-reply timeout="5000" failOnTimeout="true">      
                  <vm:inbound-endpoint path="reply" exchange-pattern="one-way" />      
                  <single-async-reply-router />      
              </async-reply>      
          </service>
 本文由用戶 jopen 自行上傳分享,僅供網友學習交流。所有權歸原作者,若您的權利被侵害,請聯系管理員。
 轉載本站原創文章,請注明出處,并保留原始鏈接、圖片水印。
 本站是一個以用戶分享為主的開源技術平臺,歡迎各類分享!