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

网站维护内容台州关键词优化服务

网站维护内容,台州关键词优化服务,买了两台服务器可以做网站吗,wordpress建一个网站在初学rabbitMq的时候,伙伴们肯定已经接触到了“发布确认”的概念,但是到了后期学习中,会接触到“springboot”中使用“发布确认”高级的概念。后者主要是解决什么问题呢?或者是什么样的场景引出这样的概念呢? 在生产环…

在初学rabbitMq的时候,伙伴们肯定已经接触到了“发布确认”的概念,但是到了后期学习中,会接触到“springboot”中使用“发布确认”高级的概念。后者主要是解决什么问题呢?或者是什么样的场景引出这样的概念呢?

在生产环境中由于一些不明原因,导致 rabbitmq 重启,在 rabbitmq 重启期间生产者投递失败,导致消息丢失,需要手动处理和恢复。因此为了确保rabbitmq 的消息可靠投递,特别是在这样比较极端的情况,rabbitmq 集群不可用的时候,对无法投递的消息进行处理。

废话不说直接开始撸代码!!!在代码中解决实际问题~

一、代码架构分析:

        接触到这里,对于一条完整的“rabbitmq消息”发布链的构成大家已经不陌生了。主要是由:“消息生产者”、“交换机”、“队列”、“消费者”四个方面构成,如图所示:

二、构造“配置类”代码: 

声明交换机“confirm_exchange”、声明队列“confirm_queue”、通过routing-key对交换机和队列进行绑定。

package com.example.rabbitmq_demo.fabuquerengaoji;import org.springframework.amqp.core.*;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;/*** @ClaseName: ConfirmConfig$* @Description:配置类 发布确认(高级)* @Author: wuhs* @Date: 2023/8/16$ 14:32$* 快捷键ctrl+shift+u  字母大小写转化*/
@Configuration
public class ConfirmConfig {// 交换机public static final String CONFIRM_EXCHANGE_NAME = "confirm_exchange";// 队列public static final String CONFIRM_QUEUE_NAME = "confirm_queue";// ROUTING-KEYpublic static final String CONFIRM_ROUTING_KEY = "key1";@Beanpublic DirectExchange confirmExchange() {return new DirectExchange(CONFIRM_EXCHANGE_NAME);}@Beanpublic Queue confirmQueue() {return QueueBuilder.durable(CONFIRM_QUEUE_NAME).build();}@Beanpublic Binding queueBindingExchange(@Qualifier("confirmExchange") DirectExchange directExchange,@Qualifier("confirmQueue") Queue queue) {//一般使用在项目中使用@Qualifier来限定注入的Bean。return BindingBuilder.bind(queue).to(directExchange).with(CONFIRM_ROUTING_KEY);}
}

三、构建消费者代码:

通过@RabbitListener(queues = ConfirmConfig.CONFIRM_QUEUE_NAME)来监听队列,以此“充当”消费者。这一块也没啥好说的,直接上代码!

package com.example.rabbitmq_demo.fabuquerengaoji;import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.stereotype.Component;/*** @ClaseName: Consumer$* @Description:消费者* @Author: wuhs* @Date: 2023/8/16$ 15:18$】*/
@Slf4j
@Component
public class Consumer {@RabbitListener(queues = ConfirmConfig.CONFIRM_QUEUE_NAME)public void reciverConfirmMessage(Message message) {String msg = new String(message.getBody());log.info("接收到的队列confirm.queue消息:{}", msg);}
}

四、创建“回调”方法

        在最开始我们说到“确保rabbitmq 的消息可靠投递”的概念,那么具体如何确保呢?如果我们在消费者每次消费成功、未消费成功交换机都能进行“回调”确认,是不是就能知道哪些消息消费成功、哪些没有消费成功呢?

        在RabbitTemplate中有一个方法接口(ConfirmCallback),我们只需要实现这个接口并实现“confirm”方法,并将它注入进RabbitTemplate工具中即可创建“回调”。具体代码如下:

