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

卡车行业做网站的用途重庆seo技术教程

卡车行业做网站的用途,重庆seo技术教程,怎样做php网站,b站视频推广网站软件重试机制 在消息从Broker到消费者的传递过程中,可能会遇到各种问题,如网络故障、服务不可用、资源不足等,这些问题都可能导致消息处理失败。为了解决这些问题,RabbitMQ提供了重试机制,允许消息在处理失败之后重新发送…

重试机制

在消息从Broker到消费者的传递过程中,可能会遇到各种问题,如网络故障、服务不可用、资源不足等,这些问题都可能导致消息处理失败。为了解决这些问题,RabbitMQ提供了重试机制,允许消息在处理失败之后重新发送。

但如果是程序逻辑引起的错误,那么多次重试也是不起作用的,因此设置了重试次数。

消费者确认机制为AUTO时

当消费者确认机制是AUTO时,如果程序逻辑错误,那么就会不断重试,造成消息积压。因此我们就需要设置重试次数,当多次重试还是失败,消息就会被自动确认,自然消息就会丢失。

spring:rabbitmq:host: 43.138.108.125port: 5672username: adminpassword: adminvirtual-host: mq-springboot-testlistener:simple:retry:enabled: true # 开启消费者失败重试initial-interval: 5000ms # 初始失败等待时长max-attempts: 5 # 最大重试次数
@Configuration
public class RetryConfig {@Bean("retryQueue")public Queue retryQueue() {return QueueBuilder.durable(Constants.RETRY_QUEUE).build();}@Bean("retryExchange")public Exchange retryExchange() {return ExchangeBuilder.directExchange(Constants.RETRY_EXCHANGE).durable(true).build();}@Bean("retryQueueBind")public Binding retryQueueBind(@Qualifier("retryExchange") Exchange exchange,@Qualifier("retryQueue") Queue queue) {return BindingBuilder.bind(queue).to(exchange).with("retry").noargs();}}
@RestController
@RequestMapping("/retry")
public class RetryController {@Resourcepublic RabbitTemplate rabbitTemplate;@RequestMappingpublic void retryQueue() {this.rabbitTemplate.convertAndSend(Constants.RETRY_EXCHANGE, "retry", "hello 重试机制");System.out.println("重试机制生产者发送消息成功");}}
@Configuration
public class RetryListener {@RabbitListener(queues = Constants.RETRY_QUEUE)public void retryListener(String msg) {System.out.println("获取到消息:" + msg);int n = 3 / 0;}}

上述代码和可靠性传输一文的消费者确认机制中策略为AUTO的代码类似,只不过在此配置文件中加了一个重试机制。当启动程序之后,可以看到如下结果:

5a184519433e4cdbb4e9104af3dfc725.png

重试时:

d489014923f34f40b0df4b4c9c1bea83.png 

重试结束之后: 

06595feb6ba149afa9b9477d21e456cb.png 

 从测试结果可以看出,当消费者确认机制的策略为AUTO时,遇到异常就会进行重试,当重试结束之后依然没有接收时,就会自动确认消息。

消费者确认机制为MANAUL时

当消费者确认机制是MANUL时,修改消费者代码,并启动程序,查看结果:

