spring boot 中如何将队列和交换机绑定(含实例讲解)
在使用 spring boot 开发高并发的秒杀系统或者其他场景时,rabbitmq 是常用的消息队列中间件之一。本文将详细讲解如何在配置类中通过代码将队列与交换机绑定,并指定路由键来实现消息路由。
一、rabbitmq中的关键概念
- exchange(交换机):交换机负责接收消息,并根据路由规则分发给绑定的队列。常见的交换机类型有 direct、fanout、topic 等。
- queue(队列):队列是消息实际存储的地方,消费者从队列中获取消息。
- routing key(路由键):生产者发送消息时,会携带一个路由键,rabbitmq 根据这个路由键决定把消息发送到哪个队列。binding(绑定):绑定是将队列和交换机关联在一起,消息通过路由键决定是否路由到某个队列。
二、需求描述
假设我们在秒杀系统中有一个秒杀订单的队列和对应的交换机,分别为 seckill.queue
和 seckill.exchange
。为了将订单处理的消息路由到正确的队列,我们需要将它们通过一个 seckill.routingkey
绑定在一起。
三、配置代码实现
1. 引入必要的依赖
首先,在 pom.xml
中引入 rabbitmq 的依赖:
<dependency> <groupid>org.springframework.boot</groupid> <artifactid>spring-boot-starter-amqp</artifactid> </dependency>
2. 配置类中绑定队列和交换机
在配置类中,我们需要定义交换机、队列,以及将两者通过路由键绑定。以下是具体实现:
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; @configuration public class rabbitmqconfig { // 定义常量表示交换机、队列和路由键 public static final string seckill_exchange = "seckill.exchange"; public static final string seckill_queue = "seckill.queue"; public static final string seckill_routingkey = "seckill.routingkey"; // 1. 定义秒杀交换机 @bean public topicexchange seckillexchange() { return new topicexchange(seckill_exchange); } // 2. 定义秒杀队列 @bean public queue seckillqueue() { return new queue(seckill_queue); } // 3. 绑定队列到交换机,并指定路由键 @bean public binding bindingseckillqueue(queue seckillqueue, topicexchange seckillexchange) { return bindingbuilder.bind(seckillqueue).to(seckillexchange).with(seckill_routingkey); } }
3. 代码详细解读
seckillexchange()
:这是定义的一个topicexchange
类型的交换机。在 rabbitmq 中,topicexchange
允许根据路由键的模式匹配将消息路由到不同的队列中。seckillqueue()
:定义了一个queue
队列,用来存储秒杀订单的消息。此处的queue
是持久化的,当 rabbitmq 重启时,队列中的消息不会丢失。bindingseckillqueue()
:通过bindingbuilder
将队列和交换机绑定在一起,并使用with(seckill_routingkey)
指定了路由键。这样,当消息生产者发送带有seckill.routingkey
的消息时,消息会被路由到seckill.queue
队列中。
四、如何发送消息
绑定完成后,你可以使用 rabbittemplate
将消息发送到交换机,并指定路由键:
import org.springframework.amqp.rabbit.core.rabbittemplate; import org.springframework.beans.factory.annotation.autowired; import org.springframework.stereotype.service; @service public class seckillmessagesender { @autowired private rabbittemplate rabbittemplate; // 发送秒杀订单消息 public void sendseckillordermessage(string message) { rabbittemplate.convertandsend(rabbitmqconfig.seckill_exchange, rabbitmqconfig.seckill_routingkey, message); system.out.println("秒杀消息已发送:" + message); } }
在上面的代码中,rabbittemplate
提供了 convertandsend
方法,将消息发送到 seckill.exchange
交换机,并且指定 seckill.routingkey
作为路由键,消息最终会被路由到绑定的 seckill.queue
队列。
五、消息接收方如何处理
消费者(监听队列消息的服务)可以使用 @rabbitlistener
来监听队列中的消息。例如:
import org.springframework.amqp.rabbit.annotation.rabbitlistener; import org.springframework.stereotype.component; @component public class seckillmessagereceiver { // 监听秒杀队列 @rabbitlistener(queues = rabbitmqconfig.seckill_queue) public void receivemessage(string message) { system.out.println("接收到秒杀消息:" + message); // 处理消息的逻辑 } }
六、几种常见的绑定示例
1. 使用 direct exchange 进行精确匹配
如果你想要根据路由键的精确匹配来路由消息,可以使用 directexchange
,而不是 topicexchange
。
@bean public directexchange directexchange() { return new directexchange("direct.exchange"); } @bean public binding bindingdirectqueue(queue seckillqueue, directexchange directexchange) { return bindingbuilder.bind(seckillqueue).to(directexchange).with("direct.routingkey"); }
这种方式下,只有当路由键完全匹配 direct.routingkey
时,消息才会被路由到对应的队列。
2. 使用 fanout exchange 广播消息
如果你想将消息广播到多个队列,可以使用 fanoutexchange
,它会忽略路由键,将消息发送到所有绑定的队列。
@bean public fanoutexchange fanoutexchange() { return new fanoutexchange("fanout.exchange"); } @bean public binding bindingfanoutqueue(queue seckillqueue, fanoutexchange fanoutexchange) { return bindingbuilder.bind(seckillqueue).to(fanoutexchange); }
到此这篇关于springboot rabbit mq topic 配置文件绑定队列和交换机的文章就介绍到这了,更多相关springboot rabbit mq topic 配置文件绑定队列内容请搜索代码网以前的文章或继续浏览下面的相关文章希望大家以后多多支持代码网!
发表评论