使用 Azure CLI 创建自定义主题并订阅 Azure 订阅的事件

本文提供了一个示例 Azure CLI 脚本,演示如何使用 Azure CLI 创建自定义主题并向其发送事件。

如果没有 Azure 订阅,可在开始前创建一个试用帐户

先决条件

可以使用本地 Azure CLI。

  • 如果需要,请安装 Azure CLI 来运行 CLI 参考命令。

  • 本地 Azure CLI,请了解如何安装 Azure CLI。 如果在 Windows 或 macOS 上运行,请考虑在 Docker 容器中运行 Azure CLI。 有关详细信息,请参阅如何在 Docker 容器中运行 Azure CLI

    • 通过使用 az login 命令登录到 Azure CLI。 若要完成身份验证过程,请遵循终端中显示的步骤。 有关其他登录选项,请参阅使用 Azure CLI 登录

    • 出现提示时,请在首次使用时安装 Azure CLI 扩展。 有关扩展详细信息,请参阅使用 Azure CLI 的扩展

    • 运行 az version 以查找安装的版本和依赖库。 若要升级到最新版本,请运行 az upgrade

示例脚本

运行脚本

# Create Event Grid custom topic

# Variable block
let "randomIdentifier=$RANDOM*$RANDOM"
location="chinanorth2"
subscriptionId="$(az account show --query id -o tsv)"
resourceGroup="msdocs-event-grid-rg-$randomIdentifier"
tag="event-grid"
topic="msdocs-event-grid-topic-$randomIdentifier"
site="msdocs-event-grid-site-$randomIdentifier"
eventSubscription="msdocs-event-subscription-$randomIdentifier"
webappEndpoint="https://$site.chinacloudsites.cn/api/updates"
storage="msdocsstorage$randomIdentifier"

# Create a resource group
echo "Creating  in "$location"..."
az group create --name $resourceGroup --location "$location" --tag $tag

# Enable and then show the Event Grid resource provider
az provider register --namespace Microsoft.EventGrid
az provider show --namespace Microsoft.EventGrid --query "registrationState"

# Create custom topic
echo "Creating $topic"
az eventgrid topic create \
--resource-group $resourceGroup \
--name $topic \
--location "$location"

# Create a message endpoint
echo "Creating $site"
az deployment group create \
  --resource-group $resourceGroup \
  --template-uri "https://raw.githubusercontent.com/Azure-Samples/azure-event-grid-viewer/master/azuredeploy.json" \
  --parameters siteName=$site hostingPlanName=viewerhost

# To view your web app, navigate to https://<your-site-name>.chinacloudsites.cn

# Subscribe to custom topic
az eventgrid event-subscription create \
  --source-resource-id "/subscriptions/$subscriptionId/resourceGroups/$resourceGroup/providers/Microsoft.EventGrid/topics/$topic" \
  --name demoViewerSub \
  --endpoint $webappEndpoint

# View your web app again to see the subscription validation event.
# Select the eye icon to expand the event data
  
# Send an event to your custom topic
url=$(az eventgrid topic show --name $topic -g $resourceGroup --query "endpoint" --output tsv)
key=$(az eventgrid topic key list --name $topic -g $resourceGroup --query "key1" --output tsv)
echo $url
echo $key
event='[ {"id": "'"$RANDOM"'", "eventType": "recordInserted", "subject": "myapp/vehicles/motorcycles", "eventTime": "'`date +%Y-%m-%dT%H:%M:%S%z`'", "data":{ "make": "Ducati", "model": "Monster"},"dataVersion": "1.0"} ]'
curl -X POST -H "aeg-sas-key: $key" -d "$event" $url

# View your web app again to see the event that you just sent

清理资源

使用 az group delete 命令删除资源组以及与其关联的所有资源 - 除非你持续需要这些资源。 其中一些资源在创建和删除时可能要稍等片刻。

az group delete --name $resourceGroup

示例参考

此脚本使用以下命令创建事件订阅。 表中的每条命令链接到特定于命令的文档。

命令 注释
az eventgrid event-subscription create 创建事件网格订阅。

后续步骤