您现在访问的是微软AZURE全球版技术文档网站,若需要访问由世纪互联运营的MICROSOFT AZURE中国区技术文档网站,请访问 https://docs.azure.cn.

快速入门:使用 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 free account before you begin.

先决条件Prerequisites

  • Azure Cloud Shell 中使用 Bash 环境。Use the Bash environment in Azure Cloud Shell.

    在新窗口中启动 Cloud ShellLaunch Cloud Shell in a new window

  • 如果需要,请安装 Azure CLI 来运行 CLI 参考命令。If you prefer, install the Azure CLI to run CLI reference commands.

    • 如果使用的是本地安装,请使用 az login 命令登录到 Azure CLI。If you're using a local installation, sign in to the Azure CLI by using the az login command. 若要完成身份验证过程,请遵循终端中显示的步骤。To finish the authentication process, follow the steps displayed in your terminal. 有关其他登录选项,请参阅登录 Azure CLIFor additional sign-in options, see Sign in with the Azure CLI.

    • 出现提示时,请在首次使用时安装 Azure CLI 扩展。When you're prompted, install Azure CLI extensions on first use. 有关扩展详细信息,请参阅使用 Azure CLI 的扩展For more information about extensions, see Use extensions with the Azure CLI.

    • 运行 az version 以查找安装的版本和依赖库。Run az version to find the version and dependent libraries that are installed. 若要升级到最新版本,请运行 az upgradeTo upgrade to the latest version, run az upgrade.

  • 本文需要 Azure CLI 版本 2.0.56 或更高版本。This article requires version 2.0.56 or later of the Azure CLI. 如果使用 Azure Cloud Shell,则最新版本已安装。If using Azure Cloud Shell, the latest version is already installed.

  • 如果使用本地计算机上的 Azure PowerShell 而非使用 Azure 门户中的 Cloud Shell,请确保具有 Azure PowerShell 版本 1.1.0 或更高版本。If you are using Azure PowerShell on your local machine instead of using Cloud Shell in the Azure portal, ensure that you have 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.

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

az group create --name gridResourceGroup --location westus2

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

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

az provider register --namespace Microsoft.EventGrid

完成注册可能需要一些时间。It might 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 westus2 -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 westus2 --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.

显示消息

备注

如果对从事件网格接收消息的队列使用 Azure Functions 的 Azure 队列存储触发器,那么在执行函数时,你可能会看到以下错误消息:The input is not a valid Base-64 string as it contains a non-base 64 character, more than two padding characters, or an illegal character among the padding characters.If you use an Azure Queue storage trigger for Azure Functions for a queue that receives messages from Event Grid, you may see the following error message on the function execution: The input is not a valid Base-64 string as it contains a non-base 64 character, more than two padding characters, or an illegal character among the padding characters.

原因是当你使用 Azure 队列存储触发器时,Azure Functions 需要使用 base64 编码的字符串,但事件网格以纯文本格式将消息发送到存储队列。The reason is that when you use an Azure Queue storage trigger, Azure Functions expect a base64 encoded string, but Event Grid sends messages to a storage queue in a plain text format. 目前,无法将 Azure Functions 的队列触发器配置为接受纯文本。Currently, it's not possible to configure the queue trigger for Azure Functions to accept plain text.

清理资源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: