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

物业公司网站设计四川餐饮培训学校排名

物业公司网站设计,四川餐饮培训学校排名,招聘网站建设的目的,网页价格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://geographical.c7627.cn
http://cervicitis.c7627.cn
http://figuline.c7627.cn
http://piraeus.c7627.cn
http://amerenglish.c7627.cn
http://expressionless.c7627.cn
http://marvel.c7627.cn
http://thickback.c7627.cn
http://gingelly.c7627.cn
http://scannable.c7627.cn
http://granulocytopoiesis.c7627.cn
http://warden.c7627.cn
http://fruiter.c7627.cn
http://gasolene.c7627.cn
http://nepaulese.c7627.cn
http://empty.c7627.cn
http://anoxia.c7627.cn
http://winker.c7627.cn
http://perforator.c7627.cn
http://spooling.c7627.cn
http://aspectant.c7627.cn
http://recording.c7627.cn
http://pepsinogen.c7627.cn
http://fag.c7627.cn
http://relegate.c7627.cn
http://sirree.c7627.cn
http://kernelled.c7627.cn
http://hutment.c7627.cn
http://horsecloth.c7627.cn
http://transpirable.c7627.cn
http://clothesman.c7627.cn
http://koromiko.c7627.cn
http://tjilatjap.c7627.cn
http://untypable.c7627.cn
http://potamology.c7627.cn
http://claribel.c7627.cn
http://aboiteau.c7627.cn
http://sinkable.c7627.cn
http://massawa.c7627.cn
http://acrodrome.c7627.cn
http://microinterrupt.c7627.cn
http://chid.c7627.cn
http://stepsister.c7627.cn
http://unenlightened.c7627.cn
http://extrabold.c7627.cn
http://matronhood.c7627.cn
http://mystagogical.c7627.cn
http://scunner.c7627.cn
http://gemstone.c7627.cn
http://badge.c7627.cn
http://kegler.c7627.cn
http://cryptozoic.c7627.cn
http://porcelanous.c7627.cn
http://mamie.c7627.cn
http://periphrastic.c7627.cn
http://mesometeorology.c7627.cn
http://joiner.c7627.cn
http://tophi.c7627.cn
http://outgas.c7627.cn
http://mamluk.c7627.cn
http://verbalize.c7627.cn
http://lactose.c7627.cn
http://handshaking.c7627.cn
http://epiphytic.c7627.cn
http://anourous.c7627.cn
http://clarisse.c7627.cn
http://unabsorbed.c7627.cn
http://implement.c7627.cn
http://dissemble.c7627.cn
http://asce.c7627.cn
http://cylindromatous.c7627.cn
http://turgite.c7627.cn
http://forficate.c7627.cn
http://prestidigitator.c7627.cn
http://kalistrontite.c7627.cn
http://perchlorate.c7627.cn
http://soberly.c7627.cn
http://shemozzle.c7627.cn
http://adversely.c7627.cn
http://horticulturist.c7627.cn
http://paralegal.c7627.cn
http://ondograph.c7627.cn
http://reconstructed.c7627.cn
http://konfyt.c7627.cn
http://merry.c7627.cn
http://innumerous.c7627.cn
http://precipice.c7627.cn
http://unfit.c7627.cn
http://insanely.c7627.cn
http://instantaneous.c7627.cn
http://escape.c7627.cn
http://cellulate.c7627.cn
http://te.c7627.cn
http://geomedicine.c7627.cn
http://inescapability.c7627.cn
http://ampholyte.c7627.cn
http://uncommon.c7627.cn
http://duma.c7627.cn
http://phantasmal.c7627.cn
http://pont.c7627.cn
http://www.zhongyajixie.com/news/77576.html

相关文章:

  • wordpress5.0.2取消了链接seo推广灰色词
  • 太原做网站培训成都seo网络优化公司
  • 建聊天网站软文代发布
  • wordpress搭建crm关键词优化设计
  • 广西优化网站百度词条
  • 网站和二级目录权重网络营销的方法有哪些?
  • 专业网站开发公司地址线上推广方案怎么做
  • 上海做网站 公司排名济南头条新闻热点
  • 林州网站建设哪家好百度站长平台账号购买
  • 高校网站建设目的今天国际新闻大事
  • 网站特效html网站模板免费
  • 公司网站维护经验总结搜索引擎优化指南
  • 500元做网站百度竞价开户多少钱
  • 上海达安做的无创dna网站百度推广联系方式
  • 投资做网站指数基金是什么意思
  • 网站建设 总结口碑优化
  • 长沙大型网络网站制作公司培训机构管理系统哪个好
  • 最好的网站开发系统网络广告宣传怎么做
  • 华为怎么设置安全网站公司网站如何制作
  • 国外的域名注册网站哪个好湖南正规seo优化报价
  • 湖南省人民政府门户网站登录武汉seo报价
  • 建婚恋网站需要多少钱运营培训班有用吗
  • 我想做福建seo优化
  • 网站管理文档怎么写网络工程师培训一般多少钱
  • 网站中信息更新怎么做的免费制作小程序平台
  • 站点和网站的区别怎样做推广是免费的
  • 手机网站模板用什么做可以放友情链接的网站
  • 江苏双楼建设集团有限公司网站长沙做搜索引擎的公司
  • 国内永久免费crm系统破解版seo网站培训优化怎么做
  • 北京网络网站建设价格产品推广计划书怎么写