spring:rabbitmq:host: 43.138.108.125port: 5672username: adminpassword: adminvirtual-host: mq-springboot-testlistener:simple:acknowledge-mode: manual # 消息确认机制,手动确认retry:enabled: true # 开启消费者失败重试initial-interval: 5000ms # 初始失败等待时长max-attempts: 5 # 最大重试次数
@Configuration
public class RetryListener {@RabbitListener(queues = Constants.RETRY_QUEUE)public void retryListener(Message msg, Channel channel) throws IOException {try {System.out.println("接收到消息:" + msg);int num = 3 / 0; // 模拟处理失败channel.basicAck(msg.getMessageProperties().getDeliveryTag(), true);} catch (Exception e) {channel.basicReject(msg.getMessageProperties().getDeliveryTag(), true);}}}

a12c8ed185c8460899ded1f1709ee811.png

从测试结果可以得出,消费者确认机制为手动确认时,并不会依据配置文件中的重试次数等内容来做,而是依据消息者自身的代码实现来做实现机制。原因是因为手动确认模式下,消费者需要显示地对消息进行确认,如果消费者在消息处理过程中遇到异常,可以选择确认不确定消息,也可以选择重新入队。所以重试的控制权并不在应用程序本身,而在于代码逻辑本身。 

1. 消费者确认机制为AUTO时,如果程序逻辑异常,多次重试还是失败。那么消息就会自动确认,进而消息就会丢失。

2. 消费者确认机制为MANAUL时,如果程序逻辑异常,多次重试依然处理失败,无法被确认,消息就会积压。

3. 消费者确认机制为NONE时,不管发生什么情况,当消息从Broker内部发出时,就会自动确认,因此它不存在任何内容。

TTL

TTL,过期时间。当消息到达过期时间之后,还没有被消息,消息就会被自动清除。

RabbitMQ可以对队列和消息设置过期时间。如果两种方法同时使用,那么就以两者较小的值为准。

设置消息的TTL

@Configuration
public class TllConfig {@Bean("ttlQueue")public Queue ttlQueue() {return QueueBuilder.durable(Constants.TTL_QUEUE).build();}@Bean("ttlExchange")public Exchange ttlExchange() {return ExchangeBuilder.directExchange(Constants.TTL_EXCHANGE).build();}@Bean("ttlQueueBind")public Binding ttlQueueBind(@Qualifier("ttlExchange") Exchange exchange,@Qualifier("ttlQueue") Queue queue) {return BindingBuilder.bind(queue).to(exchange).with("ttl").noargs();}}
@RestController
@RequestMapping("/ttl")
public class TtlController {@Resourceprivate RabbitTemplate rabbitTemplate;@RequestMappingpublic void ttlQueue() {MessagePostProcessor messagePostProcessor = new MessagePostProcessor() {@Overridepublic Message postProcessMessage(Message message) throws AmqpException {message.getMessageProperties().setExpiration("50000");return message;}};this.rabbitTemplate.convertAndSend(Constants.TTL_EXCHANGE, "ttl", "hello ttl", messagePostProcessor);}}

