搭建环境

1、安装RabbitMQ,我是用的是Docker方式安装的,大家根据个人习惯自行安装哈

  1. docker run -d -p 5672:5672 -p 15672:15672 --name rabbitmq -e RABBITMQ_DEFAULT_USER=qbb -e RABBITMQ_DEFAULT_PASS=qbb rabbitmq:3.8-management

SpringBoot整合RabbitMQ

Producer生产者端代码

1、创建一个maven工程

2、导入相关的依赖

  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <project xmlns="http://maven.apache.org/POM/4.0.0"
  3. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  4. xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
  5. <modelVersion>4.0.0</modelVersion>
  6. <groupId>com.qbb</groupId>
  7. <artifactId>springboot-rabbitmq</artifactId>
  8. <version>1.0-SNAPSHOT</version>
  9. <parent>
  10. <groupId>org.springframework.boot</groupId>
  11. <artifactId>spring-boot-starter-parent</artifactId>
  12. <version>2.6.7</version>
  13. </parent>
  14. <dependencies>
  15. <dependency>
  16. <groupId>org.springframework.boot</groupId>
  17. <artifactId>spring-boot-starter-amqp</artifactId>
  18. </dependency>
  19. <dependency>
  20. <groupId>org.springframework.boot</groupId>
  21. <artifactId>spring-boot-starter-test</artifactId>
  22. </dependency>
  23. </dependencies>
  24. </project>

3、编写application.yml配置文件

  1. server:
  2. port: 9001
  3. spring:
  4. application:
  5. name: springboot-rabbitmq
  6. rabbitmq:
  7. host: # 你MQ的地址
  8. port: 5672
  9. username: admin
  10. password: admin
  11. virtual-host: /

4、主启动类

  1. package com.qbb.rabbitmq;
  2. import org.springframework.boot.SpringApplication;
  3. import org.springframework.boot.autoconfigure.SpringBootApplication;
  4. /**
  5. * @author QiuQiu&LL (个人博客:https://www.cnblogs.com/qbbit)
  6. * @version 1.0
  7. * @date 2022-07-28 21:33
  8. * @Description:
  9. */
  10. @SpringBootApplication
  11. public class SpringBootRabbitMQ {
  12. public static void main(String[] args) {
  13. SpringApplication.run(SpringBootRabbitMQ.class, args);
  14. }
  15. }

5、业务逻辑

(1)我们创建一个RabbitMQConfig的配置类,帮助我们来创建交换机、队列以及绑定关系

  1. package com.qbb.rabbitmq.config;
  2. import org.springframework.amqp.core.*;
  3. import org.springframework.beans.factory.annotation.Qualifier;
  4. import org.springframework.context.annotation.Bean;
  5. import org.springframework.context.annotation.Configuration;
  6. /**
  7. * @author QiuQiu&LL (个人博客:https://www.cnblogs.com/qbbit)
  8. * @version 1.0
  9. * @date 2022-07-28 21:34
  10. * @Description:
  11. */
  12. @Configuration
  13. public class RabbitMQConfig {
  14. /**
  15. * 交换机名称
  16. */
  17. public static final String TOPIC_EXCHANGE_NAME = "qiu_topic_exchange";
  18. /**
  19. * 队列名称
  20. */
  21. public static final String QUEUE_NAME = "qiu_queue";
  22. /**
  23. * 创建交换机
  24. *
  25. * @return
  26. */
  27. @Bean("qiuExchange")
  28. public Exchange createExchange() {
  29. return ExchangeBuilder.topicExchange(TOPIC_EXCHANGE_NAME).durable(true).build();
  30. }
  31. /**
  32. * 创建队列
  33. *
  34. * @return
  35. */
  36. @Bean("qiuQueue")
  37. public Queue createQueue() {
  38. return QueueBuilder.durable(QUEUE_NAME).build();
  39. }
  40. /**
  41. * 绑定交换机和队列
  42. *
  43. * @param qiuExchange
  44. * @param qiuQueue
  45. * @return
  46. */
  47. @Bean
  48. public Binding binding(@Qualifier("qiuExchange") Exchange qiuExchange, @Qualifier("qiuQueue") Queue qiuQueue) {
  49. return BindingBuilder.bind(qiuQueue).to(qiuExchange).with("qiu.#").noargs();
  50. }
  51. }

