Mosquitto 服务质量Qos

PS:原创文章,如需转载,请注明出处,谢谢!     

本文地址:http://flyer0126.iteye.com/blog/2228953

 

    MQTT(MQ Telemetry Transport),消息队列遥测传输协议,轻量级的发布/订阅协议,适用于一些条件比较苛刻的环境,进行低带宽、不可靠或间歇性的通信。值得一提的是mqtt提供三种不同质量的消息服务:

  • “至多一次”,消息发布完全依赖底层 TCP/IP 网络。会发生消息丢失或重复。这一级别可用于如下情况,环境传感器数据,丢失一次读记录无所谓,因为不久后还会有第二次发送。
  • “至少一次”,确保消息到达,但消息重复可能会发生。
  • “只有一次”,确保消息到达一次。这一级别可用于如下情况,在计费系统中,消息重复或丢失会导致不正确的结果。

    设置

    1. mosquitto_pub 客户端可发布一条消息到指定主题

    1.1. 用法:

    mosquitto_pub [-d] [-h hostname] [-i client_id] [-I client id prefix] [-p port number] [-q message QoS] [--quiet] [-r] { -f file | -l | -m message | -n | -s} [-u username [-P password] ] [ --will-topic topic [--will-payload payload] [--will-qos qos] [--will-retain] ] -t message-topic  

    1.2. 选项:

    -q, --qos

    指定消息的服务质量,可以为0,1,2,默认是0.

 

    2. sub_client 客户端订阅一个或多个主题的消息

    2.1. 用法:

    mosquitto_sub [-c] [-d] [-h hostname] [-i client_id] [-I client id prefix] [-k keepalive time] [-p port number] [-q message QoS] [--quiet] [-v] [ -u username [-Ppassword] ] [ --will-topic topic [--will-payload payload] [--will-qos qos] [--will-retain] ] -t message topic ...  

    2.2. 命令:

    mosquitto_sub 订阅到主题,接收到消息时打印

    2.3. 选项:

    -q, --qos

    指定消息的服务质量,可以为0,1,2,默认是0.

 

附:官方文档描述

    MQTT defines three levels of Quality of Service (QoS). The QoS defines how hard the broker/client will try to ensure that a message is received. Messages may be sent at any QoS level, and clients may attempt to subscribe to topics at any QoS level. This means that the client chooses the maximum QoS it will receive. For example, if a message is published at QoS 2 and a client is subscribed with QoS 0, the message will be delivered to that client with QoS 0. If a second client is also subscribed to the same topic, but with QoS 2, then it will receive the same message but with QoS 2. For a second example, if a client is subscribed with QoS 2 and a message is published on QoS 0, the client will receive it on QoS 0.

Higher levels of QoS are more reliable, but involve higher latency and have higher bandwidth requirements.

  • 0: The broker/client will deliver the message once, with no confirmation.

  • 1: The broker/client will deliver the message at least once, with confirmation required.

  • 2: The broker/client will deliver the message exactly once by using a four step handshake.

你可能感兴趣的:(mqtt)