Spring Boot 消息队列与异步处理

Spring Boot 消息队列与异步处理

Spring Boot 消息队列与异步处理

在这里插入图片描述
28.1 学习目标与重点提示

学习目标:掌握Spring Boot消息队列与异步处理的核心概念与使用方法,包括消息队列的定义与特点、异步处理的定义与特点、Spring Boot与消息队列的集成、Spring Boot的实际应用场景,学会在实际开发中处理消息队列与异步处理问题。
重点:消息队列的定义与特点异步处理的定义与特点Spring Boot与消息队列的集成Spring Boot的实际应用场景

28.2 消息队列与异步处理概述

消息队列与异步处理是Java开发中的重要组件。

28.2.1 消息队列的定义

定义:消息队列是一种用于在不同应用程序之间传递消息的中间件,允许应用程序异步处理消息。
作用

  • 提高应用程序的性能。
  • 提高应用程序的可靠性。
  • 实现应用程序之间的解耦。

常见的消息队列

  • RabbitMQ:RabbitMQ是一种开源的消息队列。
  • ActiveMQ:ActiveMQ是一种开源的消息队列。
  • Kafka:Kafka是一种开源的消息队列。
  • Redis:Redis是一种开源的内存数据库,支持消息队列。

✅ 结论:消息队列是一种用于在不同应用程序之间传递消息的中间件,作用是提高应用程序的性能、可靠性、实现应用程序之间的解耦。

28.2.2 异步处理的定义

定义:异步处理是指应用程序在处理请求时,不阻塞主线程,而是将请求发送到消息队列,由其他线程或进程处理。
作用

  • 提高应用程序的响应速度。
  • 提高应用程序的吞吐量。
  • 提高应用程序的可扩展性。

✅ 结论:异步处理是指应用程序在处理请求时,不阻塞主线程,作用是提高应用程序的响应速度、吞吐量、可扩展性。

28.3 Spring Boot与消息队列的集成

Spring Boot与消息队列的集成是Java开发中的重要内容。

28.3.1 集成RabbitMQ的步骤

定义:集成RabbitMQ的步骤是指使用Spring Boot与RabbitMQ集成的方法。
步骤

  1. 创建Spring Boot项目。
  2. 添加所需的依赖。
  3. 配置RabbitMQ。
  4. 创建消息生产者。
  5. 创建消息消费者。
  6. 测试应用。

示例
pom.xml文件中的依赖:

<dependencies><!-- Web依赖 --><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-web</artifactId></dependency><!-- RabbitMQ依赖 --><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-amqp</artifactId></dependency><!-- 测试依赖 --><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-test</artifactId><scope>test</scope></dependency></dependencies>

application.properties文件中的配置:

# 服务器端口 server.port=8080 # RabbitMQ连接信息 spring.rabbitmq.host=localhost spring.rabbitmq.port=5672 spring.rabbitmq.username=guest spring.rabbitmq.password=guest 

消息生产者类:

importorg.springframework.amqp.rabbit.core.RabbitTemplate;importorg.springframework.beans.factory.annotation.Autowired;importorg.springframework.stereotype.Component;@ComponentpublicclassProductMessageProducer{@AutowiredprivateRabbitTemplate rabbitTemplate;privatestaticfinalStringEXCHANGE_NAME="product-exchange";privatestaticfinalStringROUTING_KEY="product-routing-key";publicvoidsendProductMessage(String message){ rabbitTemplate.convertAndSend(EXCHANGE_NAME,ROUTING_KEY, message);System.out.println("消息已发送:"+ message);}}

消息消费者类:

