hystrixTimeout 、 ribbonTimeout 合理设定

1、在 DefaultClientConfigImpl.class 

public static final int DEFAULT_MAX_AUTO_RETRIES_NEXT_SERVER = 1;//maxAutoRetriesNextServer 默认是1
public static final int DEFAULT_MAX_AUTO_RETRIES = 0;//maxAutoRetries 默认是0

2、在 AbstractRibbonCommand.java 中

//警告的代码

protected static int getHystrixTimeout(IClientConfig config, String commandKey) {
    /*...*/
   if(hystrixTimeout < ribbonTimeout) {
      LOGGER.warn("The Hystrix timeout of " + hystrixTimeout + "ms for the command " + commandKey +
         " is set lower than the combination of the Ribbon read and connect timeout, " + ribbonTimeout + "ms.");
   }
   return hystrixTimeout;
}

//ribbon超时计算 

protected static int getRibbonTimeout(IClientConfig config, String commandKey) {
   int ribbonTimeout;
   if (config == null) {
      ribbonTimeout = RibbonClientConfiguration.DEFAULT_READ_TIMEOUT + RibbonClientConfiguration.DEFAULT_CONNECT_TIMEOUT;
   } else {
      int ribbonReadTimeout = getTimeout(config, commandKey, "ReadTimeout",
         IClientConfigKey.Keys.ReadTimeout, RibbonClientConfiguration.DEFAULT_READ_TIMEOUT);
      int ribbonConnectTimeout = getTimeout(config, commandKey, "ConnectTimeout",
         IClientConfigKey.Keys.ConnectTimeout, RibbonClientConfiguration.DEFAULT_CONNECT_TIMEOUT);
      int maxAutoRetries = getTimeout(config, commandKey, "MaxAutoRetries",
         IClientConfigKey.Keys.MaxAutoRetries, DefaultClientConfigImpl.DEFAULT_MAX_AUTO_RETRIES);
      int maxAutoRetriesNextServer = getTimeout(config, commandKey, "MaxAutoRetriesNextServer",
         IClientConfigKey.Keys.MaxAutoRetriesNextServer, DefaultClientConfigImpl.DEFAULT_MAX_AUTO_RETRIES_NEXT_SERVER);
      ribbonTimeout = (ribbonReadTimeout + ribbonConnectTimeout) * (maxAutoRetries + 1) * (maxAutoRetriesNextServer + 1);
   }
   return ribbonTimeout;
}

3、比如:配置文件中的配置

hystrix:
  command:
    default:
      execution:
        isolation:
          thread:
            timeoutInMilliseconds: 5000 # 全局熔断超时时长:5000ms

ribbon:
  ConnectTimeout: 1000 #ribbon 链接超时时长
  ReadTimeout: 3500 #ribbon 读取超时时长
  MaxAutoRetries: 0  #当前服务重试次数
  MaxAutoRetriesNextServer: 0 #切换服务重试次数

#ribbonTimeout = (ReadTimeout + ConnectTimeout) * (maxAutoRetries + 1) * (maxAutoRetriesNextServer + 1);

5000>(3500+1000) *(0+1)*(0+1)

所以不会报警告。

 

 

你可能感兴趣的:(springCloud)