本地 MQTT 代理的连接 uri

Connection uri of local MQTT broker

我目前有两个 MQTT 代理都工作得很好。其中之一是我的 local 网络上的 Raspberry Pi。另一个是托管在 CloudMQTT 上的 远程 代理。

目标是使两个代理相互同步。当然,最简单的解决方案是使用网桥,但我在设置正确的网桥时遇到了一些问题。

如下图所示,当我尝试在 CloudMQTT 上设置网桥时,它要求 'Connection uri'。对于远程代理,这将是 mqtt://username:password@m20.cloudmqtt.com:1883,但我似乎无法弄清楚如何为本地代理执行此操作。

据我所知,只有当您拥有静态 IP 且端口转发到 Raspberry Pi 时,您才能连接到本地网络,那么我该如何解决这个问题?两个 broker 需要保持一个 permanent TCP 连接打开,但是 remote broker 不能自己打开它。本地broker打开这个连接不就可以了吗?如果是这样,我如何让这两个代理同步?

感谢任何帮助,非常感谢您的提前帮助!

编辑:

这是我的完整 /etc/mosquitto/mosquitto.conf 文件:

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


connection CloudMQTT-Bridge

topic # both 2 local/testing remote/testing

bridge_protocol_version mqttv311

address m20:cloudmqtt.com:17001
remote_username jcw******
remote_password p-fO*****
remote_clientid RaspberryPiBroker

您可以使用 mosquitto 创建桥梁,man 页面的 "Configuring Bridges" 部分涵盖了所有选项,但您需要的基础知识是:

connection cloudMQ-Bridge
address m20.cloudmqtt.com
remote_username user
remote_password password
remote_clientid clientid
topic # in 2

棘手的一点是末尾的主题行,它控制如何在两个代理之间映射主题。您必须小心不要创建循环。

在示例中,所有主题都以 QOS 2 从远程代理映射到本地代理,没有任何主题重新映射。请仔细阅读手册中关于主题映射的部分,因为它很容易出错。