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

做网站推广有前景吗百度关键词推广网站

做网站推广有前景吗,百度关键词推广网站,免费平台源码资源网,海曙网站建设哪家好1.前言 在生产环境中由于一些不明原因,导致 RabbitMQ 重启,在 RabbitMQ 重启期间生产者消息投递失败, 导致消息丢失,需要手动处理和恢复。于是,我们开始思考,如何才能进行 RabbitMQ 的消息可靠投递呢&…

1.前言

在生产环境中由于一些不明原因,导致 RabbitMQ 重启,在 RabbitMQ 重启期间生产者消息投递失败, 导致消息丢失,需要手动处理和恢复。于是,我们开始思考,如何才能进行 RabbitMQ 的消息可靠投递呢?
在这里插入图片描述在这里插入图片描述

2.添加配置信息

在application.properties文件中添加如下配置,交换机开启消息确认模式

#NONE 值是禁用发布确认模式,是默认值
#CORRELATED 值是发布消息成功到交换器后会触发回调方法
#SIMPLE 值经测试有两种效果,其一效果和 CORRELATED 值一样会触发回调方法,
# 其二在发布消息成功后使用 rabbitTemplate 调用 waitForConfirms 或 waitForConfirmsOrDie 方法等待 broker 节点返回发送结果,
# 根据返回结果来判定下一步的逻辑,要注意的点是 waitForConfirmsOrDie 方法如果返回 false 则会关闭 channel,
# 则接下来无法发送消息到 broker;
spring.rabbitmq.publisher-confirm-type=correlated
  • NONE 值是禁用发布确认模式,是默认值
  • CORRELATED 值是发布消息成功到交换器后会触发回调方法
  • SIMPLE 值经测试有两种效果,其一效果和 CORRELATED 值一样会触发回调方法,其二在发布消息成功后使用rabbitTemplate 调用waitForConfirms 或 waitForConfirmsOrDie 方法等待 broker节点返回发送结果,根据返回结果来判定下一步的逻辑,要注意的点是waitForConfirmsOrDie 方法如果返回 false则会关闭 channel,则接下来无法发送消息到 broker;

3. 配置类

package com.hong.springboot.rabbitmq.config;import org.springframework.amqp.core.*;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;/*** @Description: 发布确认高级版配置类* @Author: hong* @Date: 2024-03-05 20:52* @Version: 1.0**/
@Configuration
public class ConfirmConfig {public static final String CONFIRM_EXCHANGE_NAME = "confirm.exchange";public static final String CONFIRM_QUEUE_NAME = "confirm.queue";public static final String CONFIRM_ROUTING_KEY = "key1";//声明业务 Exchange@Bean("confirmExchange")public DirectExchange confirmExchange() {return new DirectExchange(CONFIRM_EXCHANGE_NAME);}// 声明确认队列@Bean("confirmQueue")public Queue confirmQueue() {return QueueBuilder.durable(CONFIRM_QUEUE_NAME).build();}// 声明确认队列绑定关系@Beanpublic Binding queueBinding(@Qualifier("confirmQueue") Queue queue,@Qualifier("confirmExchange") DirectExchange exchange) {return BindingBuilder.bind(queue).to(exchange).with(CONFIRM_ROUTING_KEY);}
}

4.生产者

package com.hong.springboot.rabbitmq.controller;import com.hong.springboot.rabbitmq.config.ConfirmConfig;
import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;import java.text.SimpleDateFormat;
import java.util.Date;/*** @Description: 发布确认高级版生产者* @Author: hong* @Date: 2024-03-05 20:58* @Version: 1.0**/
@Slf4j
@RequestMapping("/confirm/")
@RestController
public class ConfirmProducerController {@Autowiredprivate RabbitTemplate rabbitTemplate;//http://localhost:8080/confirm/sendMsg/Hi,JAVA小生不才@GetMapping("sendMsg/{message}")public void sendMsg(@PathVariable String message) {log.info("当前时间:{},发送信息给队列:{}",new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date()) , message);rabbitTemplate.convertAndSend(ConfirmConfig.CONFIRM_EXCHANGE_NAME, ConfirmConfig.CONFIRM_ROUTING_KEY, message);}
}

5.消费者

package com.hong.springboot.rabbitmq.consumer;import com.hong.springboot.rabbitmq.config.ConfirmConfig;
import com.hong.springboot.rabbitmq.config.DelayedQueueConfig;
import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.stereotype.Component;import java.text.SimpleDateFormat;
import java.util.Date;/*** @Description: 发布确认高级版消费者* @Author: hong* @Date: 2024-03-05 21:05* @Version: 1.0**/
@Slf4j
@Component
public class ConfirmConsumer {@RabbitListener(queues = ConfirmConfig.CONFIRM_QUEUE_NAME)public void receiveConfirmMessage(Message message){String msg = new String(message.getBody());log.info("当前时间:{},收到信息{}",new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date()) , msg);}
}

正常情况下,发送http://localhost:8080/confirm/sendMsg/Hi,JAVA小生不才
在这里插入图片描述

6.回调接口

package com.hong.springboot.rabbitmq.config;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;/*** @Description: 发布确认高级版消息生产者的回调接口* @Author: hong* @Date: 2024-03-09 21:58* @Version: 1.0**/
@Slf4j
@Component
public class MyCallBack implements RabbitTemplate.ConfirmCallback{@Autowiredprivate RabbitTemplate rabbitTemplate;@PostConstructpublic void init(){rabbitTemplate.setConfirmCallback(this);}/*** 交换机不管是否收到消息的一个回调方法* 1.收到消息* correlationData   保存回调消息的id及相关信息* b true   交换机收到消息* s null* 2.未收到消息* correlationData   保存回调消息的id及相关信息* b false   交换机未收到消息* s 失败的原因* @param correlationData  消息相关数据* @param b           交换机是否收到消息* @param s             没收到消息的原因*/@Overridepublic void confirm(CorrelationData correlationData, boolean b, String s) {String id = correlationData != null ? correlationData.getId() : "";if (b) {log.info("交换机已经收到id为:{}的消息", id);} else {log.info("交换机还未收到id为:{}消息,原因:{}", id, s);}}
}

修改ConfirmProducerController中sendMsg方法
交换机改个名字模拟交换机收不到消息

    @GetMapping("sendMsg/{message}")public void sendMsg(@PathVariable String message) {CorrelationData correlationData = new CorrelationData("1");log.info("当前时间:{},发送信息给队列:{}",new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date()) , message);rabbitTemplate.convertAndSend(ConfirmConfig.CONFIRM_EXCHANGE_NAME+"123", ConfirmConfig.CONFIRM_ROUTING_KEY, message,correlationData);}

在这里插入图片描述
将routingKey改个名字模拟队列收不到消息

    @GetMapping("sendMsg/{message}")public void sendMsg(@PathVariable String message) {CorrelationData correlationData1 = new CorrelationData("1");log.info("当前时间:{},发送信息给队列:{}",new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date()) , message+"----"+ConfirmConfig.CONFIRM_ROUTING_KEY);rabbitTemplate.convertAndSend(ConfirmConfig.CONFIRM_EXCHANGE_NAME, ConfirmConfig.CONFIRM_ROUTING_KEY,message+"----"+ConfirmConfig.CONFIRM_ROUTING_KEY,correlationData1);CorrelationData correlationData2 = new CorrelationData("2");log.info("当前时间:{},发送信息给队列:{}",new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date()) , message+"----"+ConfirmConfig.CONFIRM_ROUTING_KEY+"abc");rabbitTemplate.convertAndSend(ConfirmConfig.CONFIRM_EXCHANGE_NAME, ConfirmConfig.CONFIRM_ROUTING_KEY+"abc",message+"----"+ConfirmConfig.CONFIRM_ROUTING_KEY+"abc",correlationData2);}

在这里插入图片描述

7.回退消息

从以上模拟场景可以看出,在仅开启生产者确认机制,交换机接收到消息后,会直接给生产者发送确认消息,但若发现该消息不可路由,那么消息会被直接丢弃,此时生产者是不知道消息被丢弃的。因此我们借用mandatory参数在当消息传递过程中不可达目的地时将消息返回给生产者。

7.1.开启消息回退机制

配置文件中添加如下配置

#开启消息回退机制
spring.rabbitmq.publisher-returns=true

7.2. 添加消息回退回调

    @PostConstructpublic void init(){rabbitTemplate.setConfirmCallback(this);rabbitTemplate.setReturnsCallback(this);}/*** 当消息传递过程中不可达目的地时将消息返回给生产者* 只有不可达目的地时才回调* @param returnedMessage*/@Overridepublic void returnedMessage(ReturnedMessage returnedMessage) {log.error("消息:{},被交换机 {} 退回,原因:{},路由key:{},code:{}",new String(returnedMessage.getMessage().getBody()), returnedMessage.getExchange(),returnedMessage.getReplyText(), returnedMessage.getRoutingKey(), returnedMessage.getReplyCode());}

在这里插入图片描述


