Eclipse Mosquitto 安装及介绍

一、主要内容

本文主要讲解Eclipse Mosquitto的安装和简单的订阅与发布。

二、安装Eclipse Mosquitto

mosquitto:MQTT broker 服务

mosquitto-clients:命令行发布 / 订阅调试工具

复制代码
# 更新软件源
sudo apt update && sudo apt upgrade -y
# 安装服务端+客户端工具
sudo apt install mosquitto mosquitto-clients -y

查看是否运行:

启动的相关命令

复制代码
# 启动
sudo systemctl start mosquitto
# 开机自启
sudo systemctl enable mosquitto
# 查看状态
sudo systemctl status mosquitto
# 重启/停止
sudo systemctl restart mosquitto
sudo systemctl stop mosquitto

三、配置文件的修改

(1)明确配置文件

根据sudo systemctl status mosquitto可知主配置路径:/etc/mosquitto/mosquitto.conf ;但是我们创建 conf.d 下面 dev.conf 它读取到该配置文件。

复制代码
tian@hang:/etc/mosquitto$ cat conf.d/README 
Any files placed in this directory that have a .conf ending will be loaded as
config files by the broker. Use this to make your local config.
tian@hang:/etc/mosquitto$ ls
aclfile.example  ca_certificates  certs  conf.d  mosquitto.conf  pskfile.example  pwfile.example

(2)创建配置文件

创建配置文件dev.conf

复制代码
sudo vim /etc/mosquitto/conf.d/dev.conf

配置文件内容

复制代码
# 监听所有网卡,标准1883端口
listener 1883 0.0.0.0
# 允许匿名登录(开发阶段关闭账号验证,生产必须禁用)
allow_anonymous true
# 日志输出控制台+文件,方便调试
log_type all
log_dest file /var/log/mosquitto/mosquitto.log
# 最大连接数,开发可调大
max_connections 1000
# MQTT 5支持
mqtt_v5 true

重启服务

复制代码
sudo systemctl reload mosquitto

放行防火墙

复制代码
# ufw防火墙放行1883
sudo ufw allow 1883/tcp
# 查看端口监听验证
ss -tulpn | grep mosquitto

四、主题的订阅与发布

(1)简单的订阅与发布

只能接收到订阅后发布的消息,订阅前发布的消息接收不到

bash 复制代码
mosquitto_sub -t test   //订阅消息
mosquitto_pub -t test -m "hello mosquitto"   //发布消息
参数说明:
  -t 后面跟着是主题title
  -m 发布的消息,message

(2)带质量等级的订阅与发布

可以接收到订阅之前发布的消息

复制代码
mosquitto_sub -t test -q 1 -c -i 1314       //订阅消息

mosquitto_pub -t test -q 1 -m '{hello 你好}'   //发布消息

参数解释:
    -q :消息质量等级
    -c : 不关闭客户端
    -i : 设置客户端id号
相关推荐
小羊没烦恼!3 天前
微服务化的基石——持续集成
java·大数据·word·powerpoint·.net
俊昭喜喜里3 天前
java中的继承和多态的区别
java
小羊没烦恼!3 天前
初探性能优化——2个月到4小时的性能提升
java·开发语言·windows·算法·c#
譕痕3 天前
JSONObject与JSONArray封装数据格式区别
java·json
虎头金猫3 天前
4K 视频总卡在公网带宽?用 N1 + OpenList 把网盘播放链路重新理顺
运维·服务器·网络·python·容器·beautifulsoup·pandas
胡写代码3 天前
别再前后端各写一套表单校验了
java·后端
小鱼能吃糖3 天前
缺陷修复总览 · mall电商项目:5类缺陷,1个病根,4个业务域
java·电商
此时不提桶,更待何时3 天前
01-06-A-JVM排查实战详解
java·jvm
小白男神3 天前
MySQL进阶学习四(存储过程、游标、触发器)
mysql
这个DBA有点耶3 天前
MVCC深入:Read View、版本链与快照读——InnoDB并发控制的内核
数据库·mysql·架构