JAVA:Spring-Retry异常重试

博客介绍了Spring Retry,它能自动重新调用失败操作,适用于网络等瞬时误差场景。在调用接口因不可控原因失败时,可借助Spring-Retry解决重试问题,具体操作包括引入依赖、启动类增加注解、指定异常重试等,还说明了@Retryable注解参数及不生效的可能原因。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

前文

        根据 Baeldung 网站介绍:Spring Retry provides an ability to automatically re-invoke a failed operation. This is helpful where the errors may be transient (like a momentary network glitch).

        译文:Spring Retry提供了自动重新调用失败操作的能力。这在误差可能是瞬时(就像一个瞬间的网络故障)的情况下是有帮助的。


场景

        在调用接口(如第三方接口)时,往往会因为网络等不可控的原因,导致调用失败,但是为了确保调用接口时出现的异常不会影响我们业务正常的进行,所以我们需要针对不同的异常进行一个重试或者其他策略的进行,此时需要重新调用该接口,那么该怎么做呢?写个for循环重复调用该接口?那么该代码就显得比较冗余,无意义的代码也比较多,不方便阅读,此时就可以借助Spring-Retry解决这个问题了,具体操作如下。


引入依赖

<dependency>
    <groupId>org.springframework.retry</groupId>
    <artifactId>spring-retry</artifactId>
    <version>1.2.5.RELEASE</version>
</dependency>
<dependency>
    <groupId>org.springframework</groupId>
    <artifactId>spring-aspects</artifactId>
</dependency>

 启动类增加注解

package com.alex.examples;

import cn.xuyanwu.spring.file.storage.EnableFileStorage;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.retry.annotation.EnableRetry;
import org.springframework.scheduling.annotation.EnableScheduling;

@SpringBootApplication
@EnableScheduling
@EnableFileStorage
//增加下面这个注解,意思是:开启Retry服务
@EnableRetry
public class ExamplesApplication {

    public static void main(String[] args) {
        SpringApplication.run(ExamplesApplication.class, args);
    }

}

指定异常重试

将 @Retryable 注解加在对应的方法上即可。

@Retryable不生效的可能原因:

        1、@Retryable方法必须为 public;

        2、重试方法与调用它的非重试方法在同一个类中,如下写法则不生效(异常了就会立刻抛出,而不会重试)。

// 错误写法
@GetMapping("/hello")
public void hello() {
    System.out.println("重试一下吧!");
    aaa();
}

@Retryable(value = IllegalArgumentException.class, backoff = @Backoff(delay = 2000, multiplier = 2, maxDelay = 5000))
public void aaa() {
    System.out.println("进入!");
    throw new IllegalArgumentException();
}

// 正确写法
@GetMapping("/hello")
@Retryable(value = IllegalArgumentException.class, backoff = @Backoff(delay = 2000, multiplier = 2, maxDelay = 5000))
public void hello() {
    System.out.println("重试一下吧!");
    throw new IllegalArgumentException();
}

注解参数说明

        例子:@Retryable(value = {IllegalArgumentException.class}, maxAttempts = 5, maxAttemptsExpression = "${max.attempts:}" )

        1.value:指定需要重试的异常类型,多个则以{XXX,XXX}形式,如针对 NPE、IAE 异常会进行重试的写法为:

@Retryable(value = {NullPointerException.class, IllegalArgumentException.class})

        2. maxAttempts:直接指定重试次数。

        3.maxAttemptsExpression = "${max.attempts:}":从配置文件中获取异常重试次数,如果maxAttempts和maxAttemptsExpression都进行配置了,那么以 maxAttemptsExpression 为主。


简单的例子 

@GetMapping("/hello")
@Retryable(value = {IllegalArgumentException.class}, maxAttempts = 5)
public void hello() {
    System.out.println("重试一下吧!");
    throw new IllegalArgumentException();
}


详情可以参看这位同学的笔记,写得很具体:一行注解搞定异常重试,这么牛? - 掘金

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值