Spring Boot 集成 RabbitMQ 自动创建交换机、队列并绑定

在使用 Spring Boot 开发消息队列应用时,我们经常需要在应用启动时自动创建 RabbitMQ 的交换机、队列和绑定关系。 

一、添加依赖

在 Spring Boot 项目中使用 RabbitMQ,首先需要添加 RabbitMQ 的依赖。在 pom.xml 文件中添加以下依赖

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

二、配置 RabbitMQ

在 Spring Boot 的配置文件 application.yml 中,配置 RabbitMQ 的连接信息

spring:
  rabbitmq:
    host: 127.0.0.1
    port: 5672
    username: guest
    password: guest

 三、代码

使用 @Configuration注解

import org.springframework.amqp.core.Binding;
import org.springframework.amqp.core.BindingBuilder;
import org.springframework.amqp.core.Queue;
import org.springframework.amqp.core.TopicExchange;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

@Configuration
public class RabbitMQConfig {

    @Bean
    public Queue myQueue() {
        return new Queue("myQueue", true);
    }

    @Bean
    public TopicExchange myExchange() {
        return new TopicExchange("myExchange");
    }

    @Bean
    public Binding binding() {
        return BindingBuilder.bind(myQueue()).to(myExchange()).with("routingKey");
    }
}

使用 @PostConstruct 注解

import org.springframework.amqp.core.Binding;
import org.springframework.amqp.core.BindingBuilder;
import org.springframework.amqp.core.Queue;
import org.springframework.amqp.core.TopicExchange;
import org.springframework.amqp.rabbit.connection.ConnectionFactory;
import org.springframework.amqp.rabbit.core.RabbitAdmin;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import javax.annotation.PostConstruct;

@Component
public class RabbitMQConfig {

    @Autowired
    private ConnectionFactory connectionFactory;

    @PostConstruct
    public void init() {
        RabbitAdmin admin = new RabbitAdmin(connectionFactory);
        // 定义交换机
        DirectExchange directExchange = new DirectExchange("directExchange");
        admin.declareExchange(directExchange);
        // 定义队列
        Queue myQueue = new Queue("myQueue");
        admin.declareQueue(myQueue);
        // 定义绑定关系
        admin.declareBinding(BindingBuilder.bind(myQueue).to(directExchange).with("routingKey"));
    }

}

 实现 ApplicationListener 接口


import org.springframework.amqp.core.BindingBuilder;
import org.springframework.amqp.core.Queue;
import org.springframework.amqp.core.TopicExchange;
import org.springframework.amqp.rabbit.connection.ConnectionFactory;
import org.springframework.amqp.rabbit.core.RabbitAdmin;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationListener;
import org.springframework.context.event.ContextRefreshedEvent;
import org.springframework.stereotype.Component;

@Component
public class RabbitMQApplicationListener implements ApplicationListener<ContextRefreshedEvent> {


    @Autowired
    private ConnectionFactory connectionFactory;


    @Override
    public void onApplicationEvent(ContextRefreshedEvent event) {
        RabbitAdmin admin = new RabbitAdmin(connectionFactory);
        // 定义交换机 广播模式
        FanoutExchange fanoutExchange = new FanoutExchange("fanoutExchange");
        admin.declareExchange(fanoutExchange);
        // 定义队列
        Queue myQueue1 = new Queue("myQueue1");
        admin.declareQueue(myQueue1);
        // 定义队列
        Queue myQueue2 = new Queue("myQueue2");
        admin.declareQueue(myQueue2);
        // 定义绑定关系
        admin.declareBinding(BindingBuilder.bind(myQueue1).to(fanoutExchange));
        admin.declareBinding(BindingBuilder.bind(myQueue2).to(fanoutExchange));
    }
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值