(2)编写Producer发送消息的测试代码

  1. package com.qbb.rabbitmq;
  2. import com.qbb.rabbitmq.config.RabbitMQConfig;
  3. import org.junit.jupiter.api.Test;
  4. import org.springframework.amqp.rabbit.core.RabbitTemplate;
  5. import org.springframework.beans.factory.annotation.Autowired;
  6. import org.springframework.boot.test.context.SpringBootTest;
  7. /**
  8. * @author QiuQiu&LL (个人博客:https://www.cnblogs.com/qbbit)
  9. * @version 1.0
  10. * @date 2022-07-28 21:56
  11. * @Description:
  12. */
  13. @SpringBootTest
  14. public class ProducerTest {
  15. @Autowired
  16. private RabbitTemplate rabbitTemplate;
  17. @Test
  18. public void testSendMsg(){
  19. rabbitTemplate.convertAndSend(RabbitMQConfig.TOPIC_EXCHANGE_NAME,
  20. "qiu.haha", "等我完成目标就来找你~");
  21. }
  22. }

Consumer消费者端代码

1、创建一个maven工程

2、导入相关的依赖

  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <project xmlns="http://maven.apache.org/POM/4.0.0"
  3. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  4. xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
  5. <modelVersion>4.0.0</modelVersion>
  6. <groupId>com.qbb</groupId>
  7. <artifactId>springboot-rabbitmq-consumer</artifactId>
  8. <version>1.0-SNAPSHOT</version>
  9. <parent>
  10. <groupId>org.springframework.boot</groupId>
  11. <artifactId>spring-boot-starter-parent</artifactId>
  12. <version>2.6.7</version>
  13. </parent>
  14. <dependencies>
  15. <dependency>
  16. <groupId>org.springframework.boot</groupId>
  17. <artifactId>spring-boot-starter-amqp</artifactId>
  18. </dependency>
  19. <dependency>
  20. <groupId>org.springframework.boot</groupId>
  21. <artifactId>spring-boot-starter-test</artifactId>
  22. </dependency>
  23. </dependencies>
  24. </project>

3、编写application.yml配置文件

  1. server:
  2. port: 9002
  3. spring:
  4. application:
  5. name: springboot-rabbitmq
  6. rabbitmq:
  7. host:
  8. port: 5672
  9. username: admin
  10. password: admin
  11. virtual-host: /

4、主启动类

  1. package com.qbb.rabbitmq;
  2. import org.springframework.boot.SpringApplication;
  3. import org.springframework.boot.autoconfigure.SpringBootApplication;
  4. /**
  5. * @author QiuQiu&LL (个人博客:https://www.cnblogs.com/qbbit)
  6. * @version 1.0
  7. * @date 2022-07-28 22:09
  8. * @Description:
  9. */
  10. @SpringBootApplication
  11. public class SpringBootRabbitMQConsumer {
  12. public static void main(String[] args) {
  13. SpringApplication.run(SpringBootRabbitMQConsumer.class, args);
  14. }
  15. }

5、业务逻辑

(1)创建一个MQListener监听器,用于监听队列消费消息

  1. package com.qbb.rabbitmq.listener;
  2. import org.springframework.amqp.core.Message;
  3. import org.springframework.amqp.rabbit.annotation.RabbitListener;
  4. import org.springframework.stereotype.Component;
  5. /**
  6. * @author QiuQiu&LL (个人博客:https://www.cnblogs.com/qbbit)
  7. * @version 1.0
  8. * @date 2022-07-28 22:10
  9. * @Description:
  10. */
  11. @Component
  12. public class RabbitMQListener {
  13. /**
  14. * 监听队列
  15. *
  16. * @param message
  17. */
  18. @RabbitListener(queues = "qiu_queue")
  19. public void msgConsumer(Message message) {
  20. System.out.println("message = " + message);
  21. }
  22. }

(2)启动主程序测试一下

RabbitMQ高级特性

