0


Spring Boot 启动时自动配置 RabbitMQ 交换机、队列和绑定关系

在使用 Spring Boot 开发消息队列应用时,我们经常需要在应用启动时自动创建 RabbitMQ 的交换机、队列和绑定关系。本文将介绍如何通过 Spring Boot 的启动后执行方法来实现这一功能,并提供相应的演示代码和依赖配置。

一、添加依赖

为了在 Spring Boot 项目中使用 RabbitMQ,首先需要添加 RabbitMQ 的依赖。在

pom.xml

文件中添加以下依赖:

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

这个依赖引入了 Spring AMQP,简化了与 RabbitMQ 的集成。

二、配置 RabbitMQ

在 Spring Boot 的配置文件

application.properties

application.yml

中,配置 RabbitMQ 的连接信息。以下是

application.yml

的配置示例:

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

三、演示代码

以下是使用

@Configuration 注解、
@PostConstruct

注解、

CommandLineRunner

接口和

ApplicationListener

接口来在 Spring Boot 启动时创建 RabbitMQ 交换机、队列和绑定关系的代码示例。

使用 @Configuration注解

importorg.springframework.amqp.core.Binding;importorg.springframework.amqp.core.BindingBuilder;importorg.springframework.amqp.core.Queue;importorg.springframework.amqp.core.TopicExchange;importorg.springframework.context.annotation.Bean;importorg.springframework.context.annotation.Configuration;@ConfigurationpublicclassRabbitMQConfig{@BeanpublicQueuemyQueue(){returnnewQueue("myQueue",true);}@BeanpublicTopicExchangemyExchange(){returnnewTopicExchange("myExchange");}@BeanpublicBindingbinding(){returnBindingBuilder.bind(myQueue()).to(myExchange()).with("routingKey");}}

使用 @PostConstruct 注解

importorg.springframework.amqp.core.Binding;importorg.springframework.amqp.core.BindingBuilder;importorg.springframework.amqp.core.Queue;importorg.springframework.amqp.core.TopicExchange;importorg.springframework.amqp.rabbit.connection.ConnectionFactory;importorg.springframework.amqp.rabbit.core.RabbitAdmin;importorg.springframework.beans.factory.annotation.Autowired;importorg.springframework.stereotype.Component;importjavax.annotation.PostConstruct;@ComponentpublicclassRabbitMQConfig{@AutowiredprivateConnectionFactory connectionFactory;@PostConstructpublicvoidinit(){RabbitAdmin admin =newRabbitAdmin(connectionFactory);// 定义交换机
        admin.declareExchange(newTopicExchange("myExchange"));// 定义队列
        admin.declareQueue(newQueue("myQueue"));// 定义绑定关系
        admin.declareBinding(BindingBuilder.bind(newQueue("myQueue")).to(newTopicExchange("myExchange")).with("routingKey"));}}

实现 CommandLineRunner 接口

importorg.springframework.amqp.core.BindingBuilder;importorg.springframework.amqp.core.Queue;importorg.springframework.amqp.core.TopicExchange;importorg.springframework.amqp.rabbit.connection.ConnectionFactory;importorg.springframework.amqp.rabbit.core.RabbitAdmin;importorg.springframework.beans.factory.annotation.Autowired;importorg.springframework.boot.CommandLineRunner;importorg.springframework.stereotype.Component;@ComponentpublicclassRabbitMQInitializerimplementsCommandLineRunner{@AutowiredprivateConnectionFactory connectionFactory;@Overridepublicvoidrun(String... args)throwsException{RabbitAdmin rabbitAdmin =newRabbitAdmin(connectionFactory);// 定义交换机
        rabbitAdmin.declareExchange(newTopicExchange("myExchange"));// 定义队列
        rabbitAdmin.declareQueue(newQueue("myQueue"));// 定义绑定关系
        rabbitAdmin.declareBinding(BindingBuilder.bind(newQueue("myQueue")).to(newTopicExchange("myExchange")).with("routingKey"));}}

实现 ApplicationListener 接口

importorg.springframework.amqp.core.BindingBuilder;importorg.springframework.amqp.core.Queue;importorg.springframework.amqp.core.TopicExchange;importorg.springframework.amqp.rabbit.connection.ConnectionFactory;importorg.springframework.amqp.rabbit.core.RabbitAdmin;importorg.springframework.beans.factory.annotation.Autowired;importorg.springframework.context.ApplicationListener;importorg.springframework.context.event.ContextRefreshedEvent;importorg.springframework.stereotype.Component;@ComponentpublicclassRabbitMQApplicationListenerimplementsApplicationListener<ContextRefreshedEvent>{@AutowiredprivateConnectionFactory connectionFactory;@OverridepublicvoidonApplicationEvent(ContextRefreshedEvent event){RabbitAdmin rabbitAdmin =newRabbitAdmin(connectionFactory);// 定义交换机
        rabbitAdmin.declareExchange(newTopicExchange("myExchange"));// 定义队列
        rabbitAdmin.declareQueue(newQueue("myQueue"));// 定义绑定关系
        rabbitAdmin.declareBinding(BindingBuilder.bind(newQueue("myQueue")).to(newTopicExchange("myExchange")).with("routingKey"));}}

以上方法都可以在 Spring Boot 应用启动后自动配置 RabbitMQ 的交换机、队列和绑定关系,你可以根据实际需求选择合适的方法。希望这篇文章能帮助你更好地理解和应用 Spring Boot 与 RabbitMQ 的集成。

标签: spring boot rabbitmq

本文转载自: https://blog.csdn.net/qq_29752857/article/details/143734167
版权归原作者 好奇的菜鸟 所有, 如有侵权,请联系我们删除。

“Spring Boot 启动时自动配置 RabbitMQ 交换机、队列和绑定关系”的评论:

还没有评论