springboot声明(创建)RabbitMQ交换机和队列

在之前我们都是基于RabbitMQ控制台来创建队列、交换机。但是在实际开发时,队列和交换机是程序员定义的,将来项目上线,又要交给运维去创建。那么程序员就需要把程序中运行的所有队列和交换机都写下来,交给运维。在这个过程中是很容易出现错误的。

因此推荐的做法是由程序启动时检查队列和交换机是否存在,如果不存在自动创建。

1. 使用@Bean

java 复制代码
@Configuration
public class FanoutConfig {

    /**
     * 创建一个FanoutExchange实例,用于将消息广播到多个队列。
     * @return FanoutExchange实例
     */
    @Bean
    public FanoutExchange fanoutExchange() {
        return new FanoutExchange("springboot.fanout");
    }

    /**
     * 创建一个队列实例,用于接收广播的消息。
     * @return 队列实例
     */
    @Bean
    public Queue fanoutQueue1() {
        return new Queue("springboot.fanout.queue1");
    }

    /**
     * 创建一个Binding实例,将队列与FanoutExchange绑定。
     * @param fanoutExchange FanoutExchange实例
     * @param fanoutQueue1 队列实例
     * @return Binding实例
     */
    @Bean
    public Binding bindingFanoutQueue1(FanoutExchange fanoutExchange, Queue fanoutQueue1) {
        return BindingBuilder.bind(fanoutQueue1).to(fanoutExchange);
    }

    /**
     * 创建一个队列实例,用于接收广播的消息。
     * @return 队列实例
     */
    @Bean
    public Queue fanoutQueue2() {
        return new Queue("springboot.fanout.queue2");
    }

    /**
     * 创建一个Binding实例,将队列与FanoutExchange绑定。
     * @param fanoutExchange FanoutExchange实例
     * @param fanoutQueue2 队列实例
     * @return Binding实例
     */
    @Bean
    public Binding bindingFanoutQueue2(FanoutExchange fanoutExchange, Queue fanoutQueue2) {
        return BindingBuilder.bind(fanoutQueue2).to(fanoutExchange);
    }
}
java 复制代码
@Configuration
public class DirectConfig {
    /**
     * 创建一个直接交换机
     *
     * @return DirectExchange实例
     */
    @Bean
    public DirectExchange directExchange() {
        return ExchangeBuilder.directExchange("springboot.direct").build();
    }

    /**
     * 创建一个队列
     *
     * @return Queue实例
     */
    @Bean
    public Queue directQueue1() {
        return new Queue("springboot.direct.queue1");
    }

    /**
     * 创建一个绑定队列到交换机的Binding
     *
     * @param directQueue1   队列
     * @param directExchange 交换机
     * @return Binding实例
     */
    @Bean
    public Binding bindingQueue1WithRed(Queue directQueue1, DirectExchange directExchange) {
        return BindingBuilder.bind(directQueue1).to(directExchange).with("red");
    }

    /**
     * 创建一个绑定队列到交换机的Binding
     *
     * @param directQueue1   队列
     * @param directExchange 交换机
     * @return Binding实例
     */
    @Bean
    public Binding bindingQueue1WithBlue(Queue directQueue1, DirectExchange directExchange) {
        return BindingBuilder.bind(directQueue1).to(directExchange).with("blue");
    }

    /**
     * 创建一个队列
     *
     * @return Queue实例
     */
    @Bean
    public Queue directQueue2() {
        return new Queue("springboot.direct.queue2");
    }

    /**
     * 创建一个绑定队列到交换机的Binding
     *
     * @param directQueue2   队列
     * @param directExchange 交换机
     * @return Binding实例
     */
    @Bean
    public Binding bindingQueue2WithRed(Queue directQueue2, DirectExchange directExchange) {
        return BindingBuilder.bind(directQueue2).to(directExchange).with("red");
    }

    /**
     * 创建一个绑定队列到交换机的Binding
     *
     * @param directQueue2   队列
     * @param directExchange 交换机
     * @return Binding实例
     */
    @Bean
    public Binding bindingQueue2WithYellow(Queue directQueue2, DirectExchange directExchange) {
        return BindingBuilder.bind(directQueue2).to(directExchange).with("yellow");
    }
}

2. 使用@RabbitListener

java 复制代码
@Component
public class DirectConsumer {
    @RabbitListener(bindings = @QueueBinding(
            value = @Queue(name = "springboot.direct.queue1"),
            exchange = @Exchange(name = "springboot.direct", type = ExchangeTypes.DIRECT),
            key = {"red", "blue"}
    ))
    public void listenDirectQueue1(String msg) {
        System.out.println("消费者1接收到direct.queue1的消息:【" + msg + "】");
    }

    @RabbitListener(bindings = @QueueBinding(
            value = @Queue(name = "springboot.direct.queue2"),
            exchange = @Exchange(name = "springboot.direct", type = ExchangeTypes.DIRECT),
            key = {"red", "yellow"}
    ))
    public void listenDirectQueue2(String msg) {
        System.out.println("消费者2接收到direct.queue2的消息:【" + msg + "】");
    }
}
java 复制代码
@Component
public class TopicConsumer {
    @RabbitListener(bindings = @QueueBinding(
            value = @Queue(name = "springboot.topic.queue1"),
            exchange = @Exchange(name = "springboot.topic", type = ExchangeTypes.TOPIC),
            key = "china.#"
    ))
    public void listenTopicQueue1(String msg) {
        System.out.println("消费者1接收到topic.queue1的消息:【" + msg + "】");
    }

    @RabbitListener(bindings = @QueueBinding(
            value = @Queue(name = "springboot.topic.queue2"),
            exchange = @Exchange(name = "springboot.topic", type = ExchangeTypes.TOPIC),
            key = "#.news"
    ))
    public void listenTopicQueue2(String msg) {
        System.out.println("消费者2接收到topic.queue2的消息:【" + msg + "】");
    }
}
相关推荐
野生风长5 分钟前
c++类和对象(this指针,重载operator,习题总结)
java·开发语言·c++
霸道流氓气质15 分钟前
基于 Spring 事务同步机制的事务后置动作收集器 Starter 实践
java·后端·spring
过期动态21 分钟前
【LeetCode 热题 100】找到字符串中所有字母异位词
java·数据结构·算法·leetcode·职场和发展·rabbitmq
刘小八1 小时前
Spring AI Tool Calling 生产化:参数校验、权限控制与超时隔离
java·人工智能·spring
奶糖 肥晨1 小时前
一次Spring Boot编译报错排查:三元运算符与包装类型的“隐形陷阱”
java·spring boot·后端
谢栋_1 小时前
设计模式从入门到精通之(七)责任链模式
java·设计模式·责任链模式
愚公移码1 小时前
蓝凌EKP18产品:核心执行流程
java·流程引擎
VortMall1 小时前
全维度打磨细节体验,赋能商城稳定有序运营|VortMall 微服务商城 v1.3.11 版本发布
java·微服务·云原生·架构·商城系统·开源商城·vortmall
Tirzano1 小时前
java 精简使用ffmpeg
java·开发语言·ffmpeg
唐青枫1 小时前
Java Jersey 实战指南:用 JAX-RS 注解写清晰的 REST API
java