在之前我们都是基于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 + "】");
}
}