openfeign接口自定义重试机制的实现
1.Spring 重试机制 @Retryable
@Retryable是Spring提供的可重试注解,使用spring提供的重试机制,实现步骤如下:
1.添加maven依赖
<dependency>
<groupId>org.springframework.retry</groupId>
<artifactId>spring-retry</artifactId>
<version>1.2.5.RELEASE</version>
</dependency>
2.在启动类或者方法所在的类上添加注解@EnableRetry
@EnableRetry
public class Application {
public static void main(String[] args) {
SpringApplication.run(Application.class, args);
}
}
3.在需要重试的方法上添加注解@Retryable,示例如下:
/**
* 最大重试3次,时间间隔(秒) 2, 4, 8 ...
* 注: 这里的3次包括了第一次正常执行的次数
*/
@Retryable(value = Exception.class, maxAttempts = 3, backoff = @Backoff(delay = 2000L, multiplier = 2))
@Async
public void doNotify() {
// TODO doSomeThings...
}
@Retryable注解中的参数说明:
maxAttempts :最大重试次数,默认为3,如果要设置的重试次数为3,可以不写;
value:抛出指定异常才会重试
include:和value一样,默认为空,当exclude也为空时,默认所有异常
exclude:指定不处理的异常
backoff:重试等待策略,默认使用@Backoff的value默认为1000L @Backoff注解中的参数说明:
delay:每次重试延迟毫秒数,默认为0L
value:delay的别名,默认为1000L,当delay>0时,value将会被忽略
maxDelay:最大延迟毫秒数,默认为0L,
multiplier:(指定延迟倍数)默认为0;大于0时生效;如果delay等于2,multiplier等于2,则第一次重试为2秒,第二次为4秒,第三次为8秒…
random:随机值加权,默认为false;当multiplier>0时,上次延迟毫秒 < 延迟时间 < 最大延迟 * multiplier 结合@Recover使用 可以在指定方法上标记@Recover来开启重试失败后调用的方法(注意,需跟重处理方法在同一个类中) @Recover 当重试到达指定次数时,被注解的方法将被回调,可以在该方法中进行日志处理。需要注意的是发生的异常和入参类型一致时才会回调。
@Slf4j
@Component
@EnableRetry
public class RecoryTest {
@Retryable(value = {RetryException.class}, // 指定发生的异常进行重试
maxAttempts=3, // 重试次数, 默认即为3
backoff = @Backoff(delay = 2000L, multiplier = 2)) // 每次重试延迟毫秒数 及 延迟倍数
@Async
public void retry() {
log.info("retry start");
throw new RetryException("retry fail");
}
@Recover
public void recover (RetryException e) {
log.error("recovery,{}",e.getMessage());
}
}
2.自定义注解实现重试
1.自定义重试补充注解
import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
/**
* 重试补充机制
* @author : wangjg
* @date : 2023/3/17 10:22
*/
@Target(ElementType.TYPE)
@Retention(RetentionPolicy.RUNTIME)
public @interface Backoff {
/**
* 延迟多久重试 单位毫秒
* @return long 延迟时间
*/
long delay() default 1000L;
/**
* 重试之间的最大等待时间(单位为毫秒)(默认0 =忽略)
* @return long 重试之间的最大延迟
*/
long maxDelay() default 0L;
/**
* 如果为正,则用作乘数,用于产生下一次后退的延迟。(默认0 =忽略)例如:配了1,第一次1s重试,第二次2s,第三次4秒重试
* @return double 返回一个乘数用于计算下一次回退延迟
*/
double multiplier() default 0.0D;
}
2.自定义重试调用注解
import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
/**
* feign重试调用注解
* @author : wangjg
* @date : 2023/3/17 10:18
*/
@Target({ElementType.METHOD,ElementType.TYPE})
@Retention(RetentionPolicy.RUNTIME)
public @interface FeignRetry {
/**
* 重试补充机制 默认是{@link Backoff ()}注解
* @return Backoff
*/
Backoff backoff() default @Backoff();
/**
* 重试次数,默认3
* @return int 重试次数
*/
int maxAttempt() default 3;
/**
* 可重试的异常类型。默认为空
* @return []可重试的异常类型
*/
Class<? extends Throwable>[] include() default {};
}
3.自定义切面拦截
import com.asset.middle.annotations.FeignRetry;
import feign.RetryableException;
import lombok.extern.slf4j.Slf4j;
import org.apache.http.NoHttpResponseException;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.reflect.MethodSignature;
import org.springframework.retry.backoff.BackOffPolicy;
import org.springframework.retry.backoff.ExponentialBackOffPolicy;
import org.springframework.retry.backoff.FixedBackOffPolicy;
import org.springframework.retry.policy.SimpleRetryPolicy;
import org.springframework.retry.support.RetryTemplate;
import org.springframework.stereotype.Component;
import java.lang.reflect.Method;
import java.util.HashMap;
import java.util.Map;
/**
* @author : wangjg
* @date : 2023/3/17 10:48
*/
@Slf4j
@Aspect
@Component
public class FeignRetryAspect {
@Around("@annotation(com.asset.middle.annotations.FeignRetry)")
public Object retry(ProceedingJoinPoint joinPoint) throws Throwable{
Method method = getCurrentMethod(joinPoint);
FeignRetry feignRetry = method.getAnnotation(FeignRetry.class);
RetryTemplate retryTemplate = new RetryTemplate();
retryTemplate.setBackOffPolicy(prepareBackOffPolicy(feignRetry));
retryTemplate.setRetryPolicy(prepareSimpleRetryPolicy(feignRetry));
//重试
return retryTemplate.execute(retryContext->{
int retryCount = retryContext.getRetryCount();
log.info("send request method:{},max attempt:{},delay:{},retryCount:{}",method.getName(),feignRetry.maxAttempt(),feignRetry.backoff().delay(),retryCount);
return joinPoint.proceed(joinPoint.getArgs());
});
}
/**
* 补偿策略
* @param feignRetry 标识方法的FeignRetry注解
* @return BackOffPolicy
*/
private BackOffPolicy prepareBackOffPolicy(FeignRetry feignRetry) {
if (feignRetry.backoff().multiplier() != 0) {
ExponentialBackOffPolicy backOffPolicy = new ExponentialBackOffPolicy();
backOffPolicy.setInitialInterval(feignRetry.backoff().delay());
backOffPolicy.setMaxInterval(feignRetry.backoff().maxDelay());
backOffPolicy.setMultiplier(feignRetry.backoff().multiplier());
return backOffPolicy;
} else {
FixedBackOffPolicy fixedBackOffPolicy = new FixedBackOffPolicy();
fixedBackOffPolicy.setBackOffPeriod(feignRetry.backoff().delay());
return fixedBackOffPolicy;
}
}
/**
* 简单的重试策略
* @param feignRetry 标识方法的FeignRetry注解
* @return SimpleRetryPolicy
*/
private SimpleRetryPolicy prepareSimpleRetryPolicy(FeignRetry feignRetry) {
//重试的异常类型
Map<Class<? extends Throwable>, Boolean> policyMap = new HashMap<>();
policyMap.put(RetryableException.class, true); // Connection refused or time out
policyMap.put(NoHttpResponseException.class, true);//Caused by: org.apache.http.NoHttpResponseException: partner.api.asset.com:443 failed to respond
policyMap.put(IllegalArgumentException.class, true);
policyMap.put(Exception.class, true);
for (Class<? extends Throwable> t : feignRetry.include()) {
policyMap.put(t, true);
}
return new SimpleRetryPolicy(feignRetry.maxAttempt(), policyMap, true);
}
private Method getCurrentMethod(JoinPoint joinPoint){
MethodSignature signature = (MethodSignature) joinPoint.getSignature();
return signature.getMethod();
}
}
4.自定义注解使用方法
可以直接在service的实现类方法上使用,例如:
@Override
@FeignRetry(maxAttempt = 2,backoff = @Backoff(delay = 500L))
public Boolean returnBack(LaborBackRO laborBackRO){
EmsResponseDTO<Boolean> responseDTO;
try {
responseDTO = emsAssetLaborReturnClient.returnBack(emsAuthService.getTokenRequestHeads(),laborBackRO);
}catch (Exception e){
throw new IllegalArgumentException("请求通知回执失败!",e);
}
if (responseDTO == null || responseDTO.getErrCode() != 0) {
throw new IllegalArgumentException("请求通知回执失败: " + JsonUtil.toJson(responseDTO));
}
return responseDTO.getData();
}
}