可靠性利器-重试机制

简介: 在日常开发中,我们经常会遇到需要调用外部服务和接口的场景。外部服务对于调用者来说一般都是不可靠的,尤其是在网络环境比较差的情况下,网络抖动很容易导致请求超时等异常情况,这时候就需要使用失败重试策略重新调用 API 接口来获取。重试策略在服务治理方面也有很广泛的使用,通过定时检测,来查看服务是否存活。

Spring异常重试框架Spring Retry


Spring Retry支持集成到Spring或者Spring Boot项目中,而它支持AOP的切面注入写法,所以在引入时必须引入aspectjweaver.jar包。


1.引入maven依赖


<dependency>
    <groupId>org.springframework.retry</groupId>
    <artifactId>spring-retry</artifactId>
    <version>1.1.2.RELEASE</version>
</dependency>
<dependency>
    <groupId>org.aspectj</groupId>
    <artifactId>aspectjweaver</artifactId>
    <version>1.8.6</version>
</dependency>


2.添加@Retryable和@Recover注解


@Retryable注解,被注解的方法发生异常时会重试


  • value:指定发生的异常进行重试
  • include:和value一样,默认空,当exclude也为空时,所有异常都重试
  • exclude:指定异常不重试,默认空,当include也为空时,所有异常都重试
  • maxAttemps:重试次数,默认3
  • backoff:重试补偿机制,默认没有


@Backoff注解


  • delay:指定延迟后重试
  • multiplier:指定延迟的倍数,比如delay=5000l,multiplier=2时,第一次重试为5秒后,第二次为10秒,第三次为20秒


@Recover注解: 当重试到达指定次数时,被注解的方法将被回调,可以在该方法中进行日志处理。需要注意的是发生的异常和入参类型一致时才会回调。


@Service
public class RemoteService {
@Retryable(value = {Exception.class}, maxAttempts = 5, backoff = @Backoff(delay = 5000L, multiplier = 1))
public void call() {
    System.out.println(LocalDateTime.now() + ": do something...");
    throw new RuntimeException(LocalDateTime.now() + ": 运行调用异常");
}
@Recover
public void recover(Exception e) {
    System.out.println(e.getMessage());
}


3.启用重试功能


启动类上面添加@EnableRetry注解,启用重试功能,或者在使用retry的service上面添加也可以,或者Configuration配置类上面。


建议所有的Enable配置加在启动类上,可以清晰的统一管理使用的功能。


@SpringBootApplication
@EnableRetry
public class App {
    public static void main(String[] args) throws Exception {
        ConfigurableApplicationContext context = SpringApplication.run(App.class, args);
        System.out.println("Start app success.");
        RemoteService bean = context.getBean(RemoteService.class);
        bean.call();
    }
}


4.启动服务,运行测试


通过在启动类Context调用服务看到如下打印:

2019-03-09T15:22:12.781: do something...
2019-03-09T15:22:17.808: do something...
2019-03-09T15:22:22.835: do something...
2019-03-09T15:22:27.861: do something...
2019-03-09T15:22:32.887: do something...
2019-03-09T15:22:32.887: 运行调用异常


基于guava的重试组件Guava-Retryer


直接看组件作者对此组件的介绍: This is a small extension to Google’s Guava library to allow for the creation of configurable retrying strategies for an arbitrary function call, such as something that talks to a remote service with flaky uptime.(这是对Google的guava库的一个小扩展,允许为任意函数调用创建可配置的重试策略,例如与运行时间不稳定的远程服务对话的策略。)


第一步引入maven坐标:


<dependency>
    <groupId>com.github.rholder</groupId>
    <artifactId>guava-retrying</artifactId>
    <version>2.0.0</version>
</dependency>


1.其主要接口及策略介绍


