教程:通过 IoT 中心设备预配服务配置云资源以进行设备预配Tutorial: Configure cloud resources for device provisioning with the IoT Hub Device Provisioning Service

本教程演示如何利用 IoT 中心设备预配服务来设置云资源以进行自动设备预配。This tutorial shows how to set up the cloud for automatic device provisioning using the IoT Hub Device Provisioning Service. 在本教程中,你将了解如何执行以下操作:In this tutorial, you learn how to:

  • 使用 Azure 门户创建 IoT 中心设备预配服务并获取 ID 范围Use the Azure portal to create an IoT Hub Device Provisioning Service and get the ID scope
  • 创建 IoT 中心Create an IoT hub
  • 将 IoT 中心链接到设备预配服务Link the IoT hub to the Device Provisioning Service
  • 设置设备预配服务上的分配策略Set the allocation policy on the Device Provisioning Service

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

先决条件Prerequisites

登录 Azure 门户Sign in to the Azure portal.

创建设备预配服务实例并获取 ID 范围Create a Device Provisioning Service instance and get the ID scope

按照下列步骤创建新的设备预配服务实例。Follow these steps to create a new Device Provisioning Service instance.

  1. 在 Azure 门户的左上角单击“创建资源”。 In the upper left-hand corner of the Azure portal, click Create a resource.

  2. 在搜索框中键入“设备预配” 。In the Search box, type device provisioning.

  3. 单击“IoT 中心设备预配服务” 。Click IoT Hub Device Provisioning Service.

  4. 用以下信息填写“IoT 中心设备预配服务”表 :Fill out the IoT Hub Device Provisioning Service form with the following information:

    设置Setting       建议的值Suggested value 说明Description
    名称Name 任何唯一名称Any unique name --
    订阅Subscription 订阅Your subscription 有关订阅的详细信息,请参阅订阅For details about your subscriptions, see Subscriptions.
    资源组Resource group myResourceGroupmyResourceGroup 如需有效的资源组名称,请参阅 Naming rules and restrictions(命名规则和限制)。For valid resource group names, see Naming rules and restrictions.
    位置Location 任何有效的位置Any valid location 有关区域的信息,请参阅 Azure 区域For information about regions, see Azure Regions.

    在门户中输入有关设备预配服务的基本信息

  5. 单击“创建”。 Click Create. 片刻之后,将创建设备预配服务实例并显示“概述”页面。 After a few moments, the Device Provisioning Service instance is created and the Overview page is displayed.

  6. 在新服务实例的“概述” 页面上,复制“ID 范围”的值供稍后使用。 On the Overview page for the new service instance, copy the value for the ID scope for use later. 该值用于标识注册 ID,并保证注册 ID 的唯一性。That value is used to identify registration IDs, and provides a guarantee that the registration ID is unique.

  7. 另外,复制“服务终结点” 值供稍后使用。Also, copy the Service endpoint value for later use.

创建 IoT 中心Create an IoT hub

