服务总线队列和主题作为 Azure 事件网格事件的事件处理程序Service Bus queues and topics as event handlers for Azure Event Grid events

事件处理程序是发送事件的位置。An event handler is the place where the event is sent. 处理程序将执行一些进一步的操作来处理事件。The handler takes some further action to process the event. 几个 Azure 服务已自动配置为处理事件,Azure 服务总线是其中之一。Several Azure services are automatically configured to handle events and Azure Service Bus is one of them.

可以使用服务队列或主题作为事件网格事件的处理程序。You can use a Service queue or topic as a handler for events from Event Grid.

服务总线队列Service Bus queues

可以将事件网格中的事件直接路由到服务总线队列,用于企业应用程序中的缓冲或命令和控制方案。You can route events in Event Grid directly to Service Bus queues for use in buffering or command & control scenarios in enterprise applications.

在 Azure 门户中,请在创建事件订阅时选择“服务总线队列”作为终结点类型,然后单击“选择终结点”以选择服务总线队列。In the Azure portal, while creating an event subscription, select Service Bus Queue as endpoint type and then click select an endpoint to choose a Service Bus queue.

使用 CLI 添加服务总线队列处理程序Using CLI to add a Service Bus queue handler

在 Azure CLI 中,以下示例可订阅事件网格主题并将其连接到服务总线队列:For Azure CLI, the following example subscribes and connects an event grid topic to a Service Bus queue:

az eventgrid event-subscription create \
    --name <my-event-subscription> \
    --source-resource-id /subscriptions/{SubID}/resourceGroups/{RG}/providers/Microsoft.EventGrid/topics/topic1 \
    --endpoint-type servicebusqueue \
    --endpoint /subscriptions/{SubID}/resourceGroups/TestRG/providers/Microsoft.ServiceBus/namespaces/ns1/queues/queue1

服务总线主题Service Bus topics

可以将事件网格中的事件直接路由到服务总线主题,以使用服务总线主题处理 Azure 系统事件,或者将其用于命令和控制消息方案。You can route events in Event Grid directly to Service Bus topics to handle Azure system events with Service Bus topics, or for command & control messaging scenarios.

在 Azure 门户中,请在创建事件订阅时选择“服务总线主题”作为终结点类型,然后单击“选择终结点”以选择服务总线主题。In the Azure portal, while creating an event subscription, select Service Bus Topic as endpoint type and then click select and endpoint to choose a Service Bus topic.

使用 CLI 添加服务总线主题处理程序Using CLI to add a Service Bus topic handler

在 Azure CLI 中,以下示例可订阅事件网格主题并将其连接到服务总线队列:For Azure CLI, the following example subscribes and connects an event grid topic to a Service Bus queue:

az eventgrid event-subscription create \
    --name <my-event-subscription> \
    --source-resource-id /subscriptions/{SubID}/resourceGroups/{RG}/providers/Microsoft.EventGrid/topics/topic1 \
    --endpoint-type servicebustopic \
    --endpoint /subscriptions/{SubID}/resourceGroups/TestRG/providers/Microsoft.ServiceBus/namespaces/ns1/topics/topic1

消息属性Message properties

如果使用服务总线主题或队列作为事件网格事件的事件处理程序,则会在消息标头中收到以下属性:If you use a Service Bus topic or queue as an event handler for events from Event Grid, these are the properties you receive in the message headers:

属性名称Property name 说明Description
aeg-subscription-nameaeg-subscription-name 事件订阅的名称。Name of event subscription.
aeg-delivery-countaeg-delivery-count

针对该事件进行尝试的次数。Number of attempts made for the event.

示例:“1”Example: "1"

aeg-event-typeaeg-event-type

事件的类型。Type of the event.

示例:“Microsoft.Storage.blobCreated”Example: "Microsoft.Storage.blobCreated"

aeg-metadata-versionaeg-metadata-version

事件的元数据版本。Metadata version of the event.

示例:“1”。Example: "1".

对于“事件网格事件架构”,此属性表示元数据版本;对于“云事件架构”,此属性表示规范版本。For Event Grid event schema , this property represents the metadata version and for cloud event schema , it represents the spec version .

