mosquitto是一款实现了消息推送协议 MQTT v3.1 的开源消息代理软件,提供轻量级的,支持可发布/可订阅的的消息推送模式,使设备对设备之间的短消息通信变得简单,比如现在应用广泛的低功耗传感器,手机、嵌入式计算机、微型控制器等移动设备。
(一) Ubuntu 安装 mosquitto
apt-add-repository ppa:mosquitto-dev/mosquitto-ppa
apt-get update
apt-get install mosquitto
apt-get install mosquitto-clients
(二) 修改/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
(三) mosquitto配置文件[详细参考]
安全配置 (/etc/mosquitto/conf.d/security.conf)
# =================================================================
# Security
# =================================================================
# 禁止匿名用户访问
allow_anonymous false
# 用户密码配置文件,路径为绝对路径
password_file /etc/mosquitto/pwdfile.example
# 访问规则列表
acl_file /etc/mosquitto/aclfile.example
acl_file (/etc/mosquitto/aclfile.example)
# 用户限制
user <username>
# 用户可以访问的topic和权限
topic [read|write] <topic>
(四) 新建用户
# 用户格式为<username>:<password>
# 第一次创建用户,再次创建会覆盖原文件
mosquitto_passwd -c /etc/mosquitto/pwdfile.example <userName>
# 新增用户
mosquitto_passwd -U /etc/mosquitto/pwdfile.example <userName>
(五) 启动服务
service mosquitto start
or
mosquitto -c /etc/mosquitto/mosquitto.conf -d
(六) 终端测试
订阅 mosquitto_sub -t <topic> -u <username> -P <password>
mosquitto_sub -t mqtt -u hao -P 123456
发布 mosquitto_pub -t <topic> -m <message> -u <username> -P <password>
mosquitto_pub -t mqtt -m message -u hao -P 123456
(七) paho.mqtt-spy图形化界面测试
启动paho.mqtt-spy
java -jar mqtt-spy-1.0.1-beta-b5-jar-with-dependencies.jar
(八) 创建paho.mqtt-spy桌面图标
vim ~/.local/share/applications/mqtt-spy.desktop
# 添加以下内容
[Desktop Entry]
Version=1.0
Name=mqtt-spy
Comment=mqtt-spy
Type=Application
Categories=Development;
Exec=java -jar 安装目录/paho.mqtt-spy/jar/mqtt-spy-1.0.1-beta-b5-jar-with-dependencies.jar
Terminal=false
Icon=安装目录/paho.mqtt-spy/mqtt-spy/src/main/resources/ui/images/mqtt-spy-logo.png