将函数用作事件网格事件的事件处理程序Use a function as an event handler for Event Grid events

事件处理程序是发送事件的位置。An event handler is the place where the event is sent. 处理程序将通过一个操作来处理事件。The handler takes an action to process the event. 几个 Azure 服务已自动配置为处理事件,Azure Functions 就是其中之一。Several Azure services are automatically configured to handle events and Azure Functions is one of them.

若要使用 Azure 中的函数作为事件的处理程序,请遵循以下一种方法:To use a function in Azure as a handler for events, follow one of these approaches:

  • 使用事件网格触发器Use Event Grid trigger. 将“Azure 函数”指定为“终结点类型” 。Specify Azure Function as the endpoint type. 然后,指定将处理事件的 Azure 函数应用和函数。Then, specify the Azure function app and the function that will handle events.
  • 使用 HTTP 触发器Use HTTP trigger. 将“Web Hook”指定为“终结点类型” 。Specify Web Hook as the endpoint type. 然后,指定将处理事件的 Azure 函数的 URL。Then, specify the URL for the Azure function that will handle events.

建议使用第一种方法(事件网格触发器),因为它与第二种方法相比具有以下优势:We recommend that you use the first approach (Event Grid trigger) as it has the following advantages over the second approach:

  • 事件网格会自动验证事件网格触发器。Event Grid automatically validates Event Grid triggers. 使用通用 HTTP 触发器时,必须自行实现验证响应With generic HTTP triggers, you must implement the validation response yourself.

  • 事件网格根据函数处理事件的感知速率自动调整事件传递到事件网格事件触发的函数的速率。Event Grid automatically adjusts the rate at which events are delivered to a function triggered by an Event Grid event based on the perceived rate at which the function can process events. 这种速率匹配功能可避免由于函数无法处理事件而导致的传递错误,因为函数的事件处理速率可能会随时间而改变。This rate match feature averts delivery errors that stem from the inability of a function to process events as the function's event processing rate can vary over time. 若要在高吞吐量下提高效率,请在事件订阅上启用批处理。To improve efficiency at high throughput, enable batching on the event subscription. 有关详细信息,请参阅启用批处理For more information, see Enable batching.

    备注

    目前,在以 CloudEvents 架构传递事件时,无法对函数应用使用事件网格触发器。Currently, you can't use an Event Grid trigger for a function app when the event is delivered in the CloudEvents schema. 应转而使用 HTTP 触发器。Instead, use an HTTP trigger.

教程Tutorials

标题Title 说明Description
快速入门:使用函数处理事件Quickstart: Handle events with function 将自定义事件发送到函数进行处理。Sends a custom event to a function for processing.
教程:使用事件网格自动调整上传图像的大小Tutorial: automate resizing uploaded images using Event Grid 用户通过 Web 应用将映像上传到存储帐户。Users upload images through web app to storage account. 创建存储 Blob 后,事件网格会向用于重设已上传映像的大小的函数应用发送一个事件。When a storage blob is created, Event Grid sends an event to the function app, which resizes the uploaded image.
教程:将大数据流式传输到数据仓库Tutorial: stream big data into a data warehouse 当事件中心创建捕获文件时,事件网格会将一个事件发送到函数应用。When Event Hubs creates a Capture file, Event Grid sends an event to a function app. 应用会检索捕获文件并将数据迁移到数据仓库。The app retrieves the Capture file and migrates data to a data warehouse.
教程:Azure 服务总线到 Azure 事件网格集成示例Tutorial: Azure Service Bus to Azure Event Grid integration examples 事件网格会将消息从服务总线主题发送到函数应用和逻辑应用。Event Grid sends messages from Service Bus topic to a function app and a logic app.

REST 示例(对于 PUT)REST example (for PUT)

{
    "properties": 
    {
        "destination": 
        {
            "endpointType": "AzureFunction",
            "properties": 
            {
                "resourceId": "/subscriptions/<AZURE SUBSCRIPTION ID>/resourceGroups/<RESOURCE GROUP NAME>/providers/Microsoft.Web/sites/<FUNCTION APP NAME>/functions/<FUNCTION NAME>",
                "maxEventsPerBatch": 10,
                "preferredBatchSizeInKilobytes": 6400
            }
        },
        "eventDeliverySchema": "EventGridSchema"
    }
}

启用批处理Enable batching

若要提高吞吐量,请对订阅启用批处理。For a higher throughput, enable batching on the subscription. 如果使用的是 Azure 门户,则可以在创建订阅时或创建订阅后设置每个批的最大事件数和首选批大小(以 KB 为单位)。If you are using the Azure portal, you can set maximum events per batch and the preferred batch size in kilo bytes at the time of creating a subscription or after the creation.

可以使用 Azure 门户、PowerShell、CLI 或资源管理器模板配置批处理设置。You can configure batch settings using the Azure portal, PowerShell, CLI, or Resource Manager template.

Azure 门户Azure portal

在 UI 中创建订阅时,在“创建事件订阅”页上切换到“高级功能”选项卡,设置“每批最大事件数”和“首选批大小(KB)”的值。At the time creating a subscription in the UI, on the Create Event Subscription page, switch to the Advanced Features tab, and set values for Max events per batch and Preferred batch size in kilobytes.

在创建订阅时启用批处理

可以在“事件网格主题”页的“功能”选项卡上更新现有订阅的这些值。You can update these values for an existing subscription on the Features tab of the Event Grid Topic page.

创建后启用批处理

Azure Resource Manager 模板Azure Resource Manager template

可以在 Azure 资源管理器模板中设置 maxEventsPerBatch 和 preferredBatchSizeInKilobytes。You can set maxEventsPerBatch and preferredBatchSizeInKilobytes in an Azure Resource Manager template. 有关详细信息,请参阅 Microsoft.EventGrid eventSubscriptions 模板参考For more information, see Microsoft.EventGrid eventSubscriptions template reference.

Azure CLIAzure CLI

可以使用 az eventgrid event-subscription createaz eventgrid event-subscription update 命令,通过以下参数配置与批处理相关的设置:--max-events-per-batch--preferred-batch-size-in-kilobytesYou can use the az eventgrid event-subscription create or az eventgrid event-subscription update command to configure batch-related settings using the following parameters: --max-events-per-batch or --preferred-batch-size-in-kilobytes.

Azure PowerShellAzure PowerShell

可以使用 New-AzEventGridSubscriptionUpdate-AzEventGridSubscription cmdlet,通过以下参数配置与批处理相关的设置:-MaxEventsPerBatch-PreferredBatchSizeInKiloBytesYou can use the New-AzEventGridSubscription or Update-AzEventGridSubscription cmdlet to configure batch-related settings using the following parameters: -MaxEventsPerBatch or -PreferredBatchSizeInKiloBytes.

备注

当你使用事件网格触发器时,事件网格服务会提取目标 Azure 函数的客户端密码,并使用该密码将事件传递给 Azure 函数。When you use Event Grid Trigger, the Event Grid service fetches the client secret for the target Azure function, and uses it to deliver events to the Azure function. 如果使用 Azure Active Directory 应用程序保护 Azure 函数,则必须采用通用的 Webhook 方法并使用 HTTP 触发器。If you protect your azure function with an Azure Active Directory application, you have to take the generic web hook approach and use the HTTP Trigger.

后续步骤Next steps

如需支持的事件处理程序的列表,请参阅事件处理程序一文。See the Event handlers article for a list of supported event handlers.