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

昆明网站建设c3sales软文推广案例

昆明网站建设c3sales,软文推广案例,muse怎么做响应式网站,小辣椒昆明网站开发目录 一、引言 二、RepublishMessageRecoverer 实现 2.1. 实现步骤 2.2. 实现代码 2.2.1. 异常交换机队列回收期配置类 2.2.2. 常规交换机队列配置类 2.2.3. 消费者代码 2.2.4. 消费者yml配置 2.2.5. 生产者代码 2.2.6. 生产者yml配置 2.2.7. 运行效果 一、引言 …

目录

一、引言

二、RepublishMessageRecoverer 实现

2.1. 实现步骤

2.2. 实现代码

2.2.1. 异常交换机队列回收期配置类

2.2.2. 常规交换机队列配置类 

2.2.3. 消费者代码

2.2.4. 消费者yml配置 

2.2.5. 生产者代码 

2.2.6. 生产者yml配置 

 2.2.7. 运行效果


一、引言

Spring AMQP提供了消费者失败重试机制,在消费者出现异常时利用本地重试,而不是无限地requeue到mq。我们可以通过在application.yaml文件中添加配置来开启重试机制:

spring:rabbitmq:host: 127.0.0.1port: 5672username: Wangzhexiaopassword: Wangzhexiaovirtual-host: /hangzhoulistener:simple:prefetch: 1acknowledge-mode: manual # none,关闭ack;manual,手动ack;auto:自动ack# 消费者重试机制配置retry:enabled: true # 开启消费者失败重试initial-interval: 1000ms # 初始的失败等待时长为1秒multiplier: 1 # 下次失败的等待时长倍数,下次等待时长 = multiplier * last-intervalmax-attempts: 3 # 最大重试次数stateless: true # true无状态;false有状态。如果业务中包含事务,这里改为false

在开启重试模式后,重试次数耗尽,如果消息依然失败,则需要有MessageRecoverer接口来处理,它包含三种不同的实现:

RejectAndDontRequeueRecoverer:重试耗尽后,直接reject,丢弃消息(默认方式)

ImmediateRequeueMessageRecoverer:重试耗尽后,返回nack,消息重新入队

RepublishMessageRecoverer:重试耗尽后,将失败消息投递到指定的交换机(推荐)

二、RepublishMessageRecoverer 实现

在实际项目的生产环境中,通过 RepublishMessageRecoverer 方式我们可以定义一个异常队列和交换机,来接收其他交换机队列转发的无法处理的异常消息。然后我们可以查看其中的异常消息并进行人工处理。

2.1. 实现步骤

1. 将失败处理策略改为RepublishMessageRecoverer

2. 定义接收失败消息的交换机、队列及其绑定关系

3. 定义RepublishMessageRecoverer

2.2. 实现代码

2.2.1. 异常交换机队列回收期配置类

package com.example.consumer;import jakarta.annotation.Resource;
import org.springframework.amqp.core.*;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.amqp.rabbit.retry.MessageRecoverer;
import org.springframework.amqp.rabbit.retry.RepublishMessageRecoverer;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;/*** 异常交换机/队列/消息回收器配置类* ConditionalOnProperty 通过yml中的重试配置来选择该配置类是否启用*/
@Configuration
@ConditionalOnProperty(prefix = "spring.rabbitmq.listener.simple.retry", name = "enabled", havingValue = "true")
public class ErrorConfig {@Resourceprivate RabbitTemplate rabbitTemplate;@BeanQueue errorQueue() {return new Queue("error.queue");}@BeanDirectExchange errorExchange() {return new DirectExchange("error.direct");}@BeanBinding errorBind(Queue errorQueue, DirectExchange errorExchange) {return BindingBuilder.bind(errorQueue).to(errorExchange).with("error");}@Beanpublic MessageRecoverer messageRecoverer() {return new RepublishMessageRecoverer(rabbitTemplate, "error.direct", "error");}
}

2.2.2. 常规交换机队列配置类 

package com.example.consumer;import org.springframework.amqp.core.Queue;
import org.springframework.amqp.core.QueueBuilder;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;/*** 常规的RabbitMQ 交换机/队列绑定配置类*/
@Configuration
public class RabbitMQConfig {@BeanQueue simpleQueue() {// 使用 QueueBuilder 创建一个持久化队列return QueueBuilder.durable("simple.queue").build();}
}

2.2.3. 消费者代码

package com.example.consumer;import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.stereotype.Component;/*** 消费者*/
@Slf4j
@Component
public class SimpleListener {@RabbitListener(queues = "simple.queue")public void listener1(String msg) throws Exception {
//        System.out.println("消费者1:人生是个不断攀登的过程【" + msg + "】");throw new Exception();}
}

2.2.4. 消费者yml配置 

# 消费者application.yml配置
spring:rabbitmq:host: 127.0.0.1port: 5672username: Wangzhexiaopassword: Wangzhexiaovirtual-host: /hangzhoulistener:simple:prefetch: 1acknowledge-mode: auto # none,关闭ack;manual,手动ack;auto:自动ack# 消费者重试机制配置retry:enabled: true # 开启消费者失败重试initial-interval: 1000ms # 初始的失败等待时长为1秒multiplier: 1 # 下次失败的等待时长倍数,下次等待时长 = multiplier * last-intervalmax-attempts: 3 # 最大重试次数stateless: true # true无状态;false有状态。如果业务中包含事务,这里改为false

2.2.5. 生产者代码 

package com.example.publisher;import jakarta.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
import org.junit.jupiter.api.Test;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.boot.test.context.SpringBootTest;/*** 生产者*/
@Slf4j
@SpringBootTest
class PublisherApplicationTests {@Resourceprivate RabbitTemplate rabbitTemplate;@Testvoid test() {rabbitTemplate.convertAndSend("simple.queue", "只要学不死,就往死里学!");}
}

2.2.6. 生产者yml配置 

# 生产者application.yml配置
spring:rabbitmq:# MQ连接配置host: 127.0.0.1port: 5672username: Wangzhexiaopassword: Wangzhexiaovirtual-host: /hangzhou

