spring cloud alibaba 学习(十一)feign 重试策略 Retryer

发布时间:2024-12-05 08:49

策略十七:学习理财知识,提升自我管理能力 #生活知识# #生活指南# #理财建议# #退休储备策略#

最新推荐文章于 2024-11-02 21:38:11 发布

_lrs 于 2022-02-10 14:20:35 发布

版权声明:本文为博主原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。

前言

请求远程接口失败时,会进入到重试策略。

一、Retryer

1、Retryer 接口

public interface Retryer extends Cloneable { /** * if retry is permitted, return (possibly after sleeping). Otherwise propagate the exception. * 在休眠后执行重试,或抛出异常 */ void continueOrPropagate(RetryableException e); Retryer clone(); } 1234567891011

2、默认不进行重试 NEVER_RETRY

Retryer NEVER_RETRY = new Retryer() { @Override public void continueOrPropagate(RetryableException e) { //直接抛出异常 throw e; } @Override public Retryer clone() { return this; } }; 12345678910111213

在 FeignClientsConfiguration 中 定义了默认的重试策略

@Bean@ConditionalOnMissingBeanpublic Retryer feignRetryer() {return Retryer.NEVER_RETRY;} 12345

二、Default

feign 里定义的默认重试策略

class Default implements Retryer {//最大重试次数 private final int maxAttempts; //重试周期 private final long period; //重试最大周期 private final long maxPeriod; //当前重试次数 int attempt; //休眠总时间 long sleptForMillis;public void continueOrPropagate(RetryableException e) { //达到了最大重试次数,抛异常 if (attempt++ >= maxAttempts) { throw e; } long interval; if (e.retryAfter() != null) { //根据 RetryableException 里 retryAfter 计算时间间隔 interval = e.retryAfter().getTime() - currentTimeMillis(); //不能超过 maxPeriod if (interval > maxPeriod) { interval = maxPeriod; } //时间到了,直接返回进行重试 if (interval < 0) { return; } } else { //计算时间间隔 interval = nextMaxInterval(); } try { //休眠 Thread.sleep(interval); } catch (InterruptedException ignored) { Thread.currentThread().interrupt(); throw e; } sleptForMillis += interval; } /** * Calculates the time interval to a retry attempt. <br> * The interval increases exponentially with each attempt, at a rate of nextInterval *= 1.5 * (where 1.5 is the backoff factor), to the maximum interval. * * @return time in nanoseconds from now until the next attempt. */ long nextMaxInterval() { //根据当前重试次数和重试周期计算时间间隔,重试次数越多,周期越长 long interval = (long) (period * Math.pow(1.5, attempt - 1)); //时间间隔不能超过最大重试周期 return interval > maxPeriod ? maxPeriod : interval; } }

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960

注册Default

@Configuration public class CustomFeignClientsConfiguration { @Bean public Retryer feignRetryer() { return new Retryer.Default(); } } 12345678

网址:spring cloud alibaba 学习(十一)feign 重试策略 Retryer https://www.yuejiaxmz.com/news/view/382259

相关内容

My Alibaba全面解析:优化店铺运营的终极秘籍
【项目】在线教育平台项目总结
学习策略的分类
学习啦xuexila.com
学习六大策略
高效学习策略
高效学习策略范例6篇
6s高效学习策略:自我调节策略
优化学习的策略.ppt
高效学习策略范文

随便看看