当前位置: 首页 > news >正文

在Spring Boot项目中集成和使用MQTT

在物联网(IoT)应用中,MQTT(消息队列遥测传输)协议因其轻量级和高效性被广泛使用。在Spring Boot项目中,我们可以通过集成org.springframework.integration:spring-integration-mqtt依赖来实现对MQTT的支持。本文将逐步介绍如何在Spring Boot应用中使用MQTT。

1. 添加依赖

首先,我们需要在项目的pom.xml文件中添加Spring Integration MQTT的依赖:

<dependencies><!-- Spring Boot Starter --><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter</artifactId></dependency><!-- Spring Integration MQTT --><dependency><groupId>org.springframework.integration</groupId><artifactId>spring-integration-mqtt</artifactId></dependency><!-- MQTT Client Library (Paho) --><dependency><groupId>org.eclipse.paho</groupId><artifactId>org.eclipse.paho.client.mqttv3</artifactId></dependency>
</dependencies>

2. 配置MQTT

在Spring Boot应用的配置文件application.properties中添加MQTT相关配置:

mqtt.broker.url=tcp://localhost:1883
mqtt.client.id=spring-boot-mqtt-client
mqtt.username=your-username
mqtt.password=your-password
mqtt.default.topic=your/topic

3. 创建MQTT配置类

创建一个新的配置类,用于配置MQTT连接和消息处理:

import org.eclipse.paho.client.mqttv3.MqttConnectOptions;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.integration.annotation.ServiceActivator;
import org.springframework.integration.channel.DirectChannel;
import org.springframework.integration.core.MessageProducer;
import org.springframework.integration.mqtt.core.DefaultMqttPahoClientFactory;
import org.springframework.integration.mqtt.core.MqttPahoClientFactory;
import org.springframework.integration.mqtt.inbound.MqttPahoMessageDrivenChannelAdapter;
import org.springframework.integration.mqtt.outbound.MqttPahoMessageHandler;
import org.springframework.integration.mqtt.support.DefaultPahoMessageConverter;
import org.springframework.messaging.MessageChannel;
import org.springframework.messaging.MessageHandler;@Configuration
public class MqttConfig {@Beanpublic MqttPahoClientFactory mqttClientFactory() {DefaultMqttPahoClientFactory factory = new DefaultMqttPahoClientFactory();MqttConnectOptions options = new MqttConnectOptions();options.setServerURIs(new String[] { "tcp://localhost:1883" });options.setUserName("your-username");options.setPassword("your-password".toCharArray());factory.setConnectionOptions(options);return factory;}@Beanpublic MessageChannel mqttInputChannel() {return new DirectChannel();}@Beanpublic MessageProducer inbound() {MqttPahoMessageDrivenChannelAdapter adapter =new MqttPahoMessageDrivenChannelAdapter("spring-boot-mqtt-client-inbound",mqttClientFactory(), "your/topic");adapter.setCompletionTimeout(5000);adapter.setConverter(new DefaultPahoMessageConverter());adapter.setQos(1);adapter.setOutputChannel(mqttInputChannel());return adapter;}@Bean@ServiceActivator(inputChannel = "mqttInputChannel")public MessageHandler handler() {return message -> {String payload = (String) message.getPayload();System.out.println("Received message: " + payload);};}@Beanpublic MessageChannel mqttOutboundChannel() {return new DirectChannel();}@Bean@ServiceActivator(inputChannel = "mqttOutboundChannel")public MessageHandler mqttOutbound() {MqttPahoMessageHandler messageHandler =new MqttPahoMessageHandler("spring-boot-mqtt-client-outbound", mqttClientFactory());messageHandler.setAsync(true);messageHandler.setDefaultTopic("your/topic");return messageHandler;}
}

4. 发送和接收消息

在你的服务或控制器中,可以使用如下方法发送消息:

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.integration.support.MessageBuilder;
import org.springframework.messaging.MessageChannel;
import org.springframework.stereotype.Service;@Service
public class MqttMessageSender {@Autowiredprivate MessageChannel mqttOutboundChannel;public void sendMessage(String topic, String payload) {mqttOutboundChannel.send(MessageBuilder.withPayload(payload).setHeader("mqtt_topic", topic).build());}
}

要接收消息,可以配置handler方法中的处理逻辑,或将消息发送到另一个Spring Integration通道进行进一步处理。

5. 使用示例

在一个控制器中调用发送消息方法:

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;@RestController
public class MqttController {@Autowiredprivate MqttMessageSender mqttMessageSender;@GetMapping("/send")public String send(@RequestParam String topic, @RequestParam String message) {mqttMessageSender.sendMessage(topic, message);return "Message sent to topic " + topic;}
}

这样,你就可以通过HTTP请求发送MQTT消息了。例如,访问http://localhost:8080/send?topic=test/topic&message=Hello,将消息发送到MQTT主题test/topic

这就是一个完整的Spring Boot应用中集成MQTT的简单示例,希望对你有所帮助!


MQTT报文头介绍

