快速入门:使用 Java 服务 SDK 将 TPM 设备注册到 IoT 中心设备预配服务Quickstart: Enroll TPM device to IoT Hub Device Provisioning Service using Java Service SDK

在本快速入门中,你将借助示例 Java 应用程序,使用 Java 服务 SDK 以编程方式在 Azure IoT 中心设备预配服务中为模拟 TPM 设备创建单个注册。In this quickstart, you programmatically create an individual enrollment for a simulated TPM device in the Azure IoT Hub Device Provisioning Service using the Java Service SDK with the help of a sample Java application.

先决条件Prerequisites

准备开发环境Prepare the development environment

  1. 确保已在计算机上安装 Java SE 开发工具包 8Make sure you have Java SE Development Kit 8 installed on your machine.

  2. 设置 Java 安装的环境变量。Set up environment variables for your Java installation. PATH 变量应包括 jdk1.8.x\bin 目录的完整路径。The PATH variable should include the full path to jdk1.8.x\bin directory. 如果这是计算机的首次 Java 安装,则请创建名为 JAVA_HOME 的新的环境变量,将其指向 jdk1.8.x 目录的完整路径。If this is your machine's first Java installation, then create a new environment variable named JAVA_HOME and point it to the full path to the jdk1.8.x directory. 在 Windows 计算机上,该目录位于 C:\Program Files\Java\ 文件夹中。可以通过在 Windows 计算机的“控制面板”上搜索“编辑系统环境变量”来创建或编辑环境变量。 On Windows machine, this directory is found in the C:\Program Files\Java\ folder, and you can create or edit environment variables by searching for Edit the system environment variables on the Control panel of your Windows machine.

    可以在命令窗口中运行以下命令,查看 Java 是否已成功安装在计算机上:You may check if Java is successfully set up on your machine by running the following command on your command window:

    java -version
    
  3. Maven 3 下载并解压缩到计算机上。Download and extract Maven 3 on your machine.

  4. 编辑环境变量 PATH,使之指向 Maven 解压缩时所在文件夹中的 apache-maven-3.x.x\bin 文件夹。Edit environment variable PATH to point to the apache-maven-3.x.x\bin folder inside the folder where Maven is extracted. 可以在命令窗口中运行以下命令,确认 Maven 已成功安装:You may confirm that Maven is successfully installed by running this command on your command window:

    mvn --version
    
  5. 确保在计算机上安装 git 并将其添加到环境变量 PATHMake sure git is installed on your machine and is added to the environment variable PATH.

下载并修改 Java 示例代码Download and modify the Java sample code

此部分演示如何向示例代码添加 TPM 设备的预配详细信息。This section shows how to add the provisioning details of your TPM device to the sample code.

  1. 打开命令提示符。Open a command prompt. 使用 Java 服务 SDK 克隆设备注册代码示例的 GitHub 存储库:Clone the GitHub repo for device enrollment code sample using the Java Service SDK:

    git clone https://github.com/Azure/azure-iot-sdk-java.git --recursive
    
  2. 在下载的源代码中,导航到示例文件夹 azure-iot-sdk-java/provisioning/provisioning-samples/service-enrollment-sampleIn the downloaded source code, navigate to the sample folder azure-iot-sdk-java/provisioning/provisioning-samples/service-enrollment-sample. 在所选编辑器中打开文件 /src/main/java/samples/com/microsoft/azure/sdk/iot/ServiceEnrollmentSample.java ,添加以下详细信息:Open the file /src/main/java/samples/com/microsoft/azure/sdk/iot/ServiceEnrollmentSample.java in an editor of your choice, and add the following details:

    1. 在门户中为预配服务添加 [Provisioning Connection String],如下所示:Add the [Provisioning Connection String] for your provisioning service, from the portal as following:

      1. Azure 门户中导航到预配服务。Navigate to your provisioning service in the Azure portal.

      2. 打开“共享访问策略”,选择具有 EnrollmentWrite 权限的策略。 Open the Shared access policies, and select a policy that has the EnrollmentWrite permission.

      3. 复制“主密钥连接字符串” 。Copy the Primary key connection string.

        从门户获取预配连接字符串

      4. 在示例代码文件 ServiceEnrollmentSample.java 中,将 [Provisioning Connection String] 替换为“主密钥连接字符串”。 In the sample code file ServiceEnrollmentSample.java, replace the [Provisioning Connection String] with the Primary key connection string.

        private static final String PROVISIONING_CONNECTION_STRING = "[Provisioning Connection String]";
        
    2. 添加 TPM 设备详细信息:Add the TPM device details:

      1. 获取 TPM 设备模拟的“注册 ID”和“TPM 认可密钥”, 只需执行模拟 TPM 设备部分之前的步骤即可。Get the Registration ID and the TPM endorsement key for a TPM device simulation, by following the steps leading to the section Simulate TPM device.

      2. 使用前面步骤的输出中的“注册 ID” 和“认可密钥” ,替换示例代码文件 ServiceEnrollmentSample.java 中的 [RegistrationId][TPM Endorsement Key]Use the Registration ID and the Endorsement Key from the output of the preceding step, to replace the [RegistrationId] and [TPM Endorsement Key] in the sample code file ServiceEnrollmentSample.java:

        private static final String REGISTRATION_ID = "[RegistrationId]";
        private static final String TPM_ENDORSEMENT_KEY = "[TPM Endorsement Key]";
        
    3. 也可选择通过示例代码配置预配服务:Optionally, you may configure your provisioning service through the sample code:

      • 若要将此配置添加到示例,请执行以下步骤:To add this configuration to the sample, follow these steps:

        1. Azure 门户中导航到已链接到预配服务的 IoT 中心。Navigate to the IoT hub linked to your provisioning service in the Azure portal. 打开中心的“概览”选项卡,复制“主机名”。 Open the Overview tab for the hub, and copy the Hostname. 将该“主机名” 指定给 IOTHUB_HOST_NAME 参数。Assign this Hostname to the IOTHUB_HOST_NAME parameter.
          private static final String IOTHUB_HOST_NAME = "[Host name].azure-devices.cn";
          
        2. DEVICE_ID 参数指定一个友好名称,并保留 PROVISIONING_STATUS 的默认值 ENABLEDAssign a friendly name to the DEVICE_ID parameter, and keep the PROVISIONING_STATUS as the default ENABLED value.
      • 或者,如果选择不配置预配服务,请确保注释掉或删除 ServiceEnrollmentSample.java 文件中的以下语句:OR, if you choose not to configure your provisioning service, make sure to comment out or delete the following statements in the ServiceEnrollmentSample.java file:

        // The following parameters are optional. Remove it if you don't need.
        individualEnrollment.setDeviceId(DEVICE_ID);
        individualEnrollment.setIotHubHostName(IOTHUB_HOST_NAME);
        individualEnrollment.setProvisioningStatus(PROVISIONING_STATUS);
        
    4. 研究示例代码。Study the sample code. 此代码用于创建、更新、查询和删除单个 TPM 设备注册。It creates, updates, queries, and deletes an individual TPM device enrollment. 若要验证是否已在门户中成功注册,请暂时性地注释掉 ServiceEnrollmentSample.java 文件末尾的以下代码行:To verify successful enrollment in portal, temporarily comment out the following lines of code at the end of the ServiceEnrollmentSample.java file:

      // *********************************** Delete info of individualEnrollment ************************************
      System.out.println("\nDelete the individualEnrollment...");
      provisioningServiceClient.deleteIndividualEnrollment(REGISTRATION_ID);
      
    5. 保存 ServiceEnrollmentSample.java 文件。Save the file ServiceEnrollmentSample.java.