  • Attempt:一次执行任务;
  • AttemptTimeLimiter:单次任务执行时间限制(如果单次任务执行超时,则终止执行当前任务);
  • BlockStrategies:任务阻塞策略(通俗的讲就是当前任务执行完,下次任务还没开始这段时间做什么……),默认策略为:BlockStrategies.THREAD_SLEEP_STRATEGY 也就是调用 Thread.sleep(sleepTime);
  • RetryException:重试异常;
  • RetryListener:自定义重试监听器,可以用于异步记录错误日志;
  • StopStrategy:停止重试策略,提供三种:
  • StopAfterDelayStrategy :设定一个最长允许的执行时间;比如设定最长执行10s,无论任务执行次数,只要重试的时候超出了最长时间,则任务终止,并返回重试异常RetryException;
  • NeverStopStrategy :不停止,用于需要一直轮训直到返回期望结果的情况;
  • StopAfterAttemptStrategy :设定最大重试次数,如果超出最大重试次数则停止重试,并返回重试异常;
  • WaitStrategy:等待时长策略(控制时间间隔),返回结果为下次执行时长:
  • FixedWaitStrategy:固定等待时长策略;
  • RandomWaitStrategy:随机等待时长策略(可以提供一个最小和最大时长,等待时长为其区间随机值)
  • IncrementingWaitStrategy:递增等待时长策略(提供一个初始值和步长,等待时间随重试次数增加而增加)
  • ExponentialWaitStrategy:指数等待时长策略;
  • FibonacciWaitStrategy :Fibonacci 等待时长策略;
  • ExceptionWaitStrategy :异常时长等待策略;
  • CompositeWaitStrategy :复合时长等待策略;


2.根据结果判断是否重试


使用场景:如果返回值决定是否要重试。

重试接口:

private static Callable<String> callableWithResult() {
    return new Callable<String>() {
        int counter = 0;
        public String call() throws Exception {
            counter++;
            System.out.println(LocalDateTime.now() + ": do something... " + counter);
            if (counter < 5) {
                return "james";
            }
            return "kobe";
        }
    };
}

测试:

public static void main(String[] args) {
    Retryer<String> retry = RetryerBuilder.<String>newBuilder()
            .retryIfResult(result -> !result.contains("kobe")).build();
    retry.call(callableWithResult());
}

输出:

2019-03-09T15:40:23.706: do something... 1
2019-03-09T15:40:23.710: do something... 2
2019-03-09T15:40:23.711: do something... 3
2019-03-09T15:40:23.711: do something... 4
2019-03-09T15:40:23.711: do something... 5


3.根据异常判断是否重试


使用场景:根据抛出异常类型判断是否执行重试。 重试接口:

private static Callable<String> callableWithResult() {
    return new Callable<String>() {
        int counter = 0;
        public String call() throws Exception {
            counter++;
            System.out.println(LocalDateTime.now() + ": do something... " + counter);
            if (counter < 5) {
                throw new RuntimeException("Run exception");
            }
            return "kobe";
        }
    };
}

测试:

public static void main(String[] args) throws ExecutionException, RetryException{
    Retryer<String> retry = RetryerBuilder.<String>newBuilder()
            .retryIfRuntimeException()
            .withStopStrategy(StopStrategies.neverStop())
            .build();
    retry.call(callableWithResult());
}

输出:

2019-03-09T15:53:27.682: do something... 1
2019-03-09T15:53:27.686: do something... 2
2019-03-09T15:53:27.686: do something... 3
2019-03-09T15:53:27.687: do something... 4
2019-03-09T15:53:27.687: do something... 5


4.重试策略——设定无限重试


使用场景:在有异常情况下,无限重试(默认执行策略),直到返回正常有效结果;

Retryer<String> retry = RetryerBuilder.<String>newBuilder()
            .retryIfRuntimeException()
            .withStopStrategy(StopStrategies.neverStop())
            .build();
    retry.call(callableWithResult());


5.重试策略——设定最大的重试次数


使用场景:在有异常情况下,最多重试次数,如果超过次数则会抛出异常;

private static Callable<String> callableWithResult() {
    return new Callable<String>() {
        int counter = 0;
        public String call() throws Exception {
            counter++;
            System.out.println(LocalDateTime.now() + ": do something... " + counter);
            throw new RuntimeException("Run exception");
        }
    };
}

测试:

public static void main(String[] args) throws ExecutionException, RetryException{
    Retryer<String> retry = RetryerBuilder.<String>newBuilder()
            .retryIfRuntimeException()
            .withStopStrategy(StopStrategies.stopAfterAttempt(4))
            .build();
    retry.call(callableWithResult());
}

输出:

2019-03-09T16:02:29.471: do something... 1
2019-03-09T16:02:29.477: do something... 2
2019-03-09T16:02:29.478: do something... 3
2019-03-09T16:02:29.478: do something... 4
Exception in thread "main" com.github.rholder.retry.RetryException: Retrying failed to complete successfully after 4 attempts.


6.等待策略——设定重试等待固定时长策略


使用场景:设定每次重试等待间隔固定为10s;

public static void main(String[] args) throws ExecutionException, RetryExceptio{
    Retryer<String> retry = RetryerBuilder.<String>newBuilder()
            .retryIfRuntimeException()
            .withStopStrategy(StopStrategies.stopAfterAttempt(4))
            .withWaitStrategy(WaitStrategies.fixedWait(10, TimeUnit.SECONDS))
            .build();
    retry.call(callableWithResult());
}

测试输出,可以看出调用间隔是10S:

2019-03-09T16:06:34.457: do something... 1
2019-03-09T16:06:44.660: do something... 2
2019-03-09T16:06:54.923: do something... 3
2019-03-09T16:07:05.187: do something... 4
Exception in thread "main" com.github.rholder.retry.RetryException: Retrying failed to complete successfully after 4 attempts.


7.等待策略——设定重试等待时长固定增长策略


场景:设定初始等待时长值,并设定固定增长步长,但不设定最大等待时长;

public static void main(String[] args) throws ExecutionException, RetryException {
    Retryer<String> retry = RetryerBuilder.<String>newBuilder()
            .retryIfRuntimeException()
            .withStopStrategy(StopStrategies.stopAfterAttempt(4))
            .withWaitStrategy(WaitStrategies.incrementingWait(1, SECONDS, 1, SECONDS))
            .build();
    retry.call(callableWithResult());
}

测试输出,可以看出调用间隔时间递增1秒:

2019-03-09T18:46:30.256: do something... 1
2019-03-09T18:46:31.260: do something... 2
2019-03-09T18:46:33.260: do something... 3
2019-03-09T18:46:36.260: do something... 4
Exception in thread "main" com.github.rholder.retry.RetryException: Retrying failed to complete successfully after 4 attempts.


8.等待策略——设定重试等待时长按指数增长策略


使用场景:根据multiplier值按照指数级增长等待时长,并设定最大等待时长;

public static void main(String[] args) throws ExecutionException, RetryExceptio{
    Retryer<String> retry = RetryerBuilder.<String>newBuilder()
            .retryIfRuntimeException()
            .withStopStrategy(StopStrategies.stopAfterAttempt(4))
            .withWaitStrategy(WaitStrategies.exponentialWait(1000, 10,SECONDS))
            .build();
    retry.call(callableWithResult());
}

这个重试策略和入参不是很懂,好吧,查看源码:

@Immutable
private static final class ExponentialWaitStrategy implements WaitStrategy {
    private final long multiplier;
    private final long maximumWait;
    public ExponentialWaitStrategy(long multiplier, long maximumWait) {
        Preconditions.checkArgument(multiplier > 0L, "multiplier must be > 0 but is %d", new Object[]{Long.valueOf(multiplier)});
        Preconditions.checkArgument(maximumWait >= 0L, "maximumWait must be >= 0 but is %d", new Object[]{Long.valueOf(maximumWait)});
        Preconditions.checkArgument(multiplier < maximumWait, "multiplier must be < maximumWait but is %d", new Object[]{Long.valueOf(multiplier)});
        this.multiplier = multiplier;
        this.maximumWait = maximumWait;
    }
    public long computeSleepTime(Attempt failedAttempt) {
        double exp = Math.pow(2.0D, (double)failedAttempt.getAttemptNumber());
        long result = Math.round((double)this.multiplier * exp);
        if(result > this.maximumWait) {
            result = this.maximumWait;
        }
        return result >= 0L?result:0L;
    }
}

通过源码看出ExponentialWaitStrategy是一个不可变的内部类,构造器中校验入参,最重要的延迟时间计算方法computeSleepTime(),可以看出延迟时间计算方式


