SpringCloud Fegin超時重試源碼

springCloud中最重要的就是微服務之間的調用,由於網絡延遲或者調用超時會直接致使程序異常,所以超時的配置及處理就相當重要。算法

在開發過程當中被調用的微服務打斷點發現會又屢次重試的狀況,測試環境有的請求響應時間過長也會出現屢次請求,網上查詢了配置試了一下無果,決定本身看看源碼。
本人使用的SpringCloud版本是Camden.SR3。spring

微服務間調用其實走的是http請求,debug了一下默認的ReadTimeout時間爲5s,ConnectTimeout時間爲2s,我使用的是Fegin進行微服務間調用,底層用的仍是Ribbon,網上提到的配置以下網絡

ribbon:
  ReadTimeout: 60000
  ConnectTimeout: 60000
  MaxAutoRetries: 0
  MaxAutoRetriesNextServer: 1

超時時間是有效的可是重試的次數無效,若是直接使用ribbon應該是有效的。ide

下面開始測試:
在微服務B中創建測試方法,sleep 8s 確保請求超時微服務

public Integer testee(){
        try {
            Thread.sleep(8000L);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        return 9;
    }

在微服務A中使用fegin調用此方法時看到有異常測試

看到在SynchronousMethodHandler中請求的方法ui

Request request = targetRequest(template);
    if (logLevel != Logger.Level.NONE) {
      logger.logRequest(metadata.configKey(), logLevel, request);
    }
    Response response;
    long start = System.nanoTime();
    try {
      response = client.execute(request, options);
      response.toBuilder().request(request).build();
    } catch (IOException e) {
      if (logLevel != Logger.Level.NONE) {
        logger.logIOException(metadata.configKey(), logLevel, e, elapsedTime(start));
      }
     //出現異常後拋出RetryableException
      throw errorExecuting(request, e);
    }

出現異常後調用 throw errorExecuting(request, e) 拋出異常this

在調用executeAndDecode的地方catch編碼

@Override
  public Object invoke(Object[] argv) throws Throwable {
    RequestTemplate template = buildTemplateFromArgs.create(argv);
    Retryer retryer = this.retryer.clone();
    while (true) {
      try {
        return executeAndDecode(template);
      } catch (RetryableException e) {
         //重試的地方
        retryer.continueOrPropagate(e);
        if (logLevel != Logger.Level.NONE) {
          logger.logRetry(metadata.configKey(), logLevel);
        }
        continue;
      }
    }
  }

retryer.continueOrPropagate(e); 這句就是關鍵繼續跟進url

public void continueOrPropagate(RetryableException e) {
        //maxAttempts是構造方法傳進來的大於重試次數拋出異常,不然繼續循環執行請求
      if (attempt++ >= maxAttempts) {
        throw e;
      }
     ....

默認的Retryer構造器

public Default() {
      this(100, SECONDS.toMillis(1), 5);
    }

第一個參數period是請求重試的間隔算法參數,第二個參數maxPeriod 是請求間隔最大時間,第三個參數是重試的次數。算法以下:

long interval = (long) (period * Math.pow(1.5, attempt - 1));
      return interval > maxPeriod ? maxPeriod : interval;
    }

咱們可否改寫參數呢?咱們再看看SpringCloud的文檔中關於Retry的配置

 

新建一個配置類

@Configuration
public class FeginConfig {
    @Bean
    public Retryer feginRetryer(){
        Retryer retryer = new Retryer.Default(100, SECONDS.toMillis(10), 3);
        return retryer;
    }
}

在feginClient是加入configuration的配置

@FeignClient(value = "fund-server",fallback = FundClientHystrix.class,configuration = FeginConfig.class)
public interface FundClient

重啓重試,只調用了一次,Fegin重試次數解決。
咱們再看看請求超時這裏的參數

@Override
    public Response execute(Request request, Request.Options options) throws IOException {
        try {
            URI asUri = URI.create(request.url());
            String clientName = asUri.getHost();
            URI uriWithoutHost = cleanUrl(request.url(), clientName);
            FeignLoadBalancer.RibbonRequest ribbonRequest = new FeignLoadBalancer.RibbonRequest(
                    this.delegate, request, uriWithoutHost);
                     //請求參數
            IClientConfig requestConfig = getClientConfig(options, clientName);
            return lbClient(clientName).executeWithLoadBalancer(ribbonRequest,
                    requestConfig).toResponse();
        }
        catch (ClientException e) {
            IOException io = findIOException(e);
            if (io != null) {
                throw io;
            }
            throw new RuntimeException(e);
        }
    }

其中ReadTimeout 和 ConnectTimeout 讀取的就是ribbon的配置,再來看一眼

ribbon:
  ReadTimeout: 60000
  ConnectTimeout: 60000
  MaxAutoRetries: 0
  MaxAutoRetriesNextServer: 1

若是想覆蓋ribbon的超時設置能夠在剛剛寫的FeginConfig裏注入下面的bean

public Request.Options feginOption(){
        Request.Options option = new Request.Options(7000,7000);
        return option;
    }

總結:使用開源的東西在弄不清問題出在哪時最好能看看源碼,對原理的實現以及本身的編碼思路都有很大的提高。

相關文章
相關標籤/搜索