了解 Azure IoT Edge 模块Understand Azure IoT Edge modules

通过 Azure IoT Edge,可让你以模块 的形式来部署和管理 Edge 上的业务逻辑。Azure IoT Edge lets you deploy and manage business logic on the edge in the form of modules. Azure IoT Edge 模块是由 IoT Edge 托管的最小计算单位,可以包含 Azure 服务(如 Azure 流分析)或你自己特定于解决方案的代码。Azure IoT Edge modules are the smallest unit of computation managed by IoT Edge, and can contain Azure services (such as Azure Stream Analytics) or your own solution-specific code. 若要了解如何开发、部署和维护模块,请考虑模块的四个概念元素:To understand how modules are developed, deployed, and maintained, consider the four conceptual elements of a module:

  • 模块映像 是包含定义模块的程序包。A module image is a package containing the software that defines a module.
  • 模块实例 是在 IoT Edge 设备上运行模块映像的特定计算单位。A module instance is the specific unit of computation running the module image on an IoT Edge device. 该模块实例由 IoT Edge 运行时启动。The module instance is started by the IoT Edge runtime.
  • 模块标识 是存储在 IoT 中心的一段信息(包括安全凭据),与每个模块实例相关联。A module identity is a piece of information (including security credentials) stored in IoT Hub, that is associated to each module instance.
  • 模块孪生 是存储在 IoT 中心的 JSON 文档,包含模块实例的状态信息,其中包括元数据、配置和条件。A module twin is a JSON document stored in IoT Hub, that contains state information for a module instance, including metadata, configurations, and conditions.

模块映像和实例Module images and instances

IoT Edge 模块映像包含能够充分利用 IoT Edge 运行时的管理、安全性和通信功能的应用程序。IoT Edge module images contain applications that take advantage of the management, security, and communication features of the IoT Edge runtime. 你可以开发自己的模块映像,或从支持的 Azure 服务导出一个模块映像,如 Azure 流分析。You can develop your own module images, or export one from a supported Azure service, such as Azure Stream Analytics. 映像存在于云中,可以在不同解决方案中对其进行更新、更改和部署。The images exist in the cloud and they can be updated, changed, and deployed in different solutions. 例如,使用机器学习来预测生产线输出的模块作为一个单独映像(而不是一个使用计算机视觉来控制无人机的模块)存在。For instance, a module that uses machine learning to predict production line output exists as a separate image than a module that uses computer vision to control a drone.

每当将模块映像部署到设备上,并由 IoT Edge 运行时启动时,就会创建该模块的一个新实例。Each time a module image is deployed to a device and started by the IoT Edge runtime, a new instance of that module is created. 在世界不同地区的两个设备可以使用相同的模块映像。Two devices in different parts of the world could use the same module image. 但是,在设备上启动模块时每个设备都会有其自己的模块实例。However, each device would have its own module instance when the module is started on the device.

关系图 - 云中的模块映像,设备上的模块实例

在实现中,模块映像作为存储库中的容器映像存在,而模块实例则是设备上的容器。In implementation, modules images exist as container images in a repository, and module instances are containers on devices.

模块标识Module identities

当 IoT Edge 运行时创建新的模块实例时,它将获得相应的模块标识。When a new module instance is created by the IoT Edge runtime, it gets a corresponding module identity. 模块标识将存储在 IoT 中心,并用作该模块实例的所有本地和云通信的寻址和安全范围。The module identity is stored in IoT Hub, and is used as the addressing and security scope for all local and cloud communications for that module instance.

与模块实例相关联的标识将取决于在其上运行实例的设备的标识,以及在解决方案中为该模块提供的名称。The identity associated with a module instance depends on the identity of the device on which the instance is running and the name you provide to that module in your solution. 例如,如果调用使用 Azure 流分析的 insight 模块,并将其部署到一个名为 Hannover01 的设备上,那么 IoT Edge 运行时将创建一个相应的称为 /devices/Hannover01/modules/insight 的模块标识。For instance, if you call insight a module that uses an Azure Stream Analytics, and you deploy it on a device called Hannover01, the IoT Edge runtime creates a corresponding module identity called /devices/Hannover01/modules/insight.

很显然,当需要在同一设备上多次部署一个模块映像时,可以使用不同的名称多次部署相同的映像。Clearly, in scenarios when you need to deploy one module image multiple times on the same device, you can deploy the same image multiple times with different names.

关系图 - 模块标识在设备中和设备之间具有唯一性

模块孪生Module twins

此外,每个模块实例都有一个对应的模块孪生,你可以使用它来配置模块实例。Each module instance also has a corresponding module twin that you can use to configure the module instance. 实例和孪生通过模块标识进行相互关联。The instance and the twin are associated with each other through the module identity.

模块孪生是存储模块信息和配置属性的 JSON 文档。A module twin is a JSON document that stores module information and configuration properties. 此概念与 IoT 中心的设备孪生概念类似。This concept parallels the device twin concept from IoT Hub. 模块孪生的结构与设备孪生相同。The structure of a module twin is the same as a device twin. 用于与这两种类型的孪生进行交互的 API 也是相同的。The APIs used to interact with both types of twins are also the same. 两者之间唯一的区别是用来实例化客户端 SDK 的标识。The only difference between the two is the identity used to instantiate the client SDK.

// Create a ModuleClient object. This ModuleClient will act on behalf of a
// module since it is created with a module’s connection string instead
// of a device connection string.
ModuleClient client = new ModuleClient.CreateFromEnvironmentAsync(settings);
await client.OpenAsync();

// Get the module twin
Twin twin = await client.GetTwinAsync();

脱机功能Offline capabilities

在与 IoT 中心同步至少一次后,Azure IoT Edge 模块可以无限地脱机运行。Azure IoT Edge modules can operate offline indefinitely after syncing with IoT Hub at least once. IoT Edge 设备还可以将此脱机功能扩展到其他 IoT 设备。IoT Edge devices can also extend this offline capability to other IoT devices. 有关详细信息,请参阅了解 IoT Edge 设备、模块和子设备的扩展脱机功能For more information, see Understand extended offline capabilities for IoT Edge devices, modules, and child devices.

后续步骤Next steps