教程:使用 Azure 逻辑应用响应通过 Azure 事件网格收到的 Azure 服务总线事件

本教程介绍如何使用 Azure 逻辑应用对通过 Azure 事件网格收到的 Azure 服务总线事件做出响应。

先决条件

如果没有 Azure 试用版订阅,请在开始前创建一个试用版订阅

创建服务总线命名空间

请遵照以下教程中的说明:快速入门:使用 Azure 门户创建服务总线主题和主题的订阅来执行以下任务:

  • 创建一个高级服务总线命名空间。
  • 获取连接字符串。
  • 创建服务总线主题。
  • 创建主题的订阅。 本教程只需要一个订阅,因此无需创建订阅 S2 和 S3。

向服务总线主题发送消息

在此步骤中,你将使用示例应用程序将消息发送到在上一步中创建的服务总线主题。

  1. 克隆 GitHub azure-service-bus 存储库,或者下载 zip 文件并从中提取文件。

  2. 在 Visual Studio 中转到 \samples\DotNet\Azure.Messaging.ServiceBus\ServiceBusEventGridIntegrationV2 文件夹,然后打开 SBEventGridIntegration.sln 文件 。

  3. 在解决方案资源管理器窗口中,展开“MessageSender”项目,然后选择“Program.cs” 。

  4. <SERVICE BUS NAMESPACE - CONNECTION STRING> 替换为服务总线命名空间的连接字符串,并将 <TOPIC NAME> 替换为主题的名称。

    const string ServiceBusConnectionString = "<SERVICE BUS NAMESPACE - CONNECTION STRING>";
    const string TopicName = "<TOPIC NAME>";
    
  5. 生成并运行程序,以将 5 条测试消息 (const int numberOfMessages = 5;) 发送到服务总线主题。

    Console app output

使用逻辑应用接收消息

在此步骤中,将创建一个通过 Azure 事件网格接收服务总线事件的 Azure 逻辑应用。

  1. 依次选择“+ 创建资源”、“集成”、“逻辑应用”。

    Screenshot that shows the Create a resource -> Integration -> Logic app menu.

  2. 在“创建逻辑应用”页上,执行以下步骤:

    1. 选择 Azure 订阅
    2. 为“资源组”选择“使用现有项”,然后选择以前创建的、用于其他资源(例如 Azure 函数、服务总线命名空间)的资源组。
    3. 输入逻辑应用的“名称”。
    4. 为逻辑应用选择“区域”。
    5. 计划类型选择“消耗”。
    6. 选择“查看 + 创建” 。 Screenshot that shows the Create a logic app page.
    7. 在“查看 + 创建”页面中,选择“创建”,以创建逻辑应用 。
  3. 在“部署完成”页中,选择“转到资源”来导航到“逻辑应用”页。

  4. 在“逻辑应用设计器”页上,选择“模板”下的“空白逻辑应用”。

添加步骤以通过事件网格从服务总线接收消息

  1. 在“逻辑应用”页中,选择左侧菜单中的“逻辑应用设计器”。

  2. 在右侧窗格中,在“模板”下选择“空白逻辑应用”。

    Screenshot that shows the Logic app designer page with the Blank logic app option selected.

  3. 在设计器中执行以下步骤:

    1. 搜索“事件网格”。

    2. 选择“发生资源事件时 - Azure 事件网格”。

      Screenshot that shows the Logic Apps Designer with Event Grid trigger selected.

  4. 选择“登录”。

    Screenshot that shows the Logic Apps Designer with the Sign-in button selected.

  5. 在“登录到帐户”页上,选择要用于登录到 Azure 的帐户。 1.

  6. 在“当资源事件发生时”页上执行以下步骤:

    1. 选择 Azure 订阅。

    2. 对于“资源类型”,请选择“Microsoft.ServiceBus.Namespaces”。

    3. 对于“资源名称”,请选择你的服务总线命名空间。

    4. 选择“添加新参数”,然后选择“后缀筛选器”,然后将焦点移到下拉列表之外 。

      Screenshot that shows adding of a new parameter of type Suffix filter.

    5. 对于“后缀筛选器”,请输入服务总线主题订阅的名称。

      Screenshot that shows the Logic Apps Designer with connection configuration for the Service Bus namespace.

  7. 在设计器中选择“+ 新建步骤”,然后执行以下步骤:

    1. 搜索“服务总线”。

    2. 在列表中选择“服务总线”。

      Screenshot that shows the selection of Service Bus.

    3. 在“操作”列表中选择“获取消息”。

    4. 选择“从主题订阅中获取消息(扫视锁定)”。

      Screenshot that shows the Logic Apps Designer with Get messages from a topic subscription selected.

    5. 执行以下步骤:

      1. 输入连接的名称。 例如“从主题订阅获取消息”。

      2. 确认“身份验证类型”设置为“访问密钥”。

      3. 对于“连接字符串”,请将连接字符串复制并粘贴到之前保存的服务总线命名空间。

      4. 选择创建

        Screenshot that shows the Logic Apps Designer with the Service Bus connection string specified.

    6. 选择你的主题和订阅 。

      Screenshot that shows the Logic Apps Designer with the Service Bus topic and subscription specified.

