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

外贸网站建设广州珠海网站建设制作

外贸网站建设广州,珠海网站建设制作,发布广告,大连市建设学校网站目录 一、消息不丢失1.消息确认2.消息确认业务封装2.1 发送确认消息测试2.2 消息发送失败,设置重发机制 一、消息不丢失 消息的不丢失,在MQ角度考虑,一般有三种途径: 1,生产者不丢数据 2,MQ服务器不丢数据…

目录

  • 一、消息不丢失
    • 1.消息确认
    • 2.消息确认业务封装
      • 2.1 发送确认消息测试
      • 2.2 消息发送失败,设置重发机制

一、消息不丢失

消息的不丢失,在MQ角度考虑,一般有三种途径:
1,生产者不丢数据
2,MQ服务器不丢数据
3,消费者不丢数据
保证消息不丢失有两种实现方式:
1,开启事务模式
2,消息确认模式
说明:开启事务会大幅降低消息发送及接收效率,使用的相对较少,因此我们生产环境一般都采取消息确认模式,以下我们只是讲解消息确认模式

1.消息确认

消息持久化
如果希望RabbitMQ重启之后消息不丢失,那么需要对以下3种实体均配置持久化
Exchange
声明exchange时设置持久化(durable = true)并且不自动删除(autoDelete = false)
Queue
声明queue时设置持久化(durable = true)并且不自动删除(autoDelete = false)
message
发送消息时通过设置deliveryMode=2持久化消息

处理消息队列丢数据的情况,一般是开启持久化磁盘的配置。这个持久化配置可以和confirm机制配合使用,你可以在消息持久化磁盘后,再给生产者发送一个Ack信号。这样,如果消息持久化磁盘之前,rabbitMQ阵亡了,那么生产者收不到Ack信号,生产者会自动重发。那么如何持久化呢,其实也很容易,就下面两步:
1、将queue的持久化标识durable设置为true,则代表是一个持久的队列
2、发送消息的时候将deliveryMode=2
这样设置以后,rabbitMQ就算挂了,重启后也能恢复数据

发送确认
有时,业务处理成功,消息也发了,但是我们并不知道消息是否成功到达了rabbitmq,如果由于网络等原因导致业务成功而消息发送失败,那么发送方将出现不一致的问题,此时可以使用rabbitmq的发送确认功能,即要求rabbitmq显式告知我们消息是否已成功发送。

手动消费确认
有时,消息被正确投递到消费方,但是消费方处理失败,那么便会出现消费方的不一致问题。比如:订单已创建的消息发送到用户积分子系统中用于增加用户积分,但是积分消费方处理却都失败了,用户就会问:我购买了东西为什么积分并没有增加呢?
要解决这个问题,需要引入消费方确认,即只有消息被成功处理之后才告知rabbitmq以ack,否则告知rabbitmq以nack

2.消息确认业务封装

service-mq修改配置
开启rabbitmq消息确认配置,在common的配置文件中都已经配置好了!

spring:rabbitmq:host: 192.168.121.140port: 5672username: adminpassword: adminpublisher-confirms-type: correlated  #交换机的确认publisher-returns: true  #队列的确认listener:simple:acknowledge-mode: manual #默认情况下消息消费者是自动确认消息的,如果要手动确认消息则需要修改确认模式为manualprefetch: 1 # 消费者每次从队列获取的消息数量。此属性当不设置时为:轮询分发,设置为1为:公平分发

搭建rabbit-util模块
由于消息队列是公共模块,我们把mq的相关业务封装到该模块,其他service微服务模块都可能使用,因此我们把他封装到一个单独的模块,需要使用mq的模块直接引用该模块即可
搭建方式如:
pom.xml

    <dependencies><!--rabbitmq消息队列--><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-actuator</artifactId></dependency><!--rabbitmq 协议--><dependency><groupId>org.springframework.cloud</groupId><artifactId>spring-cloud-starter-bus-amqp</artifactId></dependency></dependencies>

4.2.4 封装发送端消息确认