package com.example.rabbitmq_demo.fabuquerengaoji;import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.rabbit.connection.CorrelationData;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;import javax.annotation.PostConstruct;/*** @ClaseName: MyCallBack$* @Description:* @Author: wuhs* @Date: 2023/8/16$ 16:17$*/
@Slf4j
@Component
public class MyCallBack implements RabbitTemplate.ConfirmCallback {@Autowiredprivate RabbitTemplate rabbitTemplate;@PostConstructpublic void init() {// 注入rabbitTemplate.setConfirmCallback(this);}//交换机确认回调方法  @Overridepublic void confirm(CorrelationData correlationData, boolean ack, String reason) {String id = correlationData != null ? correlationData.getId() : "";if (ack) {//发消息 交换机接收到了消息 回调log.info("交换机已经收到了ID为:{}的消息", id);} else {//发消息 交换机没有接收到了消息 回调log.info("交换机没有收到了ID为:{}发的的消息,失败的原因是:{}", id, reason);}}
}

confirm方法参数介绍:

* 1. correlationData 保存回调消息的ID及相关信息
* 2. 交换机是否收到了消息 ack=true(收到)、ack=false(未收到)
* 3. reason 失败的原因

 六、配置类声明:(application.yml)

        在这里需要注意!!这也是最容易踩得坑,不知道有没有小伙伴遇没遇到,“publisher-confirm-type: correlated”也声明了,但是项目创建启动发布消息之后“没有成功回调”的情况,查看了很多的文章,很多博主只配置了publisher-confirm-type、但是并没有开启“confirm 确认机制”,所以会存在“误导”,导致一直找不到失败的原因~具体正确配置,看代码:

server:port: 8899spring:rabbitmq:host: 124.221.94.214port: 5672username: xgsmpassword: xgsm123# 发送者开启 confirm 确认机制publisher-confirms: truepublisher-confirm-type: correlated

 publisher-confirm-type参数介绍:

publisher-confirm-type这个参数一共有三种配置方法:

# NONE:禁用发布确认,是默认值。

# CORRELATED:发布消息后,交换机会触发回调方法。

# SIMPLE:有两种效果:

1:和CORRELATED一样会触发回调方法

2:发布消息成功后使用 rabbitTemplate 调用 waitForConfirms 或waitForConfirmsOrDie 方法等待 broker 节点返回发送结果,根据返回结果来判定下一步的逻辑,

# 要注意的点是waitForConfirmsOrDie 方法如果返回 false 则会关闭 channel,则接下来无法发送消息到 broker。

 七、创建Controller层(消息生产者)

        这里演示三种情况。第一种为正常情况下,发送成功后的回调;第二种消息为发送失败、当交换机不存在则发送失败(模拟发送失败),所以将交换机名称修改即可

package com.example.rabbitmq_demo.fabuquerengaoji;import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.rabbit.connection.CorrelationData;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;/*** @ClaseName: ProducerController$* @Description:消息生产者* @Author: wuhs* @Date: 2023/8/16$ 14:58$*/
@Slf4j
@RestController
@RequestMapping("/confirm")
public class ProducerController {@Autowiredprivate RabbitTemplate rabbitTemplate;// @PathVariable主要作用:映射URL绑定的占位符@RequestMapping("/sendMessage/{message}")public void sendMessage(@PathVariable String message) {//正常发送CorrelationData correlationData = new CorrelationData("1");rabbitTemplate.convertAndSend(ConfirmConfig.CONFIRM_EXCHANGE_NAME, ConfirmConfig.CONFIRM_ROUTING_KEY, message,correlationData);//发送失败-交换机不存在的情况CorrelationData correlationData2 = new CorrelationData("2");rabbitTemplate.convertAndSend(ConfirmConfig.CONFIRM_EXCHANGE_NAME+"2", ConfirmConfig.CONFIRM_ROUTING_KEY, message,correlationData2);log.info("发送的消息为:{}", message);}
}

测试结果: 

 如果是routing-key错误,这种情况会触发回调嘛?让我们验证一下;修改routing-key为“错误值”

  CorrelationData correlationData3 = new CorrelationData("3");rabbitTemplate.convertAndSend(ConfirmConfig.CONFIRM_EXCHANGE_NAME, ConfirmConfig.CONFIRM_ROUTING_KEY+"key3", message,correlationData3);

测试结果:

         通过结果可以看出,消息发送成功了,而且也触发了“成功的回调”。但是我们知道的是,由于路由失败,这里消费者并没有对消息进行消费,这是为什么呢?那是因为,在仅开启了生产者确认机制的情况下,交换机接收到消息后,会直接给消息生产者发送确认消息,如果发现该消息不可路由,那么消息会被直接丢弃,此时生产者是不知道消息被丢弃这个事件的。解决方式为:通过设置 mandatory 参数可以在当消息传递过程中不可达目的地时将消息返回给生产者。具体操作如下

1、application.yml文件中添加消息回退配置

# 发送者开启 return 确认机制publisher-returns: true