此部分介绍如何使用 Azure 门户创建 IoT 中心。This section describes how to create an IoT hub using the Azure portal.

  1. 登录到 Azure 门户Sign in to the Azure portal.

  2. 从 Azure 主页中选择“+ 创建资源”按钮,然后在“搜索市场”字段中输入“IoT 中心”。 From the Azure homepage, select the + Create a resource button, and then enter IoT Hub in the Search the Marketplace field.

  3. 在搜索结果中选择“IoT 中心”,然后选择“创建” 。Select IoT Hub from the search results, and then select Create.

  4. 在“基本信息”选项卡上,按如下所示填写字段: On the Basics tab, complete the fields as follows:

    • 订阅:选择要用于中心的订阅。Subscription: Select the subscription to use for your hub.

    • 资源组:选择一个资源组或新建一个资源组。Resource Group: Select a resource group or create a new one. 若要新建资源组,请选择“新建”并填写要使用的名称。 To create a new one, select Create new and fill in the name you want to use. 若要使用现有的资源组,请选择它。To use an existing resource group, select that resource group. 有关详细信息,请参阅管理 Azure 资源管理器资源组For more information, see Manage Azure Resource Manager resource groups.

    • 区域:选择中心所在的区域。Region: Select the region in which you want your hub to be located. 选择最靠近你的位置。Select the location closest to you.

    • IoT 中心名称:输入中心的名称。IoT Hub Name: Enter a name for your hub. 此名称必须全局唯一。This name must be globally unique. 如果输入的名称可用,会显示一个绿色复选标记。If the name you enter is available, a green check mark appears.

    重要

    IoT 中心将公开为 DNS 终结点,因此,命名时请务必避免包含任何敏感信息。The IoT hub will be publicly discoverable as a DNS endpoint, so make sure to avoid any sensitive information while naming it.

    在 Azure 门户中创建中心

  5. 在完成时选择“下一步: 大小和规模”,以继续创建中心。Select Next: Size and scale to continue creating your hub.

    使用 Azure 门户为新的中心设置大小和规模

    在此屏幕中可以设置以下值:This screen allows you to set the following values:

    • 定价和缩放层:选择的层。Pricing and scale tier: Your selected tier. 可以根据你需要的功能数以及每天通过解决方案发送的消息数从多个层级中进行选择。You can choose from several tiers, depending on how many features you want and how many messages you send through your solution per day. 免费层适用于测试和评估。The free tier is intended for testing and evaluation. 允许 500 台设备连接到中心,每天最多可传输 8,000 条消息。It allows 500 devices to be connected to the hub and up to 8,000 messages per day. 每个 Azure 订阅可以在免费层中创建一个 IoT 中心。Each Azure subscription can create one IoT Hub in the free tier.

    • IoT 中心单元:每个单位每日允许的消息数取决于中心的定价层。IoT Hub units: The number of messages allowed per unit per day depends on your hub's pricing tier. 例如,如果希望中心支持 700,000 条消息引入,请选择两个 S1 层单位。For example, if you want the hub to support ingress of 700,000 messages, you choose two S1 tier units. 有关其他层选项的详细信息,请参阅选择合适的 IoT 中心层For details about the other tier options, see Choosing the right IoT Hub tier.

    • 高级设置 > 设备到云的分区:此属性将设备到云消息与这些消息的同步读取器数目相关联。Advanced Settings > Device-to-cloud partitions: This property relates the device-to-cloud messages to the number of simultaneous readers of the messages. 大多数中心只需要 4 个分区。Most hubs need only four partitions.

  6. 对于本文,请接受默认选项,然后选择“查看 + 创建”以查看所做的选择。 For this article, accept the default choices, and then select Review + create to review your choices. 会显示类似于以下的屏幕。You see something similar to this screen.

    查看用于创建新中心的信息

  7. 选择“创建”以创建新的中心 。Select Create to create your new hub. 创建中心需要几分钟时间。Creating the hub takes a few minutes.

检索 IoT 中心的连接字符串Retrieve connection string for IoT hub

创建中心以后,请检索中心的连接字符串。After your hub has been created, retrieve the connection string for the hub. 该字符串用于将设备和应用程序连接到中心。This is used to connect devices and applications to your hub.

  1. 单击中心,查看“IoT 中心”窗格,其中包含“设置”等内容。Click on your hub to see the IoT Hub pane with Settings, and so on. 单击“共享访问策略”。Click Shared access policies.

  2. 在“共享访问策略”中,选择 iothubowner 策略。In Shared access policies, select the iothubowner policy.

  3. 在“共享访问密钥”下,复制“连接字符串 -- 主密钥”供以后使用。Under Shared access keys, copy the Connection string -- primary key to be used later.

    显示如何检索连接字符串

    有关详细信息,请参阅“IoT 中心开发人员指南”中的访问控制For more information, see Access control in the "IoT Hub developer guide."

现已创建 IoT 中心,因此已获得完成本教程的其余部分所需的主机名和 IoT 中心连接字符串。You have now created your IoT hub, and you have the host name and IoT Hub connection string that you need to complete the rest of this tutorial.