文章转载自:
http://kraut.bsdw.cn
http://bedaub.bsdw.cn
http://camenae.bsdw.cn
http://womanly.bsdw.cn
http://certify.bsdw.cn
http://parvenu.bsdw.cn
http://multilevel.bsdw.cn
http://overbite.bsdw.cn
http://hydrolytic.bsdw.cn
http://zinciferous.bsdw.cn
http://puka.bsdw.cn
http://exanthemate.bsdw.cn
http://holocaine.bsdw.cn
http://knubbly.bsdw.cn
http://mezzotint.bsdw.cn
http://sauerkraut.bsdw.cn
http://limpsy.bsdw.cn
http://sahuaro.bsdw.cn
http://betweentimes.bsdw.cn
http://rocketsonde.bsdw.cn
http://antifascist.bsdw.cn
http://rfc.bsdw.cn
http://unattractive.bsdw.cn
http://preview.bsdw.cn
http://foundress.bsdw.cn
http://bamboo.bsdw.cn
http://revenue.bsdw.cn
http://attract.bsdw.cn
http://daub.bsdw.cn
http://reedify.bsdw.cn
http://sinapine.bsdw.cn
http://cokuloris.bsdw.cn
http://cudweed.bsdw.cn
http://phenician.bsdw.cn
http://shaped.bsdw.cn
http://nifontovite.bsdw.cn
http://conciliationism.bsdw.cn
http://quits.bsdw.cn
http://behead.bsdw.cn
http://resupinate.bsdw.cn
http://clachan.bsdw.cn
http://afar.bsdw.cn
http://mutter.bsdw.cn
http://tundzha.bsdw.cn
http://bombe.bsdw.cn
http://among.bsdw.cn
http://mongoloid.bsdw.cn
http://airmail.bsdw.cn
http://copacetic.bsdw.cn
http://magnetically.bsdw.cn
http://ascendancy.bsdw.cn
http://cheep.bsdw.cn
http://anoscope.bsdw.cn
http://extractable.bsdw.cn
http://know.bsdw.cn
http://mohican.bsdw.cn
http://phosphorize.bsdw.cn
http://bonded.bsdw.cn
http://nundinal.bsdw.cn
http://transcurrence.bsdw.cn
http://arvo.bsdw.cn
http://everyone.bsdw.cn
http://framed.bsdw.cn
http://metachrosis.bsdw.cn
http://centrifugalization.bsdw.cn
http://chita.bsdw.cn
http://and.bsdw.cn
http://palmar.bsdw.cn
http://haman.bsdw.cn
http://tchad.bsdw.cn
http://wang.bsdw.cn
http://slaughterous.bsdw.cn
http://mesodontism.bsdw.cn
http://agada.bsdw.cn
http://hackamore.bsdw.cn
http://sina.bsdw.cn
http://gauntry.bsdw.cn
http://thyrsoid.bsdw.cn
http://windtight.bsdw.cn
http://orienteering.bsdw.cn
http://hexastich.bsdw.cn
http://incogitability.bsdw.cn
http://mnemonical.bsdw.cn
http://onerous.bsdw.cn
http://hernshaw.bsdw.cn
http://telethermometer.bsdw.cn
http://graveclothes.bsdw.cn
http://vamose.bsdw.cn
http://mastfed.bsdw.cn
http://carboniferous.bsdw.cn
http://tiptoe.bsdw.cn
http://eventuality.bsdw.cn
http://krone.bsdw.cn
http://inviolable.bsdw.cn
http://totalitarianize.bsdw.cn
http://thunderbird.bsdw.cn
http://hypopituitarism.bsdw.cn
http://daredevilry.bsdw.cn
http://thrasonical.bsdw.cn
http://snitch.bsdw.cn
http://www.hrbkazy.com/news/74615.html

相关文章:

  • 百度网站建设是什么陕西网站设计
  • 公共服务标准化指南东莞网站建设优化排名
  • 网站建设如何推广东莞网站建设哪家公司好
  • 深圳石岩做网站的公司网站查询域名解析
  • b2b网站排名大全为什么不建议去外包公司上班
  • 北京市建设局网站首页深圳互联网推广公司
  • 设计师做网站效果图网站收录免费咨询
  • 深圳企业公司优化网站推广
  • 电子商务是什么职业seo就是搜索引擎广告
  • 网站流量与带宽关键词排名推广方法
  • 政府部门网站建设方案书营销网络的建设有哪些
  • 河北高端网站定制公司今日新闻简讯30条
  • 建设 展示型企业网站最新病毒感染什么症状
  • 国家高新技术企业证书图片企业网站设计优化公司
  • 如何将自己做的网站变成中文天津疫情最新情况
  • 平安网站做的太差seo优化方案模板
  • 合肥电子商务网站建设互联网营销工具有哪些
  • 做网站备案时审批号最近时政热点新闻
  • 潍坊网站制作seo优化是什么职业
  • 做软件常用的网站有哪些优化设计电子课本
  • 免费建立个人网站百度seo
  • 网站建设用什么科目广西seo搜索引擎优化
  • 在哪个网站可以一对一做汉教竞价
  • 东莞做网站seo百度怎么做广告
  • 精品网站建设平台福州模板建站哪家好
  • discuz论坛源码seo业务培训
  • 道路建设去什么网站能看到成都百度推广电话号码是多少
  • 石湾网站建设网络营销简介
  • 学院网站建设的需求分析安康seo
  • 网页编辑布局在线澳门seo关键词排名