SpringBoot使用RabbitMQ自动创建Exchange和Queue

背景

小项目,使用RabbitMQ作为消息队列,发布到不同的新环境时,由于新搭建的MQ中不存在Exchange和Queue,就会出错,还得手动去创建,比较麻烦,于是想在代码中将这些定义好后,自动控制MQ去创建。

原理与步骤

  1. 第一步定义RabbitAdmin Bean
  2. 第二步定义交换机 Bean
  3. 第三步定义Queue Bean
  4. 第四步定义Binding Bean, 这一步最关键,即将Queue与交换机

完整代码如下,定义如下代码后,即便使用新的MQ,启动项目也会自动创建,但是请注意 virtual-host 是无法被自动创建的(我目前的研究是这样,有自动创建的办法请留言告诉我,谢谢),因此如果不使用默认的virtual-host,请在MQ控制台中去创建。

package com.app.config;

import org.springframework.amqp.core.*;
import org.springframework.amqp.rabbit.connection.ConnectionFactory;
import org.springframework.amqp.rabbit.core.RabbitAdmin;
import org.springframework.amqp.support.converter.Jackson2JsonMessageConverter;
import org.springframework.amqp.support.converter.MessageConverter;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

/**
 * @author Administrator
 * @date 2023/8/4 16:25
 * @description
 */
@Configuration
public class RabbitConfig {

    @Autowired
    private ConnectionFactory connectionFactory;

    @Bean
    public RabbitAdmin rabbitAdmin() {
        RabbitAdmin rabbitAdmin = new RabbitAdmin(connectionFactory);
        rabbitAdmin.setAutoStartup(true);
        return rabbitAdmin;
    }

    @Bean("addOrderExchange")
    public DirectExchange addOrderExchange() {
        return ExchangeBuilder
                .directExchange("addOrderExchange")
                .durable(true)
                .build();
    }

    @Bean("defaultExchange")
    public DirectExchange defaultExchange() {
        return ExchangeBuilder
                .directExchange("")
                .durable(true)
                .build();
    }

    @Bean("addEvtDayInvest")
    public Queue addEvtDayInvest() {
        Queue queue = QueueBuilder.durable("addEvtDayInvest").build();
        rabbitAdmin().declareQueue(queue);
        return queue;
    }


    @Bean("addEvtDayInvestBind")
    public Binding addEvtDayInvestBind() {
        return BindingBuilder.bind(addEvtDayInvest()).to(addOrderExchange()).with("");
    }

    @Bean("addMemberData")
    public Queue addMemberData() {
        Queue queue = QueueBuilder.durable("addMemberData").build();
        rabbitAdmin().declareQueue(queue);
        return queue;
    }


    @Bean("addMemberDataBind")
    public Binding addMemberDataBind() {
        return BindingBuilder.bind(addMemberData()).to(addOrderExchange()).with("");
    }

    @Bean("addRepaymentPlant")
    public Queue addRepaymentPlant() {
        Queue queue = QueueBuilder.durable("addRepaymentPlant").build();
        rabbitAdmin().declareQueue(queue);
        return queue;
    }


    @Bean("addRepaymentPlantBind")
    public Binding addRepaymentPlantBind() {
        return BindingBuilder.bind(addRepaymentPlant()).to(addOrderExchange()).with("");
    }


    @Bean("imQueue")
    public Queue imQueue() {
        Queue queue = QueueBuilder.durable("imQueue").build();
        rabbitAdmin().declareQueue(queue);
        return queue;
    }


    @Bean("imQueueBind")
    public Binding imQueueBind() {
        return BindingBuilder.bind(imQueue()).to(defaultExchange()).withQueueName();
    }


    @Bean("settleInterestQueue")
    public Queue settleInterestQueue() {
        Queue queue = QueueBuilder.durable("settleInterestQueue").build();
        rabbitAdmin().declareQueue(queue);
        return queue;
    }


    @Bean("settleInterestQueueBind")
    public Binding settleInterestQueueBind() {
        return BindingBuilder.bind(settleInterestQueue()).to(defaultExchange()).withQueueName();
    }


    @Bean("registerQueue")
    public Queue registerQueue() {
        Queue queue = QueueBuilder.durable("registerQueue").build();
        rabbitAdmin().declareQueue(queue);
        return queue;
    }


    @Bean("registerQueueBind")
    public Binding registerQueueBind() {
        return BindingBuilder.bind(registerQueue()).to(defaultExchange()).withQueueName();
    }

    @Bean("memberLoginLogQueue")
    public Queue memberLoginLogQueue() {
        Queue queue = QueueBuilder.durable("memberLoginLogQueue").build();
        rabbitAdmin().declareQueue(queue);
        return queue;
    }


    @Bean("memberLoginLogQueueBind")
    public Binding memberLoginLogQueueBind() {
        return BindingBuilder.bind(memberLoginLogQueue()).to(defaultExchange()).withQueueName();
    }


    @Bean("addParentDataQueue")
    public Queue addParentDataQueue() {
        Queue queue = QueueBuilder.durable("addParentDataQueue").build();
        rabbitAdmin().declareQueue(queue);
        return queue;
    }


    @Bean("addParentDataQueueBind")
    public Binding addParentDataQueueBind() {
        return BindingBuilder.bind(addParentDataQueue()).to(defaultExchange()).withQueueName();
    }


    @Bean("addOrderQueue")
    public Queue addOrderQueue() {
        Queue queue = QueueBuilder.durable("addOrderQueue").build();
        rabbitAdmin().declareQueue(queue);
        return queue;
    }


    @Bean("addOrderQueueBind")
    public Binding addOrderQueueBind() {
        return BindingBuilder.bind(addOrderQueue()).to(defaultExchange()).withQueueName();
    }
    @Bean("memberPerformanceQueue")
    public Queue memberPerformanceQueue() {
        Queue queue =  QueueBuilder.durable("memberPerformanceQueue").build();
        rabbitAdmin().declareQueue(queue);
        return queue;
    }


    @Bean("memberPerformanceQueueBind")
    public Binding memberPerformanceQueueBind() {
        return BindingBuilder.bind(memberPerformanceQueue()).to(defaultExchange()).withQueueName();
    }


    @Bean("computeExpectedIncomeQueue")
    public Queue computeExpectedIncomeQueue() {
        Queue queue = QueueBuilder.durable("computeExpectedIncomeQueue").build();
        rabbitAdmin().declareQueue(queue);
        return queue;
    }


    @Bean("computeExpectedIncomeQueueBind")
    public Binding computeExpectedIncomeQueueBind() {
        return BindingBuilder.bind(computeExpectedIncomeQueue()).to(defaultExchange()).withQueueName();
    }

    /**
     * 自定义 json 格式发送消息
     *
     * @return
     */
    @Bean
    public MessageConverter messageConverter() {
        return new Jackson2JsonMessageConverter();
    }

}

你可能感兴趣的:(java-rabbitmq,spring,boot,rabbitmq)