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

物流炒货怎么做网站方象科技专注于什么领域

物流炒货怎么做网站,方象科技专注于什么领域,网站制作网站建设需要多少钱,企业做网站要注意哪些1. 实现方式 1. 设置队列过期时间:延迟队列消息过期 死信队列,所有消息过期时间一致 2. 设置消息的过期时间:此种方式下有缺陷,MQ只会判断队列第一条消息是否过期,会导致消息的阻塞需要额外安装 rabbitmq_delayed_me…

1. 实现方式

1. 设置队列过期时间:延迟队列消息过期 + 死信队列,所有消息过期时间一致
2. 设置消息的过期时间:此种方式下有缺陷,MQ只会判断队列第一条消息是否过期,会导致消息的阻塞需要额外安装 `rabbitmq_delayed_message_exchange` 插件才能解决此问题
  • 导入Spring 集成RabbitMQ MAEVN
<dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-amqp</artifactId><version>2.2.5.RELEASE</version>
</dependency>

2. 设置队列过期时间:延迟队列消息过期 + 死信队列

推送消息至延迟队列 -> 消息过期自动推送到死信队列 -> 消费死信队列

2.1. MQ配置信息

2.1.1. 自定义队列配置

…/bootstrap.yml

# rabbitmq自定义配置
rabbitmq:ttlExchange: medical_dev_ttl_topic_changettlKey: dev_ttlttlQueue: medical.dev.ttl.topic.queuedelayExpireTime: 600ttlQueueSize: 10000deadExchange: medical_dev_dead_topic_changedeadKey: dev_deaddeadQueue: medical.dev.dead.topic.queue
2.1.2. 读取自定义MQ配置信息
/*** amqp配置文件*/
@Data
@Component
@ConfigurationProperties(prefix = "rabbitmq")
public class MyConfigProperties {/*** 延迟队列*/public String ttlExchange;public String ttlKey;public String ttlQueue;private Integer delayExpireTime;public Integer ttlQueueSize;/*** 死信队列*/public String deadExchange;public String deadKey;public String deadQueue;}

2.2. 配置文件自动生成队列

2.2.1. 延迟队列
import com.awsa.site.mq.MyConfigProperties;
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;import javax.annotation.Resource;
import java.util.HashMap;/*** 延迟队列配置文件* * @author mingAn.xie*/
@Configuration
public class RabbitMQConfigTTL {@ResourceMyConfigProperties myConfigProperties;// 1: 声明交换机@Beanpublic TopicExchange ttlTopicExchange(){return new TopicExchange(myConfigProperties.getTtlExchange());}// 2: 声明队列// durable:是否持久化,默认是false,持久化队列:会被存储在磁盘上,当消息代理重启时仍然存在,暂存队列:当前连接有效// exclusive:默认也是false,只能被当前创建的连接使用,而且当连接关闭后队列即被删除。此参考优先级高于durable// autoDelete:是否自动删除,当没有生产者或者消费者使用此队列,该队列会自动删除。@Beanpublic Queue ttlTopicduanxinQueue(){HashMap<String, Object> args = new HashMap<>();// 给队列设置消息过期时间:毫秒值args.put("x-message-ttl", mqConfigProperties.getDelayExpireTime() * 1000);// 设置队列最大长度args.put("x-max-length", myConfigProperties.getTtlQueueSize());// 设置死信队列交换机名称// 当消息在一个队列中变成死信后,它能就发送到另一个交换机中,这个交换机就是DLX,绑定DLX的队列被称之为死信队列// 编程死信队列的原因:消息被拒绝,消息过期,队列达到最大长度args.put("x-dead-letter-exchange", myConfigProperties.getDeadExchange());// 设置死信队列路由keyargs.put("x-dead-letter-routing-key", myConfigProperties.getDeadKey());return new Queue(myConfigProperties.getTtlQueue(), true, false, false, args);}// 3: 绑定对用关系@Beanpublic Binding ttlTopicsmsBinding(){return BindingBuilder.bind(ttlTopicduanxinQueue()).to(ttlTopicExchange()).with(myConfigProperties.getTtlKey());}}
2.2.2. 死信队列

import com.awsa.site.mq.MyConfigProperties;
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;import javax.annotation.Resource;/*** 死信队列配置文件* * @author mingAn.xie*/
@Configuration
public class RabbitMQConfigDead {@ResourceMyConfigProperties myConfigProperties;// 1: 声明交换机@Beanpublic TopicExchange deadTopicExchange(){return new TopicExchange(myConfigProperties.getDeadExchange());}// 2: 声明队列// durable:是否持久化,默认是false,持久化队列:会被存储在磁盘上,当消息代理重启时仍然存在,暂存队列:当前连接有效// exclusive:默认也是false,只能被当前创建的连接使用,而且当连接关闭后队列即被删除。此参考优先级高于durable// autoDelete:是否自动删除,当没有生产者或者消费者使用此队列,该队列会自动删除。@Beanpublic Queue deadTopicduanxinQueue(){return new Queue(myConfigProperties.getDeadQueue(), true);}// 3: 绑定对用关系@Beanpublic Binding deadTopicsmsBinding(){return BindingBuilder.bind(deadTopicduanxinQueue()).to(deadTopicExchange()).with(myConfigProperties.getDeadKey());}}

2.3. 生产者推送消息

import com.awsa.site.mq.MyConfigProperties;
import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.stereotype.Component;import javax.annotation.Resource;/*** RabbitMQ生产者推送消息类* * @author xiemingan*/
@Component
@Slf4j
public class RabbitmqProducer {@Resourceprivate RabbitTemplate rabbitTemplate;@Resourceprivate MyConfigProperties myConfigProperties;/*** @param pushMessage 推送消息体*/public void pushTtlMessage(String pushMessage) {// 推送消息至交换机,并指定路由keyrabbitTemplate.convertAndSend(myConfigProperties.getTtlExchange(), myConfigProperties.getTtlKey(), pushMessage);log.info("MQ消息推送队列, exchange: {}, key: {}, message: {}", myConfigProperties.getTtlExchange(), myConfigProperties.getTtlKey(), pushMessage);}}

2.4. 消费者处理消息

import lombok.extern.log4j.Log4j2;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.stereotype.Component;
import org.springframework.util.StringUtils;/*** @author mingAn.xie*/
@Log4j2
@Component
public class RabbitmqConsumer {/*** 消费死信队列* @param message 消息体*/@RabbitListener(queues = "${rabbitmq.deadQueue}")public void pushMessages(Message message) {String body = new String(message.getBody()).trim();if (StringUtils.isEmpty(body)){return;}log.info("MQ消息消费, RabbitmqConsumer.pushMessages() : {}", body);}}

3. 设置消息的过期时间

设置交换机类型为 x-delayed-type,推送消息至交换机,直连队列消费

3.1. 安装插件 rabbitmq_delayed_message_exchange

前言:这里默认使用环境为 Liunx 系统 Docker 安装 RabbitMQ

具体可以参考这篇文章:Docker 安装 RabbitMQ 挂载配置文件

安装插件版本需要与RabbitMQ版本一致,否则可能会导致安装失败,可先进入RabbitMQ容器中查看其他插件版本

插件各版本地址: https://github.com/rabbitmq/rabbitmq-delayed-message-exchange/releases

