教程:使用 Azure Functions 响应通过 Azure 事件网格收到的 Azure 服务总线事件Tutorial: Respond to Azure Service Bus events received via Azure Event Grid by using Azure Functions

本教程介绍如何使用 Azure Functions 和 Azure 逻辑应用对通过 Azure 事件网格收到的 Azure 服务总线事件做出响应。In this tutorial, you learn how to respond to Azure Service Bus events that are received via Azure Event Grid by using Azure Functions and Azure Logic Apps.

在本教程中,你将了解如何执行以下操作:In this tutorial, you learn how to:

  • 创建服务总线命名空间Create a Service Bus namespace
  • 准备用于发送消息的示例应用程序Prepare a sample application to send messages
  • 向服务总线主题发送消息Send messages to the Service Bus topic
  • 使用逻辑应用接收消息Receive messages by using Logic Apps
  • 在 Azure 上设置测试函数Set up a test function on Azure
  • 通过事件网格连接函数和命名空间Connect the function and namespace via Event Grid
  • 使用 Azure Functions 接收消息Receive messages by using Azure Functions

Prerequisites

If you don't have an Azure subscription, create a trial subscription before you begin.

Create a Service Bus namespace

Follow instructions in this tutorial: Quickstart: Use the Azure portal to create a Service Bus topic and subscriptions to the topic to do the following tasks:

  • Create a premium Service Bus namespace.
  • Get the connection string.
  • Create a Service Bus topic.
  • Create a subscription to the topic. You need only one subscription in this tutorial, so no need to create subscriptions S2 and S3.

Send messages to the Service Bus topic

In this step, you use a sample application to send messages to the Service Bus topic you created in the previous step.

  1. Clone the GitHub azure-service-bus repository.

  2. In Visual Studio, go to the \samples\DotNet\Azure.Messaging.ServiceBus\ServiceBusEventGridIntegration folder, and then open the SBEventGridIntegration.sln file.

  3. In the Solution Explorer window, expand the MessageSender project, and select Program.cs.

  4. Replace <SERVICE BUS NAMESPACE - CONNECTION STRING> with the connection string to your Service Bus namespace and <TOPIC NAME> with the name of the topic.

    const string ServiceBusConnectionString = "<SERVICE BUS NAMESPACE - CONNECTION STRING>";
    const string TopicName = "<TOPIC NAME>";
    
  5. Build and run the program to send 5 test messages (const int numberOfMessages = 5;) to the Service Bus topic.

    Console app output

其他先决条件Additional prerequisites

安装 Visual Studio 2019,并包含 Azure 开发工作负载。Install Visual Studio 2019 and include the Azure development workload. 此工作负载中有在 Visual Studio 中创建、生成和部署 Azure Functions 项目所需的 Azure Functions 工具。This workload includes Azure Function Tools that you need to create, build, and deploy Azure Functions projects in Visual Studio.

部署函数应用Deploy the function app

备注

