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

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

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

若要了解这些协议如何支持特定的 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
基于 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.
基于 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. 发出更多 HTTPS 接收会导致 IoT 中心限制请求。Issuing more HTTPS receives results in IoT Hub throttling the requests. 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 连接使用单个设备标识(设备 ID 加上凭据)。MQTT and HTTPS support only a single device identity (device ID plus credentials) per TLS connection. 因此,现场网关方案不支持这些协议,这些方案要求在到 IoT 中心的单个上游连接或一个上游连接池中使用多个设备标识多路复用消息。For this reason, these protocols are not supported for field gateway scenarios that require multiplexing messages using multiple device identities across a single or a pool of upstream connections to IoT Hub. 此类网关可以将一个支持每个连接使用多个设备标识的协议(例如 AMQP)用于其上游流量。Such gateways can use a protocol that supports multiple device identities per connection, like AMQP, for their upstream traffic.

  • 低资源设备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.


使用 HTTPS 时,每台设备应每 25 分钟或更短时间轮询一次云到设备消息。When using HTTPS, each device should poll for cloud-to-device messages no more than once every 25 minutes. 在开发中,每台设备都可以根据需要更频繁地轮询。In development, each device can poll more frequently, if desired.


使用 X.509 证书颁发机构 (CA) 身份验证的设备的以下功能尚未正式发布,必须启用预览模式The following functionality for devices that use X.509 certificate authority (CA) authentication is not yet generally available, and preview mode must be enabled:

  • HTTPS、基于 WebSocket 的 MQTT 和基于 WebSocket 的 AMQP 协议。HTTPS, MQTT over WebSockets, and AMQP over WebSockets protocols.
  • 文件上传(所有协议)。File uploads (all protocols).

它在使用 X.509 指纹身份验证的设备上已正式发布。It is generally available on devices that use X.509 thumbprint authentication. 若要了解有关使用 IoT 中心进行 x.509 身份验证的详细信息,请参阅支持的 x.509 证书To learn more about X.509 authentication with IoT Hub, see Supported X.509 certificates.

端口号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

在 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.