在使用 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));
}
}