aeg-data-versionaeg-data-version

事件的数据版本。Data version of the event.

示例:“1”。Example: "1".

对于“事件网格事件架构”,此属性表示数据版本;对于“云事件架构”,此属性不适用。For Event Grid event schema , this property represents the data version and for cloud event schema , it doesn't apply.

消息标头Message headers

将事件作为中转消息发送到服务总线队列或主题时,中转消息的 messageid 是内部系统 ID。When sending an event to a Service Bus queue or topic as a brokered message, the messageid of the brokered message is an internal system ID.

消息的内部系统 ID 会在事件重新传递期间保留,因此你可以在服务总线实体上启用“重复项检测”,避免重复传递。The internal system ID for the message will be maintained across redelivery of the event so that you can avoid duplicate deliveries by turning on duplicate detection on the service bus entity. 建议在服务总线实体上启用重复项检测持续时间,并将其设为事件的生存时间 (TTL) 或最大重试持续时间(以较长者为准)。We recommend that you enable duration of the duplicate detection on the Service Bus entity to be either the time-to-live (TTL) of the event or max retry duration, whichever is longer.

REST 示例(适用于 PUT)REST examples (for PUT)

服务总线队列Service Bus queue

{
    "properties": 
    {
        "destination": 
        {
            "endpointType": "ServiceBusQueue",
            "properties": 
            {
                "resourceId": "/subscriptions/<AZURE SUBSCRIPTION ID>/resourceGroups/<RESOURCE GROUP NAME>/providers/Microsoft.ServiceBus/namespaces/<SERVICE BUS NAMESPACE NAME>/queues/<SERVICE BUS QUEUE NAME>"
            }
        },
        "eventDeliverySchema": "EventGridSchema"
    }
}

服务总线队列 - 使用托管标识传送Service Bus queue - delivery with managed identity

{
    "properties": {
        "deliveryWithResourceIdentity": 
        {
            "identity": 
            {
                "type": "SystemAssigned"
            },
            "destination": 
            {
                "endpointType": "ServiceBusQueue",
                "properties": 
                {
                    "resourceId": "/subscriptions/<AZURE SUBSCRIPTION ID>/resourceGroups/<RESOURCE GROUP NAME>/providers/Microsoft.ServiceBus/namespaces/<SERVICE BUS NAMESPACE NAME>/queues/<SERVICE BUS QUEUE NAME>"
                }
            }
        },
        "eventDeliverySchema": "EventGridSchema"
    }
}

服务总线主题Service Bus topic

{
    "properties": 
    {
        "destination": 
        {
            "endpointType": "ServiceBusTopic",
            "properties": 
            {
                "resourceId": "/subscriptions/<AZURE SUBSCRIPTION ID>/resourceGroups/<RESOURCE GROUP NAME>/providers/Microsoft.ServiceBus/namespaces/<SERVICE BUS NAMESPACE NAME>/topics/<SERVICE BUS TOPIC NAME>"
            }
        },
        "eventDeliverySchema": "EventGridSchema"
    }
}

服务总线主题 - 使用托管标识传送Service Bus topic - delivery with managed identity

{
    "properties": 
    {
        "deliveryWithResourceIdentity": 
        {
            "identity": 
            {
                "type": "SystemAssigned"
            },
            "destination": 
            {
                "endpointType": "ServiceBusTopic",
                "properties": 
                {
                    "resourceId": "/subscriptions/<AZURE SUBSCRIPTION ID>/resourceGroups/<RESOURCE GROUP NAME>/providers/Microsoft.ServiceBus/namespaces/<SERVICE BUS NAMESPACE NAME>/topics/<SERVICE BUS TOPIC NAME>"
                }
            }
        },
        "eventDeliverySchema": "EventGridSchema"
    }
}

备注

不支持将事件传递到另一个租户中的 Azure 服务总线队列或主题。Delivering events to an Azure Servive Bus queue or topic in another tenant isn't supported.

后续步骤Next steps

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