1、消息可靠性投递confirm确认模式和return回退模式

  • 修改yml配置文件
  1. spring:
  2. application:
  3. name: springboot-rabbitmq
  4. rabbitmq:
  5. host:
  6. port: 5672
  7. username: admin
  8. password: admin
  9. virtual-host: /
  10. # 是否触发回调方法
  11. # NONE值是禁用发布确认模式,是默认值
  12. # CORRELATED值是发布消息成功到交换器后会触发回调方法
  13. # SIMPLE值经测试有两种效果:
  14. # 其一效果和CORRELATED值一样会触发回调方法
  15. # 其二在发布消息成功后使用rabbitTemplate
  16. # 调用waitForConfirms或waitForConfirmsOrDie方法等待broker节点返回发送结果,根据返回结果来判定下一步的逻辑,
  17. # 要注意的点是waitForConfirmsOrDie方法如果返回false则会关闭channel,则接下来无法发送消息到broker;
  18. publisher-confirm-type: correlated
  19. publisher-returns: true
  20. listener:
  21. simple:
  22. # 开启手动确定
  23. acknowledge-mode: manual
  • 编写测试代码
  1. package com.qbb.rabbitmq;
  2. import com.qbb.rabbitmq.config.RabbitMQConfig;
  3. import org.junit.jupiter.api.Test;
  4. import org.springframework.amqp.core.Message;
  5. import org.springframework.amqp.rabbit.connection.CorrelationData;
  6. import org.springframework.amqp.rabbit.core.RabbitTemplate;
  7. import org.springframework.beans.factory.annotation.Autowired;
  8. import org.springframework.boot.test.context.SpringBootTest;
  9. /**
  10. * @author QiuQiu&LL (个人博客:https://www.cnblogs.com/qbbit)
  11. * @version 1.0
  12. * @date 2022-07-28 21:56
  13. * @Description:
  14. */
  15. @SpringBootTest
  16. public class ProducerTest {
  17. @Autowired
  18. private RabbitTemplate rabbitTemplate;
  19. @Test
  20. public void testSendMsg(){
  21. /**
  22. * false:RabbitMQ会把消息直接丢弃
  23. * true:RabbitMQ会调用Basic.Return命令将消息返回给生产者
  24. */
  25. rabbitTemplate.setMandatory(true);
  26. //设置ReturnsCallback
  27. rabbitTemplate.setReturnsCallback( returnedMessage -> {
  28. //消息对象
  29. Message message = returnedMessage.getMessage();
  30. //错误码
  31. int replyCode = returnedMessage.getReplyCode();
  32. //错误信息
  33. String replyText = returnedMessage.getReplyText();
  34. //交换机
  35. String exchange = returnedMessage.getExchange();
  36. //路由key
  37. String routingKey = returnedMessage.getRoutingKey();
  38. System.out.println("消息从交换机到队列失败,详细信息如下:");
  39. System.out.println("消息对象:"+message);
  40. System.out.println("错误码:"+replyCode);
  41. System.out.println("错误信息:"+replyText);
  42. System.out.println("交换机:"+exchange);
  43. System.out.println("路由key:"+routingKey);
  44. });
  45. //设置ConfirmCallback
  46. rabbitTemplate.setConfirmCallback(new RabbitTemplate.ConfirmCallback() {
  47. /**
  48. * @param correlationData 相关配置信息
  49. * @param ack 交换机是否成功收到消息
  50. * @param cause 错误信息
  51. */
  52. @Override
  53. public void confirm(CorrelationData correlationData, boolean ack, String cause) {
  54. if (ack){
  55. System.out.println("成功发送消息到交换机");
  56. }else {
  57. System.out.println("消息发送到交换机失败");
  58. System.out.println("失败原因:"+cause);
  59. }
  60. }
  61. });
  62. // 错误的示例,大家自行修改
  63. rabbitTemplate.convertAndSend(RabbitMQConfig.TOPIC_EXCHANGE_NAME,
  64. "qiu.haha", "等我完成目标就来找你~");
  65. }
  66. }

2、Consumer端手动ACK

RabbitMQ提供了三种确认方式:

  • 自动确认:acknowledge="none"
  • 手动确认:acknowledge="manual"
  • 根据异常情况确认:acknowledge="auto" (不推荐)

修改配置文件

  1. listener:
  2. type: simple
  3. simple:
  4. #采用手动应答
  5. acknowledge-mode: manual
  6. prefetch: 1 #限制每次发送一条数据。

