教程:使用 Azure CLI 和 Azure 门户配置 IoT 中心消息路由Tutorial: Use the Azure CLI and Azure portal to configure IoT Hub message routing

使用 Azure CLI 创建基本资源Use the Azure CLI to create the base resources

本教程使用 Azure CLI 创建基本资源,然后使用 Azure 门户来演示如何配置消息路由和设置虚拟设备用于测试。This tutorial uses the Azure CLI to create the base resources, then uses the Azure portal to show how to configure message routing and set up the virtual device for testing.

有几个资源名称必须全局唯一,例如 IoT 中心名称和存储帐户名称。There are several resource names that must be globally unique, such as the IoT Hub name and the storage account name. 为方便进行标识,这些资源名称的后面追加了名为 randomValue 的随机字母数字值。To make this easier, those resource names are appended with a random alphanumeric value called randomValue. randomValue 在脚本的顶部生成一次,并根据需要追加到整个脚本中的资源名称。The randomValue is generated once at the top of the script and appended to the resource names as needed throughout the script. 如果不想要使用随机后缀,可将其设置为空字符串或特定值。If you don't want it to be random, you can set it to an empty string or to a specific value.

复制以下脚本并将其粘贴到命令行窗口,然后按 Enter。Copy and paste the script below into terminal window and press Enter. 系统每次运行脚本中的一行。It runs the script one line at a time. 此脚本将创建本教程所需的基本资源,包括存储帐户、IoT 中心、服务总线命名空间和服务总线队列。This will create the base resources for this tutorial, including the storage account, IoT Hub, Service Bus Namespace, and Service Bus queue.

有关调试的说明:此脚本使用续接符号(反斜杠 \),使脚本更方便阅读。A note about debugging: this script uses the continuation symbol (the backslash \) to make the script more readable. 如果在运行脚本时遇到问题,请确保任何反斜杠后面没有空格。If you have a problem running the script, make sure there are no spaces after any of the backslashes.

# This retrieves the subscription id of the account 
#   in which you're logged in.
# This field is used to set up the routing rules.
subscriptionID=$(az account show --query id)

# Concatenate this number onto the resources that have to be globally unique.
# You can set this to "" or to a specific value if you don't want it to be random.
# This retrieves a random value.
randomValue=$RANDOM

# Set the values for the resource names that 
#   don't have to be globally unique.
location=chinaeast
resourceGroup=ContosoResources
iotHubConsumerGroup=ContosoConsumers
containerName=contosoresults

# Create the resource group to be used
#   for all the resources for this tutorial.
az group create --name $resourceGroup \
    --location $location

# The IoT hub name must be globally unique, 
#   so add a random value to the end.
iotHubName=ContosoTestHub$randomValue 
echo "IoT hub name = " $iotHubName

# Create the IoT hub.
az iot hub create --name $iotHubName \
    --resource-group $resourceGroup \
    --sku S1 --location $location

# Add a consumer group to the IoT hub for the 'events' endpoint.
az iot hub consumer-group create --hub-name $iotHubName \
    --name $iotHubConsumerGroup

# The storage account name must be globally unique, 
#   so add a random value to the end.
storageAccountName=contosostorage$randomValue
echo "Storage account name = " $storageAccountName

# Create the storage account to be used as a routing destination.
az storage account create --name $storageAccountName \
    --resource-group $resourceGroup \
    --location $location \
    --sku Standard_LRS

# Get the primary storage account key. 
#    You need this to create the container.
storageAccountKey=$(az storage account keys list \
    --resource-group $resourceGroup \
    --account-name $storageAccountName \
    --query "[0].value" | tr -d '"') 

# See the value of the storage account key.
echo "storage account key = " $storageAccountKey

# Create the container in the storage account. 
az storage container create --name $containerName \
    --account-name $storageAccountName \
    --account-key $storageAccountKey \
    --public-access off

# The Service Bus namespace must be globally unique, 
#   so add a random value to the end.
sbNamespace=ContosoSBNamespace$randomValue
echo "Service Bus namespace = " $sbNamespace

# Create the Service Bus namespace.
az servicebus namespace create --resource-group $resourceGroup \
    --name $sbNamespace \
    --location $location

# The Service Bus queue name must be globally unique, 
#   so add a random value to the end.
sbQueueName=ContosoSBQueue$randomValue
echo "Service Bus queue name = " $sbQueueName

# Create the Service Bus queue to be used as a routing destination.
az servicebus queue create --name $sbQueueName \
    --namespace-name $sbNamespace \
    --resource-group $resourceGroup

