目录
一,Exchange(交换机)的作用
在RabbitMQ中,生产者发送消息不会直接将消息投递到队列中,而是先将消息投递到交换机中,在由交换机转发到具体的队列,队列再将消息以推送或者拉取方式给消费者进行消费
交换机原理
二,交换机的类型
1.直连交换机:Direct Exchange
直连交换机是一种带路由功能的交换机,一个队列会和一个交换机绑定,除此之外再绑定一个routing_key,当消息被发送的时候,需要指定一个binding_key,这个消息被送达交换机的时候,就会被这个交换机送到指定的队列里面去。同样的一个binding_key也是支持应用到多个队列中的。这样当一个交换机绑定多个队列,就会被送到对应的队列去处理。
注1:什么是路由键
每个消息都有一个称为路由键(routing key)的属性,它其实就是一个简单的字符串注2:直连交换机适用场景
有优先级的任务,根据任务的优先级把消息发送到对应的队列,这样可以指派更多的资源去处理高优先级的队列。
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.默认交换机
实际上是一个由RabbitMQ预先声明好的名字为空字符串的直连交换机(direct exchange)。它有一个特殊的属性使得它对于
简单应用特别有用处:那就是每个新建队列(queue)都会自动绑定到默认交换机上,绑定的路由键(routing key)名称与队列名称相同。如:当你声明了一个名为”hello”的队列,RabbitMQ会自动将其绑定到默认交换机上,绑定(binding)的路由键名称也是为”hello”。
因此,当携带着名为”hello”的路由键的消息被发送到默认交换机的时候,此消息会被默认交换机路由至名为”hello”的队列中
类似amq.*的名称的交换机:
这些是RabbitMQ默认创建的交换机。这些队列名称被预留做RabbitMQ内部使用,不能被应用使用,否则抛出403 (ACCESS_REFUSED)错误
5.Dead Letter Exchange(死信交换机)
在默认情况,如果消息在投递到交换机时,交换机发现此消息没有匹配的队列,则这个消息将被悄悄丢弃。
为了解决这个问题,RabbitMQ中有一种交换机叫死信交换机。当消费者不能处理接收到的消息时,将这个消息重新发布到另外一个队列中,
等待重试或者人工干预。这个过程中的exchange和queue就是所谓的”Dead Letter Exchange 和 Queue
三,交换机的属性
除交换机类型外,在声明交换机时还可以附带许多其他的属性,其中最重要的几个分别是:
Name:交换机名称
Durability:是否持久化。如果持久性,则RabbitMQ重启后,交换机还存在
Auto-delete:当所有与之绑定的消息队列都完成了对此交换机的使用后,删掉它
Arguments:扩展参数
四,综合案例(交换机的使用)
新建一个项目,包含主模块以及两个子模块(比作生产者与消费者)
添加相关依赖(添加在子模块即可)
<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>
直连交换机(目前只是生产者再生产,还未消费)
首先在config包中的类中将交换机与队列绑定关系,代码如下
package com.dengxiyan.rabbitmqprovider.config;
import org.springframework.amqp.core.Binding;
import org.springframework.amqp.core.BindingBuilder;
import org.springframework.amqp.core.DirectExchange;
import org.springframework.amqp.core.Queue;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
/**
* @author dxy
* @site www.javadxy.com
* @company ds公司
* @create 2022-12-14 20:08
*/
@Configuration
public class RabbitmqDirectConfig {
//直连交换机对应的队列
@Bean
public Queue directQueue(){
return new Queue("dengxiyan-direct-Queue");
}
//直连交换机
@Bean
public DirectExchange directExchange(){
return new DirectExchange("dengxiyan-direct-Exchange");
}
//直连交换机与对列的绑定关系
@Bean
public Binding directbinding(){
return BindingBuilder.bind(directQueue())
.to(directExchange())
.with("direct_routing_key");
}
}
之后在controller层中进行访问
package com.dengxiyan.rabbitmqprovider.Controller;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.HashMap;
import java.util.Map;
/**
* @author dxy
* @site www.javadxy.com
* @company ds公司
* @create 2022-12-14 20:18
*/
@RestController
public class SendMessageController {
@Autowired
private RabbitTemplate rabbitTemplate;
@RequestMapping("/sendDirect")
public Map sendDriect(String routingKey){
Map msg = new HashMap();
msg.put("msg","直接交换机dengxiyan-direct-Exchange发送消息");
msg.put("time", LocalDateTime.now().format(DateTimeFormatter.ofPattern("yyyy-MM-dd hh:mm:ss")));
// rabbitTemplate.convertAndSend("dengxiyan-direct-Exchange",routingKey,msg);
rabbitTemplate.convertAndSend("dengxiyan-direct-Exchange",routingKey,msg);
Map res = new HashMap();
res.put("code",200);
res.put("msg","成功");
return res;
}
}
测试:取Rabbitmq管理界面看是否推送成功
主题交换机操作
代码如下:(工厂)
package com.dengxiyan.rabbitmqconsumer.config;
import org.springframework.amqp.rabbit.annotation.RabbitHandler;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.stereotype.Component;
import java.util.Map;
/**
* dnegxiyan
*/
@Component
public class TopicReceiver {
@RabbitListener(queues = {"fanout-queue-a"})
@RabbitHandler
public void handlerA(Map msg){
System.out.println("fanout-queue-a接受到的消息:"+msg);
}
@RabbitListener(queues = {"fanout-queue-b"})
@RabbitHandler
public void handlerB(Map msg){
System.out.println("fanout-queue-b接受到的消息:"+msg);
}
@RabbitListener(queues = {"fanout-queue-c"})
@RabbitHandler
public void handlerC(Map msg){
System.out.println("fanout-queue-c接受到的消息:"+msg);
}
}
效果图
主题交换机加进来了
在执行controller层的方法
预测结果
消费者
package com.dengxiyan.rabbitmqprovider.config;
import org.springframework.amqp.core.*;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
/**
* @author dxy
* @site www.javadxy.com
* @company ds公司
* @create 2022-12-14 21:08
*/
@Configuration
public class RabbitTopicConfig {
@Bean
public Queue topicQueueA(){
return new Queue("topic-queue-a");
}
@Bean
public Queue topicQueueB(){
return new Queue("topic-queue-b");
}
@Bean
public Queue topicQueueC(){
return new Queue("topic-queue-c");
}
@Bean
public TopicExchange topicExchange(){
return new TopicExchange("topic-exchange");
}
@Bean
public Binding topicBindingA(){
return BindingBuilder.bind(topicQueueA())
.to(topicExchange())
.with("topic.person.xx");
}
@Bean
public Binding topicBindingB(){
return BindingBuilder.bind(topicQueueB())
.to(topicExchange())
.with("topic.person.yy");
}
@Bean
public Binding topicBindingC(){
return BindingBuilder.bind(topicQueueC())
.to(topicExchange())
.with("topic.person.*");
}
}
当执行消费者端是,RabbitM管理界面就会变成0,就相当于产品被消费出去了。
扇形交换机(与主题交换记得区别是没有routingKey)
代码演示:工厂端生产
package com.dengxiyan.rabbitmqconsumer.config;
import org.springframework.amqp.rabbit.annotation.RabbitHandler;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.stereotype.Component;
import java.util.Map;
/**
* dengxiyan
*/
@Component
@RabbitListener(queues = {"fanout-queue"})
public class FanoutReceiver {
@RabbitHandler
public void handler(Map msg){
System.out.println(msg);
}
}
消费端接收
package com.dengxiyan.rabbitmqprovider.config;
import org.springframework.amqp.core.*;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
/**
* @author dxy
* @site www.javadxy.com
* @company ds公司
* @create 2022-12-14 20:30
*/
@Configuration
public class RabbitmqFanoutConfig {
@Bean
public Queue fanoutQueueA(){
return new Queue("fanout-queue-a");
}
@Bean
public Queue fanoutQueueB(){
return new Queue("fanout-queue-b");
}
@Bean
public Queue fanoutQueueC(){
return new Queue("fanout-queue-c");
}
@Bean
public FanoutExchange fanoutExchange(){
return new FanoutExchange("fanout-exchange");
}
@Bean
public Binding fanoutBindingA(){
return BindingBuilder.bind(fanoutQueueA())
.to(fanoutExchange());
}
@Bean
public Binding fanoutBindingB(){
return BindingBuilder.bind(fanoutQueueB())
.to(fanoutExchange());
}
@Bean
public Binding fanoutBindingC(){
return BindingBuilder.bind(fanoutQueueC())
.to(fanoutExchange());
}
}
后台打印(消费者消费)