如何在Docker中安裝MQTT

這篇文章將為大家詳細講解有關如何在Docker中安裝MQTT,文章內容質量較高,因此小編分享給大家做個參考,希望大家閱讀完這篇文章后對相關知識有一定的了解。

創(chuàng)新互聯(lián)是一家專注于網(wǎng)站設計、成都做網(wǎng)站與策劃設計,老河口網(wǎng)站建設哪家好?創(chuàng)新互聯(lián)做網(wǎng)站,專注于網(wǎng)站建設十載,網(wǎng)設計領域的專業(yè)建站公司;建站業(yè)務涵蓋:老河口等地區(qū)。老河口做網(wǎng)站價格咨詢:18980820575

MQTT簡介

MQTT(Message Queuing Telemetry Transport,消息隊列遙測傳輸)是IBM開發(fā)的一個即時通訊協(xié)議,有可能成為物聯(lián)網(wǎng)的重要組成部分。該協(xié)議支持所有平臺,幾乎可以把所有聯(lián)網(wǎng)物品和外部連接起來,被用來當做傳感器和制動器(比如通過Twitter讓房屋聯(lián)網(wǎng))的通信協(xié)議。

Docker安裝RabbitMQ配置MQTT

使用RabbitMQ作為MQTT服務端,Eclipse Paho作為客戶端。宿主機系統(tǒng)為ubuntu16.04

Docker下載鏡像

docker pull daocloud.io/library/rabbitmq:3.7.4

啟動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管理界面默認訪問端口

  • 5672 是amqp默認端口

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

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

啟用插件

默認安裝后我們需要手動開啟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

當然你也可以寫個腳本start.sh,復制到容器中

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

進入容器執(zhí)行這個腳本。

sh start.sh

開放宿主機端口

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客戶端實現(xiàn)

安裝python包

pip install paho-mqtt

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

# 使用前需要啟動hbase和thrift服務器
# 啟動hbase在cd /usr/local/hbase下bin/start-hbase.sh  默認端口為 60000
# 啟動thrift服務器cd /usr/local/hbase/bin執(zhí)行./hbase-daemon.sh start thrift  默認端口為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不能重復,所以使用當前時間
  client.username_pw_set("guest", "guest") # 必須設置,否則會返回「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不能重復,所以使用當前時間
  client.username_pw_set("guest", "guest") # 必須設置,否則會返回「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不能重復,所以使用當前時間
  # client.username_pw_set("guest", "guest") # 必須設置,否則會返回「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

關于如何在Docker中安裝MQTT就分享到這里了,希望以上內容可以對大家有一定的幫助,可以學到更多知識。如果覺得文章不錯,可以把它分享出去讓更多的人看到。

網(wǎng)頁題目:如何在Docker中安裝MQTT
文章來源:http://bm7419.com/article42/gihehc.html

成都網(wǎng)站建設公司_創(chuàng)新互聯(lián),為您提供微信公眾號、企業(yè)網(wǎng)站制作、軟件開發(fā)、網(wǎng)站內鏈云服務器、網(wǎng)站收錄

廣告

聲明:本網(wǎng)站發(fā)布的內容(圖片、視頻和文字)以用戶投稿、用戶轉載內容為主,如果涉及侵權請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內容未經(jīng)允許不得轉載,或轉載時需注明來源: 創(chuàng)新互聯(lián)

成都定制網(wǎng)站網(wǎng)頁設計