下一步,将设备预配服务链接到 IoT 中心,以便 IoT 中心设备预配服务能向该中心注册设备。The next step is to link the Device Provisioning Service and IoT hub so that the IoT Hub Device Provisioning Service can register devices to that hub. 该服务只能将设备预配到已链接至设备预配服务的 IoT 中心。The service can only provision devices to IoT hubs that have been linked to the Device Provisioning Service. 执行以下步骤。Follow these steps.

  1. 在“所有资源”页上,单击之前创建的设备预配服务实例 。In the All resources page, click the Device Provisioning Service instance you created previously.

  2. 在“设备预配服务”页上,单击“链接 IoT 中心” 。In the Device Provisioning Service page, click Linked IoT hubs.

  3. 单击“添加” 。Click Add.

  4. 在“添加 IoT 中心的链接” 页面上,提供以下信息,并单击“保存” :In the Add link to IoT hub page, provide the following information, and click Save:

    • 订阅: 确保选择包含 IoT 中心的订阅。Subscription: Make sure the subscription that contains the IoT hub is selected. 可以链接到位于其他订阅中的 IoT 中心。You can link to IoT hub that resides in a different subscription.

    • IoT 中心: 选择要与此设备预配服务实例进行链接的 IoT 中心的名称。IoT hub: Choose the name of the IoT hub that you want to link with this Device Provisioning Service instance.

    • 访问策略: 选择 iothubowner 作为用于与 IoT 中心建立链接的凭据 。Access Policy: Select iothubowner as the credentials to use for establishing the link to the IoT hub.

    链接中心名称,以便链接到门户中的设备预配服务

设置设备预配服务上的分配策略Set the allocation policy on the Device Provisioning Service

分配策略是一项 IoT 中心设备预配服务设置,用于确定将设备分配到 IoT 中心的方式。The allocation policy is an IoT Hub Device Provisioning Service setting that determines how devices are assigned to an IoT hub. 支持三种分配策略:There are three supported allocation policies: 

  1. 最低延迟:将设备预配到具有最低延迟的 IoT 中心。Lowest latency: Devices are provisioned to an IoT hub based on the hub with the lowest latency to the device.

  2. 均衡加权分布(默认):链接的 IoT 中心等可能地获得预配到它们的设备。Evenly weighted distribution (default): Linked IoT hubs are equally likely to have devices provisioned to them. 此设置为默认设置。This setting is the default. 如果只将设备预配到一个 IoT 中心,则可以保留此设置。If you are provisioning devices to only one IoT hub, you can keep this setting. 

  3. 通过注册列表进行静态注册:注册列表中所需 IoT 中心的规范优先于设备预配服务级别的分配策略。Static configuration via the enrollment list: Specification of the desired IoT hub in the enrollment list takes priority over the Device Provisioning Service-level allocation policy.

若要设置分配策略,请单击“设备预配服务”页上的“管理分配策略” 。To set the allocation policy, in the Device Provisioning Service page click Manage allocation policy. 确保将分配策略设置为“均匀加权分发”(默认) 。Make sure the allocation policy is set to Evenly weighted distribution (the default). 如果进行了任何更改,请在完成后单击“保存” 。If you make any changes, click Save when you are done.

管理分配策略

清理资源Clean up resources

本教程系列中的其他教程是在本教程的基础上制作的。Other tutorials in this collection build upon this tutorial. 如果计划继续使用后续的快速入门或相关教程,请勿清除在本教程中创建的资源。If you plan to continue on to work with subsequent quick starts or with the tutorials, do not clean up the resources created in this tutorial. 如果不打算继续,请在 Azure 门户中执行以下步骤来删除此教程创建的所有资源。If you do not plan to continue, use the following steps to delete all resources created by this tutorial in the Azure portal.

  1. 在 Azure 门户的左侧菜单中单击“所有资源”,然后选择 IoT 中心设备预配服务实例 。From the left-hand menu in the Azure portal, click All resources and then select your IoT Hub Device Provisioning Service instance. 在“所有资源”页的顶部单击“删除” 。At the top of the All resources page, click Delete.

  2. 在 Azure 门户的左侧菜单中单击“所有资源”,然后选择 IoT 中心 。From the left-hand menu in the Azure portal, click All resources and then select your IoT hub. 在“所有资源”页的顶部单击“删除” 。At the top of the All resources page, click Delete.

后续步骤Next steps

在本教程中,你了解了如何执行以下操作:In this tutorial, you learned how to:

  • 使用 Azure 门户创建 IoT 中心设备预配服务并获取 ID 范围Use the Azure portal to create an IoT Hub Device Provisioning Service and get the ID scope
  • 创建 IoT 中心Create an IoT hub
  • 将 IoT 中心链接到设备预配服务Link the IoT hub to the Device Provisioning Service
  • 设置设备预配服务上的分配策略Set the allocation policy on the Device Provisioning Service

前往下一教程,了解如何设置设备以进行预配。Advance to the next tutorial to learn how to set up your device for provisioning.