服务总线队列和主题作为 Azure 事件网格事件的事件处理程序

事件处理程序通过事件网格从事件源接收事件,并处理这些事件。 可以使用一些 Azure 服务的实例来处理事件,Azure 服务总线就是其中之一。 本文介绍如何使用服务总线队列或主题作为事件网格中事件的处理程序。

服务总线队列

可以将事件网格中的事件直接路由到服务总线队列,用于企业应用程序中的缓冲或命令和控制方案。

使用 Azure 门户

在 Azure 门户中,请在创建事件订阅时选择“服务总线队列”作为终结点类型,然后单击“选择终结点”以选择服务总线队列。

Screenshot showing the configuration of a Service Bus queue handler.

注意

不支持将启用了会话的队列作为 Azure 事件网格事件的事件处理程序

使用 Azure CLI

使用 az eventgrid event-subscription create 命令,--endpoint-type 设置为 servicebusqueue--endpoint 设置为 /subscriptions/{AZURE SUBSCRIPTION}/resourceGroups/<RESOURCE GROUP NAME>/providers/Microsoft.ServiceBus/namespaces/<NAMESPACE NAME>/queues/<QUEUE NAME>。 下面是一个示例:

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

还可以对自定义主题使用 az eventgrid topic event-subscription 命令,对系统主题使用 az eventgrid system-topic event-subscription 命令,对合作伙伴主题使用 az eventgrid partner topic event-subscription create 命令。

使用 Azure PowerShell

使用 New-AzEventGridSubscription 命令,将 -EndpointType 设置为 servicebusqueue-Endpoint 设置为 /subscriptions/{AZURE SUBSCRIPTION}/resourceGroups/<RESOURCE GROUP NAME>/providers/Microsoft.ServiceBus/namespaces/<NAMESPACE NAME>/queues/<QUEUE NAME>。 下面是一个示例:

New-AzEventGridSubscription -ResourceGroup MyResourceGroup `
            -TopicName Topic1 `
            -EndpointType servicebusqueue `
            -Endpoint /subscriptions/{SubID}/resourceGroups/TestRG/providers/Microsoft.ServiceBus/namespaces/ns1/queues/queue1 `
            -EventSubscriptionName EventSubscription1

还可以对系统主题使用 New-AzEventGridSystemTopicEventSubscription 命令,对合作伙伴主题使用 New-AzEventGridPartnerTopicEventSubscription 命令。

服务总线主题

可以将事件网格中的事件直接路由到命令和控制消息传送方案的服务总线主题。

使用 Azure 门户

在 Azure 门户中,请在创建事件订阅时选择“服务总线主题”作为终结点类型,然后单击“选择终结点”以选择服务总线主题。

Screenshot showing the configuration of a Service Bus topic handler.

使用 Azure CLI

使用 az eventgrid event-subscription create 命令,--endpoint-type 设置为 servicebustopic--endpoint 设置为 /subscriptions/{AZURE SUBSCRIPTION}/resourceGroups/<RESOURCE GROUP NAME>/providers/Microsoft.ServiceBus/namespaces/<NAMESPACE NAME>/topics/<TOPIC NAME>。 下面是一个示例:

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

还可以对自定义主题使用 az eventgrid topic event-subscription 命令,对系统主题使用 az eventgrid system-topic event-subscription 命令,对合作伙伴主题使用 az eventgrid partner topic event-subscription create 命令。

使用 Azure PowerShell

使用 New-AzEventGridSubscription 命令,将 -EndpointType 设置为 servicebustopic-Endpoint 设置为 /subscriptions/{AZURE SUBSCRIPTION}/resourceGroups/<RESOURCE GROUP NAME>/providers/Microsoft.ServiceBus/namespaces/<NAMESPACE NAME>/topics/<TOPIC NAME>。 下面是一个示例:

New-AzEventGridSubscription -ResourceGroup MyResourceGroup `
            -TopicName Topic1 `
            -EndpointType servicebustopic `
            -Endpoint /subscriptions/{SubID}/resourceGroups/TestRG/providers/Microsoft.ServiceBus/namespaces/ns1/topics/topic1 `
            -EventSubscriptionName EventSubscription1

还可以对系统主题使用 New-AzEventGridSystemTopicEventSubscription 命令,对合作伙伴主题使用 New-AzEventGridPartnerTopicEventSubscription 命令。

消息标头

以下是在消息标头中接收的属性:

属性名称 说明
aeg-subscription-name 事件订阅的名称。
aeg-delivery-count 针对该事件进行尝试的次数。
aeg-event-type

事件的类型。

可以为下列值之一:

  • SubscriptionValidation
  • 通知
  • SubscriptionDeletion
aeg-metadata-version

事件的元数据版本。

对于“事件网格事件架构”,此属性表示元数据版本;对于“云事件架构”,此属性表示规范版本。

aeg-data-version

事件的数据版本。

对于“事件网格事件架构”,此属性表示数据版本;对于“云事件架构”,此属性不适用。

aeg-output-event-id 事件网格事件的 ID。

将事件作为中转消息发送到服务总线队列或主题时,中转消息的messageid是内部系统 ID。

消息的内部系统 ID 会在事件重新传递期间保留,因此可以在服务总线实体上启用“重复项检测”,避免重复传递。 建议在服务总线实体上启用重复项检测持续时间,并将其设为事件的生存时间 (TTL) 或最大重试持续时间(以较长者为准)。

传递属性

通过事件订阅,可以设置已传递事件中包含的 HTTP 头。 此功能可用于设置目标所需的自定义标头。 可以对传递到 Azure 服务总线队列和主题的事件设置自定义标头。

Azure 服务总线支持在发送单个消息时使用以下消息属性。

标头名称 标头类型
MessageId 动态
PartitionKey 静态或动态
SessionId 静态或动态
CorrelationId 静态或动态
Label 静态或动态
ReplyTo 静态或动态
ReplyToSessionId 静态或动态
To 静态或动态
ViaPartitionKey 静态或动态

注意

  • MessageId 的默认值是事件网格事件的内部 ID。 你可以替代它。 例如,data.field
  • 只能设置 SessionIdMessageId

有关详细信息,请参阅自定义传送属性

REST 示例(适用于 PUT)

服务总线队列

{
    "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"
	}
}

服务总线队列 - 使用托管标识传送

{
	"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"
	}
}

服务总线主题

{
    "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"
	}
}

服务总线主题 - 使用托管标识传送

{
    "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"
	}
}

注意

当已启用异地灾难恢复的服务总线命名空间发生故障转移时,辅助命名空间不会向事件网格发出事件。 需要手动添加辅助命名空间的事件网格订阅。

后续步骤

如需支持的事件处理程序的列表,请参阅事件处理程序一文。