使用 Azure CLI 将 Azure 应用程序配置事件路由到 Web 终结点Route Azure App Configuration events to a web endpoint with Azure CLI

本文介绍如何设置 Azure 应用程序配置事件订阅,以将键值修改事件发送到 Web 终结点。In this article, you learn how to set up Azure App Configuration event subscriptions to send key-value modification events to a web endpoint. Azure 应用程序配置用户可以订阅每当键值发生修改时发出的事件。Azure App Configuration users can subscribe to events emitted whenever key-values are modified. 这些事件可以触发 Webhook、Azure Functions、Azure 存储队列,或 Azure 事件网格支持的任何其他事件处理程序。These events can trigger web hooks, Azure Functions, Azure Storage Queues, or any other event handler that is supported by Azure Event Grid. 通常,你会将事件发送到处理事件数据并执行操作的终结点。Typically, you send events to an endpoint that processes the event data and takes actions. 但是,为了简化本文,你将事件发送到收集并显示消息的 Web 应用。However, to simplify this article, you send the events to a web app that collects and displays the messages.

先决条件Prerequisites

备注

请先运行 az cloud set -n AzureChinaCloud 更改云环境,然后才能在 Azure 中国中使用 Azure CLI。Before you can use Azure CLI in Azure China , please run az cloud set -n AzureChinaCloud first to change the cloud environment. 若要切换回 Azure 公有云,请再次运行 az cloud set -n AzureCloudIf you want to switch back to Azure Public Cloud, run az cloud set -n AzureCloud again.

为了在本地安装并使用 CLI,本文要求运行最新版的 Azure CLI(2.0.70 或更高版本)。To install and use the CLI locally, this article requires that you're running the latest version of Azure CLI (2.0.70 or later). 要查找版本,请运行 az --versionTo find the version, run az --version. 如果需要进行安装或升级,请参阅安装 Azure CLIIf you need to install or upgrade, see Install Azure CLI.

创建资源组Create a resource group

事件网格主题是 Azure 资源,必须放置在 Azure 资源组中。Event Grid topics are Azure resources, and must be placed in an Azure resource group. 该资源组是在其中部署和管理 Azure 资源的逻辑集合。The resource group is a logical collection into which Azure resources are deployed and managed.

使用“az group create”命令创建资源组。Create a resource group with the az group create command.

以下示例在 chinaeast2 位置创建名为 <resource_group_name> 的资源组。The following example creates a resource group named <resource_group_name> in the chinaeast2 location. <resource_group_name> 替换为资源组的唯一名称。Replace <resource_group_name> with a unique name for your resource group.

az group create --name <resource_group_name> --location chinaeast2

创建应用配置存储区Create an App Configuration store

<appconfig_name> 替换为配置存储的唯一名称,将 <resource_group_name> 替换为前面创建的资源组。Replace <appconfig_name> with a unique name for your configuration store, and <resource_group_name> with the resource group you created earlier. 该名称必须唯一,因为它将用作 DNS 名称。The name must be unique because it is used as a DNS name.

az appconfig create \
  --name <appconfig_name> \
  --location chinaeast2 \
  --resource-group <resource_group_name> \
  --sku free

创建消息终结点Create a message endpoint

在订阅主题之前,让我们创建事件消息的终结点。Before subscribing to the topic, let's create the endpoint for the event message. 通常情况下,终结点基于事件数据执行操作。Typically, the endpoint takes actions based on the event data. 为了简化此快速入门,将部署用于显示事件消息的预建的 Web 应用To simplify this quickstart, you deploy a pre-built web app that displays the event messages. 所部署的解决方案包括应用服务计划、应用服务 Web 应用和 GitHub 中的源代码。The deployed solution includes an App Service plan, an App Service web app, and source code from GitHub.

<your-site-name> 替换为 Web 应用的唯一名称。Replace <your-site-name> with a unique name for your web app. Web 应用名称必须唯一,因为它是 DNS 条目的一部分。The web app name must be unique because it's part of the DNS entry.

$sitename=<your-site-name>

az deployment group create \
  --resource-group <resource_group_name> \
  --template-uri "https://raw.githubusercontent.com/Azure-Samples/azure-event-grid-viewer/master/azuredeploy.json" \
  --parameters siteName=$sitename hostingPlanName=viewerhost

部署可能需要几分钟才能完成。The deployment may take a few minutes to complete. 部署成功后,请查看 Web 应用以确保它正在运行。After the deployment has succeeded, view your web app to make sure it's running. 在 Web 浏览器中导航到 https://<your-site-name>.azurewebsites.netIn a web browser, navigate to: https://<your-site-name>.azurewebsites.net

