您现在访问的是微软AZURE全球版技术文档网站,若需要访问由世纪互联运营的MICROSOFT AZURE中国区技术文档网站,请访问 https://docs.azure.cn.

在 Azure 逻辑应用中发送、接收和批处理消息Send, receive, and batch process messages in Azure Logic Apps

若要以特定方式将消息以组的形式一起发送和处理,可以创建一个批处理解决方案。To send and process messages together in a specific way as groups, you can create a batching solution. 此解决方案将消息收集到一个 批处理 中,并等待直到满足指定的条件,然后再释放并处理批处理消息。This solution collects messages into a batch and waits until your specified criteria are met before releasing and processing the batched messages. 批处理可以减少逻辑应用处理消息的频率。Batching can reduce how often your logic app processes messages.

本文介绍如何通过在同一 Azure 订阅和 Azure 区域中创建两个逻辑应用来构建批处理解决方案,如下所示:This article shows how to build a batching solution by creating two logic apps within the same Azure subscription, Azure region, and in this order:

  1. “批接收方”逻辑应用:接受消息并将其收集到批中,直到满足发布和处理这些消息的指定条件。The "batch receiver" logic app, which accepts and collects messages into a batch until your specified criteria is met for releasing and processing those messages. 请确保首先创建此批处理接收器,以便以后在创建批处理发送方时选择该批目标。Make sure that you first create this batch receiver so that you can later select the batch destination when you create the batch sender.

  2. 一个或多个“批发送方”逻辑应用:将消息发送到前面创建的批接收方。One or more "batch sender" logic apps, which send the messages to the previously created batch receiver.

    还可以指定唯一键(如客户编号),以基于该键将目标批分区或分割为逻辑子集。You can also specify a unique key, such as a customer number, that partitions or divides the target batch into logical subsets based on that key. 这样,接收方应用便可以收集具有相同键的所有项,并统一处理它们。That way, the receiver app can collect all items with the same key and process them together.

Batch 接收方和批处理发送方需要共享同一 Azure 订阅 azure 区域。Your batch receiver and batch sender needs to share the same Azure subscription and Azure region. 否则,在创建批发送方时无法选择批接收方,因为它们相互不可见。If they don't, you can't select the batch receiver when you create the batch sender because they're not visible to each other.

先决条件Prerequisites

创建批接收方Create batch receiver