生成并运行 Java 示例代码Build and run the Java sample code

  1. 打开命令窗口,导航到文件夹 azure-iot-sdk-java/provisioning/provisioning-samples/service-enrollment-sampleOpen a command window, and navigate to the folder azure-iot-sdk-java/provisioning/provisioning-samples/service-enrollment-sample.

  2. 使用以下命令生成示例代码:Build the sample code by using this command:

    mvn install -DskipTests
    

    此命令将 Maven 包 com.microsoft.azure.sdk.iot.provisioning.service 下载到计算机。This command downloads the Maven package com.microsoft.azure.sdk.iot.provisioning.service to your machine. 此包包含编译示例代码所需的 Java 服务 SDK 的二进制文件。This package includes the binaries for the Java Service SDK, that the sample code needs to build.

  3. 运行示例,方法是在命令窗口使用以下命令:Run the sample by using these commands at the command window:

    cd target
    java -jar ./service-enrollment-sample-{version}-with-deps.jar
    
  4. 在成功注册后观察输出窗口。Observe the output window for successful enrollment.

  5. 在 Azure 门户中导航到预配服务。Navigate to your provisioning service in the Azure portal. 选择“管理注册”,然后选择“单个注册”选项卡 。请注意,现在列出了模拟 TPM 设备的“注册 ID”。 Select Manage enrollments, and select the Individual Enrollments tab. Notice that the Registration ID of your simulated TPM device is now listed.

    验证是否已在门户中成功完成 TPM 注册

清理资源Clean up resources

如果打算学习 Java 服务示例,请勿清理本快速入门中创建的资源。If you plan to explore the Java service sample, do not clean up the resources created in this quickstart. 如果不打算继续学习,请按以下步骤删除本快速入门中创建的所有资源。If you do not plan to continue, use the following steps to delete all resources created by this quickstart.

  1. 关闭计算机上的 Java 示例输出窗口。Close the Java sample output window on your machine.
  2. 关闭可能已创建的用于模拟 TPM 设备的 TPM 模拟器窗口。Close the TPM simulator window that you may have created to simulate your TPM device.
  3. 在 Azure 门户中导航到你的设备预配服务,选择“管理注册”,然后选择“单个注册”选项卡。 选中通过本快速入门创建的注册项的“注册 ID”旁边的复选框,然后按窗格顶部的“删除”按钮 。Navigate to your Device Provisioning service in the Azure portal, select Manage enrollments, and then select the Individual Enrollments tab. Select the check box next to the Registration ID for the enrollment entry you created using this quickstart, and press the Delete button at the top of the pane.

后续步骤Next steps

在本快速入门中,你将模拟的 TPM 设备注册到了设备预配服务。In this quickstart, you enrolled a simulated TPM device to your Device Provisioning service. 若要深入了解设备预配,请继续学习本教程有关如何在 Azure 门户中进行设备预配服务设置的内容。To learn about device provisioning in depth, continue to the tutorial for the Device Provisioning Service setup in the Azure portal.