 2、实现RabbitTemplate中的方法接口ReturnCallback,并实现“returnedMessage”方法,最后将类注入到RabbitTemplate的RabbitTemplate中,详细代码如下:

package com.example.rabbitmq_demo.fabuquerengaoji;import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.rabbit.connection.CorrelationData;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;import javax.annotation.PostConstruct;/*** @ClaseName: MyCallBack$* @Description:* @Author: wuhs* @Date: 2023/8/16$ 16:17$*/
@Slf4j
@Component
public class MyCallBack implements RabbitTemplate.ConfirmCallback, RabbitTemplate.ReturnCallback {@Autowiredprivate RabbitTemplate rabbitTemplate;@PostConstructpublic void init() {// 注入rabbitTemplate.setConfirmCallback(this);rabbitTemplate.setReturnCallback(this);}/*** 交换机确认回调方法* 1、发消息 交换机接收到了消息 回调* 1.1 correlationData 保存回调消息的ID及相关信息* 1.2 交换机收到消息 ack=true* 2、发消息 交换机接收失败了 回调* 2.1 correlationData 保存回调消息的ID及相关信息* 2.2 交换机接收到消息 ack=false* 2.3 reason 失败的原因*/@Overridepublic void confirm(CorrelationData correlationData, boolean ack, String reason) {String id = correlationData != null ? correlationData.getId() : "";if (ack) {log.info("交换机已经收到了ID为:{}的消息", id);} else {log.info("交换机没有收到了ID为:{}发的的消息,失败的原因是:{}", id, reason);}}//可以在当消息传递的过长中不可达目的地时将消息返回给生产者// 只有不可待目的地的时候 才进行回退@Overridepublic void returnedMessage(Message message, int replyCode, String replyText, String exchange, String routingKey) {log.info("消息{},被交换机{}退回,退回原因:{},路由key:{}", message, exchange, replyText, routingKey);}
}

测试结果:

2023-08-17 10:36:32.476  INFO 21108 --- [221.94.214:5672] c.e.r.fabuquerengaoji.MyCallBack : 消息(Body:'消息确认发布测试' MessageProperties [headers={spring_returned_message_correlation=3}, contentType=text/plain, contentEncoding=UTF-8, contentLength=0, receivedDeliveryMode=PERSISTENT, priority=0, deliveryTag=0]),被交换机confirm_exchange退回,退回原因:NO_ROUTE,路由key:key1key3

问题解决!~ 


文章转载自:
http://dinncochampion.stkw.cn
http://dinncoattainable.stkw.cn
http://dinncoauteur.stkw.cn
http://dinncocurmudgeon.stkw.cn
http://dinncobreechblock.stkw.cn
http://dinncohydremic.stkw.cn
http://dinncostatic.stkw.cn
http://dinncohalves.stkw.cn
http://dinncopotassic.stkw.cn
http://dinncorubigo.stkw.cn
http://dinncohorsejockey.stkw.cn
http://dinncotuberculation.stkw.cn
http://dinncocinder.stkw.cn
http://dinncoinconsequentia.stkw.cn
http://dinncoparody.stkw.cn
http://dinncofleetingly.stkw.cn
http://dinncocrotched.stkw.cn
http://dinncostopcock.stkw.cn
http://dinncoretiree.stkw.cn
http://dinncocatridges.stkw.cn
http://dinnconodosity.stkw.cn
http://dinncobantering.stkw.cn
http://dinncoumptieth.stkw.cn
http://dinncotricentenary.stkw.cn
http://dinncomarrier.stkw.cn
http://dinncocarton.stkw.cn
http://dinncoovershoe.stkw.cn
http://dinncometestrum.stkw.cn
http://dinncohogged.stkw.cn
http://dinncoessen.stkw.cn
http://dinncoisotone.stkw.cn
http://dinncogadget.stkw.cn
http://dinncogerminative.stkw.cn
http://dinncoexeter.stkw.cn
http://dinncoconsequentially.stkw.cn
http://dinncoinadmissible.stkw.cn
http://dinncodunk.stkw.cn
http://dinncoquadro.stkw.cn
http://dinncogrumble.stkw.cn
http://dinncopourparler.stkw.cn
http://dinncofeterita.stkw.cn
http://dinncoswob.stkw.cn
http://dinncorobinsonade.stkw.cn
http://dinncoseropurulent.stkw.cn
http://dinncobusk.stkw.cn
http://dinncoam.stkw.cn
http://dinncoloch.stkw.cn
http://dinncoeparchy.stkw.cn
http://dinncoossuarium.stkw.cn
http://dinncocolorway.stkw.cn
http://dinncolistless.stkw.cn
http://dinncometeorogram.stkw.cn
http://dinncounrough.stkw.cn
http://dinncodeputy.stkw.cn
http://dinncounisonant.stkw.cn
http://dinncoexcogitative.stkw.cn
http://dinncorootlike.stkw.cn
http://dinncolethiferous.stkw.cn
http://dinncounlessoned.stkw.cn
http://dinncointerpulse.stkw.cn
http://dinncocounterfeiting.stkw.cn
http://dinncoaposematic.stkw.cn
http://dinncotvp.stkw.cn
http://dinncofurfuraceous.stkw.cn
http://dinncopuzzler.stkw.cn
http://dinncoslam.stkw.cn
http://dinncocrenated.stkw.cn
http://dinncosnobby.stkw.cn
http://dinncochengchow.stkw.cn
http://dinncofalloff.stkw.cn
http://dinnconjorth.stkw.cn
http://dinncoodontoscope.stkw.cn
http://dinncosubchairman.stkw.cn
http://dinncomarinescape.stkw.cn
http://dinncoshale.stkw.cn
http://dinncosubsultive.stkw.cn
http://dinncoheptaglot.stkw.cn
http://dinncoredeemable.stkw.cn
http://dinncopreoviposition.stkw.cn
http://dinncopiggy.stkw.cn
http://dinncotrombone.stkw.cn
http://dinncotriumphal.stkw.cn
http://dinncoesthete.stkw.cn
http://dinncospan.stkw.cn
http://dinncocella.stkw.cn
http://dinncooctet.stkw.cn
http://dinncosaturdays.stkw.cn
http://dinncouneducational.stkw.cn
http://dinncocecf.stkw.cn
http://dinncocomous.stkw.cn
http://dinncononperiodic.stkw.cn
http://dinncocirclorama.stkw.cn
http://dinncoautonetics.stkw.cn
http://dinncobumrap.stkw.cn
http://dinncoalfie.stkw.cn
http://dinncotheopneust.stkw.cn
http://dinnconwt.stkw.cn
http://dinncomuleta.stkw.cn
http://dinncodiuretic.stkw.cn
http://dinncopsychodynamics.stkw.cn
http://www.dinnco.com/news/104321.html

相关文章:

  • 怎么给网站做关键词广州推广seo
  • dw做的网站不显示百度云网盘免费资源
  • 网站毕业作品代做软文外链代发
  • 中国住建网查询资质windows优化大师的特点
  • 企业网站用视频做首页自己的app如何接广告
  • wordpress4.5.3zhcn网站优化排名易下拉软件
  • 公需道德与能力建设培训网站网上营销怎么做
  • 定制软件开发报价徐州seo企业
  • 域名是什么举个例子seo变现培训
  • 中国平面设计网官网seo实战培训课程
  • 新网站优化怎么做百度网盘怎么用
  • 怎样更换网站模板sem优化是什么意思
  • 南宁网站建设加q.479185700如何推广公众号
  • 网站 运营工作如何做杭州百家号优化
  • 建筑案例分析网站免费的推广平台
  • 用web做购物网站怎么做搜索北京seo优化排名推广
  • 公司建网站哪家商业软文
  • 网站建设实施方案ppt爱站关键词挖掘工具
  • 个人网站备案模板福建seo外包
  • 怎么加入网站做微商城百度网盘电脑版下载
  • 英国做电商网站有哪些方面seo新手入门教程
  • 做的网站如何发布全网营销与seo
  • 网站建设询价函格式东莞市网站seo内容优化
  • 电商网站建设实验心得广告网站策划方案
  • 深圳网站开发公司seo网站优化工具大全
  • 微信网站开发哪家好百度人工客服24小时电话
  • 学计算机网站建设seo营销策略
  • 天津网站建设服务怎样在网上推广
  • 如何提升网站收录自己建网站详细流程
  • 安徽网站开发培训seo优化的技巧