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

SpringBoot 整合 RabbitMQ

1. 创建 SpringBoot 工程

把版本改为 2.7.14

引入这两个依赖:

		<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>

配置 application.yml文件

2. 编写代码

生产者 :

Config 类 : RabbitMQConfig

package com.lqf.rabbitmq.springbootrabbitmq.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 EXCHANGE_NAME = "boot_topic_exchange";public static final String QUEUE_NAME = "boot_queue";//1.交换机@Bean("bootExchange")public Exchange bootExchange(){return ExchangeBuilder.topicExchange(EXCHANGE_NAME).durable(true).build();}//2.Queue 队列@Bean("bootQueue")public Queue bootQueue(){return QueueBuilder.durable(QUEUE_NAME).build();}//3. 队列和交互机绑定关系 Binding/*1. 知道哪个队列2. 知道哪个交换机3. routing key*/@Beanpublic Binding bindQueueExchange(@Qualifier("bootQueue") Queue queue, @Qualifier("bootExchange") Exchange exchange){return BindingBuilder.bind(queue).to(exchange).with("boot.#").noargs();}}

测试类: RabbitMQConfigTests

package com.lqf.rabbitmq.springbootrabbitmq;import com.lqf.rabbitmq.springbootrabbitmq.config.RabbitMQConfig;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;@SpringBootTest
@RunWith(SpringRunner.class)
public class RabbitMQConfigTests {//1.注入RabbitTemplate@Autowiredprivate RabbitTemplate rabbitTemplate;@Testpublic void testSend(){rabbitTemplate.convertAndSend(RabbitMQConfig.EXCHANGE_NAME,"boot.haha","boot mq hello~~~");}
}

结果

当我们启动 测试类 之后就可以发现我们的 rabbitmq 界面里的 Exchange 里多了一个

Queue 中多了一个消息:

消费者:

消费者的创建与生产者一样

Component 类 : 

package com.example.rabbitmq;import org.springframework.amqp.core.Message;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.stereotype.Component;@Component
public class RabbitMQListener {@RabbitListener(queues = "boot_queue")public void ListenerQueue(Message message) {System.out.println("Message : ");System.out.println(message);}
}

结果

启动核心类:

之后我们就收到消息了

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

相关文章:

  • 气象科普丨气象站的分类与应用
  • 【论文精读】Learning Transferable Visual Models From Natural Language Supervision
  • 缓存和分布式锁笔记
  • React笔记(七)Antd
  • 无涯教程-Android - RadioButton函数
  • kafka如何避免消费组重平衡
  • 浅谈一下企业信息化管理
  • 北京APP外包开发团队人员构成
  • Node基础and包管理工具
  • 【python使用 Pillow 库】缩小|放大图片
  • 解决Ubuntu 或Debian apt-get IPv6问题:如何设置仅使用IPv4
  • Xubuntu16.04系统中解决无法识别exFAT格式的U盘
  • Pygame中Trivia游戏解析6-1
  • idea中创建springboot项目显示Spring Initializr Error
  • VScode 国内下载源 以及 nvm版本控制器下载与使用
  • GO|经典错误之回车与\n
  • 【MATLAB第71期】基于MATLAB的Abcboost自适应决策树多输入单输出回归预测及多分类预测模型(更新中)
  • ARM编程模型-内存空间和数据
  • leetcode原题: 最大数
  • docker 是什么
  • 基于Gin框架的HTTP接口限速实践
  • WSL中为Ubuntu和Debian设置固定IP的终极指南
  • axios+vite配置反向代理踩坑记录
  • Spring IOC的理解
  • 2023年京东箱包行业数据分析(京东数据运营)
  • 对称加密 非对称加密 AC认证 https原理
  • 如何在PyQt应用程序中使用Qt Designer和Pyuic工具?
  • 【云计算•云原生】5.云原生之初识DevOps
  • 20230830工作心得:巧用标记位和For循环遍历
  • AUTOSAR规范与ECU软件开发(实践篇)7.9 MCAL模块配置方法及常用接口函数介绍之Can的配置