设置基本资源后,可以在 Azure 门户中配置消息路由。Now that the base resources are set up, you can configure the message routing in the Azure portal.

设置消息路由Set up message routing

路由到存储帐户Route to a storage account

现在为存储帐户设置路由。Now set up the routing for the storage account. 你转到“消息路由”窗格,然后添加路由。You go to the Message Routing pane, then add a route. 添加路由时,请为路由定义新的终结点。When adding the route, define a new endpoint for the route. 设置此路由后,level 属性设置为 storage 的消息将自动写入存储帐户。After this routing is set up, messages where the level property is set to storage are written to a storage account automatically.

  1. Azure 门户中选择“资源组”,然后选择你的资源组。In the Azure portal, select Resource Groups, then select your resource group. 本教程使用 ContosoResources。This tutorial uses ContosoResources.

  2. 在资源列表下选择“IoT 中心”。Select the IoT hub under the list of resources. 本教程使用 ContosoTestHub。This tutorial uses ContosoTestHub.

  3. 选择“消息路由”。Select Message Routing. 在“消息路由”窗格中,选择“+添加”。In the Message Routing pane, select +Add. 在“添加路由”窗格中,选择“终结点”字段旁边的“+添加”以显示支持的终结点,如下图所示:On the Add a Route pane, select +Add next to the Endpoint field to show the supported endpoints, as displayed in the following picture:

    开始添加路由的终结点

  4. 选择“Blob 存储”。Select Blob storage. 此时会显示“添加存储终结点”窗格。You see the Add a storage endpoint pane.

    添加终结点

  5. 为终结点输入名称。Enter a name for the endpoint. 本教程使用 ContosoStorageEndpointThis tutorial uses ContosoStorageEndpoint.

  6. 选择“选取容器”。Select Pick a container. 将转到存储帐户列表。This takes you to a list of your storage accounts. 选择在准备步骤中设置的存储账户。Select the one you set up in the preparation steps. 本教程使用 contosostorageThis tutorial uses contosostorage. 它显示该存储帐户中的容器列表。It shows a list of containers in that storage account. 选择在准备步骤中设置的容器。Select the container you set up in the preparation steps. 本教程使用 contosoresults。This tutorial uses contosoresults. 返回到“添加存储终结点”窗格并查看所做的选择。You return to the Add a storage endpoint pane and see the selections you made.

  7. 将编码设置为 AVRO 或 JSON。Set the encoding to AVRO or JSON. 在本教程中,其余字段使用默认值。For the purpose of this tutorial, use the defaults for the rest of the fields. 如果所选的区域不支持 JSON 编码,则此字段将会灰显。This field will be greyed out if the region selected does not support JSON encoding.,

    Note

    可以使用 Blob 文件名格式设置 Blob 名称的格式。You can set the format of the blob name using the Blob file name format. 默认为 {iothub}/{partition}/{YYYY}/{MM}/{DD}/{HH}/{mm}The default is {iothub}/{partition}/{YYYY}/{MM}/{DD}/{HH}/{mm}. 格式必须包含 {iothub}、{partition}、{YYYY}、{MM}、{DD}、{HH} 和 {mm},顺序不限。The format must contain {iothub}, {partition}, {YYYY}, {MM}, {DD}, {HH}, and {mm} in any order.

    例如,使用默认 Blob 文件名格式时,如果中心名称为 ContosoTestHub,日期/时间为 2018 年 10 月 30 日上午 10:56,则 Blob 名称将类似于:ContosoTestHub/0/2018/10/30/10/56For example, using the default blob file name format, if the hub name is ContosoTestHub, and the date/time is October 30, 2018 at 10:56 a.m., the blob name will look like this: ContosoTestHub/0/2018/10/30/10/56.

    Blob 以 Avro 格式写入。The blobs are written in the Avro format.

  8. 选择“创建”以创建存储终结点,并将其添加到路由。Select Create to create the storage endpoint and add it to the route. 随即返回到“添加路由”窗格。You return to the Add a route pane.

  9. 现在完成余下的路由查询信息。Now complete the rest of the routing query information. 此查询指定将消息发送到刚刚添加为终结点的存储容器的条件。This query specifies the criteria for sending messages to the storage container you just added as an endpoint. 填充屏幕上的字段。Fill in the fields on the screen.

    名称:为路由查询输入名称。Name: Enter a name for your routing query. 本教程使用 ContosoStorageRouteThis tutorial uses ContosoStorageRoute.

    终结点:选择刚刚设置的终结点。Endpoint: This shows the endpoint you just set up.

    数据源:从下拉列表选择“设备遥测消息”。Data source: Select Device Telemetry Messages from the dropdown list.

    启用路由:确保此字段设置为 enabledEnable route: Be sure this field is set to enabled.

    路由查询:输入 level="storage" 作为查询字符串。Routing query: Enter level="storage" as the query string.

    为存储帐户创建路由查询

    选择“其他安全性验证” 。Select Save. 完成后,返回到“消息路由”窗格,可在其中看到存储的新路由查询。When it finishes, it returns to the Message Routing pane, where you can see your new routing query for storage. 关闭“路由”窗格,将返回到资源组页。Close the Routes pane, which returns you to the Resource group page.

