欢迎来到"雪碧聊技术"CSDN博客!
在这里,您将踏入一个专注于Java开发技术的知识殿堂。无论您是Java编程的初学者,还是具有一定经验的开发者,相信我的博客都能为您提供宝贵的学习资源和实用技巧。作为您的技术向导,我将不断探索Java的深邃世界,分享最新的技术动态、实战经验以及项目心得。
让我们一同在Java的广阔天地中遨游,携手提升技术能力,共创美好未来!感谢您的关注与支持,期待在"雪碧聊技术"与您共同成长!
目录
③结论:fanout交换机,是广播的,即:与fanout交换机绑定的所有队列,都会收到交换机中的消息。
5、注意:如果一个队列要绑定多个RoutingKey,需要绑定多次
一、Fanout交换机(广播转发)
1、什么是Fanout交换机?
Fanout 交换机(Exchange)会将接收到的消息广播到每一个跟其绑定的queue,所以也叫广播模式。
2、案例
①案例介绍

②具体操作
- 第一步:在RabbitMQ控制台,创建两个队列fanout.queue1和fanout.queue2

- 第二步:添加一台Fanout类型的交换机,名叫"hmall.fanout"

- 第三步:给hmall.fanout交换机绑定刚才那两个队列

- 第四步:编写代码,给hmall.fanout交换机发送一条消息

- 第五步:编写监听代码,监听与hmall.fanout交换机绑定的两个队列
java
@Component //交给spring管理
public class MqListener {
@RabbitListener(queues = "fanout.queue1")
public void listenFanoutQueue1(String msg) throws InterruptedException {//消费者1
System.out.println("消费者1 收到了 fanout.queue1的消息... :【" + msg + "】");
}
@RabbitListener(queues = "fanout.queue2")
public void listenFanoutQueue2(String msg) throws InterruptedException {//消费者2
System.err.println("消费者2 收到了 fanout.queue2的消息... :【" + msg + "】");
}
}
- 第六步:运行项目,查看结果
java
消费者1 收到了 fanout.queue1的消息... :【hello,everyone!】
消费者2 收到了 fanout.queue2的消息... :【hello,everyone!】
③结论:fanout交换机,是广播的,即:与fanout交换机绑定的所有队列,都会收到交换机中的消息。
3、总结

二、Direct交换机(定向转发)
1、什么是Direct交换机?
Direct交换机会根据一定规则,将收到的消息,转发给对应的队列。
2、如何确定Direct交换机给哪个队列转发消息?
①publisher(生产者)给Direct交换机发送消息的时候,会给该条消息添加一个RoutingKey。
②给Direct交换机绑定队列时,也要指定每个队列的RoutingKey。
这么一来,当Direct交换机收到publisher(生产者)的消息后,会读取该条消息的RoutingKey,找到拥有该RoutingKey的那个队列,并给该队列转发消息。
举例

3、案例
①案例介绍

②具体操作
- 第一步:创建两个队列direct.queue1和direct.queue2

- 第二步:创建一个Direct类型的交换机

- 第三步:给hmall.direct交换机绑定那两个队列,并指定其RoutingKey


结果如下:

- 第四步:编写代码,发送一条消息给hmall.direct交换机

- 第五步:编写监听direct.queue1和direct.queue2两个队列的代码
java
@Component //交给spring管理
public class MqListener {
@RabbitListener(queues = "direct.queue1")
public void listenDirectQueue1(String msg) throws InterruptedException {//消费者1
System.out.println("消费者1 收到了 direct.queue1的消息... :【" + msg + "】");
}
@RabbitListener(queues = "direct.queue2")
public void listenDirectQueue2(String msg) throws InterruptedException {//消费者2
System.out.println("消费者2 收到了 direct.queue2的消息... :【" + msg + "】");
}
}
- 第六步:运行项目,查看结果
java
消费者2 收到了 direct.queue2的消息... :【红色警报,由于日本排放核污水,惊险哥斯拉!】
可见,由于direct.queue2的RoutingKey是"red",因此能收到此条消息(因为此条消息的RoutingKey也是"red")。
4、总结
生产者给Direct类型的交换机发送消息时,要指定该消息的RoutingKey;
给Direct类型的交换机绑定队列时,也要指定队列的RoutingKey;
Direct类型的交换机在转发消息时,只转发给RoutingKey一致的队列。
5、注意:如果一个队列要绑定多个RoutingKey,需要绑定多次
举例:

这样一来,这两个队列绑定的交换机,会把RoutingKey为red的消息,转发给direct.queue1和direct.queue2;会把RoutingKey为blue的消息,转发给direct.queue1;会把RoutingKey为yellow的消息,转发给direct.queue2。
三、Topic交换机(定向转发)
1、什么是Topic交换机?
Topic交换机和Direct交换机一样,都是通过routingKey来指定交换机要将消息转发给哪个队列。
2、Topic交换机和Direct交换机的区别
Topic交换机在绑定队列时,routingKey可以是多个单词的列表,并以"."分割。
可以使用通配符来表示每个单词。
规则①:#代指任意个单词
规则②:*代指1个单词
举例:

3、案例
①案例介绍

②具体操作
- 第一步:创建两个队列,topic.queue1和topic.queue2

- 第二步:创建Topic类型的交换机,叫hmall.topic

-
第三步:给hmall.topic交换机,绑定那两个队列,并指定每个队列的routingKey

- 第四步:编写代码,向hmall.topic交换机中发送一条消息。

- 第五步:编写两个消费者方法,分别监听topic.queue1和topic.queue2
java
@Component //交给spring管理
public class MqListener {
@RabbitListener(queues = "topic.queue1")
public void listenTopicQueue1(String msg) throws InterruptedException {//消费者1
System.err.println("消费者1 收到了 topic.queue1的消息... :【" + msg + "】");
}
@RabbitListener(queues = "topic.queue2")
public void listenTopicQueue2(String msg) throws InterruptedException {//消费者2
System.err.println("消费者2 收到了 topic.queue2的消息... :【" + msg + "】");
}
}
- 第六步:启动项目,测试结果
java
消费者2 收到了 topic.queue2的消息... :【红色警报,由于日本排放核污水,惊险哥斯拉!】
- 第七步:分析hmall.topic交换机转发消息的过程

4、总结

结语
以上就是RabbitMQ中的Fanout交换机、Direct交换机、Topic交换机的全部内容,想了解更多关于RabbitMQ的知识,请关注本博主~~