了解有关 IoT Edge 设备、模块和子设备的扩展脱机功能Understand extended offline capabilities for IoT Edge devices, modules, and child devices

Azure IoT Edge 支持 IoT Edge 设备上的扩展脱机操作,同时在非 Edge 子设备上启用脱机操作。Azure IoT Edge supports extended offline operations on your IoT Edge devices, and enables offline operations on non-Edge child devices too. 只要 IoT Edge 设备有机会连接到 IoT 中心,它和任何子设备就可以在间歇性或无 Internet 连接的情况下继续运作。As long as an IoT Edge device has had one opportunity to connect to IoT Hub, it and any child devices can continue to function with intermittent or no internet connection.

工作原理How it works

当 IoT Edge 设备进入脱机模式,IoT Edge 中心将扮演三个角色。When an IoT Edge device goes into offline mode, the IoT Edge hub takes on three roles. 首先,它将存储任何向上游发送的消息并保存它们,直到设备重新连接。First, it stores any messages that would go upstream and saves them until the device reconnects. 其次,它代表 IoT 中心对模块和子设备进行身份验证,以便它们可以继续运行。Second, it acts on behalf of IoT Hub to authenticate modules and child devices so that they can continue to operate. 第三,它会在子设备之间启用通常通过 IoT 中心的通信。Third, it enables communication between child devices that normally would go through IoT Hub.

下面的示例展示了 IoT Edge 方案如何在脱机模式下运行:The following example shows how an IoT Edge scenario operates in offline mode:

  1. 配置 IoT Edge 设备Configure an IoT Edge device.

    IoT Edge 设备自动启用脱机功能。IoT Edge devices automatically have offline capabilities enabled. 若要将此功能扩展到其他 IoT 设备,需要在 IoT 中心声明设备之间的父子关系。To extend that capability to other IoT devices, you need to declare a parent-child relationship between the devices in IoT Hub.

  2. 与 IoT 中心同步Sync with IoT Hub.

    在安装 IoT Edge 运行时后,IoT Edge 设备至少要有一次处于联机状态,以便与 IoT 中心同步。At least once after installation of the IoT Edge runtime, the IoT Edge device needs to be online to sync with IoT Hub. 在此同步中,IoT Edge 设备将获取有关任何分配给它的子设备的详细信息。In this sync, the IoT Edge device gets details about any child devices assigned to it. IoT Edge 设备还可以安全更新本地缓存以启用脱机操作,并检索本地存储遥测消息的设置。The IoT Edge device also securely updates its local cache to enable offline operations and retrieves settings for local storage of telemetry messages.

  3. 脱机Go offline.

    从 IoT 中心断开连接时,IoT Edge 设备及其部署模块和任何 IoT 子设备都可以无限期运行。While disconnected from IoT Hub, the IoT Edge device, its deployed modules, and any children IoT devices can operate indefinitely. 模块和子设备可以在脱机状态下通过在 IoT Edge 中心进行身份验证来启动和重新启动。Modules and child devices can start and restart by authenticating with the IoT Edge hub while offline. 上游绑定到 IoT 中心的遥测存储在本地。Telemetry bound upstream to IoT Hub is stored locally. 模块之间或 loT 子设备之间的通信通过直接方法或消息来维护。Communication between modules or between child IoT devices is maintained through direct methods or messages.

  4. 与 IoT 中心重新连接和重新同步Reconnect and resync with IoT Hub.

    一旦还原与 IoT 中心的连接,IoT Edge 设备会再次同步。Once the connection with IoT Hub is restored, the IoT Edge device syncs again. 本地存储的消息按照它们存储的相同顺序传递。Locally stored messages are delivered in the same order in which they were stored. 模块和设备的所需属性和报告属性之间的差异已得到协调。Any differences between the desired and reported properties of the modules and devices are reconciled. IoT Edge 设备更新对其分配的 IoT 子设备集所做的任何更改。The IoT Edge device updates any changes to its set of assigned child IoT devices.

约束和限制Restrictions and limits

本文所述的扩展脱机功能可在 IoT Edge 1.0.4 版或更高版本中获得。The extended offline capabilities described in this article are available in IoT Edge version 1.0.4 or higher. 早期版本有一个脱机功能子集。Earlier versions have a subset of offline features. 不具备扩展脱机功能的现有 IoT Edge 设备不能通过更改运行时版本进行升级,但是必须用新的 IoT Edge 设备标识重新配置才能获得这些功能。Existing IoT Edge devices that don't have extended offline capabilities can't be upgraded by changing the runtime version, but must be reconfigured with a new IoT Edge device identity to gain these features.

