美文网首页
mqtt服务端架设mosquitto

mqtt服务端架设mosquitto

作者: yichen_china | 来源:发表于2023-10-21 20:18 被阅读0次

物理网中有一个核心的概念就是MQTT服务器。其实,MQTT 是一个物理网协议,性质对等于网页用到的HTTP协议。

所以说MQTT服务器就是承载、解释、传递协议的中转机构。用什么解释呢?你可以把MQTT服务器看做是银行的私人保险柜。储物人和提货人,可以通过保险柜的编号和密码,在不同时间传递东西。


image.png

分别搭建window环境和 docker 环境

MQTT 服务器(window环境实现)

第一步

  1. 挑选合适的MQTT服务器软件

  2. 下载安装mosquitto 服务器

  3. 修改 mosquitto 配置,并启动 mosquitto 服务

  4. 利用python paho编写 mqtt发布端和服务端

mosquitto,Eclipse Mosquitto使用 C 语言实现的 MQTT 服务器。
EMQX,使用 Erlang 语言开发的 MQTT 服务器,内置强大的规则引擎,支持许多其他 IoT 协议比如 MQTT-SN、 CoAP、LwM2M 等。
Mosca,使用 Node.JS 开发的 MQTT 服务器,简单易用。
VerneMQ,使用 Erlang 开发的 MQTT 服务器。

这里我选用 mosquitto 的服务器

第二步、MQTT服务器软件mosquitto下载

1、进入mosquitto下载网址:https://mosquitto.org/download/,如下图:

QQ图片20231022195152.jpg

第三步、修改mosquitto配置,并启动mosquitto服务

安装成功够,在安装文件夹下。找到mosquitto.conf 文件,用编辑器打开。

1. 修改服务器端口

打开mosquitto.conf,找到linstener,取消前面的#注释,并指定要侦听的端口,如下:

      listener 1883

2.设置用户名密码

找到找到allow_anonymous节点,这个节点作用是,是否开启匿名用户登录,默认是true。打开此项配置(将前面的 # 号去掉)之后将其值改为true

      修改前:#allow_anonymous
      修改后:allow_anonymous false

找到password_file节点,这个节点是告诉服务器你要配置的用户将存放在哪里。打开此配置并指定pwfile.example文件路劲(注意是绝对路劲)

修改前:#password_file
修改后:password_file D:\pwfile.example

3. 在管理工具->服务,中找到Mosquitto Broker,启动mosquitto服务,如下图:

启动 Broker


image.png

使用docker搭建mqtt服务

1、下载mqtt

docker pull eclipse-mosquitto

2、启动mqtt

docker run -it --name=mosquitto -p 1883:1883 -d eclipse-mosquitto

3、更改账号密码:

(1)、进入容器中
docker exec -it mosquitto sh
(2)、进入cd /mosquitto/config,修改配置文件 vi mosquitto.conf
listener 1883,这个不添加,只有本机才能够访问,其它地址访问不了。
allow_anonymous false ,不允许匿名登录。按n键搜索下一条。
password_file ,后面加上 /mosquitto/config/pwdfile.conf,写绝对地址,不要写相对地址,这个设置的是存放密码的文件的位置。
acl_file /mosquitto/aclfile 这个是配置用户订阅读写权限的
persistence_file mosquitto.db 这个是数据存储文件名
wq 保存退出
(3)、mosquitto.conf 所在位置,建立一个文件pwdfile.conf, 命令 touch pwdfile.conf
(4)使用mosquitto_passwd命令创建用户名密码
写入账号密码命令:mosquitto_passwd -b pwdfile.conf admin 123456 (admin 是账号,123456是密码)
(5)、编写aclfile作为权限控制,aclfile.conf文件内容请看下文示例

4、注意:这个是在mosquitto 2.xx版本中的操作,如果不配置密码,客户端会显示无权限连接

下面配置文件是我测试过程中配置的,可以参考

mosquitto.conf配置如下

#mosquitto.conf
#如果mosquitto.conf中没有指定监听TCP端口,那么默认情况下mosquitto将不会监听TCP端口,也就无法#通过TCP协议访问。因此,如果你需要使用TCP协议连接mosquitto,则需要在mosquitto.conf中添加#listener 1883或类似的条目,以便mosquitto监听TCP端口。
# 服务绑定的端口号
listener 1883
persistence true
persistence_file mosquitto.db
persistence_location /mosquitto/data/
# 允许匿名用户true
allow_anonymous false
# 用户/密码文件,默认格式:username:password
password_file /mosquitto/pwfile
# 配置用户访问控制
acl_file /mosquitto/aclfile
# 服务进程的PID
#pid_file /var/run/mosquitto.pid
# 服务进程的系统用户
#user mosquitto
# 服务绑定的IP地址
#bind_address centoshostnameKL3
# 允许的最大连接数,-1表示没有限制
#max_connections -1

aclfile.conf文件内容如下

user1 和user2 是用户名
user1 设置为订阅权限,并且只能访问的主题为"root/topic/#"
user2 设置为发布权限,并且只能访问的主题为"root/topic/#"
user3 设置为发布+订阅权限
test 订阅主题
使用中订阅 topic\test

user user1
topic read root/topic/#

user user2
topic write root/topic/#

user use3
topic write $SYS/#
topic read $SYS/#

到这里已经完成搭建mqtt服务。
退出容器,重启服务。docker restart mosquitto。
……………………………………………………………………………………………………………………

使用docker-compose 搭建mqtt服务

创建 docker-compose.yml文件

# 配置使用的是eclipse-mosquitto镜像,该镜像提供了一个用于MQTT服务器的基本环境。通过将主机的1883端口映射到容器的1883端口,可以使得外部设备可以通过主机访问MQTT服务器。
version: '3.8'

services:
  mqtt:
    image: eclipse-mosquitto
    ports:
#端口映射前面是本机端口 后面是容器端口
      - 1883:1883
      - 9001:9001
    volumes:
#路径映射 前面是本机 后面是容器
#  本机配置文件mosquitto.conf需要提前创建,参考上文的配置示例
# 也可以从镜像里拷贝一份出来 执行docker cp containerId:/mosquitto/config/mosquitto.conf 
      - ./mosquitto/config/mosquitto.conf:/mosquitto/config/mosquitto.conf
      - ./mosquitto/data:/mosquitto/data
      - ./mosquitto/log:/mosquitto/log
      - ./mosquitto:/mosquitto

执行docker-compose up即可启动

相关文章

网友评论

      本文标题:mqtt服务端架设mosquitto

      本文链接:https://www.haomeiwen.com/subject/gltridtx.html