应会看到站点上当前未显示任何消息。You should see the site with no messages currently displayed.

启用事件网格资源提供程序Enable Event Grid resource provider

如果以前未在 Azure 订阅中使用过事件网格,则可能需要注册事件网格资源提供程序。If you haven't previously used Event Grid in your Azure subscription, you may need to register the Event Grid resource provider. 运行以下命令,注册提供程序:Run the following command to register the provider:

az provider register --namespace Microsoft.EventGrid

完成注册可能需要一些时间。It may take a moment for the registration to finish. 若要检查状态,请运行:To check the status, run:

az provider show --namespace Microsoft.EventGrid --query "registrationState"

registrationStateRegistered 后,即可继续。When registrationState is Registered, you're ready to continue.

订阅应用程序配置存储Subscribe to your App Configuration store

订阅主题,以告知事件网格要跟踪哪些事件以及要将这些事件发送到哪个位置。You subscribe to a topic to tell Event Grid which events you want to track and where to send those events. 以下示例订阅所创建的应用程序配置,并将 Web 应用中的 URL 作为事件通知的终结点传递。The following example subscribes to the App Configuration you created, and passes the URL from your web app as the endpoint for event notification. <event_subscription_name> 替换为事件订阅的名称。Replace <event_subscription_name> with a name for your event subscription. 对于 <resource_group_name><appconfig_name>,请使用此前创建的值。For <resource_group_name> and <appconfig_name>, use the values you created earlier.

Web 应用的终结点必须包括后缀 /api/updates/The endpoint for your web app must include the suffix /api/updates/.

appconfigId=$(az appconfig show --name <appconfig_name> --resource-group <resource_group_name> --query id --output tsv)
endpoint=https://$sitename.azurewebsites.net/api/updates

az eventgrid event-subscription create \
  --resource-id $appconfigId \
  --name <event_subscription_name> \
  --endpoint $endpoint

再次查看 Web 应用,并注意现已向该应用发送了订阅验证事件。View your web app again, and notice that a subscription validation event has been sent to it. 选择眼睛图标以展开事件数据。Select the eye icon to expand the event data. 事件网格发送验证事件,以便终结点可以验证它是否想要接收事件数据。Event Grid sends the validation event so the endpoint can verify that it wants to receive event data. Web 应用包含用于验证订阅的代码。The web app includes code to validate the subscription.

查看订阅事件

触发应用程序配置事件Trigger an App Configuration event

现在,让我们触发一个事件,看事件网格如何将消息分发到终结点。Now, let's trigger an event to see how Event Grid distributes the message to your endpoint. 使用前面指定的 <appconfig_name> 创建键-值。Create a key-value using the <appconfig_name> from earlier.

az appconfig kv set --name <appconfig_name> --key Foo --value Bar --yes

现已触发事件,并且事件网格已将消息发送到订阅时配置的终结点。You've triggered the event, and Event Grid sent the message to the endpoint you configured when subscribing. 查看 Web 应用以查看刚刚发送的事件。View your web app to see the event you just sent.

[{
  "id": "deb8e00d-8c64-4b6e-9cab-282259c7674f",
  "topic": "/subscriptions/{subscription-id}/resourceGroups/eventDemoGroup/providers/microsoft.appconfiguration/configurationstores/{appconfig-name}",
  "subject": "https://{appconfig-name}.azconfig.io/kv/Foo",
  "data": {
    "key": "Foo",
    "etag": "a1LIDdNEIV6wCnfv3xaip7fMXD3"
  },
  "eventType": "Microsoft.AppConfiguration.KeyValueModified",
  "eventTime": "2019-05-31T18:59:54Z",
  "dataVersion": "1",
  "metadataVersion": "1"
}]

清理资源Clean up resources

如果你打算继续使用此应用程序配置和事件订阅,请不要清理在本文中创建的资源。If you plan to continue working with this App Configuration and event subscription, do not clean up the resources created in this article. 如果不打算继续学习,请使用以下命令删除本文中创建的资源。If you do not plan to continue, use the following command to delete the resources you created in this article.

<resource_group_name> 替换为上面创建的资源组。Replace <resource_group_name> with the resource group you created above.

az group delete --name <resource_group_name>

后续步骤Next steps

了解如何创建主题和事件订阅后,请详细了解键-值事件以及事件网格的功能:Now that you know how to create topics and event subscriptions, learn more about key-value events and what Event Grid can help you do: