使用 Azure 逻辑应用和 Azure 服务总线在云中交换消息Exchange messages in the cloud by using Azure Logic Apps and Azure Service Bus

使用 Azure 逻辑应用Azure 服务总线连接器,可以创建用于传输数据的自动化任务和工作流,例如,在组织的应用程序之间移动销售和采购订单、日记帐以及库存。With Azure Logic Apps and the Azure Service Bus connector, you can create automated tasks and workflows that transfer data, such as sales and purchase orders, journals, and inventory movements across applications for your organization. 连接器不仅会监视、发送和管理消息,而且还能对队列、会话、主题、订阅等执行操作,例如:The connector not only monitors, sends, and manages messages, but also performs actions with queues, sessions, topics, subscriptions, and so on, for example:

  • 监视消息何时抵达(自动完成),或者在队列、主题和主题订阅中接收(扫视-锁定)。Monitor when messages arrive (auto-complete) or are received (peek-lock) in queues, topics, and topic subscriptions.
  • 发送消息。Send messages.
  • 创建和删除主题订阅。Create and delete topic subscriptions.
  • 管理队列和主题订阅中的消息,例如,获取、获取延期消息、完成、延期、丢弃和加入死信。Manage messages in queues and topic subscriptions, for example, get, get deferred, complete, defer, abandon, and dead-letter.
  • 续订队列和主题订阅中的消息和会话上的锁。Renew locks on messages and sessions in queues and topic subscriptions.
  • 关闭队列和主题中的会话。Close sessions in queues and topics.

可以使用触发器从服务总线获取响应,并使输出可供逻辑应用中的其他操作使用。You can use triggers that get responses from Service Bus and make the output available to other actions in your logic apps. 还可以让其他操作使用服务总线操作的输出。You can also have other actions use the output from Service Bus actions. 如果不熟悉服务总线和逻辑应用,请参阅什么是 Azure 服务总线?什么是 Azure 逻辑应用?If you're new to Service Bus and Logic Apps, review What is Azure Service Bus? and What is Azure Logic Apps?

重要

当你同时选择具有相同连接器类型的触发器和操作并使用它们处理相同的实体(如消息队列或主题订阅)时,请务必谨慎。Use caution when you select both a trigger and action that have the same connector type and use them to work with the same entity, such as a messaging queue or topic subscription. 这种组合可以创建无限循环,从而导致逻辑应用永不结束。This combination can create an infinite loop, which results in a logic app that never ends.

先决条件Prerequisites

检查权限Check permissions

确认逻辑应用有权访问服务总线命名空间。Confirm that your logic app has permissions for accessing your Service Bus namespace.

  1. Azure 门户中,使用 Azure 帐户登录。In the Azure portal, sign in with your Azure account.

  2. 转到服务总线的命名空间。Go to your Service Bus namespace. 在命名空间页上的“设置”下,选择“共享访问策略”。 On the namespace page, under Settings, select Shared access policies. 在“声明”下,检查你是否有该命名空间的“管理”权限。 Under Claims, check that you have Manage permissions for that namespace.

    管理服务总线命名空间的权限

  3. 获取服务总线命名空间的连接字符串。Get the connection string for your Service Bus namespace. 在逻辑应用中提供连接信息时需要此字符串。You need this string when you provide the connection information in your logic app.

    1. 在“共享访问策略”窗格中,选择“RootManageSharedAccessKey”。On the Shared access policies pane, select RootManageSharedAccessKey.

    2. 在主连接字符串旁边选择复制按钮。Next to your primary connection string, select the copy button. 保存连接字符串供以后使用。Save the connection string for later use.

      复制服务总线命名空间连接字符串

    提示

    若要确认连接字符串是与服务总线命名空间关联还是与消息传送实体(例如队列)关联,请在该连接字符串中搜索 EntityPath 参数。To confirm whether your connection string is associated with your Service Bus namespace or a messaging entity, such as a queue, search the connection string for the EntityPath parameter. 如果找到了该参数,则表示连接字符串适用于特定的实体,不是适用于逻辑应用的正确字符串。If you find this parameter, the connection string is for a specific entity, and isn't the correct string to use with your logic app.

添加服务总线触发器Add Service Bus trigger