importorg.springframework.amqp.rabbit.annotation.RabbitListener;importorg.springframework.stereotype.Component;@ComponentpublicclassProductMessageConsumer{@RabbitListener(queues ="product-queue")publicvoidreceiveProductMessage(String message){System.out.println("消息已接收:"+ message);// 处理消息的业务逻辑processProductMessage(message);}privatevoidprocessProductMessage(String message){// 模拟处理消息的业务逻辑try{Thread.sleep(2000);System.out.println("消息处理完成:"+ message);}catch(InterruptedException e){ e.printStackTrace();}}}

RabbitMQ配置类:

importorg.springframework.amqp.core.Binding;importorg.springframework.amqp.core.BindingBuilder;importorg.springframework.amqp.core.DirectExchange;importorg.springframework.amqp.core.Queue;importorg.springframework.context.annotation.Bean;importorg.springframework.context.annotation.Configuration;@ConfigurationpublicclassRabbitMQConfig{@BeanpublicDirectExchangeproductExchange(){returnnewDirectExchange("product-exchange");}@BeanpublicQueueproductQueue(){returnnewQueue("product-queue");}@BeanpublicBindingproductBinding(Queue productQueue,DirectExchange productExchange){returnBindingBuilder.bind(productQueue).to(productExchange).with("product-routing-key");}}

控制器类:

importorg.springframework.beans.factory.annotation.Autowired;importorg.springframework.web.bind.annotation.*;@RestController@RequestMapping("/api/products")publicclassProductController{@AutowiredprivateProductMessageProducer productMessageProducer;@PostMapping("/send")publicStringsendProductMessage(@RequestBodyString message){ productMessageProducer.sendProductMessage(message);return"消息已发送";}}

应用启动类:

importorg.springframework.boot.SpringApplication;importorg.springframework.boot.autoconfigure.SpringBootApplication;@SpringBootApplicationpublicclassRabbitMQApplication{publicstaticvoidmain(String[] args){SpringApplication.run(RabbitMQApplication.class, args);}}

测试类:

importorg.junit.jupiter.api.Test;importorg.springframework.beans.factory.annotation.Autowired;importorg.springframework.boot.test.context.SpringBootTest;importorg.springframework.boot.test.web.client.TestRestTemplate;importorg.springframework.boot.web.server.LocalServerPort;importstaticorg.assertj.core.api.Assertions.assertThat;@SpringBootTest(webEnvironment =SpringBootTest.WebEnvironment.RANDOM_PORT)classRabbitMQApplicationTests{@LocalServerPortprivateint port;@AutowiredprivateTestRestTemplate restTemplate;@TestvoidcontextLoads(){}@TestvoidtestSendProductMessage(){String message ="Hello, RabbitMQ!";String response = restTemplate.postForObject("http://localhost:"+ port +"/api/products/send", message,String.class);assertThat(response).contains("消息已发送");}}

✅ 结论:集成RabbitMQ的步骤包括创建Spring Boot项目、添加所需的依赖、配置RabbitMQ、创建消息生产者、创建消息消费者、测试应用。

28.4 Spring Boot的实际应用场景

在实际开发中,Spring Boot消息队列与异步处理的应用场景非常广泛,如:

  • 实现产品信息的异步处理。
  • 实现用户信息的异步处理。
  • 实现订单信息的异步处理。
  • 实现支付信息的异步处理。

示例

importorg.springframework.amqp.rabbit.core.RabbitTemplate;importorg.springframework.beans.factory.annotation.Autowired;importorg.springframework.stereotype.Component;@ComponentclassProductMessageProducer{@AutowiredprivateRabbitTemplate rabbitTemplate;privatestaticfinalStringEXCHANGE_NAME="product-exchange";privatestaticfinalStringROUTING_KEY="product-routing-key";publicvoidsendProductMessage(String message){ rabbitTemplate.convertAndSend(EXCHANGE_NAME,ROUTING_KEY, message);System.out.println("消息已发送:"+ message);}}importorg.springframework.amqp.rabbit.annotation.RabbitListener;importorg.springframework.stereotype.Component;@ComponentclassProductMessageConsumer{@RabbitListener(queues ="product-queue")publicvoidreceiveProductMessage(String message){System.out.println("消息已接收:"+ message);processProductMessage(message);}privatevoidprocessProductMessage(String message){try{Thread.sleep(2000);System.out.println("消息处理完成:"+ message);}catch(InterruptedException e){ e.printStackTrace();}}}importorg.springframework.amqp.core.Binding;importorg.springframework.amqp.core.BindingBuilder;importorg.springframework.amqp.core.DirectExchange;importorg.springframework.amqp.core.Queue;importorg.springframework.context.annotation.Bean;importorg.springframework.context.annotation.Configuration;@ConfigurationclassRabbitMQConfig{@BeanpublicDirectExchangeproductExchange(){returnnewDirectExchange("product-exchange");}@BeanpublicQueueproductQueue(){returnnewQueue("product-queue");}@BeanpublicBindingproductBinding(Queue productQueue,DirectExchange productExchange){returnBindingBuilder.bind(productQueue).to(productExchange).with("product-routing-key");}}importorg.springframework.beans.factory.annotation.Autowired;importorg.springframework.web.bind.annotation.*;@RestController@RequestMapping("/api/products")classProductController{@AutowiredprivateProductMessageProducer productMessageProducer;@PostMapping("/send")publicStringsendProductMessage(@RequestBodyString message){ productMessageProducer.sendProductMessage(message);return"消息已发送";}}@SpringBootApplicationpublicclassRabbitMQApplication{publicstaticvoidmain(String[] args){SpringApplication.run(RabbitMQApplication.class, args);}}// 测试类@SpringBootTest(webEnvironment =SpringBootTest.WebEnvironment.RANDOM_PORT)classRabbitMQApplicationTests{@LocalServerPortprivateint port;@AutowiredprivateTestRestTemplate restTemplate;@TestvoidcontextLoads(){}@TestvoidtestSendProductMessage(){String message ="Hello, RabbitMQ!";String response = restTemplate.postForObject("http://localhost:"+ port +"/api/products/send", message,String.class);assertThat(response).contains("消息已发送");}}

输出结果

  • 访问http://localhost:8080/api/products/send,发送消息“Hello, RabbitMQ!”。
  • 控制台输出:消息已发送:Hello, RabbitMQ!。
  • 消费者控制台输出:消息已接收:Hello, RabbitMQ!,2秒后输出:消息处理完成:Hello, RabbitMQ!。

✅ 结论:在实际开发中,Spring Boot消息队列与异步处理的应用场景非常广泛,需要根据实际问题选择合适的消息队列和异步处理方法。

总结

本章我们学习了Spring Boot消息队列与异步处理,包括消息队列的定义与特点、异步处理的定义与特点、Spring Boot与消息队列的集成、Spring Boot的实际应用场景,学会了在实际开发中处理消息队列与异步处理问题。其中,消息队列的定义与特点、异步处理的定义与特点、Spring Boot与消息队列的集成、Spring Boot的实际应用场景是本章的重点内容。从下一章开始,我们将学习Spring Boot的其他组件、微服务等内容。

Read more

《算法题讲解指南:优选算法-分治-归并》--47.归并排序,48.数组中的逆序对

《算法题讲解指南:优选算法-分治-归并》--47.归并排序,48.数组中的逆序对

🔥小叶-duck:个人主页 ❄️个人专栏:《Data-Structure-Learning》 《C++入门到进阶&自我学习过程记录》《算法题讲解指南》--优选算法 ✨未择之路,不须回头 已择之路,纵是荆棘遍野,亦作花海遨游 目录 47.归并排序 题目链接: 题目描述: 题目示例: 解法(归并排序): 算法思路: C++算法代码: 算法总结及流程解析: 48.数组中的逆序对 题目链接: 题目描述: 题目示例: 解法(利用归并排序的过程——分治): 算法思路: C++算法代码: 算法总结及流程解析: 结束语 47.归并排序 题目链接: 215. 数组912. 排序数组 - 力扣(LeetCode)215.

By Ne0inhk
Flutter 三方库 image_compare 鸿蒙图像治理算法域双向适配解析:突破千万级相册视觉感知哈希运算指纹比对墙,大体量空间冗余清扫提供高精雷达矩阵-适配鸿蒙 HarmonyOS ohos

Flutter 三方库 image_compare 鸿蒙图像治理算法域双向适配解析:突破千万级相册视觉感知哈希运算指纹比对墙,大体量空间冗余清扫提供高精雷达矩阵-适配鸿蒙 HarmonyOS ohos

欢迎加入开源鸿蒙跨平台社区:https://openharmonycrossplatform.ZEEKLOG.net Flutter 三方库 image_compare 鸿蒙图像治理算法域双向适配解析:突破千万级相册视觉感知哈希运算指纹比对墙,为大体量空间冗余清扫提供高精雷达矩阵 前言 在 OpenHarmony 应用的内容社交或相册管理开发中,由于重复下载或连拍,用户的磁盘空间极易被重复图像挤占。image_compare 为 Flutter 开发者提供了一套高性能、专注于图像指纹算法的对比方案。本文将介绍如何在鸿蒙端打造极致的视觉资产治理底座。 一、原理解析 / 概念介绍 1.1 基础原理/概念介绍 image_compare 的核心逻辑是基于 感知哈希(Perceptual Hashing, pHash)与颜色直方图空间映射 (Visual-Entropy Map)。它并非简单的逐像素二进制对比,而是通过将图像进行灰度化、离散余弦变换(DCT)降噪,提取反映图像“骨架结构”的

By Ne0inhk
排序算法指南:快速排序(非递归)

排序算法指南:快速排序(非递归)

前言:          本文将通过图解与代码相结合的方式,详细介绍快速排序的非递归实现方法。虽然前文已展示递归实现方案,但在实际面试中,面试官更倾向于考察非递归版本的实现。这种实现方式不仅能加深对算法的理解,还能展现应聘者对栈结构的掌握程度。          一、非递归实现快排的思路          1.1核心原理:手动模拟栈                   在标准的递归快速排序中,当我们写下 quickSort(a,left, right) 时,系统会自动分配一块内存(函数调用栈)来记住当前的 left 和 right 是多少,以及函数执行完后该回到哪里。         在非递归版本中,我们不需要系统帮忙,而是自己创建一个栈(Stack)数据结构。          1.2核心操作:用栈存取数组区间          ① 向栈中存储操作:存储每一次需要排序的子数组的起止下标(begin,end)。                                  由于栈的特性是先进后出,我们优先处理左区间,再处理右区间,类似于二叉树的前序操

By Ne0inhk
《算法闯关指南:优选算法--位运算》--36.两个整数之和,37.只出现一次的数字 ||

《算法闯关指南:优选算法--位运算》--36.两个整数之和,37.只出现一次的数字 ||

🔥草莓熊Lotso:个人主页 ❄️个人专栏: 《C++知识分享》《Linux 入门到实践:零基础也能懂》 ✨生活是默默的坚持,毅力是永久的享受! 🎬 博主简介: 文章目录 * 前言: * 36. 两个整数之和 * 解法(位运算): * 算法思路: * C++算法代码: * 算法总结&&笔记展示: * 37.只出现一次的数字 || * 解法(比特位计数): * 算法思路: * C++算法代码: * 算法总结&&笔记展示: * 结尾: 前言: 聚焦算法题实战,系统讲解三大核心板块:优选算法:剖析动态规划、二分法等高效策略,学会寻找“最优解”。 递归与回溯:掌握问题分解与状态回退,攻克组合、排列等难题。 贪心算法:理解“

By Ne0inhk