EnjoyingSoft之Mule ESB开发教程第六篇:Data Transform - 数据转换

本篇主要介绍在Mule ESB中使用数据转换。数据转换是ESB最核心的功能,它消除了异构应用之间的技术差异,让不同的应用服务协调运作,实现了不同服务之间的通讯。数据转换或者说消息转换,包括了数据结构,数据类型,数据内容的转换等。

作为开源ESB产品中很成熟的平台,Mule ESB内置很多的消息转换组件,比如Object to JSON,Object to XML,XML to JSON等,同时也支持使用自定义的Transformer来扩充自定义转换逻辑。

另外Mule ESB企业版更拥有图形化的消息转换组件DataWeave,可以很灵活的进行数据的转换,包括一对一,一对多,多对一的映射。我们在社区版上也研发了相应的扩展组件InfoMapper,做到类似DataWeave的强大功能。

Mule ESB起源于一个社区项目,经过十多年的发展,Mule ESB拥有众多的企业客户案例,2017年成功在纽交所上市。我们作为MuleSoft的重要合作伙伴,参与其中,使用Mule ESB企业版实施开发,或者Mule ESB社区版实施开发,帮助国内众多的行业领先客户成功上线Mule ESB集成项目。

Mule ESB的源代码托管在GitHub上,英文文档也非常丰富,Mule ESB的中文教程却非常少,我们使用8篇Mule ESB的开发教程来帮助大家迅速上手Mule ESB开发。

1. 数据转换概念

数据转换是ESB平台的核心模块,是解决异构系统通讯的必备组件。举个例子,订单系统暴露了一个Web Service,而我们的网上商城生成的订单是Json或者Flat file格式,将JSON数据或者Flat file文本格式转换成Web Service的XML格式,这个需求就可以通过ESB来实现。我们在第三篇讲解了Mule Message的结构,回忆一下Mule Message的结构。

我们通常说的数据转换就是对Mule Message中Payload做操作,改变数据类型或者数据结构,数据内容等。在做数据转换之前,我们需要确定数据的源类型和目标类型,然后选择合适的数据转换组件。

2. 数据智能感知 - DataSense

DataSense是Mule最强大的功能之一,Anypoint Studio会主动获取元数据类型和结构等信息,以帮助开发者在应用程序中准确的映射或者使用数据。

DataSense配合企业版的DataWeave可视化数据转化组件,或者我们自研的社区版可视化数据转化组件InfoMapper,能够很大的加速应用集成的开发,方便开发者完成数据转换和映射。本文后半段会介绍DataWeave和InfoMapper的强大之处。

DataSense举例说明,如果您的应用程序连接到一个天气预报的Web Service,那么DataSense会捕获Web Service使用的数据类型和结构的信息,这些信息对开发者非常有用,它会展示这个Web Service期望的的入参是什么,出参是什么。

天气预报WebService中规定的入参和出参如下图:

Anypoint Studio中的Web Service Consumer组件,会清楚的显示Expected Data,也就是期望的入参数据格式。如下图Input页签,可以看到需要输入theCityName的XML数据。

同时Web Service Consumer组件也会展示这个WebService的输出参数是什么。如下图Output页签。

3. 简单数据转换组件

Mule ESB内置了很多常用的简单的数据转换组件,从组件名称就可以看到组件的适用范围,源数据类型和目标数据类型。另外Mule ESB也支持自定义Transformer,通过Java或者其他脚本语言实现复杂的数据转换逻辑。

3.1 Object to JSON

Object to JSON组件能够将Java对象转换成JSON格式,方便其他语言比如C#,Python解析和使用。常见的使用场景就是将Map对象转换成JSON格式。

Mule UI Config:

Mule XML Config:

<?xml version="1.0" encoding="UTF-8"?>