添加一个ACKListener监听器

  1. package com.qbb.rabbitmq.listener;
  2. import com.rabbitmq.client.Channel;
  3. import org.springframework.amqp.core.Message;
  4. import org.springframework.amqp.rabbit.annotation.RabbitListener;
  5. import org.springframework.stereotype.Component;
  6. import java.io.IOException;
  7. /**
  8. * @author QiuQiu&LL (个人博客:https://www.cnblogs.com/qbbit)
  9. * @version 1.0
  10. * @date 2022-07-29 21:32
  11. * @Description:
  12. */
  13. @Component
  14. public class AckListener {
  15. @RabbitListener(queues = {"akc-queue"})
  16. public void testQueue(Message message, Channel channel) throws IOException {
  17. System.out.println("ack-queue消费:" + new String(message.getBody()));
  18. /*
  19. listener:
  20. type: simple
  21. simple:
  22. #采用手动应答
  23. acknowledge-mode: manual
  24. prefetch: 1 #限制每次发送一条数据。
  25. */
  26. // 采用手动ack,一条条的消费
  27. channel.basicAck(message.getMessageProperties().getDeliveryTag(), false);
  28. /**
  29. * 参数一:DeliveryTag
  30. * 参数二:是否接收多个消息
  31. * 参数三:是否重回队列,如果为true消息会重新回到queue, broker会重新发送该消息给消费端
  32. */
  33. //channel.basicNack(message.getMessageProperties().getDeliveryTag(),true,true);
  34. }
  35. }

测试

3、Consumer端限流

修改配置文件

  1. #采用手动应答
  2. acknowledge-mode: manual
  3. prefetch: 1 #限制每次消费N条数据。

4、TTL

  • 设置队列的过期时间

    • x-message-ttl,单位:ms(毫秒),这样的话进入队列的所有消息都遵循这个过期时间。
  • 设置单个消息的过期时间(expiretion)
    • expiration,单位:ms(毫秒),这样的话只有这条消息有过期时间。这里的消息过期了,如果这条消息是在头部,才会丢弃

5、死信队列

  • 消息成为死信的三种情况

    • 消息超过了队列的最大长度(max-length)
    • Consumer端basicNAck|basicReject,消费端拒收,并且requeue=false不重新入队
    • 消息过期

6、延迟队列(RabbitMQ没有这个实现)

  • 但是我们可以通过TTL+死信队列解决

7、日志与监控

http://ip:15672/直接访问web图形化界面,用命令也可以查看,但是个人感觉没有图形化界面友好!!!

8、保证消息的幂等性

  • 使用乐观锁的方式解决

