本文是RabbitMQ与Spring boot集成示例,实现了三种Exchange类型:
- DefaultExchange
- FanoutExchange
- TopicExchange
(还有一种HeaderExchange,没有写示例)
关于Exchange类型介绍,戳:https://www.jianshu.com/p/d9561f13e28b
文章内容:
1. 环境
1.1 RabbitMQ用的是 3.6.1版本:
MacBook-Pro% brew install rabbitmq
Warning: rabbitmq-3.6.1 already installed
1.2 新建virtual host(不是必须):
为了不受别的vhost影响,特地新建了一个Virtual host。
如果在测试中发现遇到connect refued问题,记得给新建的vhost赋上user权限。
1.3 Spring boot用的是2.5.7版本
1.4 test用的是spring-boot-starter-test以及junit-juspier:5.7.2
1.5 引入依赖:
org.springframework.boot
spring-boot-starter-amqp
1.6 新建yaml:
spring:
rabbitmq:
username: guest
password: guest
port: 5672
host: localhost
virtual-host: spring-boot-test
2. Direct Exchange
- a. 首先是定义direct queue bean, durable=false,表示当broker重启时,这个queue就会被删除。注:我们application重启并不会影响queue的删除。
- b. 其次定义一个exchange,类名是DirectExchange,这个类是Spring的org.springframework.amqp.core包里的。
- c. 绑定是交换器和消息队列之间的关系,告诉交换器如何路有消息。
// 绑定命令的伪代码
Queue.BindTO WHERE
在Spring中可以使用BindingBuilder将exchange按routingKey的规则和queue进行绑定。
- d. 消息的消费端,使用注解@RabbitListener来监听queueName=direct.queue的队列,支持同时监听多个队列。
@Configuration
public class DirectExchangeConfig {
@Bean
public Queue directqueue() {
return new Queue("direct.queue", false);
}
@Bean
public DirectExchange directExchange() {
return new DirectExchange("direct.exchange");
}
@Bean
public Binding directBinding(Queue directqueue, DirectExchange directExchange) {
return BindingBuilder.bind(directqueue).to(directExchange).with("direct-routing-key");
}
@RabbitListener(queues = "direct.queue")
public void listen(String in) {
System.out.println("Direct Message Listener: " + in);
}
}
消息发送:使用RabbitTemplate进行发送:
@SpringBootTest
public class ProducerServiceTest {
@Autowired
private RabbitTemplate rabbitTemplate;
@Test
public void sendMessageToDirectExchange() {
rabbitTemplate.convertAndSend("direct.exchange", "direct-routing-key", "hello, i am direct message!");
}
}
3. FanoutExchange
Set fanout exchange,绑定两个queue。利用两个@RabbitListener分别监听这两个queue。
Spring支持使用Declarables对象来一次性的绑定多条:
@Configuration
public class FanoutExchangeConfig {
@Bean
public Declarables fanoutBindings() {
Queue fanoutQueue1 = new Queue("fanout.queue1", true);
Queue fanoutQueue2 = new Queue("fanout.queue2", true);
FanoutExchange fanoutExchange = new FanoutExchange("fanout.exchange");
return new Declarables(
fanoutQueue1,
fanoutQueue2,
fanoutExchange,
bind(fanoutQueue1).to(fanoutExchange),
bind(fanoutQueue2).to(fanoutExchange));
}
@RabbitListener(queues = {"fanout.queue1"})
public void receiveMessageFromFanout1(String message) {
System.out.println("Received fanout 1 message: " + message);
}
@RabbitListener(queues = {"fanout.queue2"})
public void receiveMessageFromFanout2(String message) {
System.out.println("Received fanout 2 message: " + message);
}
}
发送消息:
public void sendMessageToFanoutExchange() {
rabbitTemplate.convertAndSend("fanout.exchange", "", "hello, i am fanout message!");
}
fanout模式下,convertAndSend方法将忽略routingKey。即如果在发送的时候set了routingKey=abc,其实在fanout模式下也没有关系,绑定的queue依然能收到消息。
也就是说:在发送的时候把routingKey改成abc也不会影响消息的发送:
public void sendMessageToFanoutExchange() {
rabbitTemplate.convertAndSend("fanout.exchange", "abc", "hello, i am fanout message!");
}
4. Topic Exchange
@RabbitListener支持同时监听多个queue:
@Configuration
public class TopicExchangeConfig {
@Bean
public Declarables topicBindings() {
Queue topicQueue1 = new Queue("topic.queue1", true);
Queue topicQueue2 = new Queue("topic.queue2", true);
TopicExchange topicExchange = new TopicExchange("topic.exchange");
return new Declarables(
topicQueue1,
topicQueue2,
topicExchange,
BindingBuilder
.bind(topicQueue1)
.to(topicExchange).with("*.important.*"),
BindingBuilder
.bind(topicQueue1)
.to(topicExchange).with("#.error"),
BindingBuilder
.bind(topicQueue2)
.to(topicExchange).with("#.error"));
}
@RabbitListener(queues = {"topic.queue1", "topic.queue2"})
public void receiveMessageFromTopic(String message) {
System.out.println("Received topic message: " + message);
}
}
发送消息:
public void sendMessageToTopicExchange() {
rabbitTemplate.convertAndSend("topic.exchange", "a.important.b", "important message!");
rabbitTemplate.convertAndSend("topic.exchange", "a.error", "error message!");
}
run程序后,控制台打印:
Received topic message: important message!
Received topic message: error message!
Received topic message: error message!
按上述配置,使用routingKey=a.important.b发送消息,只有topic.queue1能收到。
使用routingKey=a.error发送消息,topic.queue1和topic.queue2都能收到。
这就是为什么会打印出三条消息的原因。
参考
baeldung - Messaging with Spring AMQP
baeldung - RabbitMQ Message Dispatching with Spring AMQP