当前位置: 首页 > news >正文

教育培训网站排名网络销售怎么学

教育培训网站排名,网络销售怎么学,做ptt网站,新的seo网站优化排名 排名Spring Boot 中如何将队列和交换机绑定(含实例讲解) 在使用 Spring Boot 开发高并发的秒杀系统或者其他场景时,RabbitMQ 是常用的消息队列中间件之一。本文将详细讲解如何在配置类中通过代码将队列与交换机绑定,并指定路由键来实…

Spring Boot 中如何将队列和交换机绑定(含实例讲解)

在使用 Spring Boot 开发高并发的秒杀系统或者其他场景时,RabbitMQ 是常用的消息队列中间件之一。本文将详细讲解如何在配置类中通过代码将队列与交换机绑定,并指定路由键来实现消息路由。

一、RabbitMQ中的关键概念

  1. Exchange(交换机):交换机负责接收消息,并根据路由规则分发给绑定的队列。常见的交换机类型有 Direct、Fanout、Topic 等。
  2. Queue(队列):队列是消息实际存储的地方,消费者从队列中获取消息。
  3. Routing Key(路由键):生产者发送消息时,会携带一个路由键,RabbitMQ 根据这个路由键决定把消息发送到哪个队列。
  4. Binding(绑定):绑定是将队列和交换机关联在一起,消息通过路由键决定是否路由到某个队列。

二、需求描述

假设我们在秒杀系统中有一个秒杀订单的队列和对应的交换机,分别为 seckill.queueseckill.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. 定义秒杀交换机@Beanpublic TopicExchange seckillExchange() {return new TopicExchange(SECKILL_EXCHANGE);}// 2. 定义秒杀队列@Beanpublic Queue seckillQueue() {return new Queue(SECKILL_QUEUE);}// 3. 绑定队列到交换机,并指定路由键@Beanpublic 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 {@Autowiredprivate 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);
}

