当前位置:   article > 正文

SpringBoot使用RabbitMQ自动创建Exchange和Queue_rabbitmq自动创建队列

rabbitmq自动创建队列

背景

小项目,使用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();
    }

}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59
  • 60
  • 61
  • 62
  • 63
  • 64
  • 65
  • 66
  • 67
  • 68
  • 69
  • 70
  • 71
  • 72
  • 73
  • 74
  • 75
  • 76
  • 77
  • 78
  • 79
  • 80
  • 81
  • 82
  • 83
  • 84
  • 85
  • 86
  • 87
  • 88
  • 89
  • 90
  • 91
  • 92
  • 93
  • 94
  • 95
  • 96
  • 97
  • 98
  • 99
  • 100
  • 101
  • 102
  • 103
  • 104
  • 105
  • 106
  • 107
  • 108
  • 109
  • 110
  • 111
  • 112
  • 113
  • 114
  • 115
  • 116
  • 117
  • 118
  • 119
  • 120
  • 121
  • 122
  • 123
  • 124
  • 125
  • 126
  • 127
  • 128
  • 129
  • 130
  • 131
  • 132
  • 133
  • 134
  • 135
  • 136
  • 137
  • 138
  • 139
  • 140
  • 141
  • 142
  • 143
  • 144
  • 145
  • 146
  • 147
  • 148
  • 149
  • 150
  • 151
  • 152
  • 153
  • 154
  • 155
  • 156
  • 157
  • 158
  • 159
  • 160
  • 161
  • 162
  • 163
  • 164
  • 165
  • 166
  • 167
  • 168
  • 169
  • 170
  • 171
  • 172
  • 173
  • 174
  • 175
  • 176
  • 177
  • 178
  • 179
  • 180
  • 181
  • 182
  • 183
  • 184
  • 185
  • 186
  • 187
  • 188
  • 189
  • 190
  • 191
  • 192
  • 193
  • 194
  • 195
  • 196
  • 197
  • 198
  • 199
  • 200
  • 201
  • 202
  • 203
  • 204
声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/我家小花儿/article/detail/639662
推荐阅读
相关标签
  

闽ICP备14008679号