RabbitMQ提供了6种消息模型,但是第6种其实是RPC,并不是MQ,因此不予学习。那么也就剩下5种。
(3、4、5这三种都属于订阅模型,只不过进行路由的方式不同。)
一个生产者对应一个消费者
生产者
/*简单队列(模式)*/
@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也成为广播模式,流程如下
生产者
/*订阅模型-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模型下:
生产者
/*订阅模型-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);
}
}
opic类型的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);
}
}