<mule xmlns:json="http://www.mulesoft.org/schema/mule/json"
	xmlns:http="http://www.mulesoft.org/schema/mule/http" xmlns="http://www.mulesoft.org/schema/mule/core" xmlns:doc="http://www.mulesoft.org/schema/mule/documentation"
	xmlns:spring="http://www.springframework.org/schema/beans" 
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xsi:schemaLocation="
http://www.mulesoft.org/schema/mule/json http://www.mulesoft.org/schema/mule/json/current/mule-json.xsd http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-current.xsd
http://www.mulesoft.org/schema/mule/core http://www.mulesoft.org/schema/mule/core/current/mule.xsd
http://www.mulesoft.org/schema/mule/http http://www.mulesoft.org/schema/mule/http/current/mule-http.xsd">
    <http:listener-config name="HTTP_Listener_Configuration" host="0.0.0.0" port="8081" doc:name="HTTP Listener Configuration"/>
    <flow name="object-to-json-flow">
        <http:listener config-ref="HTTP_Listener_Configuration" path="/" doc:name="HTTP"/>
        <set-payload value="#[[&quot;orderNo&quot;:&quot;OST0001&quot;,&quot;orderAmount&quot;:1000]]" doc:name="Set Map Payload"/>
        <json:object-to-json-transformer doc:name="Object to JSON"/>
    </flow>
</mule>

使用Postman访问,就可以得到一个标准的JSON数据。

{
	"orderAmount": 1000,
	"orderNo": "OST0001"
}

3.2 JSON to XML

JSON转换成XML是很常用的转换操作。JSON和XML都是有层级关系的数据展示结构,JSON和XML具体的区别和使用场景请参考其他文章。这里需要强调是XML有一个唯一的根节点元素,而JSON根节点元素可能是一个对象,还可能是一个数组。使用Mule完成JSON to XML也很简单。

Mule UI Config:

Mule XML Config:

<?xml version="1.0" encoding="UTF-8"?>

<mule xmlns:json="http://www.mulesoft.org/schema/mule/json" xmlns:http="http://www.mulesoft.org/schema/mule/http" xmlns="http://www.mulesoft.org/schema/mule/core" xmlns:doc="http://www.mulesoft.org/schema/mule/documentation"
	xmlns:spring="http://www.springframework.org/schema/beans" 
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-current.xsd
http://www.mulesoft.org/schema/mule/core http://www.mulesoft.org/schema/mule/core/current/mule.xsd
http://www.mulesoft.org/schema/mule/http http://www.mulesoft.org/schema/mule/http/current/mule-http.xsd
http://www.mulesoft.org/schema/mule/json http://www.mulesoft.org/schema/mule/json/current/mule-json.xsd">
    <http:listener-config name="HTTP_Listener_Configuration" host="0.0.0.0" port="8081" doc:name="HTTP Listener Configuration"/>
    <flow name="json2xml-flow">
        <http:listener config-ref="HTTP_Listener_Configuration" path="/" doc:name="HTTP"/>
        <json:json-to-xml-transformer doc:name="JSON to XML"/>
    </flow>
</mule>

使用Postman提交Json,Json数据如下:

{
	"customer": {
		"cellPhone": "13912340002",
		"customerNo": "C0002",
		"customerName": "Alibaba"
	}
}

得到转换之后的结果如下:

<?xml version='1.0'?>
<customer>
    <cellPhone>13912340002</cellPhone>
    <customerNo>C0002</customerNo>
    <customerName>Alibaba</customerName>
</customer>

这里还是要再次强调一下,这个组件要求Json数据必须只有一个根节点。如果Json数据里有多个根节点,则数据会丢失。举例如下:

有多个根节点元素的Json数据:

{
	"cellPhone": "13912340002",
	"customerNo": "C0002",
	"customerName": "Alibaba"
}

转换后的XML结果如下,可以看到只有第一个根元素被转换成了XML。

<?xml version='1.0'?>
<cellPhone>13912340002</cellPhone>

3.3 JSON to Object

这个组件会将JSON数据转成成一个Mule内置的Json对象,org.mule.module.json.JsonData。

Mule UI Config:

Mule XML Config:

<?xml version="1.0" encoding="UTF-8"?>

