Mulesoft 简明教程
MuleSoft - Core Components & Configuration
Mule 最重要的能力之一是它可以使用组件执行路由、转换和处理,因此组合各种元素的 Mule 应用程序配置文件非常大。
One of the most important abilities of Mule is that it can perform routing, transforming, and processing with the components, because of which the configuration file of Mule application that combines various elements is very large in size.
Mule 提供的配置模式类型如下:
Following are the types of configuration patterns provided by Mule −
-
Simple service pattern
-
Bridge
-
Validator
-
HTTP proxy
-
WS proxy
Configuring the component
在 Anypoint 工作室中,我们可以按照以下步骤配置组件:
In Anypoint studio, we can follow the below steps to configure a component −
Step 1
Step 1
我们需要将希望在 Mule 应用程序中使用的组件拖动过来。例如,在此处,我们将 HTTP 侦听器组件用作如下所示:
We need to drag the component we wish to use in our Mule application. For example, here we use HTTP listener component as follows −
Step 2
Step 2
接下来,双击该组件以获取配置窗口。对于 HTTP 侦听器,如下所示:
Next, double click on the component to get the configuration window. For HTTP listener, it is shown below −
Step 3
Step 3
我们可以根据项目的需要配置组件。例如,我们针对 HTTP 侦听器组件所执行的操作:
We can configure the component as per the requirement of our project. Say for example, we did for HTTP listener component −
核心组件是 Mule 应用程序中工作流程的一个重要组成部分。处理 Mule 事件的逻辑由这些核心组件提供。在 Anypoint 工作室中,要访问这些核心组件,可以从 Mule 调色板中单击“核心”,如下所示:
Core components are one of the important building blocks of work flow in the Mule app. The logic for processing a Mule event is provided by these core components. In Anypoint studio, to access these core components, you can click on the Core from Mule Palette as shown below −
以下是各种 core components and their working in Mule 4 :
Following are various core components and their working in Mule 4 −
Custom Business Events
此核心组件用于收集有关 Mule 应用程序中处理业务交易的流以及消息处理程序的信息。换句话说,我们可以使用 Custom Business Event 组件在我们的工作流中添加以下内容:
This core component is used for the collection of information about flows as well as message processors that handle the business transactions in Mule app. In other words, we can use Custom Business Event component to add the following in our working flow −
-
Metadata
-
Key performance Indicators (KPIs)
How to add KPIs?
以下是为 Mule 应用程序中的流程添加关键绩效指标的步骤:
Following are the steps to add KPIs in our flow in Mule app −
Step 1 - 按照 Mule Palette → Core → Components → Custom Business Event 添加 Custom Business Event 组件到 Mule 应用程序中的工作流中。
Step 1 − Follow Mule Palette → Core → Components → Custom Business Event, to add Custom Business Event component to a working flow in your Mule app.
Step 2 - 单击组件以将其打开。
Step 2 − Click on the component to open it.
Step 3 - 现在,我们需要为显示名称和事件名称提供值。
Step 3 − Now, we need to provide values for Display Name and Event Name.
Step 4 - 要从消息有效负载中捕获信息,请添加关键绩效指标,如下所示:
Step 4 − To capture information from the message payload, add KPIs as follows −
-
Give a name (key) for the KPI (tracking: meta-data element) and a value. The name will be used in search interface of Runtime Manager.
-
Give a value that may be any Mule expression.
Dynamic Evaluate
此核心组件用于在 Mule 应用程序中动态选择脚本。我们还可以通过转换消息组件使用硬核脚本,但使用 Dynamic Evaluate 组件是一个更好的方法。此核心组件的工作方式如下 −
This core component is used for dynamically selecting a script in Mule app. We can also use hardcore script through the Transform Message Component but using Dynamic Evaluate component is a better way. This core component works as follows −
-
Firstly, it evaluates an expression that should result in another script.
-
Then it evaluates that script for the final result.
通过这种方式,它允许我们动态选择脚本,而不是对其进行硬编码。
In this way, it allows us to dynamically select the script rather than hardcoding it.
Example
以下是一个通过 ID 查询参数从数据库中选择脚本并将其存储在名为 MyScript 的变量中的示例。现在,动态评估组件将访问变量以调用脚本,以便它可以从 UName 查询参数中添加一个 name 变量。
Following is an example of selecting a script from database through an Id query parameter and storing that script in a variable named MyScript. Now, the dynamic-evaluate component will access the variable to invoke the scripts so that it can add a name variable from UName query parameter.
流程的 XML 配置如下所示 −
The XML configuration of the flow is given below −
<flow name = "DynamicE-example-flow">
<http:listener config-ref = "HTTP_Listener_Configuration" path = "/"/>
<db:select config-ref = "dbConfig" target = "myScript">
<db:sql>#["SELECT script FROM SCRIPTS WHERE ID =
$(attributes.queryParams.Id)"]
</db:sql>
</db:select>
<ee:dynamic-evaluate expression = "#[vars.myScript]">
<ee:parameters>#[{name: attributes.queryParams.UName}]</ee:parameters>
</ee:dynamic-evaluate>
</flow>
脚本可以使用消息、负载、变量或属性之类的上下文变量。但是,如果你想要添加自定义上下文变量,你需要提供一组键值对。
The script can use context variables like message, payload, vars or attributes. However, if you want to add custom context variable, you need to provide a set of key-value pairs.
Configuring Dynamic Evaluate
以下表格提供了配置 Dynamic Evaluate 组件的方法 −
Following table provides a way to configure Dynamic Evaluate component −
Field |
Value |
Description |
Example |
Expression |
DataWeave expression |
It specifies the expression to be evaluated into the final script. |
expression="#[vars.generateOrderScript]" |
Parameters |
DataWeave expression |
It specifies key-value pairs. |
#[{joiner: ' and ', id: payload.user.id}] |
Flow Reference Component
如果你想要在同一 Mule 应用程序中将 Mule 事件路由到另一个流或子流,那么流引用组件是正确的选择。
If you want to route the Mule event to another flow or sub-flow and back within the same Mule app, then flow reference component is the right option.
Characteristics
以下是此核心组件的特征 −
Following are the characteristics of this core component −
-
This core component allows us to treat the whole referenced flow like a single component in the current flow.
-
It breaks the Mule application into discrete and reusable units. For example, a flow is listing files on regular basis. It might reference another flow that processes the output of the list operation.
-
In this way, rather than appending the whole processing steps, we can append Flow References that points to the processing flow. The screenshot below shows that the Flow Reference Core Component is pointing towards a sub-flow named ProcessFiles.
Working
通过以下图表可以了解流引用组件的工作原理 −
The working of Flow Ref component can be understood with the help of following diagram −
该图表显示当一个流引用同一个应用程序中的另一个流时,Mule 应用程序中的处理顺序。当 Mule 应用程序中的主工作流被触发时,Mule 事件会一直传递并执行流,直到 Mule 事件到达流引用。
The diagram shows the processing order in Mule application when one flow references another flow in the same application. When the main working flow in Mule application triggered, the Mule event travels all through and executes the flow until the Mule event reaches Flow Reference.
在到达流引用后,Mule 事件从头到尾执行引用流。一旦 Mule 事件完成执行引用流,它将返回到主流。
After reaching Flow Reference, the Mule event executes the referenced flow from beginning to end. Once Mule event finishes executing the Ref Flow, it returns to the main flow.
Example
为了更好地理解, let us use this component in Anypoint Studio 。在此示例中,我们将 HTTP 侦听器用于 GET 消息,正如我们在上一章节中所做的一样。因此,我们可以拖放组件并进行配置。但对于此示例,我们需要添加子流组件并在其下方设置有效负载组件,如下所示 −
For better understanding, let us use this component in Anypoint Studio. In this example, we are taking HTTP listener to GET a message, as we did in the previous chapter. So, we can drag and drop the component and configure. But for this example, we need to add a Sub-flow component and set Payload component under that, as shown below −
接下来,我们需要通过双击配置 Set Payload 。此处我们给出的值“子流已执行”,如下所示 −
Next, we need to configure Set Payload, by double clicking on it. Here we are giving the value, “Sub flow executed” as shown below −
在成功配置子流组件后,我们需要将流引用组件设置在主流中设置有效负载之后,该组件可从 Mule Palette 中拖放,如下所示 −
Once successfully configuring the sub-flow component, we need the Flow Reference Component to set after Set Payload of main flow, which we can drag and drop from the Mule Palette as shown below −
接下来,在配置流引用组件时,我们需要在通用选项卡下选择流名称,如下所示 −
Next, while configuring the Flow Reference Component, we need to choose Flow Name under Generic tab as shown below −
现在,保存并运行此应用程序。要对此进行测试,请转至 POSTMAN,在 URL 栏中键入 http:/localhost:8181/FirstAPP ,然后您将收到消息“子流已执行”。
Now, save and run this application. To test this, go to POSTMAN and type http:/localhost:8181/FirstAPP in the URL bar, and you will get the message, Sub flow executed.
Logger Component
名为记录器的核心组件通过记录重要信息来帮助我们监控和调试 Mule 应用程序,例如错误消息、状态通知、有效负载等。在 AnyPoint 工作室中,它们出现在 Console 中。
The core component called logger helps us to monitor and debug our Mule application by logging important information like error messages, status notifications, payloads, etc. In AnyPoint studio, they appear in the Console.
Advantages
以下是记录器组件的一些优点:
Following are some advantages of Logger Component −
-
We can add this core component anywhere in the working flow.
-
We can configure it to log a string specified by us.
-
We can configure it to the output of a DataWeave expression written by us.
-
We can also configure it to any combination of strings and expressions.
Example
以下示例在浏览器中 Set Payload 中显示消息“Hello World”,并同时记录消息。
The example below displays the message “Hello World” in the Set Payload in a browser and logging the message also.
以下是上述示例中的流的 XML 配置 −
Following is the XML configuration of the flow in the above example −
<http:listener-config name = "HTTP_Listener_Configuration" host = "localhost" port = "8081"/>
<flow name = "mymuleprojectFlow">
<http:listener config-ref="HTTP_Listener_Configuration" path="/"/>
<set-payload value="Hello World"/>
<logger message = "#[payload]" level = "INFO"/>
</flow>
Transfer Message Component
转换消息组件(也称为传输组件)允许我们将输入数据转换为新的输出格式。
Transform Message Component, also called Transfer component allows us to convert the input data into a new output format.
Methods to build Transformation
我们可以借助以下两个方法构建转换 −
We can build our transformation with the help of the following two methods −
Drag-and-Drop Editor (Graphical View) − 这是构建转换的第一种且是最常用的方法。在此方法中,我们可以使用此组件的可视化映射器来拖放传入数据结构的元素。例如,在以下示意图中,两个树视图显示了输入和输出的预期元数据结构。连接输入到输出字段的线表示两个树视图之间的映射。
Drag-and-Drop Editor (Graphical View) − This is the first and most used method to build our transformation. In this method, we can use this component’s visual mapper to drag-and-drop the elements of the incoming data structure. For example, in the following diagram, two tree views show the expected metadata structures of the input and output. Lines that connect input to output field represents the mapping between two tree views.
Script View − 转换的可视化映射也可在 DataWeave(用于 Mule 代码的语言)的帮助下表示。我们可以对高级转换(例如聚合、规范化、分组、连接、分区、旋转和筛选)进行编码。示例如下 −
Script View − The visual mapping of Transformation can also be represented with the help of DataWeave, a language for Mule code. We can do coding for some advanced transformations like aggregation, normalization, grouping, joining, partitioning, pivoting and filtering. The example is given below −
此核心组件主要接受变量、属性或消息有效负载的输入和输出元数据。我们可以为以下内容提供特定于格式的资源 −
This core component basically accepts input and output metadata for a variable, an attribute or message payload. We can provide format-specific resources for the following −
-
CSV
-
Schema
-
Flat file Schema
-
JSON
-
Object class
-
Simple Type
-
XML Schema
-
Excel Column name and type
-
Fixed Width Column name and type