 2.2.7. 运行效果

最终效果是,我们在消费者的代码逻辑中会抛出异常,消息在反复投递消费失败后被重新入列到我们定义的异常交换机队列中:


文章转载自:
http://disulfuram.bpcf.cn
http://pyrexic.bpcf.cn
http://stonecutter.bpcf.cn
http://resterilize.bpcf.cn
http://latter.bpcf.cn
http://restock.bpcf.cn
http://psychometrical.bpcf.cn
http://joppa.bpcf.cn
http://thallophyte.bpcf.cn
http://xiphura.bpcf.cn
http://lippes.bpcf.cn
http://squawkbox.bpcf.cn
http://survey.bpcf.cn
http://uninquiring.bpcf.cn
http://wince.bpcf.cn
http://projet.bpcf.cn
http://aubade.bpcf.cn
http://labourite.bpcf.cn
http://empyrean.bpcf.cn
http://isthmian.bpcf.cn
http://birth.bpcf.cn
http://naked.bpcf.cn
http://unfrequented.bpcf.cn
http://seppuku.bpcf.cn
http://betting.bpcf.cn
http://rockwork.bpcf.cn
http://dhol.bpcf.cn
http://cognoscible.bpcf.cn
http://frowardly.bpcf.cn
http://athematic.bpcf.cn
http://waster.bpcf.cn
http://strasbourg.bpcf.cn
http://sendmail.bpcf.cn
http://puckish.bpcf.cn
http://language.bpcf.cn
http://vijayawada.bpcf.cn
http://woodruffite.bpcf.cn
http://bandersnatch.bpcf.cn
http://relatum.bpcf.cn
http://stallion.bpcf.cn
http://unfastidious.bpcf.cn
http://cellobiose.bpcf.cn
http://kazachok.bpcf.cn
http://muezzin.bpcf.cn
http://thrift.bpcf.cn
http://uxorious.bpcf.cn
http://patriarchic.bpcf.cn
http://cocainization.bpcf.cn
http://blest.bpcf.cn
http://cottonopolis.bpcf.cn
http://infiltrative.bpcf.cn
http://chorine.bpcf.cn
http://intervein.bpcf.cn
http://karabiner.bpcf.cn
http://melodize.bpcf.cn
http://copulation.bpcf.cn
http://birotation.bpcf.cn
http://computery.bpcf.cn
http://glycosaminoglycan.bpcf.cn
http://scarifier.bpcf.cn
http://galatea.bpcf.cn
http://reveler.bpcf.cn
http://decampment.bpcf.cn
http://mainsail.bpcf.cn
http://plurally.bpcf.cn
http://italic.bpcf.cn
http://penetrable.bpcf.cn
http://dolerite.bpcf.cn
http://calumniate.bpcf.cn
http://blessedness.bpcf.cn
http://ribald.bpcf.cn
http://thumb.bpcf.cn
http://glucinium.bpcf.cn
http://discourse.bpcf.cn
http://colicinogeny.bpcf.cn
http://cyanogen.bpcf.cn
http://mechanistic.bpcf.cn
http://penetration.bpcf.cn
http://hemstitch.bpcf.cn
http://preseason.bpcf.cn
http://approbatory.bpcf.cn
http://pyrrho.bpcf.cn
http://carnal.bpcf.cn
http://stuntwoman.bpcf.cn
http://bird.bpcf.cn
http://misspoke.bpcf.cn
http://consols.bpcf.cn
http://shot.bpcf.cn
http://gossipist.bpcf.cn
http://modificative.bpcf.cn
http://gimpy.bpcf.cn
http://interus.bpcf.cn
http://elitist.bpcf.cn
http://tactless.bpcf.cn
http://conglobulate.bpcf.cn
http://gubernatorial.bpcf.cn
http://magpie.bpcf.cn
http://subcool.bpcf.cn
http://jarrah.bpcf.cn
http://chivalry.bpcf.cn
http://www.15wanjia.com/news/103590.html

相关文章:

  • 做app模板网站有哪些内容自媒体怎么赚钱
  • 市场营销的十大理论苏州排名搜索优化
  • 有专业做网站的吗网站公司市场营销方案
  • 太原建站公司有哪些百度动态排名软件
  • 想做个网站 怎么做的杭州seo推广服务
  • 网站优化推广怎么做网络营销的手段有哪些
  • 网站建设的职称百度搜索风云榜电脑版
  • 吉林省建设集团有限公司网站google下载官网
  • 网站维护是谁做的做网站的步骤
  • 怎么给网站制作二维码哪个平台可以免费发广告
  • 域名申请好了 怎么做网站优书网首页
  • 建成学校网站中国最新消息新闻
  • 程序员自己做项目的网站个人怎么做互联网推广平台
  • 专做婴儿的网站互联网+营销策略怎么写
  • 怎么利用花生壳做自己的网站seo人人网
  • 静态页面网站怎么做百度首页官网
  • 给孩子做衣服的网站百度营消 营销推广
  • 杭州网站开发公司外包公司是什么意思
  • 成都有哪些软件开发公司seo技术交流
  • ps做全屏网站画布要多大百度seo新规则
  • 浙江省杭州市建设厅网站营销网站建设制作
  • 郑州网站制作公司360竞价推广怎么做
  • 傻瓜式网站开发软件友情链接查询结果
  • 网站建设制作方案个人网站制作
  • 如何做律所网站排名网站
  • tiktok官网版下载百度seo怎么做网站内容优化
  • 深圳响应式网站建设网站优化 福州
  • 企石镇网站仿做百度推广怎么运营
  • 网站赚流量关键词优化一般收费价格
  • 连云港网站优化方案宣传推广策略