首次添加连接到服务或系统的触发器或操作时,逻辑应用设计器将提示你通过提供必要的信息(具体取决于连接)来创建连接,例如:When you add a trigger or action that connects to a service or system for the first time, the Logic App Designer prompts you to create a connection by providing the necessary information, which varies based on the connection, for example:

  • 要用于新连接的名称The name that you want to use for the new connection

  • 系统或服务器的名称The name for the system or server

  • 用户凭据或帐户凭据Your user or account credentials

  • 要使用的身份验证类型The authentication type to use

  1. 登录到 Azure 门户,然后在逻辑应用设计器中打开空白逻辑应用。Sign in to the Azure portal, and open your blank logic app in the Logic App Designer.

  2. 在门户的搜索框中,输入 azure service busIn the portal search box, enter azure service bus. 从出现的“触发器”列表中选择所需的触发器。From the triggers list that appears, select the trigger that you want.

    例如,若要在有新项发送到服务总线队列时触发逻辑应用,请选择“队列中收到消息时(自动完成)”触发器。For example, to trigger your logic app when a new item gets sent to a Service Bus queue, select the When a message is received in a queue (auto-complete) trigger.

    选择服务总线触发器

    下面是使用服务总线触发器时的一些注意事项:Here are some considerations for when you use a Service Bus trigger:

    • 所有服务总线触发器都是长轮询触发器。All Service Bus triggers are long-polling triggers. 此说明意味着,触发器在激发时会处理所有消息,然后等待 30 秒,让更多的消息出现在队列或主题订阅中。This description means that when the trigger fires, the trigger processes all the messages and then waits 30 seconds for more messages to appear in the queue or topic subscription. 如果在 30 秒内未显示任何消息,则会跳过触发器运行。If no messages appear in 30 seconds, the trigger run is skipped. 否则,该触发器将继续读取消息,直到队列或主题订阅为空。Otherwise, the trigger continues reading messages until the queue or topic subscription is empty. 下一次触发器轮询将基于在触发器的属性中指定的重复周期间隔。The next trigger poll is based on the recurrence interval specified in the trigger's properties.

    • 某些触发器(例如“一条或多条消息抵达队列时(自动完成)”触发器)可能会返回一条或多条消息。Some triggers, such as the When one or more messages arrive in a queue (auto-complete) trigger, can return one or more messages. 这些触发器在触发时返回的消息数至少为 1,至多为触发器的 最大消息计数 属性指定的消息数。When these triggers fire, they return between one and the number of messages that's specified by the trigger's Maximum message count property.

      备注

      自动完成触发器会自动完成消息,但只有在下一次调用服务总线时才会完成。The auto-complete trigger automatically completes a message, but completion happens only at the next call to Service Bus. 此行为可能会影响逻辑应用的设计。This behavior can affect your logic app's design. 例如,应避免更改自动完成触发器的并发性,因为如果逻辑应用进入受限制状态,此更改可能会导致重复的消息。For example, avoid changing the concurrency on the auto-complete trigger because this change might result in duplicate messages if your logic app enters a throttled state. 更改并发控制会形成以下情况:跳过受限制的触发器并显示 WorkflowRunInProgress 代码、完成操作不会发生以及下一次触发器运行会在轮询间隔后发生。Changing the concurrency control creates these conditions: throttled triggers are skipped with the WorkflowRunInProgress code, the completion operation won't happen, and next trigger run occurs after the polling interval. 必须将服务总线锁定持续时间设置为比轮询间隔更长的值。You have to set the service bus lock duration to a value that's longer than the polling interval. 但是尽管进行此设置,如果逻辑应用在下一个轮询间隔内保持受限制状态,则消息仍可能不会完成。However, despite this setting, the message still might not complete if your logic app remains in a throttled state at next polling interval.

    • 如果为服务总线触发器启用并发设置,则 maximumWaitingRuns 属性的默认值为 10。If you turn on the concurrency setting for a Service Bus trigger, the default value for the maximumWaitingRuns property is 10. 根据服务总线实体的锁定持续时间设置和逻辑应用实例的运行持续时间,此默认值可能太大,并可能导致“锁丢失”异常。Based on the Service Bus entity's lock duration setting and the run duration for your logic app instance, this default value might be too large and might cause a "lock lost" exception. 若要为方案找到最佳值,请对 maximumWaitingRuns 属性使用值 1 或值 2 开始测试。To find the optimal value for your scenario, start testing with a value of 1 or 2 for the maximumWaitingRuns property. 若要更改最大等待运行值,请参阅更改等待运行限制To change the maximum waiting runs value, see Change waiting runs limit.

  3. 如果触发器是首次连接到服务总线命名空间,则请在逻辑应用设计器提示你输入连接信息时执行以下步骤。If your trigger is connecting to your Service Bus namespace for the first time, follow these steps when the Logic App Designer prompts you for connection information.

    1. 请提供连接名称,并选择服务总线命名空间。Provide a name for your connection, and select your Service Bus namespace.

      显示提供连接名称和选择服务总线命名空间的屏幕截图

      若要改为手动输入连接字符串,请选择“手动输入连接信息”。To manually enter the connection string instead, select Manually enter connection information. 如果没有连接字符串,请了解如何查找连接字符串If you don't have your connection string, learn how to find your connection string.

    2. 选择服务总线策略,然后选择“创建”。Select your Service Bus policy, and select Create.

      显示选择服务总线策略的屏幕截图

    3. 选择所需的消息传送实体,例如某个队列或主题。Select the messaging entity you want, such as a queue or topic. 在此示例中,请选择服务总线队列。For this example, select your Service Bus queue.

      显示选择服务总线队列的屏幕截图

  4. 为所选触发器提供必需信息。Provide the necessary information for your selected trigger. 若要向操作添加其他可用属性,请打开“添加新参数”列表,然后选择所需属性。To add other available properties to the action, open the Add new parameter list, and select the properties that you want.

    对于此示例的触发器,请选择轮询间隔和检查队列的频率。For this example's trigger, select the polling interval and frequency for checking the queue.

    显示对服务总线触发器设置轮询间隔的屏幕截图

    有关可用触发器和属性的详细信息,请参阅连接器的参考页For more information about available triggers and properties, see the connector's reference page.

  5. 通过添加所需的操作继续生成逻辑应用。Continue building your logic app by adding the actions that you want.

    例如,可以添加一个可在收到新消息时发送电子邮件的操作。For example, you can add an action that sends email when a new message arrives. 当触发器检查队列并找到新消息时,逻辑应用会针对找到的消息运行选定的操作。When your trigger checks your queue and finds a new message, your logic app runs your selected actions for the found message.