添加步骤以处理和完成收到的消息

在此步骤中,需要添加用于在电子邮件中发送收到的消息的步骤,然后完成消息。 在真实场景中,你会先在逻辑应用中处理消息,然后再完成该消息。

添加 For each 循环

  1. 选择“+新建步骤”。

  2. 搜索“控制”,然后将其选中。

    Screenshot that shows the Control category.

  3. 在“操作”列表中,选择“For each” 。

    Screenshot that shows the For-each operation selected.

  4. 对于“选择先前步骤的输出”(如果需要,请在文本框内部单击),请选择“从主题订阅中获取消息(扫视-锁定)”下的“正文” 。

    Screenshot that shows the selection of For each input.

在 For each 循环中添加一个步骤,以发送包含消息正文的电子邮件

  1. 在“For Each”循环中,选择“添加操作” 。

    Screenshot that shows the selection of Add an action button in the For-each loop.

  2. 在“搜索连接器和操作”文本框中,输入“Office 365” 。

  3. 在搜索结果中选择“Office 365 Outlook”。

    Screenshot that shows the selection of Office 365.

  4. 在操作列表中,选择“发送电子邮件(V2)”。

    Screenshot that shows the selection of Send an email operation.

  5. 选择“登录”,然后按照步骤创建与 Office 365 Outlook 的连接。

  6. 在“发送电子邮件(V2)”窗口中,执行以下步骤:

  7. 在“正文”文本框内部单击,然后执行以下步骤:

    1. 对于“收件人”,请输入电子邮件地址。

    2. 对于“主题”,请输入“从服务总线主题的订阅收到的消息” 。

    3. 切换到“表达式”。

    4. 输入以下表达式:

      base64ToString(items('For_each')?['ContentData'])
      
    5. 选择“确定”

      Screenshot that shows the expression for Body of the Send an email activity.

在 For each 循环中添加另一个操作以完成消息

  1. 在“For Each”循环中,选择“添加操作” 。

    1. 在“最近”列表中选择“服务总线” 。

    2. 在操作列表中选择“完成主题订阅中的消息”。

      Screenshot that shows the selection of Complete a message in a topic subscription.

    3. 选择你的服务总线主题

    4. 选择主题的订阅。

    5. 对于“消息的锁定标记”,请从“动态内容”中选择“锁定标记”。

      Screenshot that shows the lock token field.

  2. 在逻辑应用设计器的工具栏上选择“保存”以保存逻辑应用。

    Screenshot that shows the Save button in the Logic app designed.

测试应用

  1. 如果尚未向主题发送测试消息,请按照向服务总线主题发送消息部分中的说明将消息发送到主题。

  2. 切换到逻辑应用的“概述”页,然后切换到底部窗格中的“运行历史记录”选项卡 。 你会看到逻辑应用运行发送到主题的消息。 可能需要几分钟时间,你才能看到逻辑应用运行。 选择工具栏上的“刷新”,以刷新页面。

    Screenshot that shows the Logic app run history.

  3. 选择一个逻辑应用运行,查看详细信息。 请注意,它在 for 循环中处理了 5 条消息。

    Screenshot that shows the details for the selected logic app run.

  4. 对于逻辑应用收到的每条消息,你都会收到一封电子邮件。

    Screenshot of Outlook with the messages received from the topics' subscription.

疑难解答

如果在等待和刷新一段时间后没有看到任何调用,请执行以下步骤:

  1. 确认消息已到达服务总线主题。 查看“服务总线主题”页面上的“传入消息”计数器 。 在本例中,我运行了两次 MessageSender 应用程序,因此我会看到 5 条消息。

    Screenshot that shows the Service Bus Topic page with incoming message count selected.

  2. 确认服务总线订阅中有“无活动消息”。 如果在此页面上未看到任何事件,请验证“服务总线订阅”页面没有显示任何“活动消息计数” 。 如果此计数器的数字大于零,则由于某些原因,订阅中的消息没有转发到处理程序函数(事件订阅处理程序)。 请验证是否已正确设置事件订阅。

    Screenshot that shows the Service Bus Subscription page with the active message count selected.

  3. 你还可在服务总线命名空间的“事件”页面上查看发送的事件数 。

    Screenshot that shows the Events page of the Service Bus Namespace page.

  4. 还可在“事件订阅”页面上看到事件已发送。 可在“事件”页面上选择事件订阅来访问此页面。

    Screenshot that shows the Event Subscription page with the delivered event count selected.

后续步骤