SpringBoot整合RabbitMQ

113 阅读2分钟

大致步骤

  • 安装RabbitMQ
  • 创建工程
  • 引入依赖
  • yml配置及配置类编写
  • 定义交换机,队列以及绑定关系
  • 注入 RabbitTemplate ,调用方法,完成消息发送及接收

一、安装

二、引入相关依赖

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

三、yml配置及配置类编写

3.1、yml配置

server:
  port: 8021
spring:
  application:
    # 项目名称
    name: rabbitmq-provider
  # 配置 RabbitMQ 服务器
  rabbitmq:
    host: 127.0.0.1
    port: 5672
    username: root
    password: root
    # 虚拟host 可以不设置,使用server默认host/
    virtual-host: /JCcccHost

3.2、生产者配置类

import org.springframework.amqp.core.*;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
 
/**
 * @Author : Orange
 * @CreateTime : 2023/3/30
 * @Description :
 **/
@Configuration
public class DirectRabbitConfig {

    public static final String EXCHANGE_NAME = "boot_topic_exchange";
    public static final String QUEUE_NAME = "boot_queue";
 
    // 1、定义Direct类型交换机
    @Bean
    public Exchange topicExchange() {
        // 构建一个Topic类型的交换机,持久化为true,自动删除为false
        return  ExchangeBuilder.topicExchange(EXCHANGE_NAME).durable(true).autoDelete(false).build();
    }

    // 2、队列 起名:bootQueue
    @Bean
    public Queue bootQueue() {
        // durable:是否持久化,默认是false,持久化队列:会被存储在磁盘上,当消息代理重启时仍然存在,暂存队列:当前连接有效
        // exclusive:默认也是false,只能被当前创建的连接使用,而且当连接关闭后队列即被删除。此参考优先级高于durable
        // autoDelete:是否自动删除,当没有生产者或者消费者使用此队列,该队列会自动删除。
        //   return new Queue("TestDirectQueue", true, true, false);
 
        //一般设置一下队列的持久化就好,其余两个就是默认false
        return QueueBuilder.durable(QUEUE_NAME).build();
    }
 
    // 3、绑定 @Qualifier() 通过名称注入
    @Bean
    public Binding bindingQueueExchange(@Qualifier("bootQueue") Queue queue, @Qualifier("topicExchange") Exchange exchange) {
        // 绑定队列及交换机,并设置RoutingKey为boot.#,不指定参数
        return BindingBuilder.bind(queue).to(exchange).with("boot.#").noargs();
    }

    @Bean
    public DirectExchange lonelyDirectExchange() {
        return new DirectExchange("lonelyDirectExchange");
    }
}

3.3、运行测试 生产者发送消息

import com.orange.producer.config.DirectRabbitConfig;
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
class ProducerApplicationTests {

    @Autowired private RabbitTemplate rabbitTemplate;

    @Test
    void testSend() {

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

3.4、消费者配置类

import org.springframework.amqp.core.Message;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.context.annotation.Configuration;

@Configuration
public class RabbitMQListener {

    /**
     * 监听的队列名称 boot_queue
     * @param message
     */
    @RabbitListener(queues = "boot_queue")
    public void ListenerQueue(Message message) {
        System.out.println(new String(message.getBody()));
    }
}

3.5、运行消费者main方法测试

boot mq hello~~~