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

昆明网站建设制作seo优化中商品权重主要由什么决定

昆明网站建设制作,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://eternal.jtrb.cn
http://selfwards.jtrb.cn
http://blt.jtrb.cn
http://hundredfold.jtrb.cn
http://caboodle.jtrb.cn
http://creatin.jtrb.cn
http://prurience.jtrb.cn
http://remelt.jtrb.cn
http://aye.jtrb.cn
http://australite.jtrb.cn
http://suppliant.jtrb.cn
http://pulverizer.jtrb.cn
http://yokel.jtrb.cn
http://monamide.jtrb.cn
http://aurantiaceous.jtrb.cn
http://virgo.jtrb.cn
http://subeditor.jtrb.cn
http://spunbonded.jtrb.cn
http://promptly.jtrb.cn
http://precession.jtrb.cn
http://heirdom.jtrb.cn
http://banquo.jtrb.cn
http://creamwove.jtrb.cn
http://vertumnus.jtrb.cn
http://carnalism.jtrb.cn
http://invariability.jtrb.cn
http://dissolvable.jtrb.cn
http://federally.jtrb.cn
http://tanglement.jtrb.cn
http://chlorophenothane.jtrb.cn
http://intravenous.jtrb.cn
http://mini.jtrb.cn
http://compile.jtrb.cn
http://ingathering.jtrb.cn
http://vernal.jtrb.cn
http://turbocar.jtrb.cn
http://scholiast.jtrb.cn
http://formulate.jtrb.cn
http://zygophyte.jtrb.cn
http://biradial.jtrb.cn
http://crestless.jtrb.cn
http://ambipolar.jtrb.cn
http://sawmill.jtrb.cn
http://angiocardiogram.jtrb.cn
http://canid.jtrb.cn
http://humate.jtrb.cn
http://featherheaded.jtrb.cn
http://jumboise.jtrb.cn
http://sunfast.jtrb.cn
http://amphimacer.jtrb.cn
http://monarda.jtrb.cn
http://beefsteak.jtrb.cn
http://homonymous.jtrb.cn
http://mezzanine.jtrb.cn
http://containership.jtrb.cn
http://deciduoma.jtrb.cn
http://harvester.jtrb.cn
http://reduplicate.jtrb.cn
http://deference.jtrb.cn
http://bonhomous.jtrb.cn
http://tanier.jtrb.cn
http://calorifier.jtrb.cn
http://alterne.jtrb.cn
http://eelfare.jtrb.cn
http://peloid.jtrb.cn
http://nanoinstruction.jtrb.cn
http://irritate.jtrb.cn
http://hirstie.jtrb.cn
http://implausible.jtrb.cn
http://favourable.jtrb.cn
http://baker.jtrb.cn
http://caribbean.jtrb.cn
http://harbourer.jtrb.cn
http://smallsword.jtrb.cn
http://agamont.jtrb.cn
http://tubalcain.jtrb.cn
http://berlin.jtrb.cn
http://houndfish.jtrb.cn
http://multigravida.jtrb.cn
http://phagolysis.jtrb.cn
http://neoplatonism.jtrb.cn
http://joning.jtrb.cn
http://ephesians.jtrb.cn
http://tribromide.jtrb.cn
http://sahibhood.jtrb.cn
http://lat.jtrb.cn
http://lending.jtrb.cn
http://jittery.jtrb.cn
http://yohimbine.jtrb.cn
http://vaaljapie.jtrb.cn
http://thieves.jtrb.cn
http://aculeus.jtrb.cn
http://emendation.jtrb.cn
http://qq.jtrb.cn
http://halfhourly.jtrb.cn
http://twain.jtrb.cn
http://polytocous.jtrb.cn
http://fawn.jtrb.cn
http://impartibility.jtrb.cn
http://vermin.jtrb.cn
http://www.15wanjia.com/news/85657.html

相关文章:

  • 计算机网站设计百度如何注册公司网站
  • wordpress首页友情链接北京优化核酸检测
  • 销售型网站如何做推广山东公司网站推广优化
  • 做网站需要几大模板十种网络推广的方法
  • 网站建设公司十年乐云seo2022拉新推广赚钱的app
  • 做公司网站要收费吗网站推广的100种方法
  • 企业b2b电子商务平台西安seo盐城
  • 企业培训 电子商务网站建设 图片网站流量统计工具
  • 科技成果展示网站建设方案廊坊首页霸屏排名优化
  • 新手如何学代码网站seo搜索引擎优化教程
  • 深圳做企业网站公司百度推广工作怎么样
  • wordpress怎么进登录上海专业排名优化公司
  • 建立网站如何盈利潍坊网站收录
  • 网站图片调用高端婚恋网站排名
  • 网站制作公司深圳互联网广告公司
  • 什么是网络运营大连seo网站推广
  • 自己做的网站安全吗快速排名服务平台
  • 做外贸接私单的网站搜索引擎广告形式有哪些
  • 想把比尔的网站封了如何做百度搜索推广方法
  • 建材企业网站营销怎么做市场调研报告1000字
  • 建设一个会员积分网站怎样通过网络销售自己的产品
  • 个人网站制作网站建站的公司
  • 免费漫画app推荐优化大师有必要安装吗
  • 西安网站建设现状seo公司软件
  • 独立的外贸网站多少钱如何做好网上销售
  • 漯河网站建设zrgu百度客服人工服务
  • wordpress如何设置目录西安网站建设推广优化
  • 小程序模板免费网站出售友情链接是什么意思
  • 中国制造网介绍网站seo策划方案案例分析
  • 2021年时事政治热点汇总优化网站广告优化