使用持久性重复消息mosquitto MQTT broker到amazon IoT Service

时间:2017-04-18 10:05:41

标签: amazon-web-services mqtt mosquitto

我有一个带有蚊子经纪人的覆盆子,并与亚马逊物联网服务桥接。 https://aws.amazon.com/es/blogs/iot/how-to-bridge-mosquitto-mqtt-broker-to-aws-iot/

这是我的mosquitto.conf文件:

# Place your local configuration in /etc/mosquitto/conf.d/
#
# A full description of the configuration file is at
# /usr/share/doc/mosquitto/examples/mosquitto.conf.example

pid_file /var/run/mosquitto.pid

persistence true
persistence_location /var/lib/mosquitto/

log_dest file /var/log/mosquitto/mosquitto.log

include_dir /etc/mosquitto/conf.d

这是/etc/mosquitto/conf.d

中的bridge.conf
# =================================================================
# Bridges to AWS IOT
# =================================================================

# AWS IoT endpoint, use AWS CLI 'aws iot describe-endpoint'
connection awsiot
address xxxxxxxxx.iot.eu-central-1.amazonaws.com:8883

# Specifying which topics are bridged
topic awsiot_to_localgateway in 1
topic localgateway_to_awsiot/iot out 1
topic both_directions both 1

# Setting protocol version explicitly
bridge_protocol_version mqttv311
bridge_insecure false

# Bridge connection name and MQTT client Id,
# enabling the connection automatically when the broker starts.
cleansession true
clientid bridgeawsiot
start_type automatic
notifications false
log_type all

# =================================================================
# Certificate based SSL/TLS support
# -----------------------------------------------------------------
#Path to the rootCA
bridge_cafile /etc/mosquitto/certs/rootCA.pem

# Path to the PEM encoded client certificate
bridge_certfile /etc/mosquitto/certs/cert.crt

# Path to the PEM encoded client private key
bridge_keyfile /etc/mosquitto/certs/private.key

一切正常。但是,如果我删除以太网电缆来测试pesistence。当重新建立通信时。经纪人向亚马逊物联网服务发送重复的消息。

这是我发送的消息

char dataToSend[] = "Message Id: ";
counter++;

snprintf(dataToSend, sizeof(dataToSend) + 10, "Message Id: %d", counter);
app_mqtt_publish(&dataToSend);

这是正常行为吗?

2 个答案:

答案 0 :(得分:2)

MQTT规范(部分)的简短版本:

  • QOS 0 - >消息可能会传递
  • QOS 1 - >消息将至少发送一次
  • QOS 2 - >消息将只传送一次。

因此,如果消息未被确认,则有可能再次传递QOS 1消息。他们应该在标题中设置DUP标志,以便接收经纪人知道他们可能已经交付了。

IIRC AWS-IoT不支持QOS 2,因此您可能已经忍受了这一点。

答案 1 :(得分:0)

AWS IoT不支持cleansession false。这座桥的后果是:

  • 发送消息时出错
  • 并拥有persistence true
  • 并没有实际断开连接
  • 和QoS>如果queue_qos0_messages true
  • ,则为0或甚至QoS = 0

=>消息保存在db。

但是如果客户端自动设置为网桥有效地断开连接,则cleansession true告诉代理不要保留数据,因此它会清除此客户端的数据库。

希望在发送cleansession false时AWS IoT没有断开连接,因此我们可以保留此本地缓存......

ps:“客户端自动设置为网桥”是指,当您在两个代理之间设置桥接时,会创建一个客户端来订阅一个客户端并始终发布到另一个。