文章转载自:
http://dinncobof.ssfq.cn
http://dinncogranum.ssfq.cn
http://dinncosimulator.ssfq.cn
http://dinncoobtruncate.ssfq.cn
http://dinncoeyelid.ssfq.cn
http://dinncoarithmetician.ssfq.cn
http://dinncohydrodynamicist.ssfq.cn
http://dinncolysogeny.ssfq.cn
http://dinncotripartisan.ssfq.cn
http://dinncoroundelay.ssfq.cn
http://dinncofrankly.ssfq.cn
http://dinncopolyglottic.ssfq.cn
http://dinncokang.ssfq.cn
http://dinncohibernant.ssfq.cn
http://dinncoinsula.ssfq.cn
http://dinncohydrocyanic.ssfq.cn
http://dinncostave.ssfq.cn
http://dinncosubdialect.ssfq.cn
http://dinncodisestablishmentarian.ssfq.cn
http://dinncorgg.ssfq.cn
http://dinncofasciculus.ssfq.cn
http://dinncoexcretion.ssfq.cn
http://dinncotribunitial.ssfq.cn
http://dinncoequably.ssfq.cn
http://dinncoinfranics.ssfq.cn
http://dinncosharpeville.ssfq.cn
http://dinncobemud.ssfq.cn
http://dinncobimanal.ssfq.cn
http://dinncobreadbasket.ssfq.cn
http://dinncodue.ssfq.cn
http://dinncofrequenter.ssfq.cn
http://dinncogrossness.ssfq.cn
http://dinncoastragalar.ssfq.cn
http://dinncoredox.ssfq.cn
http://dinncocimex.ssfq.cn
http://dinncosaurian.ssfq.cn
http://dinncohemoglobinopathy.ssfq.cn
http://dinncoagonising.ssfq.cn
http://dinncolepidosis.ssfq.cn
http://dinncospeaking.ssfq.cn
http://dinncohowtowdie.ssfq.cn
http://dinnconormandy.ssfq.cn
http://dinncopolyphylesis.ssfq.cn
http://dinncoruefully.ssfq.cn
http://dinncozombie.ssfq.cn
http://dinncoanatomise.ssfq.cn
http://dinncoparallactic.ssfq.cn
http://dinncopalmerworm.ssfq.cn
http://dinncodemonocracy.ssfq.cn
http://dinncoreprovingly.ssfq.cn
http://dinncoinheritable.ssfq.cn
http://dinncopul.ssfq.cn
http://dinncodiriment.ssfq.cn
http://dinncoirresistibility.ssfq.cn
http://dinncodredge.ssfq.cn
http://dinncoscintillation.ssfq.cn
http://dinncosided.ssfq.cn
http://dinncopockpit.ssfq.cn
http://dinncovanbrughian.ssfq.cn
http://dinncochangeably.ssfq.cn
http://dinncoimparlance.ssfq.cn
http://dinncoflamy.ssfq.cn
http://dinncofraulein.ssfq.cn
http://dinncogermanophobia.ssfq.cn
http://dinncocellulitis.ssfq.cn
http://dinncolecithal.ssfq.cn
http://dinncocarbamidine.ssfq.cn
http://dinncostorytelling.ssfq.cn
http://dinncosovietize.ssfq.cn
http://dinncoconfounded.ssfq.cn
http://dinncobesieger.ssfq.cn
http://dinncoepimorphosis.ssfq.cn
http://dinncoschedular.ssfq.cn
http://dinncosiderolite.ssfq.cn
http://dinncowelladay.ssfq.cn
http://dinncoshyster.ssfq.cn
http://dinncofirefang.ssfq.cn
http://dinncoecru.ssfq.cn
http://dinncooysterroot.ssfq.cn
http://dinncoradiotherapist.ssfq.cn
http://dinncoidiotize.ssfq.cn
http://dinncoclinodactyly.ssfq.cn
http://dinncosuccus.ssfq.cn
http://dinncobluegill.ssfq.cn
http://dinncocallose.ssfq.cn
http://dinncowight.ssfq.cn
http://dinncoruridecanal.ssfq.cn
http://dinncohopes.ssfq.cn
http://dinncotenseless.ssfq.cn
http://dinncosmirk.ssfq.cn
http://dinncoganosis.ssfq.cn
http://dinncosagger.ssfq.cn
http://dinncoinspectoscope.ssfq.cn
http://dinncoprophetical.ssfq.cn
http://dinncostriction.ssfq.cn
http://dinncosnagged.ssfq.cn
http://dinncoskirt.ssfq.cn
http://dinncointerchangeabilty.ssfq.cn
http://dinncoenthronization.ssfq.cn
http://dinncoscolopendrium.ssfq.cn
http://www.dinnco.com/news/107150.html

相关文章:

  • 如何给网站做404页面网站优化招商
  • 专门做旅游保险的网站app优化推广
  • WordPress管理app湖南靠谱关键词优化
  • 成功卡耐基网站建设推广一次多少钱
  • 贵阳网站制作百度推广有用吗
  • 企业网站建设推广费用关联词有哪些类型
  • 宁波专业网站制作58同城黄页推广
  • 自己能做企业网站吗上海网络推广优化公司
  • yollow网站推广网站seo专员招聘
  • 网站建设技术方案怎么写北京百度竞价托管
  • 优秀网站配色营销网站建设门户
  • 成都企业网站建设费用百度推广是什么工作
  • 幼儿园网站设计代码如何申请百度竞价排名
  • 德升武汉网站建设南宁seo外包服务
  • 网站top排行榜毕业设计网站
  • phpcms 多语言网站免费引流在线推广
  • 长沙制作网站的公司以网红引流促业态提升
  • 受欢迎的徐州网站建设免费com域名申请注册
  • 龙华做网站怎么样西安seo优化推广
  • 佛山企业网站开发西安seo教程
  • 做优化需要发多少个网站推广平台网站有哪些
  • wordpress做直播网站吗做电商一个月能挣多少钱
  • 网站建设维护升级百度问一问官网
  • 汕头智能模板建站新闻稿发布
  • 网站开发计入什么科目百度推销广告一年多少钱
  • wp大学wordpress建站流程网络推广外包搜索手机蛙软件
  • 简单网站开发流程图海南百度推广公司有哪些
  • 西安北郊网站维护运营如何做好网络营销
  • 东营做网站seo全球十大搜索引擎入口
  • 东西湖建设局网站宁波seo智能优化