添加服务总线操作Add Service Bus action

首次添加连接到服务或系统的触发器或操作时,逻辑应用设计器将提示你通过提供必要的信息(具体取决于连接)来创建连接,例如:When you add a trigger or action that connects to a service or system for the first time, the Logic App Designer prompts you to create a connection by providing the necessary information, which varies based on the connection, for example:

  • 要用于新连接的名称The name that you want to use for the new connection

  • 系统或服务器的名称The name for the system or server

  • 用户凭据或帐户凭据Your user or account credentials

  • 要使用的身份验证类型The authentication type to use

  1. Azure 门户的逻辑应用设计器中打开逻辑应用。In the Azure portal, open your logic app in the Logic App Designer.

  2. 在要添加操作的步骤下,选择“新建步骤”。Under the step where you want to add an action, select New step.

    或者将鼠标指针移到这些步骤之间的箭头上,以便在步骤之间添加操作。Or, to add an action between steps, move your pointer over the arrow between those steps. 选择出现的加号 ( + ),然后选择“添加操作”。Select the plus sign (+) that appears, and select Add an action.

  3. 在“选择操作”下的搜索框中输入 azure service busUnder Choose an action, in the search box, enter azure service bus. 从出现的“操作”列表中选择所需的操作。From the actions list that appears, select the action that you want.

    在此示例中,请选择“发送消息”操作。For this example, select the Send message action.

    显示选择服务总线操作的屏幕截图

  4. 如果操作是首次连接到服务总线命名空间,则请在逻辑应用设计器提示你输入连接信息时执行以下步骤。If your action is connecting to your Service Bus namespace for the first time, follow these steps when the Logic App Designer prompts you for connection information.

    1. 请提供连接名称,并选择服务总线命名空间。Provide a name for your connection, and select your Service Bus namespace.

      显示提供连接名称和选择服务总线命名空间的屏幕截图

      若要改为手动输入连接字符串,请选择“手动输入连接信息”。To manually enter the connection string instead, select Manually enter connection information. 如果没有连接字符串,请了解如何查找连接字符串If you don't have your connection string, learn how to find your connection string.

    2. 选择服务总线策略,然后选择“创建”。Select your Service Bus policy, and select Create.

      显示选择服务总线策略和选择“创建”按钮的屏幕截图

    3. 选择所需的消息传送实体,例如某个队列或主题。Select the messaging entity you want, such as a queue or topic. 在此示例中,请选择服务总线队列。For this example, select your Service Bus queue.

      显示选择服务总线队列的屏幕截图

  5. 为所选操作提供必要的详细信息。Provide the necessary details for your selected action. 若要向操作添加其他可用属性,请打开“添加新参数”列表,然后选择所需属性。To add other available properties to the action, open the Add new parameter list, and select the properties that you want.

    例如,选择“内容”和“内容类型”属性,以便将其添加到操作。 For example, select the Content and Content Type properties so that you add them to the action. 然后,指定要发送的消息的内容。Then, specify the content for the message that you want to send.

    显示提供消息内容类型和详细信息的屏幕截图

    有关可用操作及其属性的详细信息,请参阅连接器的参考页For more information about available actions and their properties, see the connector's reference page.

  6. 通过添加所需的任何其他操作继续生成逻辑应用。Continue building your logic app by adding any other actions that you want.

    例如,可以添加一个操作来发送电子邮件,确认消息已发送。For example, you can add an action that sends email to confirm that your message was sent.

  7. 保存逻辑应用。Save your logic app. 在设计器工具栏上选择“保存”。On the designer toolbar, select Save.

