spring-retry笔记
spring-retey的依赖
<dependency>
<groupId>org.springframework.retrygroupId>
<artifactId>spring-retryartifactId>
<version>1.3.1version>
dependency>
spring-retry无注解方式使用
定义重试任务
package com.example.demo.retry;
import cn.hutool.core.util.RandomUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.remoting.RemoteAccessException;
@Slf4j
public class RetryDemoTask {
public static boolean retryTask(String param){
log.info("收到请求参数:{}",param);
int i = RandomUtil.randomInt(0,11);
log.info("param - i:{}",i);
if (i==0){
throw new IllegalArgumentException("参数异常");
}else if (i==1){
return true;
}else if (i==2){
return false;
}else {
throw new RemoteAccessException("远程访问异常");
}
}
}
构建重试逻辑并执行调用
package com.example.demo;
import com.example.demo.retry.RetryDemoTask;
import com.example.demo.retry.SpringRetryDemo;
import lombok.extern.slf4j.Slf4j;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.remoting.RemoteAccessException;
import org.springframework.retry.backoff.*;
import org.springframework.retry.policy.*;
import org.springframework.retry.support.RetryTemplate;
import java.util.HashMap;
import java.util.Map;
@Slf4j
@SpringBootTest
public class SpringRetryTests {
private long fixedPeriodTime = 1000L;
private int maxRetryTimes = 3;
private Map<Class<? extends Throwable>, Boolean> exceptionMap = new HashMap<>();
@Test
void t1() {
exceptionMap.put(RemoteAccessException.class, true);
RetryTemplate retryTemplate = new RetryTemplate();
FixedBackOffPolicy backOffPolicy = new FixedBackOffPolicy();
backOffPolicy.setBackOffPeriod(fixedPeriodTime);
SimpleRetryPolicy retryPolicy = new SimpleRetryPolicy(maxRetryTimes, exceptionMap);
retryTemplate.setRetryPolicy(retryPolicy);
retryTemplate.setBackOffPolicy(backOffPolicy);
Boolean execute = retryTemplate.execute(
retryContext -> {
boolean abc = RetryDemoTask.retryTask("abc");
log.info("调用的结果:{}", abc);
return abc;
},
retryContext -> {
log.info("已达到最大重试次数或抛出了不重试的异常...");
return false;
}
);
log.info("执行结果:{}", execute);
}
}
spring-retry注解方式使用
启用@EnableRetry 注解
@EnableRetry
@SpringBootApplication
public class DemoApplication {
public static void main(String[] args) {
SpringApplication.run(DemoApplication.class, args);
}
}
定义重试任务
package com.example.demo.retry;
import cn.hutool.core.util.RandomUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.remoting.RemoteAccessException;
@Slf4j
public class RetryDemoTask {
public static boolean retryTask(String param){
log.info("收到请求参数:{}",param);
int i = RandomUtil.randomInt(0,11);
log.info("param - i:{}",i);
if (i==0){
throw new IllegalArgumentException("参数异常");
}else if (i==1){
return true;
}else if (i==2){
return false;
}else {
throw new RemoteAccessException("远程访问异常");
}
}
}
定义重试服务层控制逻辑
package com.example.demo.retry;
import lombok.extern.slf4j.Slf4j;
import org.springframework.remoting.RemoteAccessException;
import org.springframework.retry.annotation.Backoff;
import org.springframework.retry.annotation.Recover;
import org.springframework.retry.annotation.Retryable;
import org.springframework.stereotype.Service;
@Slf4j
@Service
public class SpringRetryDemoService {
@Retryable(value = {RemoteAccessException.class},
maxAttempts = 3,
backoff = @Backoff(delay = 2000L,multiplier = 2))
public boolean call(String param){
return RetryDemoTask.retryTask(param);
}
@Recover
public boolean recover(Exception e,String param){
log.error("达到最大重试次数,或抛出了一个没有指定进行重试的异常",e);
log.info("recover param:{}",param);
return false;
}
}
执行重试调用
@Slf4j
@SpringBootTest
public class SpringRetryTests2 {
@Autowired
private SpringRetryDemoService springRetryDemoService;
@Test
public void retry712(){
boolean abc = springRetryDemoService.call("abc");
log.info("---结果是:{}---",abc);
}
}