只有非 Edge loT 设备可以作为子设备添加。Only non-Edge IoT devices can be added as child devices.

IoT Edge 设备及其分配的子设备可以在初始一次性同步之后无限期脱机运行。但是,消息存储取决于生存时间 (TTL) 设置和存储消息的可用磁盘空间。IoT Edge devices and their assigned child devices can function indefinitely offline after the initial, one-time sync. However, storage of messages depends on the time to live (TTL) setting and the available disk space for storing the messages.

设置 IoT Edge 设备Set up an IoT Edge device

对于将其扩展脱机功能扩展到 loT 子设备的 IoT Edge 设备,需要在 Azure 门户中声明父子关系。For an IoT Edge device to extend its extended offline capabilities to child IoT devices, you need to declare the parent-child relationships in the Azure portal.

分配子设备Assign child devices

子设备可以是注册到同一个 IoT 中心的任何非 Edge 设备。Child devices can be any non-Edge device registered to the same IoT Hub. 父设备可以有多个子设备,但子设备只能有一个父设备。Parent devices can have multiple child devices, but a child device can only have one parent. 可以通过三个选项将子设备设置为边缘设备:There are three options to set child devices to an edge device:

选项 1:IoT 中心门户Option 1: IoT Hub Portal

可以在创建新设备时管理父子关系,或者从 IoT Edge 父设备或 loT 子设备的设备详细信息页进行管理。You can manage the parent-child relationship on creating a new device, or from the device details page of either the parent IoT Edge device or the child IoT device.

从 IoT Edge 设备的详细信息页管理子设备

选项 2:使用 az 命令行工具Option 2: Use the az command-line tool

Azure 命令行界面IoT 扩展(v0.7.0 或更高版本)配合使用时,可以通过 device-identity 子命令管理父子关系。Using the Azure command-line interface with IoT extension (v0.7.0 or newer), you can manage parent child relationships with the device-identity sub-commands. 在下面的示例中,我们执行一个查询,将中心的所有非 IoT Edge 设备作为 IoT Edge 设备的子设备分配。In the example below, we execute a query to assign all non IoT Edge devices in the hub as child devices of an IoT Edge device.

# Set IoT Edge parent device
egde_device="edge-device1"

# Get All IoT Devices
device_list=$(az iot hub query \
        --hub-name replace-with-hub-name \
        --subscription replace-with-sub-name \
        --resource-group replace-with-rg-name \
        -q "SELECT * FROM devices WHERE capabilities.iotEdge = false" \
        --query 'join(`, `, [].deviceId)' -o tsv)

# Add all IoT devices to IoT Edge (as child)
az iot hub device-identity add-children \
  --device-id $egde_device \
  --child-list $device_list \
  --hub-name replace-with-hub-name \
  --resource-group replace-with-rg-name \
  --subscription replace-with-sub-name 

可以修改查询,选择另一部分设备。You can modify the query to select a different subset of devices. 如果指定大的设备集,此命令可能需要数秒钟才能完成。The command may take several seconds if you specify a large set of devices.

选项 3:使用 IoT 中心服务 SDKOption 3: Use IoT Hub Service SDK

最后,可以使用 C#、Java 或 Node.js IoT 中心服务 SDK 以编程方式管理父子关系。Finally, you can manage parent child relationships programmatically using either C#, Java or Node.js IoT Hub Service SDK. 这是使用 C# SDK 分配子设备的示例Here is an example of assigning a child device using the C# SDK.

指定 DNS 服务器Specifying DNS servers

为了提高可靠性,强烈建议指定在环境中使用的 DNS 服务器地址。To improve robustness, it is highly recommended you specify the DNS server addresses used in your environment. 请查看故障排除文章中有关此操作的两个选项Please see the two options to do this from the troubleshooting article.

可选脱机设置Optional offline settings

如果希望收集设备在长时间脱机期中生成的所有消息,请配置 IoT Edge 中心,这样它就可以存储所有消息。If you expect to collect all the messages that your devices generate during long offline periods, configure the IoT Edge hub so that it can store all the messages. 可以对 IoT Edge 中心进行两次更改,以启用长期消息存储。There are two changes that you can make to IoT Edge hub to enable long-term message storage. 首先,增加生存时间设置。First, increase the time to live setting. 然后,为消息存储添加更多磁盘空间。Then, add additional disk space for message storage.