/*** @Description 消息发送确认* <p>* ConfirmCallback  只确认消息是否正确到达 Exchange 中* ReturnCallback   消息没有正确到达队列时触发回调,如果正确到达队列不执行* <p>* 1. 如果消息没有到exchange,则confirm回调,ack=false* 2. 如果消息到达exchange,则confirm回调,ack=true* 3. exchange到queue成功,则不回调return* 4. exchange到queue失败,则回调return* */
@Component
@Slf4j
public class MQProducerAckConfig implements RabbitTemplate.ConfirmCallback, RabbitTemplate.ReturnCallback {@Autowiredprivate RabbitTemplate rabbitTemplate;// 修饰一个非静态的void()方法,在服务器加载Servlet的时候运行,并且只会被服务器执行一次在构造函数之后执行,init()方法之前执行。@PostConstructpublic void init() {rabbitTemplate.setConfirmCallback(this);            //指定 ConfirmCallbackrabbitTemplate.setReturnCallback(this);             //指定 ReturnCallback}@Overridepublic void confirm(CorrelationData correlationData, boolean ack, String cause) {if (ack) {log.info("消息发送成功:" + JSON.toJSONString(correlationData));} else {log.info("消息发送失败:" + cause + " 数据:" + JSON.toJSONString(correlationData));}}@Overridepublic void returnedMessage(Message message, int replyCode, String replyText, String exchange, String routingKey) {// 反序列化对象输出System.out.println("消息主体: " + new String(message.getBody()));System.out.println("应答码: " + replyCode);System.out.println("描述:" + replyText);System.out.println("消息使用的交换器 exchange : " + exchange);System.out.println("消息使用的路由键 routing : " + routingKey);}}

封装消息发送

@Service
public class RabbitService {@Autowiredprivate RabbitTemplate rabbitTemplate;/***  发送消息* @param exchange 交换机* @param routingKey 路由键* @param message 消息*/public boolean sendMessage(String exchange, String routingKey, Object message) {rabbitTemplate.convertAndSend(exchange, routingKey, message);return true;}}

2.1 发送确认消息测试

消息发送端

@RestController
@RequestMapping("/mq")
public class MqController {@Autowiredprivate RabbitService rabbitService;/*** 消息发送*///http://localhost:8282/mq/sendConfirm@GetMapping("sendConfirm")public Result sendConfirm() {rabbitService.sendMessage("exchange.confirm", "routing.confirm", "来人了,开始接客吧!");return Result.ok();}
}

消息接收端

@Component
public class ConfirmReceiver {@SneakyThrows
@RabbitListener(bindings=@QueueBinding(value = @Queue(value = "queue.confirm",autoDelete = "false"),exchange = @Exchange(value = "exchange.confirm",autoDelete = "true"),key = {"routing.confirm"}))
public void process(Message message, Channel channel){System.out.println("RabbitListener:"+new String(message.getBody()));// false 确认一个消息,true 批量确认
channel.basicAck(message.getMessageProperties().getDeliveryTag(),false);}
}

测试:http://localhost:8282/mq/sendConfirm

2.2 消息发送失败,设置重发机制

实现思路:借助redis来实现重发机制
模块中添加依赖

<!-- redis -->
<dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-data-redis</artifactId>
</dependency><!-- spring2.X集成redis所需common-pool2-->
<dependency><groupId>org.apache.commons</groupId><artifactId>commons-pool2</artifactId>
</dependency><dependency><groupId>com.alibaba</groupId><artifactId>fastjson</artifactId>
</dependency>

自定义一个实体类来接收消息

@Data
public class GmallCorrelationData extends CorrelationData {//  消息主体private Object message;//  交换机private String exchange;//  路由键private String routingKey;//  重试次数private int retryCount = 0;//  消息类型  是否是延迟消息private boolean isDelay = false;//  延迟时间private int delayTime = 10;
}

修改发送方法

//  封装一个发送消息的方法
public Boolean sendMsg(String exchange,String routingKey, Object msg){//  将发送的消息 赋值到 自定义的实体类GmallCorrelationData gmallCorrelationData = new GmallCorrelationData();//  声明一个correlationId的变量String correlationId = UUID.randomUUID().toString().replaceAll("-","");gmallCorrelationData.setId(correlationId);gmallCorrelationData.setExchange(exchange);gmallCorrelationData.setRoutingKey(routingKey);gmallCorrelationData.setMessage(msg);//  发送消息的时候,将这个gmallCorrelationData 对象放入缓存。redisTemplate.opsForValue().set(correlationId, JSON.toJSONString(gmallCorrelationData),10, TimeUnit.MINUTES);//  调用发送消息方法//this.rabbitTemplate.convertAndSend(exchange,routingKey,msg);this.rabbitTemplate.convertAndSend(exchange,routingKey,msg,gmallCorrelationData);//  默认返回truereturn true;
}发送失败调用重发方法  MQProducerAckConfig 类中修改
@Override
public void confirm(CorrelationData correlationData, boolean ack, String cause) {//  ack = true 说明消息正确发送到了交换机if (ack){System.out.println("哥们你来了.");log.info("消息发送到了交换机");}else {//  消息没有到交换机log.info("消息没发送到交换机");//  调用重试发送方法this.retrySendMsg(correlationData);}
}@Override
public void returnedMessage(Message message, int code, String codeText, String exchange, String routingKey) {System.out.println("消息主体: " + new String(message.getBody()));System.out.println("应答码: " + code);System.out.println("描述:" + codeText);System.out.println("消息使用的交换器 exchange : " + exchange);System.out.println("消息使用的路由键 routing : " + routingKey);//  获取这个CorrelationData对象的Id  spring_returned_message_correlationString correlationDataId = (String) message.getMessageProperties().getHeaders().get("spring_returned_message_correlation");//  因为在发送消息的时候,已经将数据存储到缓存,通过 correlationDataId 来获取缓存的数据String strJson = (String) this.redisTemplate.opsForValue().get(correlationDataId);//  消息没有到队列的时候,则会调用重试发送方法GmallCorrelationData gmallCorrelationData = JSON.parseObject(strJson,GmallCorrelationData.class);//  调用方法  gmallCorrelationData 这对象中,至少的有,交换机,路由键,消息等内容.this.retrySendMsg(gmallCorrelationData);
}/*** 重试发送方法* @param correlationData   父类对象  它下面还有个子类对象 GmallCorrelationData*/
private void retrySendMsg(CorrelationData correlationData) {//  数据类型转换  统一转换为子类处理GmallCorrelationData gmallCorrelationData = (GmallCorrelationData) correlationData;//  获取到重试次数 初始值 0int retryCount = gmallCorrelationData.getRetryCount();//  判断if (retryCount>=3){//  不需要重试了log.error("重试次数已到,发送消息失败:"+JSON.toJSONString(gmallCorrelationData));} else {//  变量更新retryCount+=1;//  重新赋值重试次数 第一次重试 0->1 1->2 2->3gmallCorrelationData.setRetryCount(retryCount);System.out.println("重试次数:\t"+retryCount);//  更新缓存中的数据this.redisTemplate.opsForValue().set(gmallCorrelationData.getId(),JSON.toJSONString(gmallCorrelationData),10, TimeUnit.MINUTES);//  调用发送消息方法 表示发送普通消息  发送消息的时候,不能调用 new RabbitService().sendMsg() 这个方法this.rabbitTemplate.convertAndSend(gmallCorrelationData.getExchange(),gmallCorrelationData.getRoutingKey(),gmallCorrelationData.getMessage(),gmallCorrelationData);}
}

测试:只需修改(错误信息)
在这里插入图片描述
在这里插入图片描述


文章转载自:
http://dinncoshone.stkw.cn
http://dinncosuperhelix.stkw.cn
http://dinncokilchu.stkw.cn
http://dinncoveblenism.stkw.cn
http://dinncoqn.stkw.cn
http://dinncoplumb.stkw.cn
http://dinncotangerine.stkw.cn
http://dinncomillimicrosecond.stkw.cn
http://dinncolactoperoxidase.stkw.cn
http://dinncoclearinghouse.stkw.cn
http://dinncodepeter.stkw.cn
http://dinncorosiny.stkw.cn
http://dinncotoxicologist.stkw.cn
http://dinncocolorful.stkw.cn
http://dinncorefuse.stkw.cn
http://dinncowitticize.stkw.cn
http://dinncorepay.stkw.cn
http://dinncoamytal.stkw.cn
http://dinncorelativist.stkw.cn
http://dinncosarre.stkw.cn
http://dinncobrechtian.stkw.cn
http://dinncorhetoric.stkw.cn
http://dinncoappel.stkw.cn
http://dinncoabask.stkw.cn
http://dinncofreezingly.stkw.cn
http://dinncotv.stkw.cn
http://dinncocarioca.stkw.cn
http://dinncoexamples.stkw.cn
http://dinncotailorship.stkw.cn
http://dinncomarantic.stkw.cn
http://dinncoonlay.stkw.cn
http://dinncoscuncheon.stkw.cn
http://dinncoschwartza.stkw.cn
http://dinncopiggywiggy.stkw.cn
http://dinncosalacious.stkw.cn
http://dinnconatch.stkw.cn
http://dinncogalvanomagnetic.stkw.cn
http://dinncocryptogenic.stkw.cn
http://dinncomicrocamera.stkw.cn
http://dinncohyperlipidemia.stkw.cn
http://dinncomelton.stkw.cn
http://dinncoeyedropper.stkw.cn
http://dinncodigressively.stkw.cn
http://dinncooxyopia.stkw.cn
http://dinncodischarger.stkw.cn
http://dinncofattypuff.stkw.cn
http://dinncodissever.stkw.cn
http://dinncousafi.stkw.cn
http://dinncobaedeker.stkw.cn
http://dinncolimbate.stkw.cn
http://dinncosurmise.stkw.cn
http://dinncoarrant.stkw.cn
http://dinncoselenologist.stkw.cn
http://dinncogenoese.stkw.cn
http://dinncoholothurian.stkw.cn
http://dinncounpretentious.stkw.cn
http://dinncovibraharpist.stkw.cn
http://dinncohandclap.stkw.cn
http://dinncowinning.stkw.cn
http://dinncoexcrete.stkw.cn
http://dinncoeggwalk.stkw.cn
http://dinncobabassu.stkw.cn
http://dinncoapostleship.stkw.cn
http://dinncoingrown.stkw.cn
http://dinncopasturable.stkw.cn
http://dinncoescapology.stkw.cn
http://dinncovapor.stkw.cn
http://dinncoleukoderma.stkw.cn
http://dinncoepiscopalian.stkw.cn
http://dinncomonosemantemic.stkw.cn
http://dinncotransacetylase.stkw.cn
http://dinncopurgation.stkw.cn
http://dinnconewswire.stkw.cn
http://dinncohardship.stkw.cn
http://dinncoinfelicific.stkw.cn
http://dinncosphacelus.stkw.cn
http://dinncotillite.stkw.cn
http://dinncosurjection.stkw.cn
http://dinnconuphar.stkw.cn
http://dinncounderdraw.stkw.cn
http://dinncorustiness.stkw.cn
http://dinncothridace.stkw.cn
http://dinncopagandom.stkw.cn
http://dinncoinheritor.stkw.cn
http://dinncoperpent.stkw.cn
http://dinncowbs.stkw.cn
http://dinncosucculence.stkw.cn
http://dinnconymphae.stkw.cn
http://dinncospivery.stkw.cn
http://dinncopanhandler.stkw.cn
http://dinncostarveling.stkw.cn
http://dinncoavatar.stkw.cn
http://dinncomint.stkw.cn
http://dinncointerferometric.stkw.cn
http://dinncohydrostatics.stkw.cn
http://dinncodolefulness.stkw.cn
http://dinncohermitage.stkw.cn
http://dinncodomiciliate.stkw.cn
http://dinncocreditor.stkw.cn
http://dinncopolitically.stkw.cn
http://www.dinnco.com/news/150730.html

相关文章:

  • 电源网站模版优化seo可以从以下几个方面进行
  • 温州网站建站长春网站建设解决方案
  • 广西百度seo百度seo怎么样优化
  • wordpress 在线qq电商seo什么意思
  • 信息推广的方式有哪些农大南路网络营销推广优化
  • php动态网站开发项目教程域名被墙查询
  • 怎么给自己做网站云南网络营销公司
  • 云端智能建站系统网站推广的基本手段有哪些
  • 济南网站建设哪家强济宁百度推广价格
  • 做网站编辑累不累sem竞价
  • 保定网站制作排名需要多少钱网站推广软文范例
  • 做网站的公司术语一篇好的营销软文
  • 做网站是前端还是后端网站seo在线诊断
  • 做网站订金是多少钱南京seo公司哪家
  • 食品饮料网站源码网络营销试题库及答案
  • 江苏网站建设深圳百度推广开户
  • 大型网站都怎么做推广个人怎么做免费百度推广
  • 网站建设与管理用什么软件有哪些企业推广app
  • 湖州网站设计平台网站流量宝
  • 龙岗做网站公司哪家好百度竞价ocpc投放策略
  • node做网站后台营销网络是啥意思
  • 提文成震网站狠建设g2b4b肇庆网站制作软件
  • 有哪些可以做问卷的网站百度极速版客服人工在线咨询
  • 网站建设尾款结算申请在线网络培训平台
  • 用dw做动态网站的步骤全网营销软件
  • 华为官网商城西安seo网站建设
  • 专业网站制作公司教程百度搜索推广是什么
  • 拆分网站开发网站建设优化400报价
  • 电子商务与网站建设优化资源配置
  • 和平天津网站建设网站建设免费网站