Feign之Retryer重试机制

一、背景

Feign在调用外部服务的时候,偶尔会出现 SocketException: Connection reset 、NoHttpResponseException: xxxxxx failed to respond 等异常而这些异常并非是因为服务提供方服务不可用,或者负载过高引起的,百度查了查大概原因说是因为在Client 和Server建立链接之后Server端在数据为正常响应之前就关闭了链接导致的异常。具体参数 https://blog.csdn.net/siantbaicn/article/details/80854528 这个文章。
不管因为是因为啥,总之出了异常还是要处理的,而这种异常往往在调用失败之后下次调用就可以正常响应了,所以就有了Feign Retryer重试相关的配置。

二、Feign SynchronousMethodHandler重试

Feign 调用发起请求处理类 SynchronousMethodHandler 重试代码如下

这里的 Retryer 则是 Feign 的重试策略接口,默认Feign 配置的重试策略是 Retryer.NEVER_RETRY 也就是不走重试,直接异常报错。
这里需要注意几点:
1.Feign默认配置是不走重试策略的,当发生RetryableException异常时直接抛出异常。
2.并非所有的异常都会触发重试策略,只有 RetryableException 异常才会触发异常策略。
3.在默认Feign配置情况下,只有在网络调用时发生 IOException 异常时,才会抛出 RetryableException,也是就是说链接超时、读超时等不不会触发此异常。
因此常见的 SocketException、NoHttpResponseException、UnknownHostException、HttpRetryException、SocketConnectException、ConnectionClosedException 等异常都可触发Feign重试机制。
注:常见的还有 SocketTimeoutException、TimeoutException、ReadTimeoutException、WriteTimeoutException 等都不属于IO异常,因为不会触发Feign重试机制。

三、自定义重试配置

1.自定义Retryer

Feign 官方给我们提供了一个默认的 Retryer, feign.Retryer.Default 策略,该策略可指定测试次数,每次最大时间间隔时长等配置,该策略每次重试等待时长并非固定,而是一次比一次等待时间加大,例如 第一次等待 100毫秒、第二次等待 200毫秒、第三次 300毫秒 这样...

attempt 重试次数、maxPeriod最大等待时长、period时长,可自定义默认配置。
这里可以直接使用官方提供的Default,也可以自定义策略 实现feign.Retryer 接口 continueOrPropagate 方法实现自定义重试策略。
由于官方提供的Default基本上就已经能满足我们的业务诉求了,因此我们就自定义一个 Retryer然后继承Default,在此基础上添加一下日志打印,还有上报统计功能,实现如下。

@Slf4j
public static class CommonFeignRetry extends Default {
 
    private static final String METRICS_KEY = "feign_retry_count";
 
    private static final String APOLLO_CONFIG_KEY = "feign.retry.enable";
 
    private final Counter metrics = Metrics.newCounter(METRICS_KEY).build();
 
    public CommonFeignRetry() {
        //重试5次 最大间隔时间1秒
        this(100, SECONDS.toMillis(1), 5);
    }
 
    public CommonFeignRetry(long period, long maxPeriod, int maxAttempts) {
        super(period, maxPeriod, maxAttempts);
    }
 
    @Override
    public void continueOrPropagate(RetryableException e) {
        if (Boolean.FALSE.equals(ConfigService.getAppConfig().getBooleanProperty(APOLLO_CONFIG_KEY,
            Boolean.TRUE))) {
            throw e;
        }
        metrics.once();
        log.warn("【FeignRetryAble】Message【{}】", e.getMessage());
        super.continueOrPropagate(e);
    }
 
    @Override
    public Retryer clone() {
        return new CommonFeignRetry();
    }
}

2.指定FeignClient走重试策略

因业务场景的不通,有的时候可能只需要指定某个FeignClient 需要进行重试,而不是整个project所有Client都进行重试,针对这种需求Feign官方提供了两种配置策略,1.通过配置文件feign.client.config.[feignName].retryer=Retryer实现类([feignName]为Client Id名称) 方式配置,或者通过指定 configuration进行配置。
方式一

feign.client.config.testClient.retryer=com.ddmc.pfs.config.feign.CommonFeignRetryConfig.CommonFeignRetryer

方式二

public class CommonFeignRetryConfig {
    @Bean
    Retryer getRetryBean() {
        return new CommonFeignRetryer();
    }
   //@FeignClient注解指定配置
}

@FeignClient(name = "test-service", contextId = "testClient",
fallbackFactory = FmsBaseClientFallbackFactory.class, configuration = CommonFeignRetryConfig.class)

3.全局所有FeignClient走重试策略。

全局有效只需要在 配置类上加上 @Configuration 这样,此配置对就对所有FeignClient都生效。

@Configuration
public class CommonFeignRetryConfig {
 
    @Bean
    Retryer getRetryBean() {
        return new CommonFeignRetryer();
    }
}

四、扩展ErrorDecoder

ErrorDecoder作为Feign响应状态码非2xx时触发的异常处理解析器,也就是说在Server端响应非2xx状态码的时候就或触发这个异常解析处理,例如常见的502、503、、400 、404 状态码等都可以触发此异常处理。因此我们可以将ErrorDecoder和Retryer结合起来,针对502之类的状态码可以加重试处理,降低网络异常引起的中断问题。
ErrorDecoder接口只有一个decode方法,其中参数 methodKey(方法路径 例如 FmsBaseClient#getCorporations) response(响应流)

这里如果要想让Feign在状态码为502时触发重试处理,那么我们就需要判断状态为 502时返回 RetryableException 异常即可。实例代码如下

@Slf4j
public class FeignErrorDecoder implements ErrorDecoder {
 
    @Override
    public Exception decode(String methodKey, Response response) {
        if (response.status() == 502) {
            return new RetryableException("Server 502", response.request().httpMethod(), null);
        } else {
            return errorStatus(methodKey, response);
        }
    }
}

配置方式同Retryer配置方式,示例

//在配置类中加入如下代码
 @Bean
 ErrorDecoder getError() {
     return new FeignErrorDecoder();
 }

五、参考文章

https://docs.spring.io/spring-cloud-openfeign/docs/current/reference/html/#configuration-properties
https://blog.csdn.net/siantbaicn/article/details/80854528
https://www.cnblogs.com/prepared/p/15263661.html

你可能感兴趣的:(Feign之Retryer重试机制)