若要详细了解如何创建和部署 Azure Functions 应用,请参阅使用 Visual Studio 开发 Azure FunctionsTo learn more about creating and deploying an Azure Functions app, see Develop Azure Functions using Visual Studio

  1. 从 SBEventGridIntegration.sln 解决方案的 FunctionApp1 项目中打开 ReceiveMessagesOnEvent.cs 文件 。Open ReceiveMessagesOnEvent.cs file from the FunctionApp1 project of the SBEventGridIntegration.sln solution.

  2. <SERCICE BUS NAMESPACE - CONNECTION STRING> 替换为服务总线命名空间的连接字符串。Replace <SERCICE BUS NAMESPACE - CONNECTION STRING> with the connection string to your Service Bus namespace. 它应与你在同一解决方案中的 MessageSender 项目的 Program.cs 文件中使用的字符串相同 。It should be the same as the one you used in the Program.cs file of the MessageSender project in the same solution.

  3. 右键单击“FunctionApp1”,然后选择“发布” 。Right-click FunctionApp1, and select Publish.

  4. 在“发布”页面上选择“启动” 。On the Publish page, select Start. 这些步骤可能与你看到的不同,但发布过程应该是类似的。These steps may be different from what you see, but the process of publishing should be similar.

  5. 在“发布”向导的“目标”页面上,为“目标”选择“Azure” 。In the Publish wizard, on the Target page, select Azure for Target.

  6. 在“特定目标”页面上,选择“Azure 函数应用(Windows)” 。On the Specific target page, select Azure Function App (Windows).

  7. 在“函数实例”页面上,选择“创建新的 Azure 函数” 。On the Functions instance page, select Create a new Azure function.

  8. 在“函数应用(Windows)”页面上,执行以下步骤:On the Function App (Windows) page, follow these steps:

    1. 输入函数应用的名称。Enter a name for the function app.
    2. 选择 Azure 订阅。Select an Azure subscription.
    3. 选择现有 资源组 或创建新资源组。Select an existing resource group or create a new resource group. 在本教程中,请选择具有服务总线命名空间的资源组。For this tutorial, select the resource group that has the Service Bus namespace.
    4. 为应用服务选择计划类型。Select a plan type for App Service.
    5. 选择一个位置。Select a location. 选择与服务总线命名空间相同的位置。Select the same location as the Service Bus namespace.
    6. 选择一个现有的 Azure 存储,或者选择“新建”来创建一个新的存储帐户供函数应用使用 。Select an existing Azure Storage or select New to create a new Storage account to be used by the Functions app.
    7. 选择“创建”以创建函数应用。Select Create to create the Functions app.
  9. 返回到“发布”向导的“函数实例”页面,选择“完成” 。Back on the Functions instance page of the Publish wizard, select Finish.

  10. 在 Visual Studio 中的“发布”页面上选择“发布”,将函数应用发布到 Azure 。On the Publish page in Visual Studio, select Publish to publish the Functions app to Azure.

    发布函数应用

  11. 在“输出”窗口中,查看来自生成和发布的消息,并确认它们都已成功。In the Output window, see the messages from build and publish, and confirm that they both succeeded.

  12. 现在,在“发布”页面上,选择“在 Azure 门户中管理” 。Now, on the Publish page, select Manage in Azure portal.

  13. 在 Azure 门户的“函数应用”页面上,在左侧菜单中选择“函数”,并确认你看到了两个函数 :In the Azure portal, on the Function App page, select Functions in the left menu, and confirm that you see two functions:

    用于处理事件网格触发器和 HTTP 触发器的两个函数

    备注

    EventGridTriggerFunction 使用事件网格触发器HTTPTriggerFunction 使用 HTTP 触发器The EventGridTriggerFunction uses an Event Grid trigger and the HTTPTriggerFunction uses a HTTP trigger.

  14. 从列表中选择 EventGridTriggerFunction。Select EventGridTriggerFunction from the list. 建议将事件网格触发器用于 Azure Functions,因为它与使用 HTTP 触发器相比有一些优势。We recommend that you use the Event Grid trigger with Azure Functions as it has a few benefits over using the HTTP trigger. 有关详细信息,请参阅 Azure Functions 作为事件网格事件的事件处理程序For details, see Azure function as an event handler for Event Grid events.

  15. 在 EventGridTriggerFunction 的“函数”页面上,选择左菜单上的“监视” 。On the Function page for the EventGridTriggerFunction, select Monitor on the left menu.

  16. 选择“配置”,配置 Application Insights 来捕获调用日志。Select Configure to configure Application Insights to capture invocation log. 可使用此页面监视从事件网格接收服务总线事件时的函数执行。You use this page to monitor function executions upon receiving Service Bus events from Event Grid.

  17. 在 Application Insights 页面上,输入资源的名称,为资源选择一个位置,然后选择“确定” 。On the Application Insights page, enter a name for the resource, select a location for the resource, and then select OK.

  18. 在顶部(痕迹导航菜单)选择 EventGridTriggerFunction 函数,导航回到“函数”页面 。Select the function EventGridTriggerFunction at the top (breadcrumb menu) to navigate back to the Function page.

  19. 确认你在“监视”页面上。Confirm that you on the Monitor page.

    函数调用前函数的“监视”页面

    在 Web 浏览器的标签页中将此页面保持在打开状态。Keep this page open in a tab your web browser. 稍后你将刷新此页面来查看对该函数的调用。You'll refresh this page to see invocations for this function later.

通过事件网格连接函数和命名空间Connect the function and namespace via Event Grid

在本部分,你要使用 Azure 门户将函数和服务总线命名空间绑定在一起。In this section, you tie together the function and the Service Bus namespace by using the Azure portal.

