Linux 安装 Mosquitto 及 SpringBoot 整合

Linux(centos)下 Mosquitto MQTT 代理的安装与配置

MQTT(Message Queuing Telemetry Transport)是一种轻量级的、基于发布/订阅模式的消息传输协议,广泛应用于物联网(IoT)领域。Mosquitto 是一个开源的 MQTT 代理,它支持 MQTT 协议 3.1 和 3.1.1,适用于各种设备和平台。

在工业上使用 MQTT 协议来进行物联网数据传输,主要看中了以下优点:

  • 低协议开销:它的每消息标题可以短至 2 个字节,容错性好
  • 物联网的网络环境往往比较恶劣,MQTT能从断开故障中恢复,并且不需要额外的编码
  • 低功耗:MQTT专门为了低功耗的目标而设计
  • 最多能接受百万级别的客户端

安装 Mosquitto

安装

选择需要安装的版本,我这里安装的是 2.0.0 版。

官网下载:https://mosquitto.org/files/source/

# 下载
wget --no-check-certificate https://mosquitto.org/files/source/mosquitto-2.0.0.tar.gz

# 安装
tar -zxvf mosquitto-2.0.0.tar.gz

cd mosquitto-2.0.0

make

make install

如果 make 时报如下错误

In file included from mosquitto_ctrl.c:29:
mosquitto_ctrl.h:21:10: fatal error: cJSON.h: No such file or directory
21 | #include <cJSON.h>
|          ^~~~~~~~~
compilation terminated.
make[2]: *** [Makefile:49: mosquitto_ctrl.o] Error 1
make[2]: Leaving directory '/usr/local/mosquitto-2.0.0/apps/mosquitto_ctrl'
make[1]: *** [Makefile:9: all] Error 2
make[1]: Leaving directory '/usr/local/mosquitto-2.0.0/apps'
make: *** [Makefile:64: mosquitto] Error 2

出现 cJSON 找不到,需要安装 cJSON(cJSON-1.7.18.tar.gz):

下载地址:https://github.com/DaveGamble/cJSON/releases

# 安装

tar -zxvf cJSON-1.7.18.tar.gz

cd cJSON-1.7.18

make

make install

cJSON 安装成功后再进行 make 安装即可成功。

相关配置

Mosquitto 的配置文件通常位于:/etc/mosquitto/mosquitto.conf 根据需要修改配置文件中的设置,可以更改监听地址和端口、设置持久化选项、配置用户认证等。

创建配置文件

cp /etc/mosquitto/mosquitto.conf.example /etc/mosquitto/mosquitto.conf

监听端口

一般使用mqtt默认监听端口 1883, 如果需要修改监听端口,需修改配置文件设置自己的端口即可(我这里使用默认的)。

配置用户认证

MQTT 默认无需用户认证,配置 MQTT 的用户名和密码通常涉及编辑 MQTT 代理的配置文件。

Mosquitto 使用 pwfile 来存储用户名和密码,可以使用 mosquitto_passwd 命令来创建和管理这个文件。

# 创建密码文件,用户名称为 mosquittoadmin

mosquitto_passwd -c /etc/mosquitto/pwfile.conf mosquittoadmin

系统会提示输入密码并再次确认,-c 选项表示创建一个新的密码文件。如果文件已经存在,则使用 -b 选项追加用户。

编辑 Mosquitto 配置文件 /etc/mosquitto/mosquitto.conf,添加或修改以下行以启用密码文件认证:

allow_anonymous false

password_file /etc/mosquitto/pwfile

创建 mosquitto 用户

配置文件中默认使用 user mosquitto,需要创建 mosquitto 用户:

groupadd mosquitto

useradd -g mosquitto mosquitto
 
chown -R mosquitto:mosquitto /etc/mosquitto/

设置外网访问

# 设置外网访问
listener 1883 0.0.0.0

启动、查看、关闭程序

运行程序

mosquitto -c /etc/mosquitto/mosquitto.conf -d

查看

ps -aux | grep mosquitto

关闭程序

kill -9 $(pidof mosquitto)

SpringBoot 整合 Mosquitto

添加依赖

<dependency>
    <groupId>org.eclipse.paho</groupId>
    <artifactId>org.eclipse.paho.client.mqttv3</artifactId>
    <version>1.2.5</version>
</dependency>

配置 MQTT 服务器的连接信息

server:
  port: 8001

mqtt:
  url: tcp://127.0.0.1:1883
  username: mosquittoadmin
  password: mosquittoadmin@123
  client-id: test-client

MQTT 配置类

package com.demo.config;

import org.eclipse.paho.client.mqttv3.MqttClient;
import org.eclipse.paho.client.mqttv3.MqttConnectOptions;
import org.eclipse.paho.client.mqttv3.MqttException;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值