---
title: "Mule ESB浅析5——基本配置"
author: "cake918"
date: "2013-01-20T15:33:41Z"
tags: []
categories: ["开源软件"]
slug: "mule-esb-5"
draft: false
lastmod: "2013-01-20T15:34:10Z"
id: 1772734
url: "https://cake918.iteye.com/blog/1772734"
---

```
主配置文件：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.xsd
		http://www.mulesoft.org/schema/mule/jms schema/mule/jms/mule-jms.xsd
		http://www.mulesoft.org/schema/mule/jdbc schema/mule/jdbc/mule-jdbc.xsd
		http://www.mulesoft.org/schema/mule/smtp schema/mule/smtp/mule-smtp.xsd
		http://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.xsd
		http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.1.xsd">

	<description>
		This configuration uses an HTTP endpoint to receive
		requests.
	</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 class="cake.mule.server.muleServlet" />
</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中获取所要的属性或者负载等信息。

 

&lt;!--\[if !supportLists]--&gt;1，  &lt;!--\[endif]--&gt;#\[payload]可以提取消息的内容。默认调用函数的返回值会设置为muleMessage的payload，包括组件调用，转换器调用等。如果返回值的类型为muleMessage，则不会设置该payload。代码实现是可以通过setPayload来修改payload，这里可能被mule重新修改，必须保证函数的返回类型为muleMessage。注意，这里设置的payload的类型不可为简单类型，否则在处理函数查找时，无法识别。

&lt;!--\[if !supportLists]--&gt;2，  &lt;!--\[endif]--&gt;在filter中，通过不同的evaluate，可以采用不同的方法提取相关的字段。建议使用groovy来进行处理。

&lt;!--\[if !supportLists]--&gt;3，  &lt;!--\[endif]--&gt;使用mule提供的表达式，主要在filter中使用，包括，

#\[mule:message.headers(foo, bar)] - retrieves two headers foo and bar and returns a Map

#\[mule:message.attachments-list(attach1, attach2\*)] - retrieves two named attachments in a List. The asterisk on attach2 indicates that it is optional

#\[mule:message.headers(all)] - retrieves all headers and returns as a Map

#\[mule:message.payload(org.dom4j.Document)] - returns the payload and converts it to an org.dom4j.Document

#\[mule:message.correlationId] - returns the correlationId on the message

#\[mule:message.map-payload(foo)] - expects a Map payload object and retrieves the property foo from the map

#\[mule:context \*\*] - return information about the server itself，include serviceName，modelNmae，inboundEndpoint，serverId，clusterId，domainId, workingDir, homeDir.

#\[mule:registry \*\*] - return objects you have written to the registry

 

8）属性修改，添加等：

 

```
<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**

自定义属性文件，

如果需要在外部文件中额外单独配置部分属性，则可以通过如下方式导入文件：

    &lt;spring:beans&gt;

        &lt;context:property-placeholder

        location="classpath:../properties/mule.properties" /&gt;

    &lt;/spring:beans&gt;

对应的名字空间为：

http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd

注意这里classpath对应的是WEB\_INF/classes目录，其后的相对目录就是相对于WEB\_INF/classes。

Properties文件中都是键值对，形式如下：

mail\_domain = localhost

mail\_port = 8084

导入以后，可以通过${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中&lt;logger&gt;记录日志的位置与log4j一致。

 

另外，wrapper.conf中也可以配置默认的日志级别。

 

 

 

**Wrapper.conf**

 

配置系统mule的系统属性，当mule作为webapp服务器来使用时，则会使用到这些属性。

 

 

此外针对不同的transport可以定义自己的配置文件，如jetty，cxf等。

 

其他：

 

如果想要存储执行时的数据，从而可以在多个应用使用，则可以将数据作为一个对象存储到Registry中，其他地方则可以通过MuleContext来访问该Registry中的数据。

 

&lt;!--EndFragment--&gt;

 

 

关于更多的配置使用，请参考所使用的transport以及mule手册。