SpringBoot整个RabbitMQ详细~的更多相关文章

  1. Springboot接入RabbitMQ详细教程

    本文适用于对 RabbitMQ 有所了解的人,在此不讨论MQ的原理,只讨论如何接入.其实Spring Boot 集成 RabbitMQ 非常简单,本文章使用的是Spring Boot 提供了sprin ...

  2. 功能:SpringBoot整合rabbitmq,长篇幅超详细

    SpringBoot整合rabbitMq 一.介绍 消息队列(Message Queue)简称mq,本文将介绍SpringBoot整合rabbitmq的功能使用 队列是一种数据结构,就像排队一样,遵循 ...

  3. SpringBoot集成RabbitMQ

    官方说明:http://www.rabbitmq.com/getstarted.html 什么是MQ? MQ全称为Message Queue, 消息队列(MQ)是一种应用程序对应用程序的通信方法.MQ ...

  4. rabbitmq学习(五):springboot整合rabbitmq

    一.Springboot对rabbitmq的支持 springboot提供了对rabbitmq的支持,并且大大简化了rabbitmq的相关配置.在springboot中,框架帮我们将不同的交换机划分出 ...

  5. SpringBoot 整合 RabbitMQ(包含三种消息确认机制以及消费端限流)

    目录 说明 生产端 消费端 说明 本文 SpringBoot 与 RabbitMQ 进行整合的时候,包含了三种消息的确认模式,如果查询详细的确认模式设置,请阅读:RabbitMQ的三种消息确认模式 同 ...

  6. Springboot 整合RabbitMq ,用心看完这一篇就够了

    该篇文章内容较多,包括有rabbitMq相关的一些简单理论介绍,provider消息推送实例,consumer消息消费实例,Direct.Topic.Fanout的使用,消息回调.手动确认等. (但是 ...

  7. RabbitMQ入门到进阶(Spring整合RabbitMQ&SpringBoot整合RabbitMQ)

    1.MQ简介 MQ 全称为 Message Queue,是在消息的传输过程中保存消息的容器.多用于分布式系统 之间进行通信. 2.为什么要用 MQ 1.流量消峰 没使用MQ 使用了MQ 2.应用解耦 ...

  8. SpringBoot整合RabbitMQ实现六种工作模式

    RabbitMQ主要有六种种工作模式,本文整合SpringBoot分别介绍工作模式的实现. 前提概念 生产者 消息生产者或者发送者,使用P表示: 队列 消息从生产端发送到消费端,一定要通过队列转发,使 ...

  9. SpringBoot集成rabbitmq(二)

    前言 在使用rabbitmq时,我们可以通过消息持久化来解决服务器因异常崩溃而造成的消息丢失.除此之外,我们还会遇到一个问题,当消息生产者发消息发送出去后,消息到底有没有正确到达服务器呢?如果不进行特 ...

  10. SpringBoot之RabbitMQ的使用

    一 .RabbitMQ的介绍 RabbitMQ是消息中间件的一种,消息中间件即分布式系统中完成消息的发送和接收的基础软件,消息中间件的工作过程可以用生产者消费者模型来表示.即,生产者不断的向消息队列发 ...

随机推荐

  1. 《SQLi-Labs》01. Less 1~5

    @ 目录 前言 索引 Less-1 题解 原理 Less-2 题解 Less-3 题解 Less-4 题解 Less-5 题解 原理 sqli.开启新坑. 前言 对于新手,为了更加直观的看到 sql ...

  2. shopee根据ID取商品详情 API

    ​ item_get-根据ID取商品详情  注册开通 shopee.item_get 公共参数 名称 类型 必须 描述 key String 是 调用key(必须以GET方式拼接在URL中) secr ...

  3. Codeforces 1463D Pairs

    题意 对于数字\(1\)~\(2n\),可以构造出\(n\)个二元组,对于\(n\)个二元组,选择一个数组\(x\),留下\(x\)个二元组的最小值,留下\(n-x\)个二元组的最大值,其构成了一个集 ...

  4. Vim深入使用指南

    Vim深入使用指南 Vim是一款功能强大的文本编辑器,被广泛用于编写和编辑各种类型的文档和代码. 安装Vim 可以操作系统下载并安装Vim.在安装完成后,通过在终端中输入vim命令来启动Vim. Vi ...

  5. POI 操作sheet.shiftRows注意点

    sheet.shiftRows后使用getRow/getCell会导致NullPointException 正确使用应该是CreateRow CreateCell

  6. WebGPU缓冲区更新最佳实践

    介绍 在WebGPU中,GPUBuffer是您将要操作的主要对象之一.它与GPUTextures一同代表了您的应用程序向GPU传递用于渲染的大部分数据.在WebGPU中,缓冲区用于顶点和索引数据.un ...

  7. 我们又组织了一次欧洲最大开源社区活动,Hugging Face 博客欢迎社区成员发帖、Hugging Chat 功能更新!

    每一周,我们的同事都会向社区的成员们发布一些关于 Hugging Face 相关的更新,包括我们的产品和平台更新.社区活动.学习资源和内容更新.开源库和模型更新等,我们将其称之为「Hugging Ne ...

  8. 实战攻防演练--利用微软自带Certutil命令ByPassAV上传C2

    Certutil Certutil.exe是Windows操作系统中的合法程序,主要用于管理证书相关操作.它提供了转储和显示证书颁发机构(CA)的配置信息.配置证书服务.备份和还原CA组件,以及验证证 ...

  9. [Python急救站课程]猴子吃桃问题

    问题:猴子第一天摘下若干个桃子,当即吃了一半,还不过瘾,又多吃了一个:第二天早上又将剩下的桃子吃掉一半,又多吃了一个.以后,每天早上都吃了前一天剩下的一半多一个.到了第五天早上想再吃时,见只剩下一个桃 ...

  10. JavaScript高级程序设计笔记08 对象、类与面向对象编程

    对象.类与面向对象编程 对象 一组属性的无序集合 属性 类型 数据属性 value.writable 访问器属性 getter.setter至少有一 定义 .操作符:默认可配置.可枚举.可写(数据属性 ...