若要创建 Azure 事件网格订阅,请执行以下操作:To create an Azure Event Grid subscription, follow these steps:

  1. 在 Azure 门户中转到你的命名空间,然后在左窗格中选择“事件”。In the Azure portal, go to your namespace and then, in the left pane, select Events. 此时会打开命名空间窗口,两个事件网格订阅显示在右窗格中。Your namespace window opens, with two Event Grid subscriptions displayed in the right pane.

    服务总线 - 事件页

  2. 在工具栏上选择“+ 事件订阅”。Select + Event Subscription on the toolbar.

  3. 在“创建事件订阅”页中执行以下步骤:On the Create Event Subscription page, do the following steps:

    1. 输入订阅的 名称Enter a name for the subscription.

    2. 输入系统主题的名称 。Enter a name for the system topic. 系统主题是为 Azure 资源(如 Azure 存储帐户和 Azure 服务总线)创建的主题。System topics are topics created for Azure resources such as Azure Storage account and Azure Service Bus. 若要详细了解系统主题,请参阅系统主题概述To learn more about system topics, see System topics overview.

    3. 选择“Azure Function”作为终结点类型,然后单击“选择终结点” 。Select Azure Function for Endpoint Type, and click Select an endpoint.

      服务总线 - 事件网格订阅

    4. 在“选择 Azure 函数”页面上,选择订阅、资源组、函数应用、槽和函数,然后选择“确认选择” 。On the Select Azure Function page, select the subscription, resource group, function app, slot, and the function, and then select Confirm selection.

      函数 - 选择终结点

    5. 在“创建事件订阅”页面上,切换到“筛选器”选项卡,并执行以下任务 :On the Create Event Subscription page, switch to the Filters tab, and do the following tasks:

      1. 选择“启用主题筛选”Select Enable subject filtering

      2. 输入先前创建的服务总线主题 (S1) 的订阅的名称。Enter the name of the subscription to the Service Bus topic (S1) you created earlier.

      3. 选择“创建”按钮。Select the Create button.

        事件订阅筛选器

  4. 切换到“事件”页面的“事件订阅”选项卡,并确认列表中显示了事件订阅 。Switch to the Event Subscriptions tab of the Events page and confirm that you see the event subscription in the list.

    列表中的事件订阅

监视函数应用Monitor the Functions app

先前发送到服务总线主题的消息将转发到订阅 (S1)。The messages you sent to the Service Bus topic earlier are forwarded to the subscription (S1). 事件网格将订阅时的消息转发到 Azure 函数。Event Grid forwards the messages at the subscription to the Azure function. 在本教程的此步骤中,你将确认已调用该函数并查看记录的信息性消息。In this step of the tutorial, you confirm the function was invoked and view the logged informational messages.

  1. 在 Azure 函数应用页面上,切换到“监视”选项卡(如果它尚未激活)。On the page for your Azure function app, switch to the Monitor tab if it isn't already active. 应会看到发布到服务总线主题的每个消息的条目。You should see an entry for each message posted to the Service Bus topic. 如果没有看到这些条目,请在等待几分钟后刷新页面。If you don't see them, refresh the page after waiting for a few minutes.

    调用后函数的“监视”页面

  2. 从列表中选择调用以查看详细信息。Select the invocation from the list to see the details.

    函数调用详细信息

    还可以使用“监视”页的“日志”选项卡在发送消息时查看日志信息 。You can also use the Logs tab of the Monitor page to see the logging information as the messages are sent. 可能会有一些延迟,因此请在几分钟后查看记录的消息。There could some delay, so give it a few minutes to see the logged messages.

    函数日志

疑难解答Troubleshoot

如果在等待和刷新一段时间后没有看到任何函数调用,请执行以下步骤:If you don't see any function invocations after waiting and refreshing for sometime, follow these steps:

  1. 确认消息已到达服务总线主题。Confirm that the messages reached the Service Bus topic. 查看“服务总线主题”页面上的“传入消息”计数器 。See the incoming messages counter on the Service Bus Topic page. 在本例中,我运行了两次 MessageSender 应用程序,因此我会看到 10 条消息(每次运行 5 条消息)。In this case, I ran the MessageSender application twice, so I see 10 messages (5 messages for each run).

    服务总线主题页面 - 传入消息

  2. 确认服务总线订阅中有“无活动消息”。Confirm that there are no active messages at the Service Bus subscription. 如果在此页面上未看到任何事件,请验证“服务总线订阅”页面没有显示任何“活动消息计数” 。If you don't see any events on this page, verify that the Service Bus Subscription page doesn't show any Active message count. 如果此计数器的数字大于零,则由于某些原因,订阅中的消息没有转发到处理程序函数(事件订阅处理程序)。If the number for this counter is greater than zero, the messages at the subscription aren't forwarded to the handler function (event subscription handler) for some reason. 请验证是否已正确设置事件订阅。Verify that you've set up the event subscription properly.

    服务总线订阅的活动消息计数

  3. 你还可在服务总线命名空间的“事件”页面上查看发送的事件数 。You also see delivered events on the Events page of the Service Bus namespace.

    事件页面 - 发送的事件数

  4. 还可在“事件订阅”页面上看到事件已发送。You can also see that the events are delivered on the Event Subscription page. 可在“事件”页面上选择事件订阅来访问此页面。You can get to this page by selecting the event subscription on the Events page.

    事件订阅页面 - 发送的事件数

后续步骤Next steps