生存时间Time to live

生存时间设置是指在过期之前消息可以等待传递的时间量(以秒为单位)。The time to live setting is the amount of time (in seconds) that a message can wait to be delivered before it expires. 默认为 7200 秒(两个小时)。The default is 7200 seconds (two hours). 此最大值仅受整数变量的最大值(约为 20 亿)限制。The maximum value is only limited by the maximum value of an integer variable, which is around 2 billion.

此设置是 IoT Edge 中心的所需属性,它存储在模块孪生中。This setting is a desired property of the IoT Edge hub, which is stored in the module twin. 可以在 Azure 门户的“配置高级 Edge 运行时设置”部分进行配置,也可以直接在部署清单中配置。You can configure it in the Azure portal, in the Configure advanced Edge Runtime settings section, or directly in the deployment manifest.

"$edgeHub": {
    "properties.desired": {
        "schemaVersion": "1.0",
        "routes": {},
        "storeAndForwardConfiguration": {
            "timeToLiveSecs": 7200
        }
    }
}

其他脱机存储Additional offline storage

默认情况下,消息存储在 IoT Edge 中心的容器文件系统中。Messages are stored by default in the IoT Edge hub's container filesystem. 如果存储空间不足以满足你的脱机需求,可以在 IoT Edge 设备上使用本地存储。If that amount of storage isn't sufficient for your offline needs, you can dedicate local storage on the IoT Edge device. 为 IoT Edge 中心创建一个环境变量,以便指向容器中的存储文件夹。Create an environment variable for the IoT Edge hub that points to a storage folder in the container. 然后,使用创建选项将存储文件夹绑定到主机上的文件夹。Then, use the create options to bind that storage folder to a folder on the host machine.

可以在 Azure 门户的“配置高级 Edge 运行时设置”部分配置环境变量和 IoT Edge 中心模块的创建选项。You can configure environment variables and the create options for the IoT Edge hub module in the Azure portal in the Configure advanced Edge Runtime settings section. 或者,可以直接在部署清单中进行配置。Or, you can configure it directly in the deployment manifest.

"edgeHub": {
    "type": "docker",
    "settings": {
        "image": "mcr.microsoft.com/azureiotedge-hub:1.0",
        "createOptions": {
            "HostConfig": {
                "Binds": ["<HostStoragePath>:<ModuleStoragePath>"],
                "PortBindings": {
                    "8883/tcp": [{"HostPort":"8883"}],
                    "443/tcp": [{"HostPort":"443"}],
                    "5671/tcp": [{"HostPort":"5671"}]
                }
            }
        }
    },
    "env": {
        "storageFolder": {
            "value": "<ModuleStoragePath>"
        }
    },
    "status": "running",
    "restartPolicy": "always"
}

<HostStoragePath><ModuleStoragePath> 替换为你的主机和模块存储路径;主机和模块存储路径都必须是绝对路径。Replace <HostStoragePath> and <ModuleStoragePath> with your host and module storage path; both host and module storage path must be an absolute path. 在创建选项中,将主机和模块存储路径绑定在一起。In the create options, bind the host and module storage paths together. 然后,创建指向模块存储路径的环境变量。Then, create an environment variable that points to the module storage path.

例如,"Binds":["/etc/iotedge/storage/:/iotedge/storage/"] 表示主机系统上的目录 /etc/iotedge/storage 映射到容器上的目录 /iotedge/storage/。For example, "Binds":["/etc/iotedge/storage/:/iotedge/storage/"] means the directory /etc/iotedge/storage on your host system is mapped to the directory /iotedge/storage/ on the container. 或是对于 Windows 系统的另一个示例,"Binds":["C:\\temp:C:\\contemp"] 表示主机系统上的目录 C:\temp 映射到容器上的目录 C:\contemp。Or another example for Windows systems, "Binds":["C:\\temp:C:\\contemp"] means the directory C:\temp on your host system is mapped to the directory C:\contemp on the container.

你还可以从 docker 文档中找到有关创建选项的更多详细信息。You can also find more details about create options from docker docs.

后续步骤Next steps

透明网关方案中启用扩展脱机操作。Enable extended offline operations in your transparent gateway scenarios.