<mule xmlns:json="http://www.mulesoft.org/schema/mule/json" xmlns:http="http://www.mulesoft.org/schema/mule/http" xmlns="http://www.mulesoft.org/schema/mule/core" xmlns:doc="http://www.mulesoft.org/schema/mule/documentation"
	xmlns:spring="http://www.springframework.org/schema/beans" 
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-current.xsd
http://www.mulesoft.org/schema/mule/core http://www.mulesoft.org/schema/mule/core/current/mule.xsd
http://www.mulesoft.org/schema/mule/http http://www.mulesoft.org/schema/mule/http/current/mule-http.xsd
http://www.mulesoft.org/schema/mule/json http://www.mulesoft.org/schema/mule/json/current/mule-json.xsd">
    <http:listener-config name="HTTP_Listener_Configuration" host="0.0.0.0" port="8081" doc:name="HTTP Listener Configuration"/>
    <flow name="json2object-flow">
        <http:listener config-ref="HTTP_Listener_Configuration" path="/" doc:name="HTTP"/>
        <json:json-to-object-transformer doc:name="JSON to Object"/>
        <logger message="#[message]" level="INFO" doc:name="Logger"/>
    </flow>
</mule>

通过打印的日志可以看到,转化后的payload是org.mule.module.json.JsonData。


org.mule.DefaultMuleMessage
{
  id=4b3b1290-b293-11e9-9351-005056c00001
  payload=org.mule.module.json.JsonData
  correlationId=<not set>
  correlationGroup=-1
  correlationSeq=-1
  encoding=UTF-8
  exceptionPayload=<not set>
}

3.4 XML to JSON

正如2.2章节所说,XML和Json的根节点元素存在差异。而XML的根节点元素是唯一的,所以XML to JSON比较简单。

Mule UI Config:

Mule XML Config:

<?xml version="1.0" encoding="UTF-8"?>

<mule xmlns:json="http://www.mulesoft.org/schema/mule/json" xmlns:http="http://www.mulesoft.org/schema/mule/http" xmlns="http://www.mulesoft.org/schema/mule/core" xmlns:doc="http://www.mulesoft.org/schema/mule/documentation"
	xmlns:spring="http://www.springframework.org/schema/beans" 
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-current.xsd
http://www.mulesoft.org/schema/mule/core http://www.mulesoft.org/schema/mule/core/current/mule.xsd
http://www.mulesoft.org/schema/mule/http http://www.mulesoft.org/schema/mule/http/current/mule-http.xsd
http://www.mulesoft.org/schema/mule/json http://www.mulesoft.org/schema/mule/json/current/mule-json.xsd">
    <http:listener-config name="HTTP_Listener_Configuration" host="0.0.0.0" port="8081" doc:name="HTTP Listener Configuration"/>
    <flow name="xml2json-flow">
        <http:listener config-ref="HTTP_Listener_Configuration" path="/" doc:name="HTTP"/>
        <json:xml-to-json-transformer doc:name="XML to JSON"/>
    </flow>
</mule>

使用Postman提交Xml,Xml数据如下:

<?xml version='1.0'?>
<customer>
    <cellPhone>13912340002</cellPhone>
    <customerNo>C0002</customerNo>
    <customerName>Alibaba</customerName>
</customer>

转化后的JSON数据如下,可以看到这里的Json是正确的。

{
    "customer": {
        "cellPhone": "13912340002",
        "customerNo": "C0002",
        "customerName": "Alibaba"
    }
}

4. 企业版的DataWeave Transformer(可视化高级数据转换器)

DataWeave是Mule企业版提供的可视化的数据换器,使用这个组件可以通过拖拽的操作完成数据的映射。从下图可以看到,DataWeave配合DataSense,通过拖拽连线的方式,可以将Http的请求数据转换成Web Service Consumer期待的Xml数据,非常好用方便。

5. 社区版的InfoMapper(自研的可视化数据转换器)

上文提到的DataWeave是Mule企业版才提供的功能,社区版并不提供该组件。我们也可以通过扩展组件的方式达到类似的功能,我们自研的可视化转换组件InfoMapper,其功能和DataWeave相当。

数据转换是ESB很常用的功能和使用场景,使用Anypoint Studio中提供的简单数据转换组件,或者企业版的DataWeave,社区版我们自研的InfoMapper这些高级转换组件,配合强大的DataSense,数据转换是非常轻松的事情。

本文同步发文于EnjoyingSoft BlogsCSDN简书

访问EnjoyingSoft 网站,获取更多Mule ESB 社区版 实施帮助。

欢迎转载,但必须保留原文和此段声明,且在文章页面明显位置给出原文链接,否则保留追究法律责任的权利。

posted @ 2019-08-07 21:55  EnjoyingSoft  阅读(3972)  评论(2编辑  收藏  举报