在 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 that collects messages into a batch until your specified criteria are met for 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 following this specific order:

  • “批接收方”逻辑应用:接受消息并将其收集到批中,直到满足发布和处理这些消息的指定条件。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 you first create the batch receiver so you can later select the batch destination when you create the batch sender.

  • 一个或多个“批发送方”逻辑应用:将消息发送到前面创建的批接收方。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.

请确保批接收方和批发送方共享同一个 Azure 订阅和 Azure 区域。Make sure your batch receiver and batch sender 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 中,创建具有以下名称的一个逻辑应用:“BatchReceiver”In the Azure portal or Visual Studio, create a logic app with this name: "BatchReceiver"

  2. 在逻辑应用设计器中,添加触发器,这会启动逻辑应用工作流。In Logic Apps Designer, add the Batch trigger, which starts your logic app workflow. 在搜索框中,输入“批”作为筛选器。In the search box, enter "batch" as your filter. 选择此触发器:批处理消息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, choose Show advanced options.

    备注

    如果在触发器仍具有已批处理但未发送的消息时更改发布条件,则触发器将使用更新的发布条件来处理未发送的消息。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, choose New step.

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

      例如,如果你有个人帐户(如 @outlook.com 或 @hotmail.com),请选择 Outlook.com 连接器。For example, if you have a personal account, such as @outlook.com or @hotmail.com, select the Outlook.com connector. 此示例使用 Office 365 Outlook 连接器。This example uses the Office 365 Outlook connector.

    3. 选择以下操作:发送电子邮件 - <电子邮件提供程序>Select this action: Send an email - <email 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 Apps 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.

    保存逻辑应用

  8. 如果使用 Visual Studio,请确保将批接收方逻辑应用部署到 AzureIf you're using Visual Studio, make sure you 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.

  • 确保已创建批接收方,以便在创建批发送方时,可以选择现有的批接收方作为目标批。Make sure you've already created 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.

  • 请确保批接收方和批发送方共享同一个 Azure 区域和 Azure 订阅。Make sure 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. 创建具有以下名称的另一个逻辑应用:“BatchSender”Create another logic app with this name: "BatchSender"

    1. 在搜索框中,输入“定期”作为筛选器。In the search box, enter "recurrence" as your filter. 选择此触发器:定期 - 计划Select this trigger: Recurrence - Schedule

      添加“定期 - 计划”触发器

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

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

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

    1. 在定期触发器下,选择“新建步骤”。Under the recurrence trigger, choose New step.

    2. 在搜索框中,输入“批”作为筛选器。In the search box, enter "batch" as your filter. 选择“操作”列表,然后选择以下操作:选择具有批触发器的逻辑应用工作流 - 将消息发送到批Select the Actions list, and then select this action: Choose a Logic Apps workflow with batch trigger - Send messages to batch

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

    3. 选择前面创建的批接收方逻辑应用。Select your batch receiver logic app that you previously created.

      选择“批接收方”逻辑应用

      备注

      此列表还显示具有批触发器的任何其他逻辑应用。The list also shows any other logic apps that have batch triggers.

      如果使用 Visual Studio 中并且看不到任何可供选择的批接收方,请检查是否已将批接收方部署到 Azure。If you're using Visual Studio, and you don't see any batch receivers to select, check that you deployed your batch receiver to Azure. 如果未部署,请了解如何将批接收方逻辑应用部署到 AzureIf you haven't, learn how to deploy your batch receiver logic app to Azure.

    4. 选择以下操作:Batch_messages - <your-batch-receiver>Select this action: Batch_messages - <your-batch-receiver>

      选择以下操作:“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

    本示例将添加以下表达式,该表达式在发送到批的消息内容中插入当前日期和时间: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, choose Expression.

    3. 输入表达式 utcnow(),然后选择“确定”。Enter the expression utcnow(), and then choose OK.

      在“消息内容”中选择“表达式”,输入“utcnow()”,然后选择“确定”。

  4. 现在为批设置分区。Now set up a partition for the batch. 在“BatchReceiver”操作中,选择“显示高级选项”并设置以下属性:In the "BatchReceiver" action, choose Show advanced options and set 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, choose Expression.

    3. 输入表达式 rand(1,6),然后选择“确定”。Enter the expression rand(1,6), and then choose 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