SpringBoot 整合 RabbitMQ

news/2024/10/19 16:00:16

简介

一般在开发过程中:

生产者工程:

  1. application.yml 文件配置 RabbitMQ 相关信息;

  2. 在生产者工程中编写配置类,用于创建交换机和队列,并进行绑定;

  3. 注入 RabbitTemplate 对象,通过 RabbitTemplate 对象发送消息到交换机。

消费者工程:

  1. application.yml 文件配置 RabbitMQ 相关信息;

  2. 创建消息处理类,用于接收队列中的消息并进行处理。

搭建生产者工程

创建工程

创建 SpringBoot 工程 springboot-producer:

image-20241019154044546

添加依赖

pom.xml 文件中添加 spring-boot-starter-amqp:

<dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-amqp</artifactId>
</dependency>

配置 RabbitMQ

1)配置文件

创建 application.yml,内容如下:

spring:rabbitmq:host: localhostport: 5672virtual-host: /vhostusername: adminpassword: 123456

2)绑定交换机和队列

创建 RabbitMQ 队列与交换机绑定的配置类 com.itheima.rabbitmq.config.RabbitMQConfig

package com.zhangmingge.springbootproducer.config;import org.springframework.amqp.core.*;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;@Configuration
public class RabbitMQConfig {// 交换机名称public static final String ITEM_TOPIC_EXCHANGE = "item_topic_exchange";// 队列名称public static final String ITEM_QUEUE = "item_queue";// 声明交换机@Bean("itemTopicExchange")public Exchange topicExchange() {return ExchangeBuilder.topicExchange(ITEM_TOPIC_EXCHANGE).durable(true).build();}// 声明队列@Bean("itemQueue")public Queue itemQueue() {return QueueBuilder.durable(ITEM_QUEUE).build();}// 绑定队列和交换机@Beanpublic Binding itemQueueExchange(@Qualifier("itemQueue") Queue queue,@Qualifier("itemTopicExchange") Exchange exchange) {return BindingBuilder.bind(queue).to(exchange).with("item.#").noargs();}
}

搭建消费者工程

创建工程

创建 SpringBoot 工程 springboot-consumer:

image-20241019154009386

添加依赖

pom.xml 文件中添加 spring-boot-starter-amqp:

<dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-amqp</artifactId>
</dependency>

配置 RabbitMQ

创建 application.yml,内容如下:

spring:rabbitmq:host: localhostport: 5672virtual-host: /vhostusername: adminpassword: 123456

消息监听处理类

编写消息监听器 MyListener:


package com.zhangmingge.springbootconsumer.listener;import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.stereotype.Component;@Component
public class MyListener {/*** 监听某个队列的消息** @param message 接收到的消息*/@RabbitListener(queues = "item_queue")public void myListener1(String message) {System.out.println("消费者接收到的消息为:" + message);}
}

测试

在生产者工程 springboot-producer 中创建测试类,发送消息:

package com.zhangmingge.springbootproducer;import com.zhangmingge.springbootproducer.config.RabbitMQConfig;
import org.junit.jupiter.api.Test;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;@SpringBootTest
public class RabbitMQTest {@Autowiredprivate RabbitTemplate rabbitTemplate;@Testpublic void test() {rabbitTemplate.convertAndSend(RabbitMQConfig.ITEM_TOPIC_EXCHANGE, "item.insert", "商品新增,routing key 为 item.insert");rabbitTemplate.convertAndSend(RabbitMQConfig.ITEM_TOPIC_EXCHANGE, "item.update", "商品修改,routing key 为 item.update");rabbitTemplate.convertAndSend(RabbitMQConfig.ITEM_TOPIC_EXCHANGE, "item.delete", "商品删除,routing key 为 item.delete");}
}

先运行上述测试程序(交换机和队列才能先被声明和绑定),然后启动消费者,在消费者工程 springboot-consumer 中控制台查看是否接收到对应消息。

另外,也可以在 RabbitMQ 的管理控制台中查看到交换机与队列的绑定:

1556074827222

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.ryyt.cn/news/73474.html

如若内容造成侵权/违法违规/事实不符,请联系我们进行投诉反馈,一经查实,立即删除!

相关文章

P3571 [POI2014] SUP-Supercomputer 题解

P3571「POI2014」SUP-Supercomputer 题解 一道 “较” 水的黑题 (可一开始苦思冥想还是不会)。 本蒟蒻的第一篇黑题题解,求赞。 题意简化 给定一棵 $n$ 个节点、根节点为 $1$ 的有根树。$q$ 次询问中每次给定一个 $k$,输出需要最少用几次操作次数 删除 完整棵树。每次操作可…

Spring IoC

一、Spring IoC的理解IoC(Inversion of Control:控制反转) 是一种设计思想,而不是一个具体的技术实现。IoC 的思想就是将原本在程序中手动创建对象的控制权,交由 Spring 框架来管理。不过, IoC 并非 Spring 特有,在其他语言中也有应用。 控制反转?控制:指的是对象创建(…

修改VS的代码高亮颜色

点击工具->选项选择“字体和颜色”找到“用户成员-xx”、“用户类型-xx”,点击即可修改前景色、背景色

ArkUI-Image详解

ArkUI-Image详解 文章摘要: 给Image组件设置属性可以使图片显示更灵活,达到一些自定义的效果。以下是几个常用属性的使用示例。这时可以使用interpolation属性对图片进行插值,使图片显示得更清晰。Image组件引入本地图片路径,即可显示图片(根目录为ets文件夹)。通过rende…

强化学习算法笔记之【DDPG算法】

强化学习笔记第2篇,讲解DDPG算法。 感兴趣可以参考或者复刻。强化学习笔记之【DDPG算法】 目录强化学习笔记之【DDPG算法】前言:原论文伪代码DDPG 中的四个网络代码核心更新公式前言: 本文为强化学习笔记第二篇,第一篇讲的是Q-learning和DQN 就是因为DDPG引入了Actor-Crit…

python输出hello world

输出print("hello world")

2161: 【例9.3】小写字母转大写字母 【超出字符数据范围】

include <bits/stdc++.h> using namespace std; int main( ) { char a; cin >> a; cout << char(a-32); return 0; } // 反思1: cin >> a; 忘记写了 反思2: +是转为小写字母-是转为大写字母 【做错】

航飞参数计算

作者:太一吾鱼水 宣言:在此记录自己学习过程中的心得体会,同时积累经验,不断提高自己! 声明:博客写的比较乱,主要是自己看的。如果能对别人有帮助当然更好,不喜勿喷! 文章未经说明均属原创,学习笔记可能有大段的引用,一般会注明参考文献。 欢迎大家…