Mule ESB浅析5——基本配置
主配置文件:mule-config.xml<?xml version="1.0" encoding="UTF-8"?><mule xmlns="http://www.mulesoft.org/schema/mule/core" xmlns:doc="http://www.mulesoft.org/schema/mule/documentation" xmlns:jms="http://www.mulesoft.org/schema/mule/jms" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:http="http://www.mulesoft.org/schema/mule/http" xmlns:vm="http://www.mulesoft.org/schema/mule/vm" xmlns:smtp="http://www.mulesoft.org/schema/mule/smtp" xmlns:scripting="http://www.mulesoft.org/schema/mule/scripting" xmlns:jdbc="http://www.mulesoft.org/schema/mule/jdbc" xmlns:spring="http://www.springframework.org/schema/beans" xsi:schemaLocation=" http://www.mulesoft.org/schema/mule/core schema/mule/core/mule.xsd http://www.mulesoft.org/schema/mule/http schema/mule/http/3.3/mule-http.xsdhttp://www.mulesoft.org/schema/mule/jms schema/mule/jms/mule-jms.xsdhttp://www.mulesoft.org/schema/mule/jdbc schema/mule/jdbc/mule-jdbc.xsdhttp://www.mulesoft.org/schema/mule/smtp schema/mule/smtp/mule-smtp.xsdhttp://www.mulesoft.org/schema/mule/scripting schema/mule/scripting/mule-scripting.xsd http://www.mulesoft.org/schema/mule/vm schema/mule/vm/3.3/mule-vm.xsdhttp://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.1.xsd"><description>This configuration uses an HTTP endpoint to receiverequests.</description>
?1,如上所示,为mule_config.xml的起始内容,这里主要是名字空间的导入,比如我这里需要使用到http ,jms等transport,则需要导入相应的名字空间。这里与spring的配置方式类似,只是增加里一些mule自己定义的元素标签而已。注意这里名字空间是会一直更新的,目前使用的最新版本在3.3目录下。
?
?
<http:connector name="httpConnector" enableCookies="true" />
?如上所示,我的项目中需要使用到http ?transport,我这里定义一个对应的connector,该connector可以被多个http的endpoint使用,用于统一管理http输入或者输出。不定义该connector,则mule会自动创建一个默认的connector。另外,如果这里显式创建了一个connector,则不论后面endpoint定义时是否通过conector-ref引用,都会使用该创建好的connector。
?
?
?
<flow name="root" doc:name="root"><!--入口点监控localhost:8088的任意目录 --><http:inbound-endpoint address="http://localhost:8088" connector-ref="httpConnector" /><!--请求经过transformer转换 --><transformer ref="RequestToMessageTrans" /> <!--转换后的请求送交目标component处理 --><component> <spring-object bean="ProcessorComponent" /> </component> <!-- 输出为条件选择分支 --><choice><when expression="message.outboundProperties['custom-def-property'] =="branch1'"><flow-ref name="branch1" doc:name="branch1" /></when><when expression="message.outboundProperties['custom-def-property'] =="branch2'"><http:outbound-endpoint address="#[target_url]"/></when><otherwise><logger message="#[payload.address.asString]" level="INFO" doc:name="Logger" /></otherwise></choice><!--异常处理逻辑 --><choice-exception-strategy><catch-exception-strategy > ...</catch-exception-strategy></choice-exception-strategy></flow>
?3,如上所示,此处flow标签内的编排,是实现整个业务逻辑核心。
?
其中包括了
1)inound-endpoint:监听消息输入,此处监听http的请求。另外也可以同时定义jms等endpoint进行监听。
2)transformer:输入消息转换为内部component可以识别的消息,在消息输出到outbound以及outbound消息返回时,都可以指定相应的transformer。
3)component:处理消息的组件,可以通过class来指定,也可以直接使用spring的bean。
?
如果一个类中有多个处理函数,且入参相同,则需要通过如下方式指定:
<component><method-entry-point-resolver> <include-entry-point method="localService"/> </method-entry-point-resolver> <singleton-object /></component>
?
?
?
4)outbound-endpoint:消息输出端口,投递到外部服务组件进行处理。
?
直接访问外部页面时:
?
<http:outbound-endpoint address="http://localhost:8080/cake/hello.jsp" exchange-pattern="request-response"/>
?
?
5)另外,中间还可以增加router,根据不同的消息进行路由,此处使用choice实现;filter(对输入消息根据特定规则进行过滤等。
6)最终通过异常捕获分支来获取mule端处理过程中发生的异常等。
7)消息提取:
这里可以使用mule自定义语言MEL,从message中获取所要的属性或者负载等信息。
?
<message-properties-transformer><add-message-property key="GUID" value="#[string:#[xpath:/msg/header/ID]-#[xpath:/msg/body/@ref]]"/></message-properties-transformer>
?
?
9)通过mule-ref嵌套子flow处理流。具体config文件可以使用的标签或属性需要查看对应的名字空间的支持。
?
mule.properties
自定义属性文件,
如果需要在外部文件中额外单独配置部分属性,则可以通过如下方式导入文件:
对应的名字空间为:
注意这里classpath对应的是WEB_INF/classes目录,其后的相对目录就是相对于WEB_INF/classes。
Properties文件中都是键值对,形式如下:
导入以后,可以通过${key_name}来导入对应的值。
?
Registry-bootstrap.properties
?
配置启动时加载的组件。
?
?
Log4j.porperties
?
配置日志记录的相关信息。
?
使用log4j进行日志记录,此时需要配置log4j.properties来指定不同的输出,可以是控制台,文件等;也可以指定记录日志的级别,包括INFO, DEBUG, TRACE, ERROR, WARN。该配置文件放到所创建工程的根目录下,与mule-config.xml同级,从而可以通过在启动时调用PropertyConfigurator.configure( "log4j.properties")来初始化所使用的配置文件。
?
默认使用的配置文件为%MULE_HOME%/conf/log4j-properties。同时在mule-config.xml中<logger>记录日志的位置与log4j一致。
?
另外,wrapper.conf中也可以配置默认的日志级别。
?
?
?
Wrapper.conf
?
配置系统mule的系统属性,当mule作为webapp服务器来使用时,则会使用到这些属性。
?
?
此外针对不同的transport可以定义自己的配置文件,如jetty,cxf等。
?
其他:
?
如果想要存储执行时的数据,从而可以在多个应用使用,则可以将数据作为一个对象存储到Registry中,其他地方则可以通过MuleContext来访问该Registry中的数据。
?
<!--EndFragment-->?
?
关于更多的配置使用,请参考所使用的transport以及mule手册。