小知识:Docker MQTT安装使用教程

MQTT简介

MQTT(Message Queuing Telemetry Transport,消息队列遥测传输)是IBM开发的一个即时通讯协议,有可能成为物联网的重要组成部分。该协议支持所有平台,几乎可以把所有联网物品和外部连接起来,被用来当做传感器和制动器(比如通过Twitter让房屋联网)的通信协议。

Docker安装RabbitMQ配置MQTT

使用RabbitMQ作为MQTT服务端,Eclipse Paho作为客户端。宿主机系统为ubuntu16.04

Docker下载镜像

docker pull daocloud.io/library/rabbitmq:3.7.4

启动RabbitMQ

?
1
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协议默认端口 15675 是web_mqtt websocket协议默认端口

启用插件

默认安装后我们需要手动开启rabbitmq_management插件,rabbitmq_mqtt插件和rabbitmq_web_mqtt插件。

执行如下三条命令

?
1
2
3
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,复制到容器中

?
1
2
3
/usr/sbin/rabbitmq-plugins enable rabbitmq_management
/usr/sbin/rabbitmq-plugins enable rabbitmq_mqtt
/usr/sbin/rabbitmq-plugins enable rabbitmq_web_mqtt

进入容器执行这个脚本。

sh start.sh

开放宿主机端口

?
1
2
3
4
5
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客户端实现

安装python包

pip install paho-mqtt

发送数据demo(消费者)

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
# 使用前需要启动hbase和thrift服务器
# 启动hbase在cd /usr/local/hbase下bin/start-hbase.sh  默认端口为 60000
# 启动thrift服务器cd /usr/local/hbase/bin执行./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)  # 将根目录添加到系统目录,才能正常引用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()

接收数据demo(生产者)

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
import sys
import os
dir_common = os.path.split(os.path.realpath(__file__))[0] + /../
sys.path.append(dir_common)  # 将根目录添加到系统目录,才能正常引用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()

生产者demo

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
# 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) # 发布消息
publish.single(“test”, “你好 MQTT”, qos = 1,hostname=HOST,port=PORT, client_id=client

官方文档: mqtt http://www.rabbitmq.com/mqtt.html

总结

以上就是这篇文章的全部内容了,希望本文的内容对大家的学习或者工作具有一定的参考学习价值,谢谢大家对服务器之家的支持。如果你想了解更多相关内容请查看下面相关链接

原文链接:https://blog.csdn.net/luanpeng825485697/article/details/82692195

声明: 猿站网有关资源均来自网络搜集与网友提供,任何涉及商业盈利目的的均不得使用,否则产生的一切后果将由您自己承担! 本平台资源仅供个人学习交流、测试使用 所有内容请在下载后24小时内删除,制止非法恶意传播,不对任何下载或转载者造成的危害负任何法律责任!也请大家支持、购置正版! 。本站一律禁止以任何方式发布或转载任何违法的相关信息访客发现请向站长举报,会员发帖仅代表会员个人观点,并不代表本站赞同其观点和对其真实性负责。本网站的资源部分来源于网络,如有侵权烦请发送邮件至:2697268773@qq.com进行处理。
建站知识

小知识:Jenkins简介与Docker部署Jenkins的方法

2023-4-5 13:28:19

建站知识

小知识:使用Docker run的选项以覆盖Dockerfile中的设置详解

2023-4-5 13:42:48

0 条回复 A文章作者 M管理员
    暂无讨论,说说你的看法吧
个人中心
购物车
优惠劵
今日签到
有新私信 私信列表
搜索