快速入门:使用 Azure CLI 和事件网格将自定义事件路由到 Azure 事件中心Quickstart: Route custom events to Azure Event Hubs with Azure CLI and Event Grid

Azure 事件网格是针对云的事件处理服务。Azure Event Grid is an eventing service for the cloud. Azure 事件中心是受支持的事件处理程序之一。Azure Event Hubs 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 an event hub.

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

创建资源组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. <your-topic-name> 替换为自定义主题的唯一名称。Replace <your-topic-name> with a unique name for your custom topic. 自定义主题名称必须唯一,因为它由 DNS 条目表示。The custom topic name must be unique because it's represented by a DNS entry.

topicname=<your-topic-name>
az eventgrid topic create --name $topicname -l chinanorth2 -g gridResourceGroup

创建事件中心Create event hub

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

namespace=<unique-namespace-name>
hubname=demohub

az eventhubs namespace create --name $namespace --resource-group gridResourceGroup
az eventhubs eventhub create --name $hubname --namespace-name $namespace --resource-group gridResourceGroup

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

订阅事件网格主题是为了告知事件网格要跟踪哪些事件。以下示例订阅所创建的自定义主题,并传递终结点事件中心的资源 ID。You subscribe to an event grid 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 event hub for the endpoint. 终结点的格式如下:The endpoint is in the format:

/subscriptions/<subscription-id>/resourceGroups/<resource-group-name>/providers/Microsoft.EventHub/namespaces/<namespace-name>/eventhubs/<hub-name>

以下脚本获取事件中心的资源 ID,并订阅事件网格主题。The following script gets the resource ID for the event hub, and subscribes to an event grid topic. 它将终结点类型设置为 eventhub 并使用终结点的事件中心 ID。It sets the endpoint type to eventhub and uses the event hub ID for the endpoint.

hubid=$(az eventhubs eventhub show --name $hubname --namespace-name $namespace --resource-group gridResourceGroup --query id --output tsv)

az eventgrid event-subscription create \
  --topic-name $topicname \
  -g gridResourceGroup \
  --name subtoeventhub \
  --endpoint-type eventhub \
  --endpoint $hubid

创建事件订阅的帐户必须对事件中心具有写访问权限。The account that creates the event subscription must have write access to the event hub.

向自定义主题发送事件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.

endpoint=$(az eventgrid topic show --name $topicname -g gridResourceGroup --query "endpoint" --output tsv)
key=$(az eventgrid topic key list --name $topicname -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 event hub in the portal, and notice that Event Grid sent those three events to the event hub.

显示消息

通常,你将创建一个从事件中心检索事件的应用程序。Typically, you create an application that retrieves the events from the event hub. 若要创建从事件中心获取消息的应用程序,请参阅:To create an application that gets messages from an event hub, see:

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