上期回顾:
目录
一. Exchange(交换机)的作用
在RabbitMQ中,生产者发送消息不会直接将消息投递到队列中,而是先将消息投递到交换机中,在由交换机转发到具体的队列,队列再将消息以推送或者拉取方式给消费者进行消费。
交换机原理图见:

二. Exchange(交换机)的类型
1.直连交换机:Direct Exchange
直连交换机是一种带路由功能的交换机,一个队列会和一个交换机绑定,除此之外再绑定一个routing_key,当消息被发送的时候,需要指定一个binding_key,这个消息被送达交换机的时候,就会被这个交换机送到指定的队列里面去。同样的一个binding_key也是支持应用到多个队列中的。
这样当一个交换机绑定多个队列,就会被送到对应的队列去处理。
直连交换机-单个绑定:
直连交换机-多个绑定:
2.主题交换机:Topic Exchange
直连交换机的缺点!
直连交换机的routing_key方案非常简单,如果我们希望一条消息发送给多个队列,那么这个交换机需要绑定上非常多的routing_key,
假设每个交换机上都绑定一堆的routing_key连接到各个队列上。那么消息的管理就会异常地困难。
所以RabbitMQ提供了一种主题交换机,发送到主题交换机上的消息需要携带指定规则的routing_key,
主题交换机会根据这个规则将数据发送到对应的(多个)队列上。
主题交换机的routing_key需要有一定的规则,交换机和队列的binding_key需要采用*.#.*.....的格式,每个部分用.分开,其中
*表示一个单词
#表示任意数量(零个或多个)单词。
示例:
队列Q1绑定键为 *.TT.*
队列Q2绑定键为TT.#
如果一条消息携带的路由键为 A.TT.B,那么队列Q1将会收到
如果一条消息携带的路由键为TT.AA.BB,那么队列Q2将会收到
3.扇形交换机:Fanout Exchange
扇形交换机是最基本的交换机类型,它所能做的事情非常简单———广播消息。
扇形交换机会把能接收到的消息全部发送给绑定在自己身上的队列。因为广播不需要“思考”,
所以扇形交换机处理消息的速度也是所有的交换机类型里面最快的。
这个交换机没有路由键概念,就算你绑了路由键也是无视的。
4.首部交换机:Headers exchange
5.默认交换机
实际上是一个由RabbitMQ预先声明好的名字为空字符串的直连交换机(direct exchange)。它有一个特殊的属性使得它对于
简单应用特别有用处:那就是每个新建队列(queue)都会自动绑定到默认交换机上,绑定的路由键(routing key)名称与队列名称相同。
如:当你声明了一个名为”hello”的队列,RabbitMQ会自动将其绑定到默认交换机上,绑定(binding)的路由键名称也是为”hello”。
因此,当携带着名为”hello”的路由键的消息被发送到默认交换机的时候,此消息会被默认交换机路由至名为”hello”的队列中
类似amq.*的名称的交换机:
这些是RabbitMQ默认创建的交换机。这些队列名称被预留做RabbitMQ内部使用,不能被应用使用,否则抛出403 (ACCESS_REFUSED)错误
6.Dead Letter Exchange(死信交换机)
在默认情况,如果消息在投递到交换机时,交换机发现此消息没有匹配的队列,则这个消息将被悄悄丢弃。
为了解决这个问题,RabbitMQ中有一种交换机叫死信交换机。当消费者不能处理接收到的消息时,将这个消息重新发布到另外一个队列中,
等待重试或者人工干预。这个过程中的exchange和queue就是所谓的”Dead Letter Exchange 和 Queue
三. 交换机的属性
除交换机类型外,在声明交换机时还可以附带许多其他的属性,其中最重要的几个分别是:
Name:交换机名称
Durability:是否持久化。如果持久性,则RabbitMQ重启后,交换机还存在
Auto-delete:当所有与之绑定的消息队列都完成了对此交换机的使用后,删掉它
Arguments:扩展参数
四. 综合案例:交换机的使用
rabbitmq02 #主模块
rabbitmq-provider #生产者
rabbitmq-consumer #消费者
0.给子模块添加依赖
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-amqp</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<optional>true</optional>
</dependency>
1.直连交换机(Direct Exchange)
yml:
server.port=8080
## rabbitmq config
spring.rabbitmq.host=192.168.122.128
spring.rabbitmq.port=5672
spring.rabbitmq.username=ycx
spring.rabbitmq.password=yangcanxia
##与启动容器时虚拟主机名字一致~~~与启动容器时虚拟主机名字一致~~~与启动容器时虚拟主机名字一致~~~
spring.rabbitmq.virtual-host=my_vhost
注1:新版jdk日期及格式化
LocalDateTime.now().format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"));
注2:rabbitTemplate和amqpTemplate有什么关系
查看源码中会发现rabbitTemplate实现自amqpTemplate接口,两者使用起来并无区别,功能一致
注3:不要@Configuration写成了@Configurable,这两个长得很像
@Configuration该注解是可以用来替代XML文件。
手动new出来的对象,正常情况下,Spring是无法依赖注入的,这个时候可以使用 @Configurable注解
2.主题交换机(Topic Exchange)
RabbitTopicConfig
package com.ycx.rabbitmqprovider.config;
import org.springframework.amqp.core.Binding;
import org.springframework.amqp.core.BindingBuilder;
import org.springframework.amqp.core.Queue;
import org.springframework.amqp.core.TopicExchange;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
/**
* @author 杨总
* @create 2022-12-15 15:43
*/
@Configuration
public class RabbitTopicConfig {
//队列
@Bean
public Queue topicQueueA(){
return new Queue("ycx-topic-Queue-a");
}
@Bean
public Queue topicQueueB(){
return new Queue("ycx-topic-Queue-b");
}
@Bean
public Queue topicQueueC(){
return new Queue("ycx-topic-Queue-c");
}
//交换机
@Bean
public TopicExchange topicExchange(){
return new TopicExchange("ycx-topic-Exchange");
}
//绑定关系
@Bean
public Binding binding1(){
return BindingBuilder.bind(topicQueueA())
.to(topicExchange())
.with("ycx.person.xx");
}
@Bean
public Binding binding2(){
return BindingBuilder.bind(topicQueueB())
.to(topicExchange())
.with("ycx.person.yy");
}
@Bean
public Binding binding3(){
return BindingBuilder.bind(topicQueueC())
.to(topicExchange())
.with("ycx.person.*");
}
}
3.扇形交换机(Fanout Exchange)
RabbitmqFanoutConfig
package com.ycx.rabbitmqprovider.config;
import org.springframework.amqp.core.*;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
/**
* @author 杨总
* @create 2022-12-15 15:43
*/
@Configuration
public class RabbitFanoutConfig {
//队列
@Bean
public Queue fanoutQueueA(){
return new Queue("ycx-topic-Queue-a");
}
@Bean
public Queue fanoutQueueB(){
return new Queue("ycx-topic-Queue-b");
}
@Bean
public Queue fanoutQueueC(){
return new Queue("ycx-topic-Queue-c");
}
//交换机
@Bean
public FanoutExchange fanoutExchange(){
return new FanoutExchange("ycx-fanout-Exchange");
}
//绑定关系
@Bean
public Binding bindingFanout1(){
return BindingBuilder.bind(fanoutQueueA())
.to(fanoutExchange());
}
@Bean
public Binding bindingFanout2(){
return BindingBuilder.bind(fanoutQueueB())
.to(fanoutExchange());
}
@Bean
public Binding bindingFanout3(){
return BindingBuilder.bind(fanoutQueueC())
.to(fanoutExchange());
}
}
//因为是扇型交换机, 路由键无需配置,配置也不起作用,两处地方均未配置路由键
BindingBuilder.bind(queueA()).to(fanoutExchange());
rabbitTemplate.convertAndSend(RabbitFanoutConfig.EXCHANGE_NAME,null, map);
/**
* 扇形交换机
* @return
*/
@RequestMapping("/sendFanout")
public Map sendFanout(){
Map msg=new HashMap();
msg.put("msg","主题交换机 ycx-fanout-Exchange 发送的消息");
msg.put("time", LocalDateTime.now().format(DateTimeFormatter.ofPattern("yyyy-MM-dd hh-mm-ss")));
rabbitTemplate.convertAndSend("ycx-fanout-Exchange",
null,
msg);
Map res=new HashMap();
res.put("code",200);
res.put("msg","成功");
return res;
}