Eclipse Mosquitto是一个开源消息代理服务器,实现了MQTT协议版本3.1和3.1.1。Mosquitto轻量,适用于低功耗单板计算机到完整服务器的所有设备。Mosquitto项目还提供了用于实现MQTT客户端的C库以及非常受欢迎的mosquitto_pub和mosquitto_sub命令行MQTT客户端。
1. 下载安装
以Ubuntu 18.04为例,下面是通过apt-get直接进行安装。也可以在mosquitto的官方网站(http://mosquitto.org/files/source/)下载tar.gz安装包进行手动安装。
1) 引入mosquitto仓库并更新
$ sudo apt-add-repository ppa:mosquitto-dev/mosquitto-ppa
$ sudo apt-get update
2) 执行以下命令安装mosquitto包
$ sudo apt-get install mosquitto
3) 安装mosquitto开发包
$ sudo apt-get install libmosquitto-dev
4) 安装mosquitto客户端
$ sudo apt-get install mosquitto-clients
2. 配置
2.1 主配置文件 mosquitto.conf
添加禁止匿名访问、认证配置和权限限制等配置项。
$ sudo vim /etc/mosquitto/mosquitto.conf
pid_file /var/run/mosquitto.pid
#消息持久存储
persistence true
persistence_location /var/lib/mosquitto/
#日志文件
log_dest file /var/log/mosquitto/mosquitto.log
#其他配置
include_dir /etc/mosquitto/conf.d
#禁止匿名访问
allow_anonymous false
#认证配置
password_file /etc/mosquitto/pwfile
#权限配置
acl_file /etc/mosquitto/aclfile
2.2 用户认证配置文件 pwfile
没有该文件则创建:sudo touch /etc/mosquitto/pwfile
服务器开启后,输入如下命令,根据提示输入两遍密码:
$ mosquitto_passwd /etc/mosquitto/pwfile 用户名
(添加服务器完成认证的客户端用户名和密码,可添加多个)
2.3 权限配置文件 aclfile
创建并打开文件:
$ sudo touch/etc/mosquitto/aclfile
$ sudo vim /etc/mosquitto/aclfile
编辑内容,比如整了两个用户,分别是李雷lilei和韩梅梅hanmeimei:
#李雷只能发布以test为前缀的主题,订阅以$SYS开头的主题即系统主题
user lilei
topic write test/#
topic read $SYS/#
#韩梅梅只能订阅以test为前缀的主题
user hanmeimei
topic read test/#
3. 启动服务器并进行测试
启动服务器使用mosquitto命令;
发布使用mosquitto_pub命令,订阅使用mosquitto_sub命令。常用参数介绍:
参数 | 描述 |
---|---|
-h | 服务器主机,默认localhost |
-t | 指定主题 |
-u | 用户名 |
-P | 密码 |
-i | 客户端id,唯一 |
-m | 发布的消息内容 |
3.1 匿名用户,不限制用户的发布和订阅权限
如果不限制用户的发布和订阅权限,通过匿名用户发布的任何信息,其他匿名用户都可以订阅。可以不用配置,直接使用下面的命令进行测试:
1)打开一个终端1,直接启动服务器:mosquitto
2)再打开一个终端2,执行以下命令订阅主题"test"
$ mosquitto_sub -h localhost -t “test” –v
(-h localhost可以不用输,不输默认就是localhost)
3)打开另外一个终端3,发布消息到主题 “test”
$ mosquitto_pub -h localhost -t “test”-m "heat publish"
现在你会看到消息被显示在前一个终端2上了。
终端1:
终端2:
终端3:
注意:匿名用户的发布和订阅不安全,建议使用代理服务器认证的用户来发布和订阅消息。
3.2 认证用户,限制用户的发布和订阅权限
测试步骤如下:
- 打开一个终端1,启动代理服务器
-c:指定特定配置文件启动
-d:后台运行
$ sudo mosquitto -c /etc/mosquitto/mosquitto.conf -d
- 打开一个终端2,订阅:
$ mosquitto_sub -h localhost -t “test/#” -u hanmeimei -P 123456 -i "client1"
(
若订阅系统主题使用下面的命令:
#订阅客户端存活连接数
$ mosquitto_sub -h localhost –t ‘$SYS/broker/clients/active’ -u lilei -P 123456 -i "client2"
) - 重新打开一个终端3,发布消息:
$ mosquitto_pub -h localhost -t “test/abc” -u lilei -P 123456 -i “client3” -m "How are you?"
在终端2上,可以看到发布的消息。
终端1:
终端2:
终端3:
3.3 其他命令
查询mosquitto是否正确运行:
$ sudo service mosquitto status
或:
$ ps aux | grep mosquitto
4. 使用Windows下的mqtt客户端(mqtt.fx 1.7.1)进行测试
mqtt客户端官网下载地址:http://mqttfx.jensd.de/index.php/download
mqtt客户端软件界面:
配置:
填写已认证的用户名和密码,根据服务器配置文件的设置,这个用户jack的发布和订阅是受限制的。
注:往代理服务器的配置文件又添加了两个用户,分别是jack和heat
发布:
订阅:(由于代理服务器设置的限制,用户jack只能订阅以test为前缀的主题)
- 在虚拟机的Ubuntu中使用heat用户发布消息,在Windows的mqtt客户端查看订阅的消息
虚拟机:
MQTT客户端:
5. 参考资料
- 在ubuntu上安装,使用MQTT Mosquitto
https://blog.csdn.net/swedenfeng/article/details/53510048 - Mosquitto 搭建及配置
https://yq.aliyun.com/articles/664236/ - mqtt.fx的安装和使用
https://blog.csdn.net/nicholaszao/article/details/79211965 - 搭建自己的MQTT服务器
https://baijiahao.baidu.com/s?id=1610057452592802735&wfr=spider&for=pc - 【MQTT】在Windows下搭建MQTT服务器
https://blog.csdn.net/yannanxiu/article/details/52703946