溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊(cè)×
其他方式登錄
點(diǎn)擊 登錄注冊(cè) 即表示同意《億速云用戶服務(wù)條款》

Docker MQTT怎么安裝使用

發(fā)布時(shí)間:2022-03-24 16:39:47 來(lái)源:億速云 閱讀:305 作者:iii 欄目:web開發(fā)

這篇“Docker MQTT怎么安裝使用”文章的知識(shí)點(diǎn)大部分人都不太理解,所以小編給大家總結(jié)了以下內(nèi)容,內(nèi)容詳細(xì),步驟清晰,具有一定的借鑒價(jià)值,希望大家閱讀完這篇文章能有所收獲,下面我們一起來(lái)看看這篇“Docker MQTT怎么安裝使用”文章吧。

mqtt簡(jiǎn)介

mqtt(message queuing telemetry transport,消息隊(duì)列遙測(cè)傳輸)是ibm開發(fā)的一個(gè)即時(shí)通訊協(xié)議,有可能成為物聯(lián)網(wǎng)的重要組成部分。該協(xié)議支持所有平臺(tái),幾乎可以把所有聯(lián)網(wǎng)物品和外部連接起來(lái),被用來(lái)當(dāng)做傳感器和制動(dòng)器(比如通過(guò)twitter讓房屋聯(lián)網(wǎng))的通信協(xié)議。

docker安裝rabbitmq配置mqtt

使用rabbitmq作為mqtt服務(wù)端,eclipse paho作為客戶端。宿主機(jī)系統(tǒng)為ubuntu16.04

docker下載鏡像

docker pull daocloud.io/library/rabbitmq:3.7.4

啟動(dòng)rabbitmq

docker run -d --hostname my-rabbit --name some-rabbit -p 15672:15672 -p 5672:5672 -p 1883:1883 -p 15675:15675 daocloud.io/library/rabbitmq:3.7.4

注意映射容器端口

  • 15672 是rabbitmq management管理界面默認(rèn)訪問(wèn)端口

  • 5672 是amqp默認(rèn)端口

  • 1883 是mqtt tcp協(xié)議默認(rèn)端口

  • 15675 是web_mqtt websocket協(xié)議默認(rèn)端口

啟用插件

默認(rèn)安裝后我們需要手動(dòng)開啟rabbitmq_management插件,rabbitmq_mqtt插件和rabbitmq_web_mqtt插件。

執(zhí)行如下三條命令

docker exec <容器id> rabbitmq-plugins enable rabbitmq_management
docker exec <容器id> rabbitmq-plugins enable rabbitmq_mqtt
docker exec <容器id> rabbitmq-plugins enable rabbitmq_web_mqtt

當(dāng)然你也可以寫個(gè)腳本start.sh,復(fù)制到容器中

/usr/sbin/rabbitmq-plugins enable rabbitmq_management
/usr/sbin/rabbitmq-plugins enable rabbitmq_mqtt
/usr/sbin/rabbitmq-plugins enable rabbitmq_web_mqtt

進(jìn)入容器執(zhí)行這個(gè)腳本。

sh start.sh

開放宿主機(jī)端口

firewall-cmd --zone=public --add-port=15672/tcp --permanent
firewall-cmd --zone=public --add-port=5672/tcp --permanent
firewall-cmd --zone=public --add-port=1883/tcp --permanent
firewall-cmd --zone=public --add-port=15675/tcp --permanent
firewall-cmd --reload

python mqtt客戶端實(shí)現(xiàn)

安裝python包

pip install paho-mqtt

發(fā)送數(shù)據(jù)demo(消費(fèi)者)