  • 这里以最新版本 v3.13.0 举例
# 下载插件
wget https://github.com/rabbitmq/rabbitmq-delayed-message-exchange/releases/download/v3.13.0/rabbitmq_delayed_message_exchange-3.13.0.ez# 将插件复制进容器中: rabbitmq_xxxxxx
docker cp rabbitmq_delayed_message_exchange-3.13.0.ez rabbitmq_xxxxxx:/plugins# 进入容器: rabbitmq_xxxxxx
docker exec -it rabbitmq_xxxxxx bash
cd plugins# 查询插件列表, 此处可看到插件的版本
rabbitmq-plugins list# 启用插件
rabbitmq-plugins enable rabbitmq_delayed_message_exchange
  • 交换机类型中出现 x-delayed-type 表示安装成功

3.2. MQ配置信息

3.2.1. 自定义队列配置

…/bootstrap.yml

#mq队列自定义配置
rabbitmq:saveTaskTtlExchange: ey240001_pro_save_task_ttl_topic_exchangesaveTaskTtlKey: ey240001_pro_save_task_ttlsaveTaskTtlQueue: ey240001.pro.save.task.ttl.topic.queuesaveTaskTtlQueueSize: 10000
3.2.2. 读取自定义MQ配置信息
/*** amqp配置文件** @author mingAn.xie*/
@Data
@Component
@ConfigurationProperties(prefix = "rabbitmq")
public class MyConfigProperties {/*** 任务待办生成延时队列*/public String saveTaskTtlExchange;public String saveTaskTtlKey;public String saveTaskTtlQueue;public Integer saveTaskTtlQueueSize;}

3.3. 配置文件生成 x-delayed-type 交换机

import com.awsa.site.mq.MyConfigProperties;
import org.springframework.amqp.core.*;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;import javax.annotation.Resource;
import java.util.HashMap;
import java.util.Map;/*** x-delayed-type 交换机延迟队列配置* * @author mingAn.xie*/
@Configuration
public class RabbitMQConfigSaveTaskTtl {@ResourceMyConfigProperties myConfigProperties;// 1: 声明交换机@Beanpublic CustomExchange saveTaskTopicExchange() {Map<String, Object> args = new HashMap<>();// 设置延迟队列插件类型:按过期时间消费args.put("x-delayed-type", "direct");// 参数:name 交换机名称,type 交换机类型,durable 是否持久化,autoDelete 是否自动删除,arguments 参数return new CustomExchange(myConfigProperties.getSaveTaskTtlExchange(), "x-delayed-message", true, false, args);}// 2: 声明队列// durable:是否持久化,默认是false,持久化队列:会被存储在磁盘上,当消息代理重启时仍然存在,暂存队列:当前连接有效// exclusive:默认也是false,只能被当前创建的连接使用,而且当连接关闭后队列即被删除。此参考优先级高于durable// autoDelete:是否自动删除,当没有生产者或者消费者使用此队列,该队列会自动删除。@Beanpublic Queue saveTaskTopicduanxinQueue() {return new Queue(myConfigProperties.getSaveTaskTtlQueue(), true, false, false);}// 3: 绑定对用关系@Beanpublic Binding saveTaskTopicsmsBinding() {return BindingBuilder.bind(saveTaskTopicduanxinQueue()).to(saveTaskTopicExchange()).with(myConfigProperties.getSaveTaskTtlKey()).noargs();}}

3.4. 生产者推送消息

import com.awsa.site.mq.MyConfigProperties;
import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.core.MessagePostProcessor;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.stereotype.Component;import javax.annotation.Resource;/*** 生产者推送消息类* * @author xiemingan*/
@Component
@Slf4j
public class RabbitmqProducer {@Resourceprivate RabbitTemplate rabbitTemplate;@Resourceprivate MyConfigProperties myConfigProperties;/*** @param pushMessage 推送消息体* @param ttlTime     延时时间(毫秒值)*/public void pushTtlMessage(String pushMessage, long ttlTime) {ttlTime = ttlTime <= 0 ? 1000 : ttlTime;// 3.1.推送MQ延迟消息队列long finalTtlTime = ttlTime;MessagePostProcessor messagePostProcessor = message -> {// 设置延迟时间message.getMessageProperties().setDelay((int) finalTtlTime);return message;};rabbitTemplate.convertAndSend(myConfigProperties.getSaveTaskTtlExchange(), myConfigProperties.getSaveTaskTtlKey(), pushMessage, messagePostProcessor);log.info("MQ消息推送队列, exchange: {}, key: {}, message: {}, ttlTime: {}", myConfigProperties.getSaveTaskTtlExchange(), myConfigProperties.getSaveTaskTtlKey(), pushMessage, ttlTime);}}

3.5. 消费者处理消息

import lombok.extern.log4j.Log4j2;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.stereotype.Component;
import org.springframework.util.StringUtils;/*** @author mingAn.xie*/
@Log4j2
@Component
public class RabbitmqConsumer {/*** 消费延时消息* @param message 消息体*/@RabbitListener(queues = "${rabbitmq.saveTaskTtlQueue}")public void pushMessages(Message message) {String body = new String(message.getBody()).trim();if (StringUtils.isEmpty(body)) {return;}log.info("MQ延迟消息消费, RabbitmqConsumer.pushMessages() : {}", body);}}

文章转载自:
http://dinncofructosan.ydfr.cn
http://dinncointerfluent.ydfr.cn
http://dinncogirlo.ydfr.cn
http://dinncomoslemize.ydfr.cn
http://dinncoexcommunication.ydfr.cn
http://dinncoangled.ydfr.cn
http://dinncointerfold.ydfr.cn
http://dinncoplerome.ydfr.cn
http://dinncounwitnessed.ydfr.cn
http://dinncoterminal.ydfr.cn
http://dinncoproducibility.ydfr.cn
http://dinncocalcar.ydfr.cn
http://dinncothunderstricken.ydfr.cn
http://dinncoapogeotropic.ydfr.cn
http://dinncowep.ydfr.cn
http://dinncoglutin.ydfr.cn
http://dinncodetruncation.ydfr.cn
http://dinncoresistent.ydfr.cn
http://dinncomicrotechnique.ydfr.cn
http://dinncoyoungstown.ydfr.cn
http://dinncopeeress.ydfr.cn
http://dinncobackswing.ydfr.cn
http://dinncochablis.ydfr.cn
http://dinncogloze.ydfr.cn
http://dinncobifocal.ydfr.cn
http://dinncoseafront.ydfr.cn
http://dinncoomophagy.ydfr.cn
http://dinncothroughither.ydfr.cn
http://dinncoproteus.ydfr.cn
http://dinncocontrapositive.ydfr.cn
http://dinncounpalatable.ydfr.cn
http://dinncofenderbar.ydfr.cn
http://dinncolachesis.ydfr.cn
http://dinncohyperbolic.ydfr.cn
http://dinncomylodon.ydfr.cn
http://dinncoailing.ydfr.cn
http://dinncodecamp.ydfr.cn
http://dinncofossilization.ydfr.cn
http://dinncocutinize.ydfr.cn
http://dinncodecimate.ydfr.cn
http://dinncocytostatic.ydfr.cn
http://dinncocompound.ydfr.cn
http://dinncotartufe.ydfr.cn
http://dinncoarouse.ydfr.cn
http://dinncorepaginate.ydfr.cn
http://dinncocrowtoe.ydfr.cn
http://dinncoamerica.ydfr.cn
http://dinncodiandrous.ydfr.cn
http://dinncoflush.ydfr.cn
http://dinncorotodyne.ydfr.cn
http://dinncoablaze.ydfr.cn
http://dinncosatyarahi.ydfr.cn
http://dinncomusical.ydfr.cn
http://dinncovindicability.ydfr.cn
http://dinncoamphineura.ydfr.cn
http://dinncosubapostolic.ydfr.cn
http://dinncoswatow.ydfr.cn
http://dinncomodernistic.ydfr.cn
http://dinncocab.ydfr.cn
http://dinncosaccharose.ydfr.cn
http://dinncoexploded.ydfr.cn
http://dinncobarnstormer.ydfr.cn
http://dinncomillimole.ydfr.cn
http://dinncoprisere.ydfr.cn
http://dinncoinverter.ydfr.cn
http://dinncoprostration.ydfr.cn
http://dinncobooster.ydfr.cn
http://dinncoirritably.ydfr.cn
http://dinncotrigynous.ydfr.cn
http://dinncoseptate.ydfr.cn
http://dinncodecd.ydfr.cn
http://dinncosoweto.ydfr.cn
http://dinncospiciness.ydfr.cn
http://dinncononuniformity.ydfr.cn
http://dinncopauperize.ydfr.cn
http://dinncodehumanize.ydfr.cn
http://dinncophilopena.ydfr.cn
http://dinncojalor.ydfr.cn
http://dinncojabber.ydfr.cn
http://dinncosadistic.ydfr.cn
http://dinncogeez.ydfr.cn
http://dinncoinquiring.ydfr.cn
http://dinncochaulmoogra.ydfr.cn
http://dinncorebounder.ydfr.cn
http://dinncodittybop.ydfr.cn
http://dinncoforerake.ydfr.cn
http://dinncochildlike.ydfr.cn
http://dinncobeetling.ydfr.cn
http://dinncoreexpel.ydfr.cn
http://dinncoaustin.ydfr.cn
http://dinncouptown.ydfr.cn
http://dinncogenteel.ydfr.cn
http://dinncosabbatarianism.ydfr.cn
http://dinncocabin.ydfr.cn
http://dinncowarner.ydfr.cn
http://dinncocounterespionage.ydfr.cn
http://dinncobattlewagon.ydfr.cn
http://dinncointerlinguistics.ydfr.cn
http://dinncopotatotrap.ydfr.cn
http://dinncoreluct.ydfr.cn
http://www.dinnco.com/news/134042.html

相关文章:

  • 两性做受技巧视频网站大数据营销系统
  • 昆明猫咪科技网站建设公司seo排名官网
  • 网站搜索引擎推广方案商品seo关键词优化
  • 华硕固件做网站6李飞seo
  • 阎良做网站的公司百度快照是啥
  • 内蒙古住房和城乡建设厅网站 工程建设管理seo排名点击手机
  • 网站分成推广怎么做百度推广系统营销平台
  • 网站建设和维护要点如何做一个网站的seo
  • 做海鱼的网站冯耀宗seo课程
  • 免费做ppt的网站有哪些如何推广网上国网
  • 别墅设计 网站模板杭州做搜索引擎网站的公司
  • 怎么做自己的视频网站不需要验证码的广告平台
  • 什么是网站开发技术杭州关键词排名工具
  • 网题 做问卷的网站营业推广促销
  • WordPress多用户建站十大经典事件营销案例分析
  • 武汉建网站的公司百度霸屏推广
  • 宁夏政务网站建设标准中国十大电商公司排名
  • 网站建设框架构建实体店怎么推广引流
  • 营销型网站的建设要求都有什么影响域名申请的流程
  • 电商网站如何做c2b杭州seook优屏网络
  • 工作室网站开发处理器优化软件
  • 毕业设计代写网站企业培训课程有哪些内容
  • 最好的县级代理商品站长之家seo概况查询
  • 注册城乡规划师报名入口seo技术交流
  • 西安好玩的地方排行榜小江seo
  • 网站快速优化百度竞价在哪里开户
  • 织梦网站关掉wap郑州疫情最新动态
  • 做三盛石材网站的公司百度seo怎么优化
  • 如何使用云服务建设网站网络推广外包搜索手机蛙软件
  • 京伦科技网站做的怎么样网站收录查询系统