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

MQTT 在Spring Boot 中的使用

在 Spring Boot 中使用 MQTT 通常会借助 Spring Integration 项目提供的 MQTT 支持。这使得 MQTT 的集成可以很好地融入 Spring 的消息驱动和企业集成模式。

以下是如何在 Spring Boot 中集成和使用 MQTT 的详细步骤:

前提条件:

  1. MQTT Broker:需要一个正在运行的 MQTT Broker,例如 Mosquitto, EMQX, HiveMQ, RabbitMQ (with MQTT plugin), Apollo 等。确保 Broker 的地址和端口(默认通常是 tcp://localhost:1883)。
  2. Spring Boot 项目:一个基本的 Spring Boot 项目。

步骤 1:添加依赖

在你的 pom.xml (Maven) 或 build.gradle (Gradle) 文件中添加必要的依赖:

Maven (pom.xml):

<dependencies><!-- Spring Boot Starter for core functionality --><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter</artifactId></dependency><!-- Spring Boot Starter for Web (可选, 如果想通过 REST API 触发 MQTT 发布) --><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-web</artifactId></dependency><!-- Spring Integration Core --><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-integration</artifactId></dependency><!-- Spring Integration MQTT Support --><dependency><groupId>org.springframework.integration</groupId><artifactId>spring-integration-mqtt</artifactId><!-- Version managed by Spring Boot's BOM, or specify one --></dependency><!-- Eclipse Paho MQTT Client (Spring Integration MQTT uses this) --><dependency><groupId>org.eclipse.paho</groupId><artifactId>org.eclipse.paho.client.mqttv3</artifactId><version>1.2.5</version> <!-- Or a newer compatible version --></dependency>
</dependencies>

步骤 2:配置 MQTT 连接属性

src/main/resources/application.properties (或 application.yml) 中配置 MQTT Broker 的连接信息:

# MQTT Broker Configuration
mqtt.broker.url=tcp://localhost:1883
mqtt.client.id.publisher=springBootPublisher-unique # 客户端ID,对于每个连接必须唯一
mqtt.client.id.subscriber=springBootSubscriber-unique # 客户端ID,对于每个连接必须唯一
mqtt.default.topic=test/topic
mqtt.qos=1 # 默认的服务质量等级 (0, 1, or 2)# 可选:如果 Broker 需要认证
# mqtt.username=your_username
# mqtt.password=your_password

注意:Client ID 在 MQTT 中必须是唯一的。如果应用同时发布和订阅,可能需要为发布者和订阅者使用不同的 Client ID,或者使用一个 Client ID 但要确保 Paho 客户端实例的正确性。

步骤 3:创建 MQTT 配置类

创建一个 Java 配置类来定义 MQTT 相关的 Bean,如 ClientFactory、出站适配器(用于发布消息)和入站适配器(用于订阅消息)。

package com.example.mqttdemo.config;import org.eclipse.paho.client.mqttv3.MqttConnectOptions;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.integration.annotation.IntegrationComponentScan;
import org.springframework.integration.annotation.MessagingGateway;
import org.springframework.integration.annotation.ServiceActivator;
import org.springframework.integration.channel.DirectChannel;
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.integration.mqtt.support.MqttHeaders;
import org.springframework.messaging.MessageChannel;
import org.springframework.messaging.MessageHandler;
import org.springframework.messaging.handler.annotation.Header;@Configuration
@IntegrationComponentScan // 扫描 @MessagingGateway 等注解
public class MqttConfig {private static final Logger LOGGER = LoggerFactory.getLogger(MqttConfig.class);@Value("${mqtt.broker.url}")private String brokerUrl;@Value("${mqtt.client.id.publisher}")private String publisherClientId;@Value("${mqtt.client.id.subscriber}")private String subscriberClientId;@Value("${mqtt.default.topic}")private String defaultTopic;@Value("${mqtt.qos}")private int defaultQos;// 可选: 如果需要用户名密码认证// @Value("${mqtt.username}")// private String username;// @Value("${mqtt.password}")// private String password;// --- 通用 MQTT Client Factory ---@Beanpublic MqttPahoClientFactory mqttClientFactory() {DefaultMqttPahoClientFactory factory = new DefaultMqttPahoClientFactory();MqttConnectOptions options = new MqttConnectOptions();options.setServerURIs(new String[]{brokerUrl});// if (username != null && !username.isEmpty()) {//     options.setUserName(username);// }// if (password != null && !password.isEmpty()) {//     options.setPassword(password.toCharArray());// }options.setCleanSession(true); // 设置为 false 以启用持久会话和离线消息options.setAutomaticReconnect(true); // 启用自动重连options.setConnectionTimeout(10); // 连接超时时间 (秒)options.setKeepAliveInterval(20); // 心跳间隔 (秒)factory.setConnectionOptions(options);return factory;}// --- MQTT 消息发布 (Outbound) ---@Beanpublic MessageChannel mqttOutboundChannel() {return new DirectChannel(); // 或者 PublishSubscribeChannel}@Bean@ServiceActivator(inputChannel = "mqttOutboundChannel")public MessageHandler mqttOutbound(MqttPahoClientFactory clientFactory) {MqttPahoMessageHandler messageHandler =new MqttPahoMessageHandler(publisherClientId, clientFactory); // 使用独立的 Client IDmessageHandler.setAsync(true); // 推荐异步发送messageHandler.setDefaultTopic(defaultTopic); // 默认主题,可以被消息头覆盖messageHandler.setDefaultQos(defaultQos); // 默认QoS,可以被消息头覆盖// messageHandler.setDefaultRetained(false); // 默认是否保留消息return messageHandler;}// 定义一个网关接口,用于发送消息到 mqttOutboundChannel// Spring Integration 会自动实现这个接口@MessagingGateway(defaultRequestChannel = "mqttOutboundChannel")public interface MqttGateway {void sendToMqtt(String payload);void sendToMqtt(@Header(MqttHeaders.TOPIC) String topic, String payload);void sendToMqtt(@Header(MqttHeaders.TOPIC) String topic, @Header(MqttHeaders.QOS) int qos, String payload);// 可以定义更多重载方法,例如发送 byte[]// void sendToMqtt(@Header(MqttHeaders.TOPIC) String topic, byte[] payload);}// --- MQTT 消息订阅 (Inbound) ---@Beanpublic MessageChannel mqttInputChannel() {return new DirectChannel();}@Beanpublic MqttPahoMessageDrivenChannelAdapter inboundAdapter(MqttPahoClientFactory clientFactory) {// 可以订阅单个主题,或多个主题 (字符串数组)// String[] topicsToSubscribe = {defaultTopic, "another/topic", "sensor/+/data"};MqttPahoMessageDrivenChannelAdapter adapter =new MqttPahoMessageDrivenChannelAdapter(subscriberClientId, clientFactory, defaultTopic); // 使用独立的 Client IDadapter.setCompletionTimeout(5000); // 等待消息发送完成的超时时间adapter.setConverter(new DefaultPahoMessageConverter()); // 消息转换器adapter.setQos(defaultQos); // 订阅时的QoSadapter.setOutputChannel(mqttInputChannel()); // 将接收到的消息发送到此 Channelreturn adapter;}// 消息处理器,处理从 mqttInputChannel 接收到的消息@ServiceActivator(inputChannel = "mqttInputChannel")public void handleIncomingMqttMessage(org.springframework.messaging.Message<String> message) {String topic = message.getHeaders().get(MqttHeaders.RECEIVED_TOPIC, String.class);String payload = message.getPayload();Integer qos = message.getHeaders().get(MqttHeaders.RECEIVED_QOS, Integer.class);Boolean retained = message.getHeaders().get(MqttHeaders.RETAINED, Boolean.class);LOGGER.info("Received MQTT Message - Topic: [{}], QoS: [{}], Retained: [{}], Payload: [{}]",topic, qos, retained, payload);// 在这里处理你的业务逻辑}// 可选: 监听 MQTT 事件 (连接成功,连接丢失等)/*@EventListenerpublic void handleMqttEvents(MqttIntegrationEvent event) {LOGGER.info("MQTT Event: {}", event);if (event instanceof MqttConnectionFailedEvent) {MqttConnectionFailedEvent failedEvent = (MqttConnectionFailedEvent) event;LOGGER.error("MQTT Connection Failed!", failedEvent.getCause());} else if (event instanceof MqttSubscribedEvent) {MqttSubscribedEvent subscribedEvent = (MqttSubscribedEvent) event;LOGGER.info("MQTT Subscribed to: {}", subscribedEvent.getMessage());}//还有 MqttMessageSentEvent, MqttMessageDeliveredEvent 等}*/
}

解释:

  1. MqttPahoClientFactory:

    • 创建和配置底层的 Paho MQTT 客户端。
    • MqttConnectOptions 用于设置 Broker URL、用户名/密码、Clean Session、Keep Alive、自动重连等。
    • setCleanSession(true): 每次连接都是一个全新的会话,断开后 Broker 不会保留订阅信息和离线消息。
    • setCleanSession(false): 持久会话,客户端断开重连后,Broker 会尝试发送离线期间的 QoS 1 和 QoS 2 消息,并恢复订阅。需要 Client ID 保持不变。
  2. 发布消息 (Outbound):

    • mqttOutboundChannel: 一个 MessageChannel,作为消息发布的入口。
    • MqttPahoMessageHandler (mqttOutbound bean): 这是一个 MessageHandler,它监听 mqttOutboundChannel,并将接收到的消息通过 MQTT 发送出去。
      • 需要一个 clientIdMqttPahoClientFactory
      • setAsync(true): 异步发送消息,不会阻塞当前线程。
      • setDefaultTopic()setDefaultQos(): 如果消息本身没有指定主题或QoS,则使用这些默认值。
    • MqttGateway: 一个接口,使用 @MessagingGateway 注解。Spring Integration 会自动为其生成实现。通过调用这个接口的方法,可以方便的将消息发送到 defaultRequestChannel (即 mqttOutboundChannel)。
      • @Header(MqttHeaders.TOPIC)@Header(MqttHeaders.QOS) 允许在发送时动态指定主题和QoS。
  3. 订阅消息 (Inbound):

    • mqttInputChannel: 一个 MessageChannel,入站适配器会将从 MQTT Broker 收到的消息发送到这个 Channel。
    • MqttPahoMessageDrivenChannelAdapter (inboundAdapter bean): 这是一个消息驱动的通道适配器,它连接到 MQTT Broker,订阅指定的主题,并将收到的消息推送到 outputChannel (即 mqttInputChannel)。
      • 需要一个 clientIdMqttPahoClientFactory 和要订阅的主题(可以是一个或多个,支持通配符)。
      • setConverter(): 用于将 MQTT 的 byte[] 负载转换为期望的类型(例如 String)。DefaultPahoMessageConverter 可以处理 Stringbyte[]
    • handleIncomingMqttMessage 方法: 使用 @ServiceActivator(inputChannel = "mqttInputChannel") 注解,监听 mqttInputChannel。当有消息到达时,此方法会被调用。
      • message.getHeaders().get(MqttHeaders.RECEIVED_TOPIC): 获取消息来源的主题。
      • message.getPayload(): 获取消息内容。
  4. @IntegrationComponentScan: 确保 Spring Integration 扫描并处理 @MessagingGateway 等注解。

步骤 4:使用 MQTT Gateway 发布消息

你可以注入 MqttGateway 到你的 Service 或 Controller 中来发送消息。

示例 Service:

package com.example.mqttdemo.service;import com.example.mqttdemo.config.MqttConfig;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;@Service
public class MqttPublishService {private static final Logger LOGGER = LoggerFactory.getLogger(MqttPublishService.class);@Autowiredprivate MqttConfig.MqttGateway mqttGateway;public void publishMessage(String topic, String payload) {try {LOGGER.info("Publishing MQTT message - Topic: [{}], Payload: [{}]", topic, payload);mqttGateway.sendToMqtt(topic, payload);} catch (Exception e) {LOGGER.error("Error publishing MQTT message to topic {}: {}", topic, e.getMessage(), e);}}public void publishMessageWithQos(String topic, String payload, int qos) {try {LOGGER.info("Publishing MQTT message - Topic: [{}], QoS: [{}], Payload: [{}]", topic, qos, payload);mqttGateway.sendToMqtt(topic, qos, payload);} catch (Exception e) {LOGGER.error("Error publishing MQTT message to topic {} with QoS {}: {}", topic, qos, e.getMessage(), e);}}public void publishToDefaultTopic(String payload) {try {LOGGER.info("Publishing MQTT message to default topic, Payload: [{}]", payload);mqttGateway.sendToMqtt(payload); // 将使用 MqttPahoMessageHandler 中配置的默认主题和QoS} catch (Exception e) {LOGGER.error("Error publishing MQTT message to default topic: {}", e.getMessage(), e);}}
}

示例 REST Controller (可选):

package com.example.mqttdemo.controller;import com.example.mqttdemo.service.MqttPublishService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;@RestController
@RequestMapping("/api/mqtt")
public class MqttController {@Autowiredprivate MqttPublishService mqttPublishService;@PostMapping("/publish")public String publishMessage(@RequestParam String topic, @RequestBody String payload) {mqttPublishService.publishMessage(topic, payload);return "Message published to topic: " + topic;}@PostMapping("/publish-default")public String publishToDefault(@RequestBody String payload) {mqttPublishService.publishToDefaultTopic(payload);return "Message published to default topic.";}@PostMapping("/publish-qos")public String publishMessageWithQos(@RequestParam String topic,@RequestParam int qos,@RequestBody String payload) {mqttPublishService.publishMessageWithQos(topic, payload, qos);return "Message published to topic: " + topic + " with QoS: " + qos;}
}

步骤 5:运行和测试

  1. 启动 MQTT Broker (例如,使用 Docker 运行 Mosquitto):
    docker run -it -p 1883:1883 -p 9001:9001 eclipse-mosquitto
    
  2. 运行 Spring Boot 应用
  3. 测试发布
    • 如果创建了 REST Controller,可以通过 Postman 或 curl 发送 POST 请求:
      POST http://localhost:8080/api/mqtt/publish?topic=my/custom/topic
      Body (raw, text/plain): Hello from Spring Boot MQTT!
    • 或者在应用启动时通过 CommandLineRunner 调用 MqttPublishService
  4. 测试订阅
    • 当有消息发布到应用订阅的主题 (例如 test/topic 或在 inboundAdapter 中配置的其他主题) 时,handleIncomingMqttMessage 方法会被调用,在控制台能看到日志输出。
    • 也可以使用 MQTT 客户端工具 (如 MQTTX, MQTT Explorer) 连接到同一个 Broker 并发布消息到被订阅的主题。

高级主题和注意事项:

  • QoS (服务质量等级)
    • QoS 0 (At most once): 最多一次,消息可能丢失。
    • QoS 1 (At least once): 至少一次,消息可能重复。
    • QoS 2 (Exactly once): 精确一次,最可靠但开销最大。
    • 可以在 MqttPahoMessageHandlerMqttPahoMessageDrivenChannelAdapter 中设置默认 QoS,也可以在发送消息时通过 MqttHeaders.QOS 动态指定。
  • Retained Messages (保留消息)
    • 发布者可以将消息标记为 “retained”。Broker 会存储该主题下最新的保留消息。当新客户端订阅该主题时,会立即收到这条保留消息。
    • MqttPahoMessageHandler 中设置 setDefaultRetained(true) 或通过消息头 MqttHeaders.RETAINED
  • Last Will and Testament (LWT - 遗嘱消息)
    • MqttConnectOptions 中配置。如果客户端异常断开,Broker 会发布这个预设的遗嘱消息到指定主题。
    • options.setWill("client/status", "offline".getBytes(), 1, false);
  • SSL/TLS 加密
    • 如果 Broker 使用 SSL/TLS,需要在 MqttConnectOptions 中配置 SSL 属性,并将 Broker URL 改为 ssl://your-broker-address:8883
    • options.setSocketFactory(SslUtil.getSocketFactory("ca.crt", "client.crt", "client.key", "password")); (需要相应的证书文件和密码)
  • 错误处理
    • Spring Integration 提供了错误通道 (errorChannel) 来处理消息传递过程中的异常。
    • 可以监听 MqttIntegrationEvent (如 MqttConnectionFailedEvent) 来获取连接状态事件。
  • Client ID 唯一性:再次强调,连接到同一个 Broker 的每个 MQTT 客户端都必须有唯一的 Client ID。如果发布和订阅逻辑在同一个应用实例中,并且为它们配置了相同的 Client ID 但使用了不同的 MqttPahoClientFactory 实例或适配器,Paho 库内部可能会产生冲突或意外行为。建议为出站和入站适配器使用不同的 Client ID,或者共享一个 MqttPahoClientFactory 实例(确保它能正确处理这种情况)。
  • Converter (转换器): DefaultPahoMessageConverter 默认将 String 转换为 byte[] 发送,接收时根据目标类型尝试转换。如果需要处理 JSON 或其他复杂类型,需要自定义 MessageConverter 或在消息处理器中进行序列化/反序列化。

相关文章:

MQTT 在Spring Boot 中的使用

在 Spring Boot 中使用 MQTT 通常会借助 Spring Integration 项目提供的 MQTT 支持。这使得 MQTT 的集成可以很好地融入 Spring 的消息驱动和企业集成模式。 以下是如何在 Spring Boot 中集成和使用 MQTT 的详细步骤&#xff1a; 前提条件&#xff1a; MQTT Broker&#xff…...

Vue3学习(组合式API——计算属性computed详解)

目录 一、计算属性computed。 Vue官方提供的案例。(普通写法与计算属性写法) 使用计算属性computed重构——>简化描述响应式状态的复杂逻辑。 &#xff08;1&#xff09;计算属性computed小案例。 <1>需求说明。&#xff08;筛选原数组——>得新数组&#xff09; &…...

Spring 中的 @ComponentScan注解详解

在 Spring 框架中,@ComponentScan 是一个非常重要的注解,它用于自动扫描和注册 Bean。通过该注解,Spring 能够自动发现并管理标注了特定注解的类(如 @Component, @Service, @Repository 等),从而实现依赖注入和容器管理。 本文将详细介绍 @ComponentScan 的作用、常见搭…...

MySQL 数据库故障排查指南

MySQL 数据库故障排查指南 本指南旨在帮助您识别和解决常见的 MySQL 数据库故障。我们将从问题识别开始&#xff0c;逐步深入到具体的故障类型和排查步骤。 1. 问题识别与信息收集 在开始排查之前&#xff0c;首先需要清晰地了解问题的现象和范围。 故障现象&#xff1a; 数…...

Android Studio 模拟器配置方案

Android Studio 模拟器配置方案 1.引言2.使用Android Studio中的模拟器3.使用国产模拟器1.引言 前面介绍【React Native基础环境配置】的时候需要配置模拟器,当时直接使用了USB调试方案,但是有些时候可能不太方便连接手机调试,比如没有iPhone调不了ios。接下来说明另外两种可…...

k8s中ingress-nginx介绍

1. 介绍 Ingress是一种Kubernetes资源&#xff0c;用于将外部流量路由到Kubernetes集群内的服务。与NodePort相比&#xff0c;它提供了更高级别的路由功能和负载平衡&#xff0c;可以根据HTTP请求的路径、主机名、HTTP方法等来路由流量。可以说Ingress是为了弥补NodePort在流量…...

键盘输出希腊字符方法

在不同操作系统中&#xff0c;输出希腊字母的方法有所不同。以下是针对 Windows 和 macOS 系统的详细方法&#xff0c;以及一些通用技巧&#xff1a; 1.Windows 系统 1.1 使用字符映射表 字符映射表是一个内置工具&#xff0c;可以方便地找到并插入希腊字母。 • 步骤&#xf…...

字节DeerFlow开源框架:多智能体深度研究框架,实现端到端自动化研究流程

&#x1f98c; DeerFlow DeerFlow&#xff08;Deep Exploration and Efficient Research Flow&#xff09;是一个社区驱动的深度研究框架&#xff0c;它建立在开源社区的杰出工作基础之上。目标是将语言模型与专业工具&#xff08;如网络搜索、爬虫和Python代码执行&#xff0…...

MySQL 存储函数[特殊字符] VS 存储过程[特殊字符]

1、存储函数&#x1f638; 一、存储函数概述 存储函数是MySQL中一种特殊的存储程序&#xff0c;具有以下特点&#xff1a; 返回单个值&#xff1a;必须通过RETURN语句返回明确的结果SQL表达式使用&#xff1a;可以直接在SQL语句中调用输入参数&#xff1a;只接受输入参数(隐…...

reactor实现TCP遇到的问题和探究

struct conn{ int fd; char rbuffer[1024]; char wbuffer[1024]; int wlength; int rlength; int (*recv_cb)(int); int (*send_cb)(int); }; int (*recv_cb)(int); recv_cb&#xff1a;函数指针的名称*recv_cb&#xff1a;星号 * 表示 recv_cb 是一个指针。(*recv_cb)&#…...

ElasticSearch重启之后shard未分配问题的解决

以下是Elasticsearch重启后分片未分配问题的完整解决方案&#xff0c;结合典型故障场景与最新实践&#xff1a; 一、快速诊断定位 ‌检查集群状态 GET /_cluster/health?pretty # status为red/yellow时需关注unassigned_shards字段值 ‌ 2.查看未分配分片详情 …...

算法第十八天|530. 二叉搜索树的最小绝对差、501.二叉搜索树中的众数、236. 二叉树的最近公共祖先

530. 二叉搜索树的最小绝对差 题目 思路与解法 第一想法&#xff1a; 一个二叉搜索树的最小绝对差&#xff0c;从根结点看&#xff0c;它的结点与它的最小差值一定出现在 左子树的最右结点&#xff08;左子树最大值&#xff09;和右子树的最左结点&#xff08;右子树的最小值…...

QMK键盘编码器(Encoder)(理论部分)

QMK键盘编码器(Encoder)(理论部分) 前言 作为一名深耕机械键盘DIY多年的老司机,我发现很多键盘爱好者对QMK编码器的配置总是一知半解。今天我就把多年积累的经验毫无保留地分享给大家,从硬件接线到软件配置,从基础应用到高阶玩法,一文全搞定!保证看完就能让你的编码…...

微服务调试问题总结

本地环境调试。 启动本地微服务&#xff0c;使用公共nacos配置。利用如apifox进行本地代码调试解决调试问题。除必要的业务微服务依赖包需要下载到本地。使用mvn clean install -DskipTests进行安装启动前选择好profile环境进行启动&#xff0c;启动前记得mvn clean清理项目。…...

C++(2)

二、面向对象基础 1. 类与对象 1.1 核心概念 类&#xff08;Class&#xff09; ​​定义​​&#xff1a;抽象描述具有共同特征和行为的对象模板​​本质​​&#xff1a;代码复用的蓝图&#xff0c;定义数据&#xff08;属性&#xff09;与操作&#xff08;行为&#xff0…...

美SEC主席:探索比特币上市证券交易所

作者/演讲者&#xff1a;美SEC主席Paul S. Atkins 编译&#xff1a;Liam 5月12日&#xff0c;由美国SEC加密货币特别工作组发起的主题为《资产上链&#xff1a;TradFi与DeFi的交汇点》系列圆桌会议如期举行。 会议期间&#xff0c;现任美SEC主席Paul S. Atkins发表了主旨演讲。…...

@Controller 与 @RestController-笔记

1.Controller与RestController对比 Spring MVC 中 Controller 与 RestController 的功能对比&#xff1a; Controller是Spring MVC中用于标识一个类作为控制器的标准注解。它允许处理HTTP请求&#xff0c;并返回视图名称&#xff0c;通常和视图解析器一起使用来渲染页面。而R…...

JavaScript篇:揭秘函数式与命令式编程的思维碰撞

大家好&#xff0c;我是江城开朗的豌豆&#xff0c;一名拥有6年以上前端开发经验的工程师。我精通HTML、CSS、JavaScript等基础前端技术&#xff0c;并深入掌握Vue、React、Uniapp、Flutter等主流框架&#xff0c;能够高效解决各类前端开发问题。在我的技术栈中&#xff0c;除了…...

c++和c的不同

c:面向对象&#xff08;封装&#xff0c;继承&#xff0c;多态&#xff09;&#xff0c;STL&#xff0c;模板 一、基础定义与背景 C语言 诞生年代&#xff1a;20世纪70年代&#xff0c;Dennis Ritchie在贝尔实验室开发。主要特点&#xff1a; 过程式、结构化编程面向系统底层…...

MySQL Join连接算法深入解析

引言 在关系型数据库中&#xff0c;Join操作是实现多表数据关联查询的关键手段&#xff0c;直接影响查询性能和资源消耗。MySQL支持多种Join算法&#xff0c;包括经典的索引嵌套循环连接&#xff08;Index Nested-Loop Join&#xff09;、块嵌套循环连接&#xff08;Block Nes…...

从构想到交付:专业级软开发流程详解

目录 ​​一、软件开发生命周期&#xff08;SDLC&#xff09;标准化流程​​ 1. 需求工程阶段&#xff08;Requirement Engineering&#xff09; 2. 系统设计阶段&#xff08;System Design&#xff09; 3. 开发阶段&#xff08;Implementation&#xff09; 4. 测试阶段&a…...

腾讯云-人脸核身+人脸识别教程

一。产品概述 慧眼人脸核身特惠活动 腾讯云慧眼人脸核身是一组对用户身份信息真实性进行验证审核的服务套件&#xff0c;提供人脸核身、身份信息核验、银行卡要素核验和运营商类要素核验等各类实名信息认证能力&#xff0c;以解决行业内大量对用户身份信息真实性核实的需求&a…...

http请求卡顿

接口有时出现卡顿&#xff0c;而且抓包显示有时tcp目标机器没有响应&#xff0c; 但nginx和java应用又没有错误日志&#xff0c;让人抓耳挠腮&#xff0c;最终还是请运维大哥帮忙&#xff0c;一顿操作后系统暂时无卡顿了&#xff0c;佩服的同时感觉疑惑到底调整了啥东…...

使用Vite打包前端Vue项目,碰到依赖包体积大,出现内存溢出的解决办法

vite.config.ts 中 方式一 使用 esbuild build: {outDir: "dist",minify: "esbuild",sourcemap: false,chunkSizeWarningLimit: 5000,rollupOptions: {experimentalLogSideEffects: false,output: {// 最小化拆分包manualChunks(id) {if (id.includes(&q…...

C语言_函数调用栈的汇编分析

在 C 语言的底层实现中,函数调用栈是程序运行时内存管理的核心机制。它不仅负责函数间的控制转移,还管理局部变量、参数传递和返回值。本文将结合 C 语言代码和 x86-64 汇编指令,深入解析函数调用栈的工作原理。 一、函数调用栈的基本概念 函数调用栈是内存中的一块后进先…...

Java Spring Boot 控制器中处理用户数据详解

目录 一、获取请求参数1.1 获取查询参数1.2 获取路径参数 二、处理表单提交2.1 处理表单数据 三、处理 JSON 数据3.1 接收 JSON 数据 四、返回 JSON 数据五、处理文件上传5.1 单文件上传5.2 多文件上传 六、总结 在 Spring Boot 应用开发中&#xff0c;控制器&#xff08;Contr…...

vite+vue建立前端工程

​ 参考 开始 | Vite 官方中文文档 VUE教程地址 https://cn.vuejs.org/tutorial/#step-1 第一个工程 https://blog.csdn.net/qq_35221977/article/details/137171497 脚本 chcp 65001 echo 建立vite工程 set PRO_NAMEmy-vue-appif not exist %PRO_NAME% (call npm i…...

【Docker】docker login总是报各种超时错误,导致登录不成功。

报错信息&#xff1a;Error response from daemon: Get “https://registry-1.docker.io/v2/”: net/http: request canceled while waiting for connection (Client.Timeout exceeded while awaiting headers).等超时错误。 解决方案&#xff1a;docker login总是报各种超时错…...

vue使用路由技术实现登录成功后跳转到首页

文章目录 一、概述二、使用步骤安装vue-router在src/router/index.js中创建路由器&#xff0c;并导出在vue应用实例中使用router声明router-view标签&#xff0c;展示组件内容 三、配置登录成功后跳转首页四、参考资料 一、概述 路由&#xff0c;决定从起点到终点的路径的进程…...

day20-线性表(链表II)

一、调试器 1.1 gdb&#xff08;调试器&#xff09; 在程序指定位置停顿 1.1.1 一般调试 gcc直接编译生成的是发布版&#xff08;Release&#xff09; gcc -g //-g调式版本&#xff0c;&#xff08;体积大&#xff0c;内部有源码&#xff09;&#xff08;DeBug&#…...