路由到服务总线队列Route to a Service Bus queue

现在为服务总线队列设置路由。Now set up the routing for the Service Bus queue. 你转到“消息路由”窗格,然后添加路由。You go to the Message Routing pane, then add a route. 添加路由时,请为路由定义新的终结点。When adding the route, define a new endpoint for the route. 设置此路由后,level 属性设置为 critical 的消息将写入服务总线队列,这会触发某个逻辑应用,然后发送包含相关信息的电子邮件。After this route is set up, messages where the level property is set to critical are written to the Service Bus queue, which triggers a Logic App, which then sends an e-mail with the information.

  1. 在“资源组”页上选择你的 IoT 中心,然后选择“消息路由”。On the Resource group page, select your IoT hub, then select Message Routing.

  2. 在“消息路由”窗格中,选择“+添加”。In the Message Routing pane, select +Add.

  3. 在“添加路由”窗格中,选择“终结点”字段旁边的“+添加”。On the Add a Route pane, Select +Add next to the Endpoint field. 选择“服务总线队列”。Select Service Bus Queue. 随即看到“添加服务总线终结点”窗格。You see the Add Service Bus Endpoint pane.

    添加服务总线终结点

  4. 填写字段:Fill in the fields:

    终结点名称:为终结点输入名称。Endpoint Name: Enter a name for the endpoint. 本教程使用 ContosoSBQueueEndpointThis tutorial uses ContosoSBQueueEndpoint.

    服务总线命名空间:使用下拉列表选择在准备步骤中设置的服务总线命名空间。Service Bus Namespace: Use the dropdown list to select the service bus namespace you set up in the preparation steps. 本教程使用 ContosoSBNamespace。This tutorial uses ContosoSBNamespace.

    服务总线队列:使用下拉列表选择服务总线队列。Service Bus queue: Use the dropdown list to select the Service Bus queue. 本教程使用 contososbqueue。This tutorial uses contososbqueue.

  5. 选择“创建”以添加服务总线队列终结点。Select Create to add the Service Bus queue endpoint. 随即返回到“添加路由”窗格。You return to the Add a route pane.

  6. 现在完成余下的路由查询信息。Now you complete the rest of the routing query information. 此查询指定将消息发送到刚刚添加为终结点的服务总线队列的条件。This query specifies the criteria for sending messages to the Service Bus queue you just added as an endpoint. 填充屏幕上的字段。Fill in the fields on the screen.

    名称:为路由查询输入名称。Name: Enter a name for your routing query. 本教程使用 ContosoSBQueueRouteThis tutorial uses ContosoSBQueueRoute.

    终结点:选择刚刚设置的终结点。Endpoint: This shows the endpoint you just set up.

    数据源:从下拉列表选择“设备遥测消息”。Data source: Select Device Telemetry Messages from the dropdown list.

    路由查询:输入 level="critical" 作为查询字符串。Routing query: Enter level="critical" as the query string.

    为服务总线队列创建路由查询

  7. 选择“其他安全性验证” 。Select Save. 返回到“路由”窗格时,可看到这两个新的路由,如此处所示。When it returns to the Routes pane, you see both of your new routes, as displayed here.

    刚刚设置的路由

  8. 可以通过选择“自定义终结点”选项卡来查看设置的自定义终结点。You can see the custom endpoints you set up by selecting the Custom Endpoints tab.

    刚刚设置的自定义终结点

  9. 关闭“消息路由”窗格,返回到“资源组”窗格。Close the Message Routing pane, which returns you to the Resource group pane.

后续步骤Next steps

设置资源并配置消息路由后,请继续学习下一篇教程,了解如何将消息发送到 IoT 中心,并查看其如何路由到不同的目标。Now that you have the resources set up and the message routes configured, advance to the next tutorial to learn how to send messages to the IoT hub and see them be routed to the different destinations.