Azure 服务总线到事件网格的集成概述Azure Service Bus to Event Grid integration overview

Azure 服务总线已推出与 Azure 事件网格集成的新功能。Azure Service Bus has launched a new integration to Azure Event Grid. 此功能实现的重要方案是,消息量不大的服务总线队列或订阅不需要一直使用接收器来轮询消息。The key scenario of this feature is that Service Bus queues or subscriptions with a low volume of messages do not need to have a receiver that polls for messages continuously.

现在,当队列或订阅中存在消息时,服务总线可以在没有接收器的情况下,将事件发出到事件网格。Service Bus can now emit events to Event Grid when there are messages in a queue or a subscription when no receivers are present. 可以针对服务总线命名空间创建事件网格订阅、侦听这些事件,然后通过启动接收器对这些事件做出反应。You can create Event Grid subscriptions to your Service Bus namespaces, listen to these events, and then react to the events by starting a receiver. 借助此功能,可以在反应式编程模型中使用服务总线。With this feature, you can use Service Bus in reactive programming models.

若要启用此功能,需要准备好以下各项:To enable the feature, you need the following items:

  • 至少包含一个服务总线队列的服务总线高级命名空间,或者至少包含一个订阅的服务总线主题。A Service Bus Premium namespace with at least one Service Bus queue or a Service Bus topic with at least one subscription.
  • 对服务总线命名空间拥有“参与者”访问权限。Contributor access to the Service Bus namespace.
  • 此外,需要服务总线命名空间的事件网格订阅。Additionally, you need an Event Grid subscription for the Service Bus namespace. 存在可提取的消息时,此订阅将从事件网格接收通知。This subscription receives a notification from Event Grid that there are messages to be picked up. 典型的订阅方可能是 Azure 应用服务的逻辑应用功能、Azure Functions,或者与 Web 应用联系的 Webhook。Typical subscribers could be the Logic Apps feature of Azure App Service, Azure Functions, or a webhook contacting a web app. 然后,订阅方会处理消息。The subscriber then processes the messages.

19

备注

本文进行了更新,以便使用新的 Azure PowerShell Az 模块。This article has been updated to use the new Azure PowerShell Az module. 你仍然可以使用 AzureRM 模块,至少在 2020 年 12 月之前,它将继续接收 bug 修补程序。You can still use the AzureRM module, which will continue to receive bug fixes until at least December 2020. 若要详细了解新的 Az 模块和 AzureRM 兼容性,请参阅新 Azure Powershell Az 模块简介To learn more about the new Az module and AzureRM compatibility, see Introducing the new Azure PowerShell Az module. 有关 Az 模块安装说明,请参阅安装 Azure PowerShellFor Az module installation instructions, see Install Azure PowerShell.

验证你是否有参与者访问权限Verify that you have contributor access

转到“服务总线”命名空间,选择“访问控制(IAM)”,并选择“角色分配”选项卡。验证你是否拥有该命名空间的参与者访问权限。Go to your Service Bus namespace, and then select Access control (IAM), and select Role assignments tab. Verify that you have the contributor access to the namespace.

事件和事件架构Events and event schemas

目前,Azure 服务总线会针对两种情况发送事件:Service Bus today sends events for two scenarios:

此外,服务总线使用标准的事件网格安全性和身份验证机制Additionally, Service Bus uses the standard Event Grid security and authentication mechanisms.

有关详细信息,请参阅 Azure 事件网格事件架构For more information, see Azure Event Grid event schemas.

活动消息可用事件Active Messages Available event

如果队列或订阅中存在活动的消息,但却没有接收器侦听,则会生成此事件。This event is generated if you have active messages in a queue or a subscription and there are no receivers listening.

此事件的架构如下:The schema for this event is as follows:

{
  "topic": "/subscriptions/<subscription id>/resourcegroups/DemoGroup/providers/Microsoft.ServiceBus/namespaces/<YOUR SERVICE BUS NAMESPACE WILL SHOW HERE>",
  "subject": "topics/<service bus topic>/subscriptions/<service bus subscription>",
  "eventType": "Microsoft.ServiceBus.ActiveMessagesAvailableWithNoListeners",
  "eventTime": "2018-02-14T05:12:53.4133526Z",
  "id": "dede87b0-3656-419c-acaf-70c95ddc60f5",
  "data": {
    "namespaceName": "YOUR SERVICE BUS NAMESPACE WILL SHOW HERE",
    "requestUri": "https://YOUR-SERVICE-BUS-NAMESPACE-WILL-SHOW-HERE.servicebus.chinacloudapi.cn/TOPIC-NAME/subscriptions/SUBSCRIPTIONNAME/messages/head",
    "entityType": "subscriber",
    "queueName": "QUEUE NAME IF QUEUE",
    "topicName": "TOPIC NAME IF TOPIC",
    "subscriptionName": "SUBSCRIPTION NAME"
  },
  "dataVersion": "1",
  "metadataVersion": "1"
}

死信消息可用事件Dead-letter Messages Available event

对于每个有消息但没有活动接收器的死信队列,你会至少收到一个事件。You get at least one event per Dead Letter queue, which has messages and no active receivers.

此事件的架构如下:The schema for this event is as follows:

[{
  "topic": "/subscriptions/<subscription id>/resourcegroups/DemoGroup/providers/Microsoft.ServiceBus/namespaces/<YOUR SERVICE BUS NAMESPACE WILL SHOW HERE>",
  "subject": "topics/<service bus topic>/subscriptions/<service bus subscription>",
  "eventType": "Microsoft.ServiceBus.DeadletterMessagesAvailableWithNoListener",
  "eventTime": "2018-02-14T05:12:53.4133526Z",
  "id": "dede87b0-3656-419c-acaf-70c95ddc60f5",
  "data": {
    "namespaceName": "YOUR SERVICE BUS NAMESPACE WILL SHOW HERE",
    "requestUri": "https://YOUR-SERVICE-BUS-NAMESPACE-WILL-SHOW-HERE.servicebus.chinacloudapi.cn/TOPIC-NAME/subscriptions/SUBSCRIPTIONNAME/$deadletterqueue/messages/head",
    "entityType": "subscriber",
    "queueName": "QUEUE NAME IF QUEUE",
    "topicName": "TOPIC NAME IF TOPIC",
    "subscriptionName": "SUBSCRIPTION NAME"
  },
  "dataVersion": "1",
  "metadataVersion": "1"
}]

发出事件的数量和频率如何?How many events are emitted, and how often?

如果命名空间中有多个队列和主题或订阅,则对于每个队列和每个订阅,你会至少收到一个事件。If you have multiple queues and topics or subscriptions in the namespace, you get at least one event per queue and one per subscription. 如果服务总线实体中没有任何消息,在新消息抵达时,会立即发出事件。The events are emitted immediately if there are no messages in the Service Bus entity and a new message arrives. 或者,除非服务总线检测到活动的接收方,否则每隔两分钟发送事件。Or the events are emitted every two minutes unless Service Bus detects an active receiver. 对消息进行浏览不会导致事件中断。Message browsing does not interrupt the events.

默认情况下,服务总线针对命名空间中的所有实体发出事件。By default, Service Bus emits events for all entities in the namespace. 如果只需获取特定实体的事件,请参阅下一部分。If you want to get events for specific entities only, see the next section.

使用筛选器限制事件的来源Use filters to limit where you get events from

例如,如果你只想从命名空间中的某个队列或订阅接收事件,可以使用事件网格提供的“开头为”或“结尾为”筛选器。 If you want to get events only from, for example, one queue or one subscription within your namespace, you can use the Begins with or Ends with filters that are provided by Event Grid. 在某些界面中,这两个筛选器称为“前缀”和“后缀”筛选器。 In some interfaces, the filters are called Pre and Suffix filters. 如果需要接收多个(但并非所有)队列和订阅的事件,可以创建多个事件网格订阅,并为每个订阅提供一个筛选器。If you want to get events for multiple, but not all, queues and subscriptions, you can create multiple Event Grid subscriptions and provide a filter for each.

为服务总线命名空间创建事件网格订阅Create Event Grid subscriptions for Service Bus namespaces

可以使用三种不同的方法,为服务总线命名空间创建事件网格订阅:You can create Event Grid subscriptions for Service Bus namespaces in three different ways:

Azure 门户说明Azure portal instructions

若要创建新的事件网格订阅,请执行以下操作:To create a new Event Grid subscription, do the following:

  1. 在 Azure 门户中转到自己的命名空间。In the Azure portal, go to your namespace.

  2. 在左窗格中,选择“事件网格”。In the left pane, select the Event Grid.

  3. 选择“事件订阅”。Select Event Subscription.

    下图显示了包含事件网格订阅的命名空间:The following image displays a namespace that has an Event Grid subscription:

    事件网格订阅

    下图演示了如何在不使用任何特定筛选器的情况下订阅函数或 Webhook:The following image shows how to subscribe to a function or a webhook without any specific filtering:

    21

Azure CLI 说明Azure CLI instructions

首先,请确保已安装 Azure CLI 2.0 或更高版本。First, make sure that you have Azure CLI version 2.0 or later installed. 下载安装程序Download the installer. Windows + X,然后使用管理员权限打开新的 PowerShell 控制台。Select Windows + X, and then open a new PowerShell console with administrator permissions. 或者,也可以在 Azure 门户中使用命令外壳。Alternatively, you can use a command shell within the Azure portal.

执行以下代码:Execute the following code:

az cloud set -n AzureChinaCloud
az login

az account set -s "<Azure subscription name>"

namespaceid=$(az resource show --namespace Microsoft.ServiceBus --resource-type namespaces --name "<service bus namespace>" --resource-group "<resource group that contains the service bus namespace>" --query id --output tsv

az eventgrid event-subscription create --resource-id $namespaceid --name "<YOUR EVENT GRID SUBSCRIPTION NAME (CAN BE ANY NOT EXISTING)>" --endpoint "<your_function_url>" --subject-ends-with "<YOUR SERVICE BUS SUBSCRIPTION NAME>"

如果使用 BASHIf you are using BASH

PowerShell 说明PowerShell instructions

确保已安装 Azure PowerShell。Make sure you have Azure PowerShell installed. 下载安装程序Download the installer. Windows + X,然后使用管理员权限打开新的 PowerShell 控制台。Select Windows + X, and then open a new PowerShell console with Administrator permissions. 或者,也可以在 Azure 门户中使用命令外壳。Alternatively, you can use a command shell within the Azure portal.

Connect-AzAccount -Environment AzureChinaCloud

Select-AzSubscription -SubscriptionName "<YOUR SUBSCRIPTION NAME>"

# This might be installed already
Install-Module Az.ServiceBus

$NSID = (Get-AzServiceBusNamespace -ResourceGroupName "<YOUR RESOURCE GROUP NAME>" -Na
mespaceName "<YOUR NAMESPACE NAME>").Id

New-AzEVentGridSubscription -EventSubscriptionName "<YOUR EVENT GRID SUBSCRIPTION NAME (CAN BE ANY NOT EXISTING)>" -ResourceId $NSID -Endpoint "<YOUR FUNCTION URL>" -SubjectEndsWith "<YOUR SERVICE BUS SUBSCRIPTION NAME>"

可以在这里浏览其他设置选项,或者测试事件是否正在流动。From here, you can explore the other setup options or test that events are flowing.

后续步骤Next steps