  1. 计算以2为底失败次数为指数的值


  1. 第一步的值构造器第一个入参相乘,然后四舍五入得到延迟时间(毫秒)

通过以上分析可知入参为1000时间隔是应该为2,4,8s

测试输出,可以看出调用间隔时间 2×1000,4×1000,8×1000:

2019-03-09T19:11:23.905: do something... 1
2019-03-09T19:11:25.908: do something... 2
2019-03-09T19:11:29.908: do something... 3
2019-03-09T19:11:37.909: do something... 4
Exception in thread "main" com.github.rholder.retry.RetryException: Retrying failed to complete successfully after 4 attempts.


9.等待策略——设定重试等待时长按斐波那契数列策略


使用场景:根据multiplier值按照斐波那契数列增长等待时长,并设定最大等待时长,斐波那契数列:1、1、2、3、5、8、13、21、34、……

public static void main(String[] args) throws ExecutionException, RetryException {
    Retryer<String> retry = RetryerBuilder.<String>newBuilder()
            .retryIfRuntimeException()
            .withStopStrategy(StopStrategies.stopAfterAttempt(4))
            .withWaitStrategy(WaitStrategies.fibonacciWait(1000, 10, SECONDS))
            .build();
    retry.call(callableWithResult());
}

同样,看源码可知计算可知延迟时间为斐波那契数列和第一入参的乘积(毫秒)

public long computeSleepTime(Attempt failedAttempt) {
    long fib = this.fib(failedAttempt.getAttemptNumber());
    long result = this.multiplier * fib;
    if(result > this.maximumWait || result < 0L) {
        result = this.maximumWait;
    }
    return result >= 0L?result:0L;
}

测试输出,可看出间隔调用为1×1000,1×1000,2×1000:

2019-03-09T19:28:43.903: do something... 1
2019-03-09T19:28:44.909: do something... 2
2019-03-09T19:28:45.928: do something... 3
2019-03-09T19:28:47.928: do something... 4
Exception in thread "main" com.github.rholder.retry.RetryException: Retrying failed to complete successfully after 4 attempts.


10.等待策略——组合重试等待时长策略


使用场景:当现有策略不满足使用场景时,可以对多个策略进行组合使用。

public static void main(String[] args) throws ExecutionException,     RetryException {
    Retryer<String> retry = RetryerBuilder.<String>newBuilder()
            .retryIfRuntimeException()
            .withStopStrategy(StopStrategies.stopAfterAttempt(10))
            .withWaitStrategy(WaitStrategies.join(WaitStrategies.exponentialWait(1000, 100, SECONDS)
                    , WaitStrategies.fixedWait(2, SECONDS)))
            .build();
    retry.call(callableWithResult());
}

同样,看源码才能理解组合策略是什么意思:

public long computeSleepTime(Attempt failedAttempt) {
    long waitTime = 0L;
    WaitStrategy waitStrategy;
    for(Iterator i$ = this.waitStrategies.iterator(); i$.hasNext(); waitTime += waitStrategy.computeSleepTime(failedAttempt)) {
        waitStrategy = (WaitStrategy)i$.next();
    }
    return waitTime;
}

可看出组合策略其实按照多个策略的延迟时间相加得到组合策略的延迟时间。exponentialWait的延迟时间为2,4,8,16,32...,fixedWait延迟为2,2,2,2,2...,所以总的延迟时间为4,6,10,18,34...

测试输出:

2019-03-09T19:46:45.854: do something... 1
2019-03-09T19:46:49.859: do something... 2
2019-03-09T19:46:55.859: do something... 3
2019-03-09T19:47:05.859: do something... 4
2019-03-09T19:47:23.859: do something... 5
2019-03-09T19:47:57.860: do something... 6
2019-03-09T19:49:03.861: do something... 7
2019-03-09T19:50:45.862: do something... 8


11.监听器——RetryListener实现重试过程细节处理


使用场景:自定义监听器,分别打印重试过程中的细节,未来可更多的用于异步日志记录,亦或是特殊处理。

public class MyRetryListener implements RetryListener {
@Override
public <V> void onRetry(Attempt<V> attempt) {
    System.out.println(("retry times=" + attempt.getAttemptNumber()));
    // 距离第一次重试的延迟
    System.out.println("delay=" + attempt.getDelaySinceFirstAttempt());
    // 重试结果: 是异常终止, 还是正常返回
    System.out.println("hasException=" + attempt.hasException());
    System.out.println("hasResult=" + attempt.hasResult());
    // 是什么原因导致异常
    if (attempt.hasException()) {
        System.out.println("causeBy=" + attempt.getExceptionCause());
    } else {
        // 正常返回时的结果
        System.out.println("result=" + attempt.getResult());
    }
    // 增加了额外的异常处理代码
    try {
        Object result = attempt.get();
        System.out.println("rude get=" + result);
    } catch (ExecutionException e) {
        System.out.println("this attempt produce exception." + e.getCause());
    }
}

测试:

public static void main(String[] args) throws ExecutionException, RetryException {
    Retryer<String> retry = RetryerBuilder.<String>newBuilder()
            .retryIfRuntimeException()
            .withStopStrategy(StopStrategies.stopAfterAttempt(2))
            .withRetryListener(new MyRetryListener())
            .build();
    retry.call(callableWithResult());
}

输出:

2019-03-09T16:32:35.097: do something... 1
retry times=1
delay=128
hasException=true
hasResult=false
causeBy=java.lang.RuntimeException: Run exception
this attempt produce exception.java.lang.RuntimeException: Run exception
2019-03-09T16:32:35.102: do something... 2
retry times=2
delay=129
hasException=true
hasResult=false
causeBy=java.lang.RuntimeException: Run exception
this attempt produce exception.java.lang.RuntimeException: Run exception
Exception in thread "main" com.github.rholder.retry.RetryException: Retrying failed to complete successfully after 2 attempts.


总结


两种方式都是比较优雅的重试策略,Spring-retry配置更简单,实现的功能也相对简单,Guava本身就是谷歌推出的精品java类库,guava-retry也是功能非常强大,相比较于Spring-Retry在是否重试的判断条件上有更多的选择性,可以作为Spring-retry的补充。


目录
相关文章
|
JSON Java API
玩转Spring Boot之RestTemplate的使用
在java代码里想要进行restful web client服务,一般使用Apache的HttpClient。不过此种方法使用起来太过繁琐。Spring Boot提供了一种简单便捷的内置模板类来进行操作,这就是RestTemplate。
6821 0
|
4月前
|
人工智能 API 开发者
阿里云百炼 Token Plan 个人版 39 元起:Lite/Standard/Pro 套餐对比与选型指南
阿里云百炼Token Plan重磅升级:个人版正式上线,Lite档仅39元/月,支持Qwen3.8-Max、DeepSeek-V4-Pro等旗舰模型;企业版同步降价,覆盖150+多模态模型,统一Credits抵扣,API兼容OpenAI协议。阿里云百炼Token Plan官网:https://t.aliyun.com/U/EsRjVx
751 0
|
5月前
|
人工智能 API 开发工具
阿里云百炼Coding Plan指南:费用、Token计费、请求规则与使用限制指南
阿里云百炼Coding Plan是专为AI编程打造的订阅服务,目前仅剩Pro高级版(200元/月),含9万次/月请求额度,分层限流(5小时6000次、每周4.5万次)。支持Qwen3.5-plus、Kimi-k2.5、GLM-5等多模型,兼容Cursor、Qwen Code等工具。需用专属sk-sp-密钥接入,不支持退款,每日9:30限量抢购。阿里云CodingPlan 官网:https://t.aliyun.com/U/G7pldC
911 0
|
5月前
|
人工智能 缓存 API
阿里云百炼Token Plan和按量计费API调用哪个划算?算一下你就知道了
阿里云百炼TokenPlan分标准/高级/尊享三版,月费198–1398元,对应Credits单价0.00792–0.00559元;折算qwen3.6-plus模型百万Tokens成本仅1.12–1.58元,显著低于按量计费的2元。高用量团队选尊享版更划算,还可领Club折扣券。在阿里云百炼官网:https://t.aliyun.com/U/fPVHqY 免费领取千万Tokens
757 1
|
6月前
|
Linux 开发者 iOS开发
Docker 要慌了?苹果开源 Container,Mac 原生容器时代来了!
一年前苹果发布Containerization框架并开源Container工具,现迎来1.0正式版!它为每个容器分配独立轻量VM,依托Apple Silicon与Virtualization.framework,兼顾启动速度与虚拟机级隔离。Mac开发者终于迎来原生容器新选择。(239字)
820 0
|
8月前
|
人工智能 供应链 API
阿里云百炼Coding Plan抢不到?解决阿里云百炼Coding Plan抢购难题+OpenClaw 部署保姆级教程
阿里云百炼Coding Plan作为高性价比大模型服务,长期处于“秒空”状态,多数用户反复抢购仍显示售罄。核心成因集中在两点: 1. 产品策略调整,可选版本缩减 官方已停售Coding Plan Lite基础版,仅保留Pro高级版开放新购,用户选择空间大幅压缩,所有需求集中涌向Pro版,直接推高抢购难度。 2. 限量补货,供需严重失衡 平台每日固定9:30限量补货,库存极少且需求庞大,通常补货后几分钟内就被抢空。即便工作日抢购成功率略高于周末,但整体仍处于“一货难求”的状态,普通用户很难成功下单。
923 4
|
人工智能 缓存 运维
探秘 AgentRun丨通过无代码创建的 Agent,如何用高代码进行更新?
AgentRun 打破 AI Agent 开发困局,无代码快速验证想法,一键转高代码实现深度定制。60 秒创建 Agent,支持多模型、工具集成与 Prompt 优化;业务增长后可平滑演进,保留配置生成高质量代码,助力从原型到生产的持续迭代。
678 31
|
数据可视化 关系型数据库 MySQL
【MCP教程系列】上阿里云百炼,5分钟解锁数据分析与可视化能力
本文介绍如何在阿里云百炼平台通过自定义MCP部署MySQL服务,实现5分钟解锁数据分析与可视化能力。以碳排放数据库为例,详细讲解从创建公网访问的MySQL数据库、配置MCP服务到引入智能体进行数据分析的全流程。借助QuickChart等工具,可将复杂数据转化为直观图表,赋能业务决策。适合希望快速上手数据库分析的用户参考使用。
3057 0
|
NoSQL Java API
Redisson分布式锁使用详解
通过以上内容,您可以全面了解如何在Java项目中使用Redisson实现分布式锁,并根据不同的业务需求选择合适的锁机制。
1588 33
|
存储 运维 监控
实战:使用Spring Boot Admin实现运维监控平台
我们知道,使用Actuator可以收集应用系统的健康状态、内存、线程、堆栈、配置等信息,比较全面地监控了Spring Boot应用的整个生命周期。但是还有一个问题:如何呈现这些采集到的应用监控数据、性能数据呢?在这样的背景下,就诞生了另一个开源软件Spring Boot Admin。下面就来介绍什么是Spring Boot Admin以及如何使用Spring Boot Admin搭建完整的运维监控平台。
132902 3
实战:使用Spring Boot Admin实现运维监控平台

热门文章

最新文章