且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

Mule的基本配置元素:Endpoint、Transformer、Filter、Flow

更新时间:2022-08-21 18:32:01

1.Endpoint


Mule的endpoint和WS中的endpoint是非常类似的,它是消息进出(读写)的入口,不同的是mule的endpoint可以基于各种不同的传输协议。
mule的endpoint分成两类:
  •     global endpoint:全局的endpoint,一个flow或service的inbound或outbound endpoint可以通过ref属性引用这些endpoint.
  •      low或service内的inbound或outbound endpoint,这类endpoint仅限于flow或service内,可以认为是一种局部endpoint.
每一个endpoint都必须指明它的传输协议是什么,或者说它是什么传输类型的endpoint,指定方式有两种:
  •      在<endpoint/>元素上追加传输协议前缀:
[html] view plaincopy
  1. <http:endpoint name="in" host="localhost" port="8080" path = "services/orders" user="${user.name}" password ="${user.password}"/>  

  •     通过<endpoint/>元素的address属性指定:
[html] view plaincopy
  1. <endpoint addres="http://${user.name}:${user.password}@localhost:8080/services/orders/>  

刚开始接触mule时对inbound和outbound的endpoint可能一时不太好区分,实际上所谓的in和out都是以mule的视角来定位的,inbound指的是外部message“流入”mule的断点,也就是mule暴露给外部应用可以访问的端点,同理,outbound就是message从mule向外“流出”的端点,也就是一个可以访问的外部应用的端点。比如一种最典型也是最普遍的WS代理配置,也就是将不同物理地址的WS统一挂接到ESB,由ESB以一致的地址发布,如:

[html] view plaincopy
  1. <pattern:web-service-proxy name="weather-forecast-ws-proxy"  
  2.     inboundAddress="http://localhost:8090/weather-forecast"  
  3.     outboundAddress="http://server1:6090/weather-forecast"  
  4.     wsdlLocation="http://server1:6090/weather-forecast?wsdl" />  

在这段配置中,ESB把原物理地址为:http://server1:6090/weather-forecast 的服务经过包裹,以新的地址http://localhost:8090/weather-forecast 发布出去。作为inboundAddress,http://localhost:8090/weather-forecast是ESB对外“开放”或“发布”的端点,也就是消息流入ESB的端点。outboundAddress则是ESB路由的目的地,其往往是某个现存系统的访问入口。


2.Transformer


Transformer是负责消息传输过程中的数据转换,如从JSON对象到一个Java对象的转换:

[html] view plaincopy
  1. <json:json-to-object-transformer name="jsonToFruitCollection" returnClass="org.mule.module.json.transformers.FruitCollection">  
  2. <json:deserialization-mixin mixinClass="org.mule.module.json.transformers.OrangeMixin" targetClass="org.mule.tck.testmodels.fruit.Orange"/>  
  3. </json:json-to-object-transformer>  

3.Filter


Filter是用于配置是否要对消息进行过滤的配置项,除像wildcard-filter这样的基本Filter,Mule还有功能强大的逻辑Filter用于进行过滤条件的组合。

[java] view plaincopy
  1. <or-filter>  
  2.     <wildcard-filter pattern="*priority:1*"/>  
  3.     <and-filter>  
  4.         <not-filter>  
  5.             <wildcard-filter pattern="*region:Canada*"/>  
  6.         </not-filter>  
  7.         <wildcard-filter pattern="*priority:2*"/>  
  8.     </and-filter>  
  9. </or-filter>  

4.Flow


Flow是mule的最基本处理单元。它从一个inbound endpoint开始,对message进行了一系列的处理,然后从outbound endpoint输出。这期间会flow会使用到上述提及的多种组件参与处理,如transformer、filter等等,而且还可能调用java或其他语言实现的组件进行处理,应该说在应用集成里,flow所要实现的,正是所谓的“集成逻辑”。