快速入门:使用 Azure CLI 和事件网格将自定义事件路由到 Azure 队列存储Quickstart: Route custom events to Azure Queue storage with Azure CLI and Event Grid

Azure 事件网格是针对云的事件处理服务。Azure Event Grid is an eventing service for the cloud. Azure 队列存储是受支持的事件处理程序之一。Azure Queue storage is one of the supported event handlers. 在本文中,将使用 Azure CLI 创建一个自定义主题,然后订阅该自定义主题,再触发可查看结果的事件。In this article, you use the Azure CLI to create a custom topic, subscribe to the custom topic, and trigger the event to view the result. 将事件发送到队列存储。You send the events to the Queue storage.

如果没有 Azure 订阅,可在开始前创建一个试用帐户If you don't have an Azure subscription, create a trial account before you begin.

如果使用本地计算机上的 Azure CLI 或 Azure PowerShell,请确保具有以下版本的 Azure CLI 和 Azure PowerShell。If you are using Azure CLI or Azure PowerShell on your local machine, ensure that you have the following versions of Azure CLI and Azure PowerShell.

  • Azure CLI 2.0.56 或更高版本。Azure CLI version 2.0.56 or greater. 有关安装 Azure CLI 的最新版本的说明,请参阅安装 Azure CLIFor instructions on installing the latest version of Azure CLI, see Install the Azure CLI.
  • Azure PowerShell 1.1.0 或更高版本。Azure PowerShell version 1.1.0 or greater. Azure 下载 - 命令行工具在 Windows 计算机上下载 Azure PowerShell 的最新版本。Download the latest version of Azure PowerShell on your Windows machine from Azure downloads - Command-line tools.

本文提供了用于使用 Azure CLI 的命令。This article gives you commands for using 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.

以下示例在“chinanorth2”位置创建名为“gridResourceGroup”的资源组。The following example creates a resource group named gridResourceGroup in the chinanorth2 location.

az group create --name gridResourceGroup --location chinanorth2

启用事件网格资源提供程序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.

创建自定义主题Create a custom topic

事件网格主题提供用户定义的终结点,可向其发布事件。An event grid topic provides a user-defined endpoint that you post your events to. 以下示例在资源组中创建自定义主题。The following example creates the custom topic in your resource group. <topic_name> 替换为自定义主题的唯一名称。Replace <topic_name> with a unique name for your custom topic. 事件网格主题名称必须唯一,因为它由 DNS 条目表示。The event grid topic name must be unique because it's represented by a DNS entry.

az eventgrid topic create --name <topic_name> -l chinanorth2 -g gridResourceGroup

创建队列存储Create Queue storage

在订阅自定义主题之前,让我们创建事件消息的终结点。Before subscribing to the custom topic, let's create the endpoint for the event message. 创建用于收集事件的队列存储。You create a Queue storage for collecting the events.

storagename="<unique-storage-name>"
queuename="eventqueue"

az storage account create -n $storagename -g gridResourceGroup -l chinanorth2 --sku Standard_LRS
az storage queue create --name $queuename --account-name $storagename

订阅自定义主题Subscribe to a custom topic

订阅自定义主题是为了告知事件网格要跟踪哪些事件。以下示例订阅所创建的自定义主题,并传递终结点队列存储的资源 ID。You subscribe to a custom topic to tell Event Grid which events you want to track. The following example subscribes to the custom topic you created, and passes the resource ID of the Queue storage for the endpoint. 通过 Azure CLI,可采用终结点的形式传递队列存储 ID。With Azure CLI, you pass the Queue storage ID as the endpoint. 终结点的格式如下:The endpoint is in the format:

/subscriptions/<subscription-id>/resourcegroups/<resource-group-name>/providers/Microsoft.Storage/storageAccounts/<storage-name>/queueservices/default/queues/<queue-name>

以下脚本获取队列存储帐户的资源 ID。The following script gets the resource ID of the storage account for the queue. 它构造队列存储的 ID 并订阅事件网格主题。It constructs the ID for the queue storage, and subscribes to an event grid topic. 它将终结点类型设置为 storagequeue 并使用终结点的队列 ID。It sets the endpoint type to storagequeue and uses the queue ID for the endpoint.

storageid=$(az storage account show --name $storagename --resource-group gridResourceGroup --query id --output tsv)
queueid="$storageid/queueservices/default/queues/$queuename"
topicid=$(az eventgrid topic show --name <topic_name> -g gridResourceGroup --query id --output tsv)

az eventgrid event-subscription create \
  --source-resource-id $topicid \
  --name <event_subscription_name> \
  --endpoint-type storagequeue \
  --endpoint $queueid \
  --expiration-date "<yyyy-mm-dd>"

创建事件订阅的帐户必须对队列存储具有写访问权限。The account that creates the event subscription must have write access to the queue storage. 请注意为订阅设置到期日期Notice that an expiration date is set for the subscription.

如果使用 REST API 创建订阅,则以独立参数的形式传递存储帐户 ID 和队列名称。If you use the REST API to create the subscription, you pass the ID of the storage account and the name of the queue as a separate parameter.

"destination": {
  "endpointType": "storagequeue",
  "properties": {
    "queueName":"eventqueue",
    "resourceId": "/subscriptions/<subscription-id>/resourcegroups/<resource-group-name>/providers/Microsoft.Storage/storageAccounts/<storage-name>"
  }
  ...

向自定义主题发送事件Send an event to your custom topic

让我们触发一个事件,看看事件网格如何将消息分发到终结点。Let's trigger an event to see how Event Grid distributes the message to your endpoint. 首先,让我们获取自定义主题的 URL 和密钥。First, let's get the URL and key for the custom topic. 同样,请将自定义主题名称用于 <topic_name>Again, use your custom topic name for <topic_name>.

endpoint=$(az eventgrid topic show --name <topic_name> -g gridResourceGroup --query "endpoint" --output tsv)
key=$(az eventgrid topic key list --name <topic_name> -g gridResourceGroup --query "key1" --output tsv)

为简化本文,将使用要发送到自定义主题的示例事件数据。To simplify this article, you use sample event data to send to the custom topic. 通常情况下,应用程序或 Azure 服务会发送事件数据。Typically, an application or Azure service would send the event data. CURL 是发送 HTTP 请求的实用工具。CURL is a utility that sends HTTP requests. 本文使用 CURL 向自定义主题发送事件。In this article, use CURL to send the event to the custom topic. 以下示例将三个事件发送到事件网格主题:The following example sends three events to the event grid topic:

for i in 1 2 3
do
   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" $endpoint
done

导航到门户中的队列存储,请注意,事件网格会将这三个事件发送到队列。Navigate to the Queue storage in the portal, and notice that Event Grid sent those three events to the queue.

显示消息

清理资源Clean up resources

如果打算继续处理此事件,请不要清除本文中创建的资源。If you plan to continue working with this event, don't clean up the resources created in this article. 否则,请使用以下命令删除本文中创建的资源。Otherwise, use the following command to delete the resources you created in this article.

az group delete --name gridResourceGroup

后续步骤Next steps

了解如何创建主题和事件订阅以后,即可进一步学习事件网格的功能:Now that you know how to create topics and event subscriptions, learn more about what Event Grid can help you do: