你当前正在访问 Microsoft Azure Global Edition 技术文档网站。 如果需要访问由世纪互联运营的 Microsoft Azure 中国技术文档网站,请访问 https://docs.azure.cn

使用托管标识进行事件传递

本文介绍如何使用 Azure 事件网格系统主题、自定义主题或域的 托管服务标识 。 使用它可将事件转发到支持的目标,例如服务总线队列和主题、事件中心和存储帐户。

先决条件

  1. 将系统分配的标识或用户分配的标识分配给系统主题、自定义主题或域。

  2. 在目标(例如,服务总线队列)上将标识添加到相应角色(例如,服务总线数据发送方)。 有关详细步骤,请参阅向目标上的 Azure 角色添加标识

    注释

    目前,无法使用 专用终结点传递事件。 有关详细信息,请参阅本文末尾的 “专用终结点 ”部分。

创建使用标识的事件订阅

当事件网格自定义主题、系统主题或域已分配有托管标识并且已向目标上的相应角色添加该标识之后,就可以创建使用该标识的订阅了。

使用 Azure 门户

创建事件订阅时,将在“终结点详细信息”部分看到一个选项,用于启用该终结点的系统分配标识或用户分配标识。

以下示例说明如何在通过服务总线队列创建事件订阅目标时启用系统分配的标识。

显示在创建服务总线队列的事件订阅时如何启用身份的屏幕截图。

还可以在“附加功能”选项卡上启用将系统分配的标识用于死信的功能。

屏幕截图,显示如何为死信启用系统分配的标识。

可以在创建事件订阅后对其启用托管标识。 在 事件订阅的事件订阅 页上,切换到 “其他功能 ”选项卡以查看选项。 还可以在此页上为死信启用标识。

显示如何在现有事件订阅上启用系统分配标识的屏幕截图。

如果已为主题启用用户分配标识,将会在“托管标识类型”的下拉列表中看到“用户分配标识”选项已启用。 如果为托管标识类型选择用户分配,则可以选择要用于传递事件的用户分配标识。

显示如何在事件订阅上启用用户分配标识的屏幕截图。

使用 Azure CLI - 服务总线队列

本部分介绍如何使用 Azure CLI 来使用系统分配的标识将事件传送到服务总线队列。 标识必须是“Azure 服务总线数据发送方”角色的成员。 它还必须是用于死信的存储帐户上的“存储 Blob 数据参与者”角色的成员。

定义变量

首先,指定要在 CLI 命令中使用的以下变量的值。

subid="<AZURE SUBSCRIPTION ID>"
rg = "<RESOURCE GROUP of EVENT GRID CUSTOM TOPIC>"
topicname = "<EVENT GRID TOPIC NAME>"

# get the service bus queue resource id
queueid=$(az servicebus queue show --namespace-name <SERVICE BUS NAMESPACE NAME> --name <QUEUE NAME> --resource-group <RESOURCE GROUP NAME> --query id --output tsv)
sb_esname = "<Specify a name for the event subscription>" 

使用托管标识创建用于传送的事件订阅

此示例命令为事件网格自定义主题创建事件订阅,该主题的终结点类型设置为 服务总线队列

az eventgrid event-subscription create  
    --source-resource-id /subscriptions/$subid/resourceGroups/$rg/providers/Microsoft.EventGrid/topics/$topicname
    --delivery-identity-endpoint-type servicebusqueue  
    --delivery-identity systemassigned 
    --delivery-identity-endpoint $queueid
    -n $sb_esname 

使用托管标识创建用于传送和死信的事件订阅

此示例命令为事件网格自定义主题创建事件订阅,该主题的终结点类型设置为 服务总线队列。 它还指定将系统托管标识用于死信。

storageid=$(az storage account show --name demoStorage --resource-group gridResourceGroup --query id --output tsv)
deadletterendpoint="$storageid/blobServices/default/containers/<BLOB CONTAINER NAME>"

az eventgrid event-subscription create  
    --source-resource-id /subscriptions/$subid/resourceGroups/$rg/providers/Microsoft.EventGrid/topics/$topicname 
    --delivery-identity-endpoint-type servicebusqueue
    --delivery-identity systemassigned 
    --delivery-identity-endpoint $queueid
    --deadletter-identity-endpoint $deadletterendpoint 
    --deadletter-identity systemassigned 
    -n $sb_esnameq 

使用 Azure CLI - 事件中心

本部分介绍如何使用 Azure CLI 来使用系统分配的标识将事件传送到事件中心。 该标识必须是“Azure 事件中心数据发送方”角色的成员。 它还必须是用于死信的存储帐户上的“存储 Blob 数据参与者”角色的成员。

定义变量

subid="<AZURE SUBSCRIPTION ID>"
rg = "<RESOURCE GROUP of EVENT GRID CUSTOM TOPIC>"
topicname = "<EVENT GRID CUSTOM TOPIC NAME>"

hubid=$(az eventhubs eventhub show --name <EVENT HUB NAME> --namespace-name <NAMESPACE NAME> --resource-group <RESOURCE GROUP NAME> --query id --output tsv)
eh_esname = "<SPECIFY EVENT SUBSCRIPTION NAME>" 

使用托管标识创建用于传送的事件订阅

此示例命令为事件网格自定义主题创建事件订阅,该主题的终结点类型设置为 事件中心

az eventgrid event-subscription create  
    --source-resource-id /subscriptions/$subid/resourceGroups/$rg/providers/Microsoft.EventGrid/topics/$topicname 
    --delivery-identity-endpoint-type eventhub 
    --delivery-identity systemassigned 
    --delivery-identity-endpoint $hubid
    -n $sbq_esname 

使用托管标识创建用于传送和死信的事件订阅

此示例命令为事件网格自定义主题创建事件订阅,该主题的终结点类型设置为 事件中心。 它还指定将系统托管标识用于死信。

storageid=$(az storage account show --name demoStorage --resource-group gridResourceGroup --query id --output tsv)
deadletterendpoint="$storageid/blobServices/default/containers/<BLOB CONTAINER NAME>"

az eventgrid event-subscription create
    --source-resource-id /subscriptions/$subid/resourceGroups/$rg/providers/Microsoft.EventGrid/topics/$topicname 
    --delivery-identity-endpoint-type servicebusqueue  
    --delivery-identity systemassigned 
    --delivery-identity-endpoint $hubid
    --deadletter-identity-endpoint $eh_deadletterendpoint
    --deadletter-identity systemassigned 
    -n $eh_esname 

使用 Azure CLI - Azure 存储队列

本部分介绍如何使用 Azure CLI 来使用系统分配的标识将事件传送到 Azure 存储队列。 该标识必须是存储帐户上“存储队列数据消息发送方”角色的成员。 它还必须是用于死信的存储帐户上的“存储 Blob 数据参与者”角色的成员。

定义变量

subid="<AZURE SUBSCRIPTION ID>"
rg = "<RESOURCE GROUP of EVENT GRID CUSTOM TOPIC>"
topicname = "<EVENT GRID CUSTOM TOPIC NAME>"

# get the storage account resource id
storageid=$(az storage account show --name <STORAGE ACCOUNT NAME> --resource-group <RESOURCE GROUP NAME> --query id --output tsv)

# build the resource id for the queue
queueid="$storageid/queueservices/default/queues/<QUEUE NAME>" 

sa_esname = "<SPECIFY EVENT SUBSCRIPTION NAME>" 

使用托管标识创建用于传送的事件订阅

az eventgrid event-subscription create 
    --source-resource-id /subscriptions/$subid/resourceGroups/$rg/providers/Microsoft.EventGrid/topics/$topicname 
    --delivery-identity-endpoint-type storagequeue  
    --delivery-identity systemassigned 
    --delivery-identity-endpoint $queueid
    -n $sa_esname 

使用托管标识创建用于传送和死信的事件订阅

storageid=$(az storage account show --name demoStorage --resource-group gridResourceGroup --query id --output tsv)
deadletterendpoint="$storageid/blobServices/default/containers/<BLOB CONTAINER NAME>"

az eventgrid event-subscription create  
    --source-resource-id /subscriptions/$subid/resourceGroups/$rg/providers/Microsoft.EventGrid/topics/$topicname 
    --delivery-identity-endpoint-type storagequeue  
    --delivery-identity systemassigned 
    --delivery-identity-endpoint $queueid
    --deadletter-identity-endpoint $deadletterendpoint 
    --deadletter-identity systemassigned 
    -n $sa_esname 

专用终结点

目前,无法使用 专用终结点传递事件。 也就是说,如果你有严格的网络隔离要求,那么在这种情况下不支持,因为你的传递事件流量不能离开专用 IP 空间。

但是,如果你的要求要求使用加密通道和发送方的已知标识(在本例中为事件网格)使用公共 IP 空间发送事件,则可以使用 Azure 事件网格自定义主题或具有托管标识的域将事件传送到事件中心、服务总线或 Azure 存储服务,如本文中所示。 然后,可以使用在 Azure Functions 中配置的专用链接或部署在虚拟网络上的 Webhook 来拉取事件。 请参阅教程: 使用 Azure Functions 连接到专用终结点

在此配置下,流量通过公共 IP 或 Internet 从事件网格传输到事件中心、服务总线或 Azure 存储,通道可以加密,并且使用事件网格的托管标识。 如果将部署到虚拟网络的 Azure Functions 或 Webhook 配置为通过专用链接使用事件中心、服务总线或 Azure 存储,则流量的该部分显然会保留在 Azure 中。

后续步骤

若要了解托管标识,请参阅 什么是 Azure 资源的托管标识