天天看點

簡裝mosquitto 叢集 docker方式

第一步:準備三台機器

第二步:在網上拉取鏡像 docker pull eclipse-mosquitto

第三步:在主機器上的/opt/mosquittoconf/ 下建立 mosquitto.conf

放入

connection mytest1

address 從機器1ip位址:1883

topic room1/# both 2 sensor/ myhouse/

connection mytest2

address 從機器2ip位址:1883

topic room1/# both 2 sensor/ myhouse/

bridge_protocol_version mqttv311

notifications true

cleansession true

try_private true

start_type automatic

解釋:

connection 連接配接名稱,可以随便取

address 連接配接的另外伺服器位址和端口号,如果有多台伺服器,可以寫多個address

topic 主題名稱,“#”為通配符,表示釋出端可以在room1/後面接任何文字

both 服務品質,2代表隻有一次(可以檢視第一篇博文對MQTT的詳細介紹)

sensor/  本地字首辨別,可以随便命名

myhouse/ 遠端字首辨別,可以随便命名

bridge_protocol_version mqttv311 橋連接配接協定版本MQTT3.11

notifications  是否釋出橋接的狀态資訊

cleansession  橋接斷開時,是否清除遠端伺服器中的消息

start_type 橋接模式,目前有三種:automatic、lazy、once

設定好之後儲存退出。

第四步:繼續建立 config.mk

内容參考 https://github.com/eclipse/mosquitto/blob/master/config.mk

找到WITH_BRIDGE:=yes 将簽名的“#”号去掉開啟橋連接配接模式。(預設是開啟的,為了無誤檢視一下)

第五步:然後在兩個從機器上啟動 

docker run -d --name mytest12 -p 1883:1883 -p 9001:9001 eclipse-mosquitto

第六步:然後再啟動主機器上的

docker run -d --name mqtttest -p 1883:1883 -p 9001:9001 -v /opt/mosquittoconf/mosquitto.conf:/mosquitto/config/mosquitto.conf -v /opt/mosquittoconf/config.mk:/mosquitto/config/config.mk  eclipse-mosquitto

第七步:進入容器檢視

docker exec -it 容器名 /bin/sh