在将消息发送到某个批之前,该批必须存在且充当这些消息要发送到的目标。Before you can send messages to a batch, that batch must first exist as the destination where you send those messages. 因此,首先必须创建可以通过“批处理”触发器启动的“批接收方”逻辑应用。So first, you must create the "batch receiver" logic app, which starts with the Batch trigger. 这样,在创建“批发送方”逻辑应用时,便可以选择批接收方逻辑应用。That way, when you create the "batch sender" logic app, you can select the batch receiver logic app. 批接收方会持续收集消息,直到满足发布和处理这些消息的指定条件。The batch receiver continues collecting messages until your specified criteria is met for releasing and processing those messages. 尽管批接收方不需要知道有关批发送方的任何信息,但批发送方必须知道要将消息发送到的目标。While batch receivers don't need to know anything about batch senders, batch senders must know the destination where they send the messages.

  1. Azure 门户 或 Visual Studio 中,创建一个具有此名称的逻辑应用: BatchReceiverIn the Azure portal or Visual Studio, create a logic app with this name: BatchReceiver

  2. 在逻辑应用设计器中,添加 批处理 触发器,该触发器启动逻辑应用工作流。In the Logic App Designer, add the Batch trigger, which starts your logic app workflow. 在搜索框中,输入 batch ,然后选择 "此触发器:批处理消息"In the search box, enter batch, and select this trigger: Batch messages

    添加“批处理消息”触发器

  3. 为批接收方设置以下属性:Set these properties for the batch receiver:

    属性Property 说明Description
    批处理模式Batch Mode - 内联:用于在批处理触发器中定义发布条件- Inline: For defining release criteria inside the batch trigger
    - 集成帐户:用于通过集成帐户定义多个发布条件配置。- Integration Account: For defining multiple release criteria configurations through an integration account. 使用集成帐户可在一个位置而不是独立的逻辑应用中维护这些配置。With an integration account, you can maintain these configurations all in one place rather than in separate logic apps.
    批名称Batch Name 批的名称(在本示例中为“TestBatch”),仅适用于“内联”批处理模式The name for your batch, which is "TestBatch" in this example, and applies only to Inline batch mode
    发布条件Release Criteria 仅适用于“内联”批处理模式,选择在处理每个批之前所要满足的条件:Applies only to Inline batch mode and selects the criteria to meet before processing each batch:

    - 基于消息计数:根据批收集的消息数发布批。- Message count based: Release the batch based on the number of messages collected by the batch.
    - 基于大小:根据批收集的所有消息的总大小(以字节为单位)发布批。- Size based: Release the batch based on the total size in bytes for all messages collected by that batch.
    - 计划:根据指定间隔和频率的定期计划发布批。- Schedule: Release the batch based on a recurrence schedule, which specifies an interval and frequency. 在高级选项中,还可以选择时区,并提供开始日期和时间。In the advanced options, you can also select a time zone and provide a start date and time.
    - 全选:使用所有指定的条件。- Select all: Use all the specified criteria.

    消息计数Message Count 要在批中收集的消息数,例如 10 条消息。The number of messages to collect in the batch, for example, 10 messages. 批的限制为 8,000 条消息。A batch's limit is 8,000 messages.
    批大小Batch Size 批中要收集的总大小(以字节为单位),例如10 MB。The total size in bytes to collect in the batch, for example, 10 MB. 批的大小上限是 80 MB。A batch's size limit is 80 MB.
    计划Schedule 发布批的间隔时间和频率,例如 10 分钟。The interval and frequency between batch releases, for example, 10 minutes. 最小重复周期为 60 秒或 1 分钟。The minimum recurrence is 60 seconds or 1 minute. 分数分钟有效地向上舍入到 1 分钟。Fractional minutes are effectively rounded up to 1 minute. 若要指定时区或开始日期和时间,请打开 " 添加新参数 " 列表,然后选择相应的属性。To specify a time zone or a start date and time, open the Add new parameter list, and select the corresponding properties.

    备注

    如果在触发器仍具有已批处理但未发送的消息时更改发布条件,则触发器将使用更新的发布条件来处理未发送的消息。If you change the release criteria while the trigger still has batched but unsent messages, the trigger uses the updated release criteria for handling the unsent messages.

    此示例显示了所有条件,但是对于自己的测试,请仅尝试一个条件:This example shows all the criteria, but for your own testing, try just one criterion:

    提供批触发器详细信息

  4. 现在,添加一个或多个操作用于处理每个批。Now add one or more actions that process each batch.

    本示例添加一个可在批处理触发器激发时发送电子邮件的操作。For this example, add an action that sends an email when the batch trigger fires. 当批包含 10 个消息、达到 10 MB 或者在 10 分钟过后,该触发器将运行并发送电子邮件。The trigger runs and sends an email when the batch either has 10 messages, reaches 10 MB, or after 10 minutes pass.

    1. 在批处理触发器下,选择 " 新建步骤"。Under the batch trigger, select New step.

    2. 在搜索框中,输入 send email 作为筛选器。In the search box, enter send email as your filter. 基于电子邮件提供商,选择电子邮件连接器。Based on your email provider, select an email connector.

      例如,如果有工作或学校帐户(例如或),请 @fabrikam.com @fabrikam.onmicrosoft.com 选择 " Microsoft 365 Outlook 连接器"。For example, if you have a work or school account, such as @fabrikam.com or @fabrikam.onmicrosoft.com, select the Microsoft 365 Outlook connector. 如果有个人帐户(如 @outlook.com 或 @hotmail.com ),请选择 Outlook.com 连接器。If you have a personal account, such as @outlook.com or @hotmail.com, select the Outlook.com connector. 此示例使用 Microsoft 365 Outlook 连接器。This example uses the Microsoft 365 Outlook connector.

    3. 为您的提供商选择 "发送电子邮件" 操作,例如:Select the "send an email" action for your provider, for example:

      为电子邮件提供商选择“发送电子邮件”操作

  5. 根据提示登录到电子邮件帐户。If prompted, sign in to your email account.

  6. 为添加的操作设置属性。Set the properties for the action you added.

    • 在“收件人”框中,输入收件人的电子邮件地址。In the To box, enter the recipient's email address. 为进行测试,可以使用自己的电子邮件地址。For testing purposes, you can use your own email address.

    • 在 " 主题 " 框中,当 "动态内容" 列表出现时,选择 " 分区名称 " 字段。In the Subject box, when the dynamic content list appears, select the Partition Name field.

      从“动态内容”列表中选择“分区名称”

      稍后,在批发送方中,可以指定唯一分区键,将目标批划分为可以在其中发送消息的逻辑子集。Later, in the batch sender, you can specify a unique partition key that divides the target batch into logical subsets where you can send messages. 每个集都具有批发送方逻辑应用生成的唯一编号。Each set has a unique number that's generated by the batch sender logic app. 此功能使你可以使用具有多个子集的单个批,并且使用你提供的名称定义每个子集。This capability lets you use a single batch with multiple subsets and define each subset with the name that you provide.

      重要

      分区的限制为 5,000 条消息或 80 MB。A partition has a limit of 5,000 messages or 80 MB. 如果满足任一条件,则逻辑应用可以发布批,即使不满足定义的发布条件。If either condition is met, Logic Apps might release the batch, even when your defined release condition isn't met.

    • 在 " 正文 " 框中,当 "动态内容" 列表出现时,选择 " 消息 Id " 字段。In the Body box, when the dynamic content list appears, select the Message Id field.

      逻辑应用设计器会自动为 "发送电子邮件" 操作添加一个 for each 循环,因为该操作将上一操作的输出视为集合,而不是批处理。The Logic App Designer automatically adds a For each loop around the send email action because that action treats the output from the previous action as a collection, rather than a batch.

      对于“正文”,选择“消息 ID”

  7. 保存逻辑应用。Save your logic app. 现已创建批接收方。You've now created a batch receiver.

    保存逻辑应用

    重要

    如果你使用的是 Visual Studio,则在继续下一部分之前,请确保先将 batch 接收器逻辑应用部署到 AzureIf you're using Visual Studio, before you continue to the next section, make sure that you first deploy your batch receiver logic app to Azure. 否则,在创建批发送方时无法选择批接收方。Otherwise, you can't select the batch receiver when you create the batch sender.

