参考 - 选择通信协议Reference - choose a communication protocol

IoT 中心允许设备使用以下协议进行设备端通信:IoT Hub allows devices to use the following protocols for device-side communications:

  • MQTTMQTT
  • 基于 WebSocket 的 MQTTMQTT over WebSockets
  • AMQPAMQP
  • 基于 WebSockets 的 AMQPAMQP over WebSockets
  • HTTPSHTTPS

有关这些协议如何支持特定 IoT 中心功能的信息,请参阅设备到云通信指南云到设备通信指南For information about how these protocols support specific IoT Hub features, see Device-to-cloud communications guidance and Cloud-to-device communications guidance.

下表提供了针对协议选取的高水平建议:The following table provides the high-level recommendations for your choice of protocol:

协议Protocol 何时应选择此协议When you should choose this protocol
MQTTMQTT
基于 WebSocket 的 MQTTMQTT over WebSocket
用于无需使用相同的 TLS 连接来连接多台设备(各有自己的设备凭据)的所有设备。Use on all devices that do not require to connect multiple devices (each with its own per-device credentials) over the same TLS connection.
AMQPAMQP
基于 WebSocket 的 AMQPAMQP over WebSocket
用于利用跨设备连接复用的字段和云网关。Use on field and cloud gateways to take advantage of connection multiplexing across devices.
HTTPSHTTPS 用于不可支持其他协议的设备。Use for devices that cannot support other protocols.

在选择设备端通信协议时,请考虑以下几点:Consider the following points when you choose your protocol for device-side communications:

  • 云到设备模式Cloud-to-device pattern. HTTPS 没有用于实现服务器推送的有效方法。HTTPS does not have an efficient way to implement server push. 因此,使用 HTTPS 时,设备会在 IoT 中心轮询从云到设备的消息。As such, when you are using HTTPS, devices poll IoT Hub for cloud-to-device messages. 此方法对于设备和 IoT 中心而言是低效的。This approach is inefficient for both the device and IoT Hub. 根据当前 HTTPS 准则,每台设备应每 25 分钟或更长时间轮询一次消息。Under current HTTPS guidelines, each device should poll for messages every 25 minutes or more. MQTT 和 AMQP 支持在收到云到设备的消息时进行服务器推送。MQTT and AMQP support server push when receiving cloud-to-device messages. 它们会启用从 IoT 中心到设备的直接消息推送。They enable immediate pushes of messages from IoT Hub to the device. 如果传送延迟是考虑因素,最好使用 MQTT 或 AMQP 协议。If delivery latency is a concern, MQTT or AMQP are the best protocols to use. 对于很少连接的设备,HTTPS 也适用。For rarely connected devices, HTTPS works as well.
  • 现场网关Field gateways. 使用 MQTT 和 HTTPS 时,无法使用相同的 TLS 连接来连接多台设备(各有自己的设备凭据)。When using MQTT and HTTPS, you cannot connect multiple devices (each with its own per-device credentials) using the same TLS connection. 对于现场网关方案,这些并不是最理想的协议,因为对于每个连接的设备,需要在现场网关和 IoT 中心之间建立 TLS 连接。For Field gateway scenarios that require one TLS connection between the field gateway and IoT Hub for each connected device, these protocols are suboptimal.
  • 低资源设备Low resource devices. 相比 AMQP 库的占用空间,MQTT 和 HTTPS 库的占用空间更小。The MQTT and HTTPS libraries have a smaller footprint than the AMQP libraries. 因此,如果设备的资源很少(如低于 1 MB RAM),可能只可实现这些协议。As such, if the device has limited resources (for example, less than 1 MB RAM), these protocols might be the only protocol implementation available.
  • 网络遍历Network traversal. 标准 AMQP 协议使用端口 5671,而 MQTT 侦听端口 8883。The standard AMQP protocol uses port 5671, and MQTT listens on port 8883. 使用这些端口可能会给未向非 HTTPS 协议开放的网络带来问题。USe of these ports could cause problems in networks that are closed to non-HTTPS protocols. 在此情况下,使用基于 WebSockets 的 MQTT、基于 WebSockets 的 AMQP 或者 HTTPS。Use MQTT over WebSockets, AMQP over WebSockets, or HTTPS in this scenario.
  • 有效负载大小Payload size. MQTT 和 AMQP 是二进制协议,因此,其有效负载比 HTTPS 的有效负载更精简。MQTT and AMQP are binary protocols, which result in more compact payloads than HTTPS.

Warning

使用 HTTPS 时,每台设备应每 25 分钟或更长时间轮询一次从云到设备的消息。When using HTTPS, each device should poll for cloud-to-device messages every 25 minutes or more. 但在开发期间,可按低于 25 分钟的更高频率进行轮询。However, during development, it is acceptable to poll more frequently than every 25 minutes.

端口号Port numbers

设备可在 Azure 中使用各种协议来与 IoT 中心通信。Devices can communicate with IoT Hub in Azure using various protocols. 通常,选择的协议根据解决方案的具体要求而定。Typically, the choice of protocol is driven by the specific requirements of the solution. 下表列出了必须打开的、使设备能够使用特定协议的出站端口:The following table lists the outbound ports that must be open for a device to be able to use a specific protocol:

协议Protocol 端口Port
MQTTMQTT 88838883
基于 WebSocket 的 MQTTMQTT over WebSockets 443443
AMQPAMQP 56715671
基于 WebSockets 的 AMQPAMQP over WebSockets 443443
HTTPSHTTPS 443443

在 Azure 区域创建 IoT 中心后,该 IoT 中心在其生存期内会保留同一 IP 地址。Once you have created an IoT hub in an Azure region, the IoT hub keeps the same IP address for the lifetime of that IoT hub. 但如果 Microsoft 将 IoT 中心移到其他缩放单元以保持服务质量,则向其分配新的 IP 地址。However, if Microsoft moves the IoT hub to a different scale unit to maintain quality of service, then it is assigned a new IP address.

后续步骤Next steps

若要了解有关 IoT 中心如何实现 MQTT 协议的详细信息,请参阅使用 MQTT 协议与 IoT 中心通信To learn more about how IoT Hub implements the MQTT protocol, see Communicate with your IoT hub using the MQTT protocol.