# 使用前需要啟動(dòng)hbase和thrift服務(wù)器
# 啟動(dòng)hbase在cd /usr/local/hbase下bin/start-hbase.sh  默認(rèn)端口為 60000
# 啟動(dòng)thrift服務(wù)器cd /usr/local/hbase/bin執(zhí)行./hbase-daemon.sh start thrift  默認(rèn)端口為9090
import sys
import os
dir_common = os.path.split(os.path.realpath(__file__))[0] + '/../'
sys.path.append(dir_common)  # 將根目錄添加到系統(tǒng)目錄,才能正常引用common文件夾
import argparse  #
import logging
import time,datetime
from common.py_log import init_logger,init_console_logger
from common.config import *
from common.py_hbase import pyhbase
import time,json
from common.py_rabbit import rabbit_consumer
import paho.mqtt.client as mqtt
import time
host = "192.168.2.46"
port = 1883
def client_loop():
  client_id = time.strftime('%y%m%d%h%m%s',time.localtime(time.time()))
  client = mqtt.client(client_id)  # clientid不能重復(fù),所以使用當(dāng)前時(shí)間
  client.username_pw_set("guest", "guest") # 必須設(shè)置,否則會(huì)返回「connected with result code 4」
  client.on_connect = on_connect
  client.on_message = on_message
  client.connect(host, port, 60)
  client.loop_forever()
def on_connect(client, userdata, flags, rc):
  print("connected with result code "+str(rc))
  client.subscribe("test")
def on_message(client, userdata, msg):
  print(msg.topic+" "+msg.payload.decode("utf-8"))
if __name__ == '__main__':
  client_loop()

接收數(shù)據(jù)demo(生產(chǎn)者)

import sys
import os
dir_common = os.path.split(os.path.realpath(__file__))[0] + '/../'
sys.path.append(dir_common)  # 將根目錄添加到系統(tǒng)目錄,才能正常引用common文件夾
import paho.mqtt.client as mqtt
import time
host = "192.168.2.46"
port = 1883
def client_loop():
  client_id = time.strftime('%y%m%d%h%m%s',time.localtime(time.time()))
  client = mqtt.client(client_id)  # clientid不能重復(fù),所以使用當(dāng)前時(shí)間
  client.username_pw_set("guest", "guest") # 必須設(shè)置,否則會(huì)返回「connected with result code 4」
  client.on_connect = on_connect
  client.on_message = on_message
  client.connect(host, port, 60)
  client.loop_forever()
def on_connect(client, userdata, flags, rc):
  print("connected with result code "+str(rc))
  client.subscribe("test")
def on_message(client, userdata, msg):
  print(msg.topic+" "+msg.payload.decode("utf-8"))
if __name__ == '__main__':
  client_loop()

生產(chǎn)者demo

# import paho.mqtt.client as mqtt
import paho.mqtt.publish as publish
import time
host = "192.168.2.46"
port = 1883
def on_connect(client, userdata, flags, rc):
  print("connected with result code "+str(rc))
  client.subscribe("test")
def on_message(client, userdata, msg):
  print(msg.topic+" "+msg.payload.decode("utf-8"))
if __name__ == '__main__':
  client_id = time.strftime('%y%m%d%h%m%s',time.localtime(time.time()))
  # client = mqtt.client(client_id)  # clientid不能重復(fù),所以使用當(dāng)前時(shí)間
  # client.username_pw_set("guest", "guest") # 必須設(shè)置,否則會(huì)返回「connected with result code 4」
  # client.on_connect = on_connect
  # client.on_message = on_message
  # client.connect(host, port, 60)
  # client.publish("test", "你好 mqtt", qos=0, retain=false) # 發(fā)布消息
  publish.single("test", "你好 mqtt", qos = 1,hostname=host,port=port, client_id=client

以上就是關(guān)于“Docker MQTT怎么安裝使用”這篇文章的內(nèi)容,相信大家都有了一定的了解,希望小編分享的內(nèi)容對(duì)大家有幫助,若想了解更多相關(guān)的知識(shí)內(nèi)容,請(qǐng)關(guān)注億速云行業(yè)資訊頻道。

向AI問(wèn)一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI