《一线大厂Java面试题解析+核心总结学习笔记+最新讲解视频+实战项目源码》,点击传送门,即可获取!
aspectjweaver
1.9.6
cn.hutool
hutool-all
5.5.2
org.projectlombok
lombok
true
org.springframework.boot
spring-boot-starter-test
test
org.springframework.boot
spring-boot-starter-web
主类配置EnableRetry注解
package ai.guiji.csdn;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.retry.annotation.EnableRetry;
@EnableRetry
@SpringBootApplication
public class CsdnApplication {
public static void main(String[] args) {
SpringApplication.run(CsdnApplication.class, args);
}
}
准备测试的Retry组件类代码
package ai.guiji.csdn.component;
import lombok.extern.slf4j.Slf4j;
import org.springframework.retry.annotation.Backoff;
import org.springframework.retry.annotation.Recover;
import org.springframework.retry.annotation.Retryable;
import org.springframework.stereotype.Component;
import java.util.function.Supplier;
/** @Author 剑客阿良_ALiang @Date 2021/4/22 16:07 @Description: 重试工具 */
@Slf4j
@Component
public class RetryUtil {
@Retryable(
value = Exception.class,
maxAttempts = 3,
backoff = @Backoff(delay = 5000, multiplier = 1.5))
public String retry(Supplier supplier) throws Exception {
String result = null;
try {
result = supplier.get();
} catch (Exception exception) {
log.error(“异常报错:{}”, exception.getMessage());
throw exception;
}
return result;
}
@Recover
public void recover(Exception e) {
log.error(“调用超过3次异常”);
}
}
代码说明
1、我们可以看到retry方法会重试supplier的get结果,捕获异常并抛出异常。这里抛出后会被retry捕获并且重试。
2、maxAttempts参数为重试的最大次数。
3、backoff中的delay为两次重试之间的延迟,multiplier为重试阻尼,可以这么理解,每次重试间隔时间为上一次重试间隔时间的倍数。
4、如果3次重试均抛出异常,则进入recover方法。
编写测试代码
package ai.guiji.csdn.component;
import cn.hutool.core.convert.Convert;
import cn.hutool.http.HttpUtil;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
/** @Author 剑客阿良_ALaing @Date 2021/11/30 13:08 @Description: */
@SpringBootTest
class RetryUtilTest {
@Autowired private RetryUtil retryUtil;
@Test
void retry() {
try {
System.out.println(Convert.toStr(retryUtil.retry(() -> HttpUtil.post(“xxxx”, “”)), “haha”));
} catch (Exception exception) {
exception.printStackTrace();
}
}
}
执行测试结果
2021-11-30 13:37:44.012 ERROR 13600 — [ main] ai.guiji.csdn.component.RetryUtil : 异常报错:UnknownHostException: xxxx
2021-11-30 13:37:49.019 ERROR 13600 — [ main] ai.guiji.csdn.component.RetryUtil : 异常报错:UnknownHostException: xxxx
2021-11-30 13:37:58.787 ERROR 13600 — [ main] ai.guiji.csdn.component.RetryUtil : 异常报错:UnknownHostException: xxxx
org.springframework.retry.ExhaustedRetryException: Cannot locate recovery method; nested exception is cn.hutool.core.io.IORuntimeException: UnknownHostException: xxxx
at org.springframework.retry.annotation.RecoverAnnotationRecoveryHandler.recover(RecoverAnnotationRecoveryHandler.java:70)
at org.springframework.retry.interceptor.RetryOperationsInterceptor$ItemRecovererCallback.recover(RetryOperationsInterceptor.java:142)
at org.springframework.retry.support.RetryTemplate.handleRetryExhausted(RetryTemplate.