SpringBoot 整合 RabbitMQ

1. 创建 SpringBoot 工程

把版本改为 2.7.14

引入这两个依赖:

XML 复制代码
		<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

java 复制代码
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
     */
    @Bean
    public Binding bindQueueExchange(@Qualifier("bootQueue") Queue queue, @Qualifier("bootExchange") Exchange exchange){
        return BindingBuilder.bind(queue).to(exchange).with("boot.#").noargs();
    }


}

测试类: RabbitMQConfigTests

java 复制代码
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
    @Autowired
    private RabbitTemplate rabbitTemplate;

    @Test
    public void testSend(){

        rabbitTemplate.convertAndSend(RabbitMQConfig.EXCHANGE_NAME,"boot.haha","boot mq hello~~~");
    }
}

结果

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

Queue 中多了一个消息:

消费者:

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

Component 类 :

java 复制代码
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);
    }
}

结果

启动核心类:

之后我们就收到消息了

相关推荐
中国lanwp几秒前
Spring Boot 中使用 Lombok 进行依赖注入的示例
java·spring boot·后端
张先shen4 小时前
Spring Boot集成Redis:从配置到实战的完整指南
spring boot·redis·后端
Q_Q5110082855 小时前
python的婚纱影楼管理系统
开发语言·spring boot·python·django·flask·node.js·php
一线大码6 小时前
Gradle 高级篇之构建多模块项目的方法
spring boot·gradle·intellij idea
congvee8 小时前
springboot 学习第1期 - 创建工程
spring boot
风流 少年9 小时前
Cursor创建Spring Boot项目
java·spring boot·后端
毕设源码_钟学姐9 小时前
计算机毕业设计springboot宿舍管理信息系统 基于Spring Boot的高校宿舍管理平台设计与实现 Spring Boot框架下的宿舍管理系统开发
spring boot·后端·课程设计
军军君019 小时前
基于Springboot+UniApp+Ai实现模拟面试小工具二:后端项目搭建
前端·javascript·spring boot·spring·微信小程序·前端框架·集成学习
全栈凯哥10 小时前
16.Spring Boot 国际化完全指南
java·spring boot·后端
后端小肥肠13 小时前
效率革命!10分钟用Dify+Spring Boot打造AI热点雷达,自媒体选赛道再不难!(附保姆级教程)
人工智能·spring boot·agent