Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to install MQTT in Docker

2025-01-17 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Servers >

Share

Shulou(Shulou.com)06/01 Report--

This article will explain in detail how to install MQTT in Docker. The content of the article is of high quality, so the editor shares it for you as a reference. I hope you will have a certain understanding of the relevant knowledge after reading this article.

Introduction to MQTT

MQTT (Message Queuing Telemetry Transport, message queuing Telemetry Transmission) is an instant messaging protocol developed by IBM, which may become an important part of the Internet of things. The protocol supports all platforms, connects almost all connected objects to the outside, and is used as a communication protocol for sensors and brakes, such as connecting houses through Twitter.

Docker install RabbitMQ configure MQTT

Use RabbitMQ as the MQTT server and Eclipse Paho as the client. The host system is ubuntu16.04

Docker download image

Docker pull daocloud.io/library/rabbitmq:3.7.4

Start RabbitMQ

Docker run-d-- hostname my-rabbit-- name some-rabbit-p 15672 daocloud.io/library/rabbitmq:3.7.4 15672 daocloud.io/library/rabbitmq:3.7.4 15672 daocloud.io/library/rabbitmq:3.7.4

Note that the mapping container port

15672 is the default access port for the rabbitmq management management interface

5672 is the default port for amqp

1883 is the default port of mqtt tcp protocol

15675 is the default port of web_mqtt websocket protocol

Enable plug-in

After the default installation, we need to manually open the rabbitmq_management plug-in, rabbitmq_mqtt plug-in and rabbitmq_web_mqtt plug-in.

Execute the following three commands

Docker exec rabbitmq-plugins enable rabbitmq_managementdocker exec rabbitmq-plugins enable rabbitmq_mqttdocker exec rabbitmq-plugins enable rabbitmq_web_mqtt

Of course, you can also write a script start.sh and copy it to the container.

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

Go to the container and execute the script.

Sh start.sh

Open host port

Firewall-cmd-zone=public-add-port=15672/tcp-permanentfirewall-cmd-zone=public-add-port=5672/tcp-permanentfirewall-cmd-zone=public-add-port=1883/tcp-permanentfirewall-cmd-zone=public add-port=15675/tcp permanentfirewall-cmd permanentfirewall-cmd reload

Python MQTT client implementation

Install python package

Pip install paho-mqtt

Send data demo (Consumer)

# need to start hbase and thrift servers before use # launch hbase under cd / usr/local/hbase bin/start-hbase.sh default port is 6000 launch thrift server cd / usr/local/hbase/bin execution. / hbase-daemon.sh start thrift default port is 9090import sysimport osdir_common = os.path.split (os.path.realpath (_ _ file__)) [0] +'/.. / 'sys.path.append (dir_) Common) # add the root directory to the system directory Can you normally reference the common folder import argparse # import loggingimport time,datetimefrom common.py_log import init_logger,init_console_loggerfrom common.config import * from common.py_hbase import PyHbaseimport time,jsonfrom common.py_rabbit import Rabbit_Consumerimport paho.mqtt.client as mqttimport timeHOST = "192.168.2.46" PORT = 1883def client_loop (): client_id = time.strftime ('% Y% m% d% H% M% S') Time.localtime (time.time ()) client = mqtt.Client (client_id) # ClientId cannot be repeated So using the current time client.username_pw_set ("guest", "guest") # must be set Otherwise, "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) is returned Msg): print (msg.topic+ "" + msg.payload.decode ("utf-8")) if _ _ name__ = ='_ main__': client_loop ()

Receive data demo (producer)

Import sysimport osdir_common = os.path.split (os.path.realpath (_ _ file__)) [0] +'/.. / 'sys.path.append (dir_common) # add the root directory to the system directory before you can normally reference the common folder import paho.mqtt.client as mqttimport timeHOST = "192.168.2.46" PORT = 1883def client_loop (): client_id = time.strftime ('% Y% m% d% H% M% S') Time.localtime (time.time ()) client = mqtt.Client (client_id) # ClientId cannot be repeated So using the current time client.username_pw_set ("guest", "guest") # must be set Otherwise, "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) is returned Msg): print (msg.topic+ "" + msg.payload.decode ("utf-8")) if _ _ name__ = ='_ main__': client_loop ()

Producer demo

# import paho.mqtt.client as mqttimport paho.mqtt.publish as publishimport timeHOST = "192.168.2.46" PORT = 1883def 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% localtime (time.time () # client = mqtt.Client (client_id) # ClientId cannot be repeated So using the current time # client.username_pw_set ("guest", "guest") # must be set Otherwise, it will return "Connected with result code 4" # client.on_connect = on_connect # client.on_message = on_message # client.connect (HOST, PORT, 60) # client.publish ("test", "Hello MQTT", qos=0, retain=False) # post message publish.single ("test", "Hello MQTT", qos= 1 camera hostnameplate hostname host port, client_id=client 's information on how to install MQTT in Docker is here. I hope the above content can be of some help to you and learn more knowledge. If you think the article is good, you can share it for more people to see.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Servers

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report