1.概述
Eclipse Mosquitto是一个开源消息代理,实现了MQTT协议版本3.1和3.1.1。Mosquitto轻量,适用于低功耗单板计算机到完整服务器的所有设备。Mosquitto项目还提供了用于实现MQTT客户端的C库以及非常受欢迎的mosquitto_pub和mosquitto_sub命令行MQTT客户端。
其他服务器代理实现:https://github.com/mqtt/mqtt.github.io/wiki/servers
各操作系统安装指引:https://mosquitto.org/download/
2.下载安装
以Ubuntu为例
1
| sudo apt-get install mosquitto
|
1
| sudo apt-get install mosquitto-clients
|
3.配置
3.1主配置文件mosquitto.conf
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
| id_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
|
3.2认证配置pwfile
1
| sudo touch /etc/mosquitto/pwfile
|
1
| mosquitto_passwd /etc/mosquitto/pwfile 用户名
|
3.3权限配置aclfile
1
| vim /etc/mosquitto/aclfile
|
1 2 3 4 5 6 7 8
| user lilei topic write test/ topic read $SYS/
user hanmeimei topic read test/
|
4.启动
-c:指定特定配置文件启动
-d:后台运行
1
| mosquitto -c /etc/mosquitto/mosquitto.conf -d
|
4.1测试
发布使用mosquitto_pub命令,订阅使用mosquitto_sub命令。常用参数介绍:
参数 |
描述 |
-h |
服务器主机,默认localhost |
-t |
指定主题 |
-u |
用户名 |
-P |
密码 |
-i |
客户端id,唯一 |
-m |
发布的消息内容 |
订阅
1
| mosquitto_sub -h localhost -t "test/#" -u hanmeimei -P 123456 -i "client1"
|
订阅系统主题
1 2
| mosquitto_sub -h localhost –t '$SYS/broker/clients/active' -u lilei -P 123456 -i "client2"
|
发布
1
| mosquitto_pub -h localhost -t "test/abc" -u lilei -P 123456 -i "client3" -m "How are you?"
|
5.参考链接