创建批发送方Create batch sender

现在创建一个或多个批发送方逻辑应用,用于将消息发送到批接收方逻辑应用。Now create one or more batch sender logic apps that send messages to the batch receiver logic app. 在每个批发送方中,指定批接收方和批名称、消息内容及其他任何设置。In each batch sender, you specify the batch receiver and batch name, message content, and any other settings. 可以选择性地提供唯一分区键,以将批分割为子集,从而收集具有该键的消息。You can optionally provide a unique partition key to divide the batch into logical subsets for collecting messages with that key.

  • 请确保以前 创建并部署了 batch 接收器 ,以便在创建批处理发送方时,可以选择现有批处理接收器作为目标批。Make sure that you previously created and deployed your batch receiver so when you create your batch sender, you can select the existing batch receiver as the destination batch. 尽管批接收方不需要知道有关批发送方的任何信息,但批发送方必须知道要将消息发送到何处。While batch receivers don't need to know anything about batch senders, batch senders must know where to send messages.

  • 请确保 batch 接收方和批处理发送方共享同一 Azure 区域 azure 订阅。Make sure that your batch receiver and batch sender share the same Azure region and Azure subscription. 否则,在创建批发送方时无法选择批接收方,因为它们相互不可见。If they don't, you can't select the batch receiver when you create the batch sender because they're not visible to each other.

  1. 创建具有此名称的另一个逻辑应用: BatchSenderCreate another logic app with this name: BatchSender

    1. 在搜索框中,输入 recurrence 作为筛选器。In the search box, enter recurrence as your filter. 从触发器列表中选择此触发器:定期From the triggers list, select this trigger: Recurrence

      添加重复触发器

    2. 设置每分钟运行发送方逻辑应用的间隔和频率。Set the interval and frequency to run the sender logic app every minute.

      设置定期触发器的频率和间隔

  2. 添加用于将消息发送到批的新操作。Add a new action for sending messages to a batch.

    1. 定期 触发器下,选择 " 新建步骤"。Under the Recurrence trigger, select New step.

    2. 在搜索框中,输入 batch 作为筛选器,然后选择此操作: 使用批处理触发器选择逻辑应用工作流In the search box, enter batch as your filter, and select this action: Choose a Logic Apps workflow with batch trigger

      选择“选择具有批处理触发器的逻辑应用工作流”

      此时将显示一个列表,其中仅显示具有批处理触发器并与 batch 发送方逻辑应用位于同一 Azure 区域 Azure 订阅中的逻辑应用。A list appears and shows only those logic apps that have batch triggers and exist in the same Azure region and Azure subscription as your batch sender logic app.

    3. 从 "逻辑应用" 列表中,选择之前创建的批处理接收方逻辑应用。From the logic apps list, select the batch receiver logic app that you previously created.

      选择 batch 接收方逻辑应用

      重要

      如果你使用的是 Visual Studio,但未看到任何要选择的批处理接收器,请检查你以前创建的批处理接收器并将其部署到 Azure。If you're using Visual Studio, and you don't see any batch receivers to select, check that you previously created and deployed your batch receiver to Azure. 如果尚未这样做,请学习 如何将 batch 接收器逻辑应用部署到 AzureIf you haven't, learn how to deploy your batch receiver logic app to Azure.

    4. 从 "操作" 列表中,选择此操作: Batch_messages your-logic-app-name > -<From the actions list, select this action: Batch_messages - <your-logic-app-name>

      选择以下操作:“Batch_messages - ”

  3. 设置批发送方的属性:Set the batch sender's properties:

    属性Property 说明Description
    批名称Batch Name 接收方逻辑应用定义的批名称, TestBatch 此示例中为The batch name defined by the receiver logic app, which is TestBatch in this example

    重要说明:批名称在运行时将接受验证,必须与接收方逻辑应用指定的名称相匹配。Important: The batch name gets validated at runtime and must match the name specified by the receiver logic app. 更改批名称会导致批发送方失败。Changing the batch name causes the batch sender to fail.

    消息内容Message Content 要发送的消息内容The content for the message you want to send

    备注

    触发器名称工作流属性值将自动从所选逻辑应用填充。The Trigger Name and Workflow property values are automatically populated from your selected logic app.

    本示例将添加以下表达式,该表达式在要发送到批的消息内容中插入当前日期和时间:For this example, add this expression, which inserts the current date and time into the message content that you send to the batch:

    1. 在“消息内容”框中单击。Click inside the Message Content box.

    2. 显示动态内容列表时,选择 " 表达式"。When the dynamic content list appears, select Expression.

    3. 输入表达式 utcnow() ,然后选择 "确定"Enter the expression utcnow(), and select OK.

      在 "消息内容" 中,选择 "表达式",输入 "utcnow ( # A1",然后选择 "确定"。

  4. 现在为批设置分区。Now set up a partition for the batch. BatchReceiver 操作中,打开 " 添加新参数 " 列表,然后选择以下属性:In the BatchReceiver action, open the Add new parameter list, and select these properties:

    属性Property 说明Description
    分区名称Partition Name 可选的唯一分区键,用于将目标批分割为逻辑子集,并基于该键收集消息An optional unique partition key to use for dividing the target batch into logical subsets and collect messages based on that key
    消息 IDMessage Id 可选的消息标识符,为空时是系统生成的全局唯一标识符 (GUID)An optional message identifier that is a generated globally unique identifier (GUID) when empty

    对于本示例,请在“分区名称”框中添加一个表达式,用于生成介于 1 和 5 之间的随机数。For this example, in the Partition Name box, add an expression that generates a random number between one and five. 将“消息 ID”框留空。Leave the Message Id box empty.

    1. 在 " 分区名称 " 框中单击,以便显示 "动态内容" 列表。Click inside the Partition Name box so that the dynamic content list appears.

    2. 在动态内容列表中,选择“表达式”。In the dynamic content list, select Expression.

    3. 输入表达式 rand(1,6) ,然后选择 "确定"Enter the expression rand(1,6), and then select OK.

      为目标批设置分区

      rand 函数生成介于一与五之间的数字。This rand function generates a number between one and five. 因此会将此批划分为五个带编号的分区(由此表达式动态设置)。So you are dividing this batch into five numbered partitions, which this expression dynamically sets.

  5. 保存逻辑应用。Save your logic app. 发送方逻辑应用现在类似于此示例:Your sender logic app now looks similar to this example:

    保存发送方逻辑应用

测试逻辑应用Test your logic apps

若要测试批处理解决方案,请让逻辑应用运行几分钟。To test your batching solution, leave your logic apps running for a few minutes. 很快,你会开始收到五封一组的电子邮件,它们具有相同的分区键。Soon, you start getting emails in groups of five, all with the same partition key.

批发送方逻辑应用每分钟运行一次,生成介于 1 和 5 之间的随机数,并使用此生成的数字作为向其发送消息的目标批的分区键。Your batch sender logic app runs every minute, generates a random number between one and five, and uses this generated number as the partition key for the target batch where messages are sent. 每当批中包含具有相同分区键的 5 个项时,批接收方逻辑应用都会激发并针对每个消息发送邮件。Each time the batch has five items with the same partition key, your batch receiver logic app fires and sends mail for each message.

重要

完成测试后,请确保禁用 BatchSender 逻辑应用以停止发送消息,并避免重载收件箱。When you're done testing, make sure that you disable the BatchSender logic app to stop sending messages and avoid overloading your inbox.

后续步骤Next steps