按顺序发送相关消息Send correlated messages in order

如果需要按特定顺序发送相关消息,则可通过 Azure 服务总线连接器使用“顺序保护”模式。When you need to send related messages in a specific order, you can use the sequential convoy pattern by using the Azure Service Bus connector. 相关消息具有一个可定义这些消息之间的关系的属性,例如服务总线中的会话的 ID。Correlated messages have a property that defines the relationship between those messages, such as the ID for the session in Service Bus.

创建逻辑应用时,可以选择“使用服务总线会话的相关的按序送达”模板,该模板可实现“顺序保护”模式。When you create a logic app, you can select the Correlated in-order delivery using service bus sessions template, which implements the sequential convoy pattern. 有关详细信息,请参阅按顺序发送相关消息For more information, see Send related messages in order.

逻辑应用的更新延迟生效Delays in updates to your logic app taking effect

如果服务总线触发器的轮询间隔很短(例如 10 秒),则对逻辑应用的更新可能会在长达 10 分钟的时间内不会生效。If a Service Bus trigger's polling interval is small, such as 10 seconds, updates to your logic app might not take effect for up to 10 minutes. 若要解决此问题,可禁用逻辑应用,进行更改,然后重新启用逻辑应用。To work around this problem, you can disable the logic app, make the changes, and then enable the logic app again.

连接器参考Connector reference

对于每个服务总线消息传递实体(如订阅或主题),服务总线连接器一次最多可以从服务总线将 1,500 个唯一会话保存到连接器缓存。From a service bus, the Service Bus connector can save up to 1,500 unique sessions at a time to the connector cache, per Service Bus messaging entity, such as a subscription or topic. 如果会话计数超过此限制,则将从缓存中删除旧会话。If the session count exceeds this limit, old sessions are removed from the cache. 有关详细信息,请参阅消息会话For more information, see Message sessions.

有关触发器、操作和限制(请参阅连接器的 Swagger 说明)的其他技术详细信息,请查看连接器参考页For other technical details about triggers, actions, and limits, which are described by the connector's Swagger description, review the connector reference page. 有关 Azure 服务总线消息传送的详细信息,请参阅什么是 Azure 服务总线For more about Azure Service Bus Messaging, see What is Azure Service Bus?

后续步骤Next steps