MQTT协议的请求报文头非常轻量级。MQTT协议定义了固定报文头和可变报文头两部分。以下是各类报文的基本格式:

固定报文头

所有MQTT报文都有一个固定报文头,占据2-5个字节。固定报文头包含报文类型和一些控制标志。

固定报文头格式

  • 第一个字节:

    • 位7-4:报文类型(Message Type)
    • 位3-0:标志(Flags),根据报文类型不同而不同
  • 第二个字节及后续字节:

    • 剩余长度(Remaining Length),表示剩余报文的字节数。采用可变长度编码,每个字节的最高位用于指示是否有后续字节。
各类报文示例

连接报文(CONNECT)

连接报文用于客户端请求与服务器建立连接。其报文头如下:

  • 固定报文头:

    • 第一个字节:0x10(CONNECT报文类型是1,标志位为0000)
    • 第二个字节:剩余长度(根据可变部分长度而定)
  • 可变报文头:

    • 协议名(“MQTT”)
    • 协议级别(4,表示MQTT 3.1.1)
    • 连接标志(Connect Flags)
    • 保持连接时间(Keep Alive)
  • 有效载荷:

    • 客户端标识符(Client Identifier)
    • 用户名(可选)
    • 密码(可选)
    • 遗嘱主题(可选)
    • 遗嘱消息(可选)

连接确认报文(CONNACK)

服务器响应客户端的连接请求。其报文头如下:

  • 固定报文头:

    • 第一个字节:0x20(CONNACK报文类型是2,标志位为0000)
    • 第二个字节:剩余长度(2字节)
  • 可变报文头:

    • 连接确认标志(0x00或0x01)
    • 返回码(0表示连接成功,其他值表示错误)

发布报文(PUBLISH)

客户端或服务器发送消息到指定主题。其报文头如下:

  • 固定报文头:

    • 第一个字节:0x30(PUBLISH报文类型是3,标志位根据QoS等级、重复标志和保留标志变化)
    • 第二个字节:剩余长度(根据主题名、消息ID和消息体长度而定)
  • 可变报文头:

    • 主题名(Topic Name)
    • 消息ID(QoS等级为1或2时需要)
  • 有效载荷:

    • 消息内容

示例

以下是一个PUBLISH报文的示例:

30 0B                # 固定报文头 (PUBLISH,QoS 0)
00 05                # 主题名长度
74 6F 70 69 63       # 主题名 "topic"
68 65 6C 6C 6F       # 消息内容 "hello"

在这个示例中:

  • 第一个字节 0x30 表示这是一个PUBLISH报文,QoS等级为0,重复标志和保留标志为0。
  • 第二个字节 0x0B 表示剩余长度为11个字节。
  • 接下来的两个两个字节 0x00 0x05 表示主题名的长度为5个字节。
  • 接下来的5个字节 0x74 0x6F 0x70 0x69 0x63 表示主题名 “topic”。
  • 最后5个字节 0x68 0x65 0x6C 0x6C 0x6F 表示消息内容 “hello”。

这种结构使得MQTT报文非常紧凑和高效,特别适合物联网设备的通信。希望这篇文章能帮助你更好地理解和使用MQTT协议。

http://www.lryc.cn/news/354212.html

相关文章:

  • 14、设计模式之访问者模式
  • Excel如何换行不换格
  • Elasticsearch 8.1官网文档梳理 - 十五、Aggregations(聚合)
  • 计算机系统概论
  • 【Vue】diff 算法
  • Spring Boot 3.x 与 Spring Boot 2.x 的对比
  • SSLError ClosedPoolError
  • 勒索软件分析_Conti
  • Linux系统如何通过编译方式安装python3.11.3
  • 仿《Q极速体育》NBACBA体育直播吧足球直播综合体育直播源码
  • 代码随想录算法训练营第四天| 24. 两两交换链表中的节点、19.删除链表的倒数第N个节点 、 面试题 02.07. 链表相交、142.环形链表II
  • 吉林大学计科21级《软件工程》期末考试真题
  • AWS云服务器每月费用高昂,如何优化达到节省目的?
  • 关于XtremIO 全闪存储维护的一些坑(建议)
  • 《最新出炉》系列入门篇-Python+Playwright自动化测试-41-录制视频
  • 一个程序员的牢狱生涯(38)答案
  • MySQL命令
  • 装本地知识库
  • Django模板层——模板引擎配置
  • Leetcode刷题笔记2:数组基础2
  • 整理好了!2024年最常见 20 道 Redis面试题(八)
  • 【STM32项目】基于stm32智能鱼缸控制系统的设计与实现(完整工程资料源码)
  • 深入理解 Mysql 分层架构:从存储引擎到查询优化器的内部机制解析
  • Java筑基(三)
  • Zoho Campaigns邮件营销怎么发邮件?
  • Qt 界面上字体自适应控件大小 - 随控件缩放
  • 【Python】 使用SMOTE解决数据不平衡问题
  • Redis第18讲——Redis和Redission实现延迟消息
  • 返回枚举类给前端
  • A. Maximize?