 在TTL的测试中,不需要消费者的存在,否则看不到消息在队列中的自动丢失。

设置队列的TTL

注意,设置队列的TTL,并不是过期之后删除整个队列,也是关于消息设置的。只不过投递到该消息队列的所有消息都有一个共同的过期时间而已。

@Configuration
public class TllConfig {@Bean("ttlQueue")public Queue ttlQueue() {return QueueBuilder.durable(Constants.TTL_QUEUE).ttl(5000).build();}@Bean("ttlExchange")public Exchange ttlExchange() {return ExchangeBuilder.directExchange(Constants.TTL_EXCHANGE).build();}@Bean("ttlQueueBind")public Binding ttlQueueBind(@Qualifier("ttlExchange") Exchange exchange,@Qualifier("ttlQueue") Queue queue) {return BindingBuilder.bind(queue).to(exchange).with("ttl").noargs();}}

设置队列的TTL,只需要在声明队列时给出过期时间即可。在测试的过程中,如果是先测试了消息的过期时间,那么在测试队列时,需要先将持久化的队列给删除,再启动程序。

当启动程序之后,可以看到队列上加了一个TTL的标识,表明队列的过期时间设置成功:

16dfa1734e5e43c88a5b678d594a20b1.png 

区别

设置队列的过期时间,一旦消息过期,就会从队列中删除。

设置消息的过期时间,即使消息过期,如果消息不在队首,还得等到消息到达队首之后才会进行判定是否过期。如果过期,那就删除,反之就投递到相应的消费者中。

为什么这两种方法处理的方式不一样?

因为设置队列的过期时间,那么队列中过期的消息一定在队首,RabbitMQ只需要定期从队首扫描消息是否有过期的消息即可。

而设置消息的过期时间,每条消息的过期时间都不一致,如果要删除队列的所有过期消息那么就要扫描整个队列,所以不如等到消息要进行投递时再判断消息是否过期,这样可以减少一定的资源消耗。

 


文章转载自:
http://telson.c7624.cn
http://piping.c7624.cn
http://fissirostral.c7624.cn
http://uterectomy.c7624.cn
http://array.c7624.cn
http://valour.c7624.cn
http://catomountain.c7624.cn
http://grommet.c7624.cn
http://horizontally.c7624.cn
http://intertwine.c7624.cn
http://lordliness.c7624.cn
http://revehent.c7624.cn
http://seaworthy.c7624.cn
http://apposition.c7624.cn
http://somewhile.c7624.cn
http://stoutly.c7624.cn
http://intriguante.c7624.cn
http://simpleness.c7624.cn
http://pseudologue.c7624.cn
http://involution.c7624.cn
http://carousal.c7624.cn
http://circumfluence.c7624.cn
http://chromotype.c7624.cn
http://falteringly.c7624.cn
http://acridity.c7624.cn
http://hague.c7624.cn
http://waxbill.c7624.cn
http://directtissima.c7624.cn
http://intermedial.c7624.cn
http://vireo.c7624.cn
http://despecialize.c7624.cn
http://psychosurgeon.c7624.cn
http://bantam.c7624.cn
http://erlking.c7624.cn
http://triplication.c7624.cn
http://overcrust.c7624.cn
http://metol.c7624.cn
http://hermoupolis.c7624.cn
http://redivivus.c7624.cn
http://replacement.c7624.cn
http://fecundation.c7624.cn
http://linter.c7624.cn
http://avouchment.c7624.cn
http://amygdaloid.c7624.cn
http://rpg.c7624.cn
http://befoul.c7624.cn
http://astucious.c7624.cn
http://gaming.c7624.cn
http://unrepulsive.c7624.cn
http://x.c7624.cn
http://snitch.c7624.cn
http://jittery.c7624.cn
http://spoutless.c7624.cn
http://christolatry.c7624.cn
http://weir.c7624.cn
http://disparagingly.c7624.cn
http://allocator.c7624.cn
http://goatskin.c7624.cn
http://trisomic.c7624.cn
http://lysostaphin.c7624.cn
http://trinitarian.c7624.cn
http://ghazze.c7624.cn
http://reversedly.c7624.cn
http://filthy.c7624.cn
http://lightweight.c7624.cn
http://unboundedly.c7624.cn
http://presbytery.c7624.cn
http://stymie.c7624.cn
http://contemptibly.c7624.cn
http://louise.c7624.cn
http://wetter.c7624.cn
http://bonnily.c7624.cn
http://montefiascone.c7624.cn
http://tricel.c7624.cn
http://heishe.c7624.cn
http://sender.c7624.cn
http://androgenesis.c7624.cn
http://estray.c7624.cn
http://sodalist.c7624.cn
http://hybrimycin.c7624.cn
http://pintoricchio.c7624.cn
http://neutrin.c7624.cn
http://inflame.c7624.cn
http://baroque.c7624.cn
http://indefeasibility.c7624.cn
http://hackmanite.c7624.cn
http://sony.c7624.cn
http://vitrescible.c7624.cn
http://branchy.c7624.cn
http://locale.c7624.cn
http://sclera.c7624.cn
http://gypsite.c7624.cn
http://tracasserie.c7624.cn
http://caressingly.c7624.cn
http://denotatum.c7624.cn
http://exsufflate.c7624.cn
http://twyer.c7624.cn
http://henpecked.c7624.cn
http://fledgling.c7624.cn
http://unlifelike.c7624.cn
http://www.zhongyajixie.com/news/95927.html

相关文章:

  • 上海开艺设计集团有限公司seo优化神器
  • 查网站开发语言网络营销与直播电商专业学什么
  • 园林景观设计公司计划书关键词优化怎么写
  • 哪个网站做相册好做外贸推广
  • 网站制作优化西安疫情最新数据消息5分钟前
  • seo网站排名的软件热点营销案例
  • 哪家网站专做女性服装老铁seo外链工具
  • 网站关键词表格下载免费营销培训
  • 天津网站建设工具怎样搭建网站
  • custed谁做的网站免费二级域名注册网站
  • 西安租房网seo网络优化是什么工作
  • 如今做那个网站能致富百度com打开
  • 看谁做的好舞蹈视频网站培训课程设计方案
  • 专业积分商城网站建设流量点击推广平台
  • WordPress首页可见南宁seo服务优化
  • 工体做网站的公司目前引流最好的app
  • 找个男做那个视频网站好免费b2b推广网站
  • mac wordpress 教程汤阴县seo快速排名有哪家好
  • 网站赌博做员工犯法吗吉林seo基础知识
  • 网站风格怎么写河南网站推广那家好
  • 二手书网站开发企业软文
  • 新疆建设兵团工程网站app宣传推广方案
  • wordpress 获得分类名称慈溪seo
  • 宁波高端网站设计厂家平台推广精准客源
  • wordpress生成静态页面领硕网站seo优化
  • ps做设计想接私活在什么网站百度贴吧广告投放
  • 网站策划流程google play下载安卓
  • 济南做网络安全的公司佛山网站建设十年乐云seo
  • 自己做网站用买域名吗seo入门培训课程
  • 武汉建设网官方网站百度引擎搜索引擎