https://blog.csdn.net/wz6178/article/details/92843468
springboot整合rabbitmq
RabbitMQ提供了6种消息模型,但是第6种其实是RPC,并不是MQ,因此不予学习。那么也就剩下5种。
1.基本消息模型:生产者–>队列–>一个消费者
2.work消息模型:生产者–>队列–>多个消费者共同消费
3.订阅模型-Fanout:广播,将消息交给所有绑定到交换机的队列,每个消费者都可以收到同一条消息
4.订阅模型-Direct:定向,把消息交给符合指定 rotingKey 的队列(路由模式)
5.订阅模型-Topic:通配符,把消息交给符合routing pattern(主题模式) 的队列
(3、4、5这三种都属于订阅模型,只不过进行路由的方式不同。)
最终pox.xml依赖
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-amqp</artifactId>
<version>1.5.2.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-test</artifactId>
</dependency><dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-test</artifactId>
<version>5.1.7.RELEASE</version>
<scope>compile</scope>
</dependency>
一个生产者对应一个消费者
生产者
/*简单队列(模式)*/
@Test
public void contextLoads(){
String msg = "这是一个简单队列模式";
amqpTemplate.convertAndSend("spring.simple.queue", msg );
}
消费者
@Component
public class SimpleListener {
// 通过注解自动创建 spring.simple.queue 队列
@RabbitListener(queuesToDeclare = @Queue("spring.simple.queue"))
public void listen(String msg) {
System.out.println("简单队列 接收到消息:" + msg);
}
}
一个生产者对应多个消费者
生产者
/*work 模式*/
@Test
public void work() throws InterruptedException {
String msg = "这是一个work模式";
for (int i = 0; i < 10; i++) {
amqpTemplate.convertAndSend("spring.work.queue", msg + i);
}
Thread.sleep(5000);
}
- 消费者
@Component
public class WorkListener {
// 通过注解自动创建 spring.work.queue 队列
@RabbitListener(queuesToDeclare = @Queue("spring.work.queue"))
public void listen(String msg) {
System.out.println("work模式 接收到消息:" + msg);
}
// 创建两个队列共同消费
@RabbitListener(queuesToDeclare = @Queue("spring.work.queue"))
public void listen2(String msg) {
System.out.println("work模式二 接收到消息:" + msg);
}
}
订阅模型-Fanout也成为广播模式,流程如下
1.可以有多个消费者
2.每个消费者有自己的队列
3. 每个队列都要绑定到Exchange(交换机)
4. 生产者发送的消息,只能发送到交换机,交换机来决定要发给哪个队列,生产者无法决定。
5. 交换机把消息发送给绑定过的所有队列
6. 队列的消费者都能拿到消息。实现一条消息被多个消费者消费
生产者
/*订阅模型-Fanout*/
@Test
public void fanout() throws InterruptedException {
String msg = "订阅模式";
for (int i = 0; i < 10; i++) {
// 这里注意细节,第二个参数需要写,否则第一个参数就变成routingKey了
amqpTemplate.convertAndSend("spring.fanout.exchange", "", msg + i);
}
Thread.sleep(5000);
}
消费者
@Component
public class FanoutListener {
@RabbitListener(bindings = @QueueBinding(
value = @Queue(value = "spring.fanout.queue", durable = "true"),
exchange = @Exchange(
value = "spring.fanout.exchange",
ignoreDeclarationExceptions = "true",
type = ExchangeTypes.FANOUT
)
))
public void listen(String msg) {
System.out.println("订阅模式1 接收到消息:" + msg);
}
// 队列2(第二个人),同样能接收到消息
@RabbitListener(bindings = @QueueBinding(
value = @Queue(value = "spring.fanout2.queue", durable = "true"),
exchange = @Exchange(
value = "spring.fanout.exchange",
ignoreDeclarationExceptions = "true",
type = ExchangeTypes.FANOUT
)
))
public void listen2(String msg) {
System.out.println("订阅模式2 接收到消息:" + msg);
}
}
在Fanout模式中,一条消息,会被所有订阅的队列都消费。但是,在某些场景下,我们希望不同的消息被不同的队列消费。这时就要用到Direct类型的Exchange。给特定的消费者消费
在Direct模型下:
1.队列与交换机的绑定,不能是任意绑定了,而是要指定一个RoutingKey(路由key)
2.消息的发送方在 向 Exchange发送消息时,也必须指定消息的 RoutingKey。
3.Exchange不再把消息交给每一个绑定的队列,而是根据消息的Routing Key进行判断,只有队列的 Routingkey与消息的 Routing key完全一致,才会接收到消息
生产者
/*订阅模型-Direct (路由模式)*/
@Test
public void direct() throws InterruptedException {
String msg = "路由模式";
for (int i = 0; i < 10; i++) {
amqpTemplate.convertAndSend("spring.direct.exchange", "direct", msg + i);
}
Thread.sleep(5000);
}
消费者
@Component
public class DirectListener {
@RabbitListener(bindings = @QueueBinding(
value = @Queue(value = "spring.direct.queue", durable = "true"),
exchange = @Exchange(
value = "spring.direct.exchange",
ignoreDeclarationExceptions = "true"
),
key = {"direct"}
))
public void listen(String msg) {
System.out.println("路由模式1 接收到消息:" + msg);
}
// 队列2(第二个人),key值不同,接收不到消息
@RabbitListener(bindings = @QueueBinding(
value = @Queue(value = "spring.direct2.queue", durable = "true"),
exchange = @Exchange(
value = "spring.direct.exchange",
ignoreDeclarationExceptions = "true"
),
key = {"direct-test"}
))
public void listen2(String msg) {
System.out.println("路由模式2 接收到消息:" + msg);
}
}
Topic类型的Exchange与Direct相比,都是可以根据RoutingKey把消息路由到不同的队列。只不过Topic类型Exchange可以让队列在绑定Routing key 的时候使用通配符!
Routingkey 一般都是有一个或多个单词组成,多个单词之间以”.”分割,例如: user.insert
通配符规则 | 举例 |
---|---|
#:匹配一个或多个词 | person.#:能够匹配person.insert.save 或者 person.insert |
*:匹配不多不少恰好1个词 | person.*:只能匹配person.insert |
生产者
/* 订阅模型-Topic (主题模式)*/
@Test
public void topic() throws InterruptedException {
amqpTemplate.convertAndSend("spring.topic.exchange", "person.insert", "增加人员");
amqpTemplate.convertAndSend("spring.topic.exchange", "person.delete", "删除人员");
amqpTemplate.convertAndSend("spring.topic.exchange", "money.insert", "加钱");
amqpTemplate.convertAndSend("spring.topic.exchange", "money.delete", "减钱");
Thread.sleep(5000);
}
消费者
@Component
public class TopicListener {
@RabbitListener(bindings = @QueueBinding(
value = @Queue(value = "spring.topic.queue", durable = "true"),
exchange = @Exchange(
value = "spring.topic.exchange",
ignoreDeclarationExceptions = "true",
type = ExchangeTypes.TOPIC
),
key = {"person.*"}
))
public void listen(String msg) {
System.out.println("person 接收到消息:" + msg);
}
// 通配规则不同,接收不到消息
@RabbitListener(bindings = @QueueBinding(
value = @Queue(value = "spring.topic.queue", durable = "true"),
exchange = @Exchange(
value = "spring.topic.exchange",
ignoreDeclarationExceptions = "true",
type = ExchangeTypes.TOPIC
),
key = {"money.*"}
))
public void listen2(String msg) {
System.out.println("money Student 接收到消息:" + msg);
}
}