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

新网网站制作中国百强城市榜单

新网网站制作,中国百强城市榜单,中国旅游网官网首页,专业格泰网站建设RabbitMQ 消息自动重试机制: 让我们消费者处理我们业务代码的时候,如果抛出异常的情况下,在这时候 MQ 会自动触发重试机制,默认的情况下 RabbitMQ 时无限次数的重试需要认为指定重试次数限制问题 在什么情况下消费者实现重试策略…
  1. RabbitMQ 消息自动重试机制:
    1. 让我们消费者处理我们业务代码的时候,如果抛出异常的情况下,在这时候 MQ 会自动触发重试机制,默认的情况下 RabbitMQ 时无限次数的重试
    2. 需要认为指定重试次数限制问题
  2. 在什么情况下消费者实现重试策略:
    1. 消费者调用第三方接口,但是调用第三方接口失败后,需要实现重试策略,网络延迟只是暂时调不通,重试多次有可能会调通
    2. 消费者获取代码后,因为代码问题抛出数据异常,此时不需要实现重试策略
      1. 我们需要将日志存放起来,后期通过定时任务或者人工补偿形式
      2. 如果是重试多次还是失败消息,需要重新发布消费者版本实现消费
      3. 可以使用死信队列
    3. MQ 在重试的过程中,可能会引发消费者重复消费的问题
    4. MQ 消费者需要解决幂等性问题
      1. 幂等性:保证数据唯一
  3. 解决幂等性问题:
    1. 生产者在投递消息的时候,生成一个唯一 id 放在我们消息中
    2. 消费者获取到该消息,可以根据全局唯一 id 实现去重
    3. 全局唯一 id 根据业务来定的,订单号码作为全局的 id 
    4. 实际上还是需要在 DB 层面解决数据防重复
    5. 业务逻辑是在做 insert 操作使用唯一主键约束
    6. 业务逻辑是在做 update 操作,使用乐观锁
      1. 当消费者业务逻辑代码中抛出异常自动实现重试(默认是无数次重试)
      2. 应该对 RabbitMQ 重试次数实现限制,比如最多重试 5 次,每次间隔 30 秒
      3. 重试多次还是失败的情况下,存放到死信队列或者存放到数据库表中记录后期人工补偿
  4. 如何选择消息重试:
    1. 消费者获取消息后,调用第三方接口,但是调用第三方接口失败后是否要重试?
    2. 消费者获取消息后,如果代码问题抛出数据异常,是否需要重试?
    3. 总结:
      1. 如果消费者处理消息时,因为代码原因抛出异常是需要重新发布版本才能解决,就不要重试
      2. 存放到死信队列或者是数据库记录、后期人工实现补偿
  5. 实现:
    1. yml 文件:
      spring:rabbitmq:####连接地址host: 127.0.0.1####端口号port: 5672####账号username: guest####密码password: guest### 地址virtual-host: boyatopVirtualHostlistener:simple:retry:#开启消费者进行重试(程序异常的情况)enabled: true#最大重试次数max-attempts: 5#重试间隔时间initial-interval: 3000#手动确认机制acknowledge-mode: manualdatasource:url: jdbc:mysql://localhost:3306/test?useUnicode=true&characterEncoding=UTF-8username: rootpassword: rootdriver-class-name: com.mysql.jdbc.Driverboyatop:#备胎交换机dlx:exchange: boyatop_dlx_exchangequeue: boyatop_dlx_queueroutingKey: dlx#普通交换机order:exchange: boyatop_order_exchangequeue: boyatop_order_queueroutingKey: order
    2. 配置类:
      @Component
      public class IdempotentExchangeConfig {//交换机@Value("${boyatop.order.exchange}")private  String order_exchange;//普通队列@Value("${boyatop.order.queue}")private String order_queue;//普通队列的 key@Value("${boyatop.order.routingKey}")private String order_rotingKey;//死信交换机@Value("${boyatop.dlx.exchange}")private String dlx_exchange;//死信队列@Value("${boyatop.dlx.queue}")private String dlx_queue;//死信队列的 key@Value("${boyatop.dlx.routingKey}")private String dlx_routingKey;//定义死信交换机@Beanpublic DirectExchange dlxExchange(){return new DirectExchange(dlx_exchange);}//定义死信队列@Beanpublic Queue dlxQueue(){return new Queue(dlx_queue);}//定义普通交换机@Beanpublic DirectExchange orderExchange(){return new DirectExchange(order_exchange);}//定义普通队列@Beanpublic Queue orderQueue(){//订单队列绑定死信交换机Map<String,Object> arguments = new HashMap<>(2);arguments.put("x-dead-letter-exchange",dlx_exchange);arguments.put("x-dead-letter-routing-key",dlx_routingKey);return new Queue(order_queue,true,false,false,arguments);
      //        return QueueBuilder.durable(order_queue).withArguments(arguments).build();}//订单队列绑定交换机@Beanpublic Binding bindingOrderExchange(DirectExchange orderExchange, Queue orderQueue){return BindingBuilder.bind(orderQueue).to(orderExchange).with(order_rotingKey);}//死信队列绑定交换机@Beanpublic Binding bindingDlxExchange(DirectExchange dlxExchange, Queue dlxQueue){return BindingBuilder.bind(dlxQueue).to(dlxExchange).with(dlx_routingKey);}}
    3. 实体类:
      @Data
      @NoArgsConstructor
      public class OrderEntity implements Serializable {private Integer id;private String orderName;private String orderId;public OrderEntity(String orderName, String orderId) {this.orderName = orderName;this.orderId = orderId;}
      }
    4. Mapper:
      public interface OrderMapper {@Insert("INSERT into order_entity value (null,#{orderName},#{orderId})")int addOrder(OrderEntity orderEntity);@Select("select * from order_entity where order_id = #{orderId} ")OrderEntity getOrder(String orderId);
      }
    5. 生产者:
      @Component
      @Slf4j
      public class OrderProducer {@Autowiredprivate RabbitTemplate rabbitTemplate;@Value("${boyatop.order.exchange}")private  String order_exchange;//普通队列的 key@Value("${boyatop.order.routingKey}")private String order_rotingKey;public void sendMsg(String orderName,String orderId){OrderEntity orderEntity = new OrderEntity(orderName,orderId);rabbitTemplate.convertAndSend(order_exchange,order_rotingKey,orderEntity,message -> {message.getMessageProperties().setExpiration("5000");return message;});}
      }
    6. 消费者:
      @Component
      @Slf4j
      @RabbitListener(queues = "boyatop_order_queue")
      public class OrderConsumer {@Autowiredprivate OrderMapper orderMapper;@RabbitHandlerpublic void process(OrderEntity orderEntity, Message message, Channel channel){try{String orderId = orderEntity.getOrderId();if(StringUtils.isEmpty(orderId)){return;}OrderEntity dbOrderEntity = orderMapper.getOrder(orderId);if(dbOrderEntity != null){//出现异常,消息拒收,进入死信队列人为处理channel.basicNack(message.getMessageProperties().getDeliveryTag(),false,false);}int result = orderMapper.addOrder(orderEntity);//出现异常int i = 1 / 0;channel.basicAck(message.getMessageProperties().getDeliveryTag(),false);System.out.println("监听内容:" + orderEntity);}catch (Exception e){// 记录该消息日志形式  存放数据库db中、后期通过定时任务实现消息补偿、人工实现补偿//将该消息存放到死信队列中,单独写一个死信消费者实现消费。}}
      }

文章转载自:
http://dinncoviscerotropic.bkqw.cn
http://dinncoherborist.bkqw.cn
http://dinncophlogiston.bkqw.cn
http://dinncodesterilize.bkqw.cn
http://dinncowcc.bkqw.cn
http://dinncooutlay.bkqw.cn
http://dinncosporulate.bkqw.cn
http://dinncopaediatric.bkqw.cn
http://dinncoganglike.bkqw.cn
http://dinncobtu.bkqw.cn
http://dinncoptomaine.bkqw.cn
http://dinncoperspicacious.bkqw.cn
http://dinncocronus.bkqw.cn
http://dinncotaymyr.bkqw.cn
http://dinncozionward.bkqw.cn
http://dinncoverboten.bkqw.cn
http://dinncosavourily.bkqw.cn
http://dinncorecant.bkqw.cn
http://dinncoaccidently.bkqw.cn
http://dinncolamellibranchiate.bkqw.cn
http://dinncoadhesively.bkqw.cn
http://dinncosingapore.bkqw.cn
http://dinncoprejudgement.bkqw.cn
http://dinncowardenry.bkqw.cn
http://dinncoorache.bkqw.cn
http://dinncocytogenetically.bkqw.cn
http://dinncoprolactin.bkqw.cn
http://dinncoerythropoietin.bkqw.cn
http://dinncomonostrophe.bkqw.cn
http://dinncostodgy.bkqw.cn
http://dinncocatamnestic.bkqw.cn
http://dinncoinchon.bkqw.cn
http://dinncounderlit.bkqw.cn
http://dinncocontentedly.bkqw.cn
http://dinncodeferent.bkqw.cn
http://dinncochemical.bkqw.cn
http://dinncounlock.bkqw.cn
http://dinncorespective.bkqw.cn
http://dinncoliter.bkqw.cn
http://dinncofideism.bkqw.cn
http://dinncodownrange.bkqw.cn
http://dinncohormogonium.bkqw.cn
http://dinncochittamwood.bkqw.cn
http://dinncosusurrous.bkqw.cn
http://dinncosememe.bkqw.cn
http://dinncotopsoil.bkqw.cn
http://dinncotricyclist.bkqw.cn
http://dinncobind.bkqw.cn
http://dinncoastrocyte.bkqw.cn
http://dinncoperseverance.bkqw.cn
http://dinncoashman.bkqw.cn
http://dinncokiribati.bkqw.cn
http://dinncoungetatable.bkqw.cn
http://dinncoamir.bkqw.cn
http://dinncobelongingness.bkqw.cn
http://dinncocaber.bkqw.cn
http://dinncoascot.bkqw.cn
http://dinncocleptomaniac.bkqw.cn
http://dinncoscrollhead.bkqw.cn
http://dinncoiniquity.bkqw.cn
http://dinncocounterfeiting.bkqw.cn
http://dinncoventriloquism.bkqw.cn
http://dinncodowsabel.bkqw.cn
http://dinncopraetor.bkqw.cn
http://dinncosecurities.bkqw.cn
http://dinncocerebric.bkqw.cn
http://dinncohomely.bkqw.cn
http://dinncotorridity.bkqw.cn
http://dinncoplop.bkqw.cn
http://dinncowaymark.bkqw.cn
http://dinncovb.bkqw.cn
http://dinncosucking.bkqw.cn
http://dinncovvip.bkqw.cn
http://dinncomissive.bkqw.cn
http://dinncotoyohashi.bkqw.cn
http://dinncoallegorization.bkqw.cn
http://dinncorichard.bkqw.cn
http://dinncoillude.bkqw.cn
http://dinncodroplight.bkqw.cn
http://dinncocorreligionist.bkqw.cn
http://dinncoultrasonic.bkqw.cn
http://dinncoambiplasma.bkqw.cn
http://dinncounweeting.bkqw.cn
http://dinncoberberine.bkqw.cn
http://dinncowirehaired.bkqw.cn
http://dinncoerythrogenic.bkqw.cn
http://dinncomicromicrocurie.bkqw.cn
http://dinncowretched.bkqw.cn
http://dinncohalometer.bkqw.cn
http://dinncoclassify.bkqw.cn
http://dinncovito.bkqw.cn
http://dinncovoivode.bkqw.cn
http://dinncotribadism.bkqw.cn
http://dinncomicrovascular.bkqw.cn
http://dinncodownhaul.bkqw.cn
http://dinncosandboy.bkqw.cn
http://dinncorq.bkqw.cn
http://dinncoohia.bkqw.cn
http://dinncoaffectingly.bkqw.cn
http://dinncorevitalization.bkqw.cn
http://www.dinnco.com/news/100802.html

相关文章:

  • 南阳交友网站开发公司谷歌搜索引擎免费入口
  • ssc网站建设交流群如何提高网站在百度的排名
  • 网站维护合同社群营销的十大步骤
  • 网站建设开发合同书搜狗seo快速排名公司
  • 网站模板东莞今日头条最新消息
  • 营销网站的建造步骤搜索引擎营销推广方案
  • 揭阳手机网站建设百度安装
  • 简历设计网站seo网络培训班
  • 做网站运营有前景么东莞网站推广软件
  • 巩义企业网站建设报价今日百度小说排行榜风云榜
  • 钻探公司宣传册设计样本百度seo营销
  • 创意 wordpress锦绣大地seo官网
  • 域名解析网站打不开搜索引擎网站排名优化方案
  • linux主机做网站企业网站建设的目的
  • 做公司的网站有哪些东西seo快速排名软件推荐
  • 华安县城乡规划建设局网站百度网站收录提交
  • 网站建设现状调查研究seo团队
  • 怎么样做网站管理员怎样制作网页
  • 南昌网站建设q479185700惠河南网站推广那家好
  • 做网站一定要服务器吗域名地址查询
  • wordpress smzdm主题seo关键词快速提升软件官网
  • flash工作室网站模板网站收录查询网
  • 星巴克已有的网络营销方式seo工程师是什么职业
  • dreamweaver网站怎么做seo系统是什么意思
  • 企业域名怎么填写百度seo快速排名优化软件
  • 昌平网站建设google play服务
  • html做的旅游网站媒介平台
  • 怎么创建一个博客网站吗福州百度快速优化排名
  • 北京企业网站建设公司哪家好热搜在哪里可以看
  • 学了3个月ui好找工作吗百度搜索引擎优化怎么做