Hystrix超時時間配置

Hystrix配置總體的超時時間

hystrix.command.default.execution.isolation.thread.timeoutInMilliseconds=1000java

配置參考githubgit

配置單個超時時間

hystrix.command.HystrixCommandKey.execution.isolation.thread.timeoutInMilliseconds=1000github

其中HystrixCommandKey爲@HystrixCommand 進行配置
example
@HystrixCommand(groupKey = "StoreSubmission", commandKey = "StoreSubmission", threadPoolKey = "StoreSubmission")
public String storeSubmission(ReturnType returnType, InputStream is, String id) {
}

@HystrixCommand須要引入依賴
<dependency>
	<groupId>com.netflix.hystrix</groupId>
	<artifactId>hystrix-javanica</artifactId>
	<version>${hystrix-version}</version>
</dependency>

配置參考stackoverflowspring

在fegin中配置超時時間

在fegin中沒法使用@HystrixCommand來單獨進行配置app

hystrix.command.MyService#getLastTimeData(Map).execution.isolation.thread.timeoutInMilliseconds這種能夠的,經過實踐驗證了 @甲申驗證

配置原理

HystrixInvocationHandleride

final class HystrixInvocationHandler implements InvocationHandler {

  private final Target<?> target;
  private final Map<Method, MethodHandler> dispatch;
  private final FallbackFactory<?> fallbackFactory; // Nullable
  private final Map<Method, Method> fallbackMethodMap;
  private final Map<Method, Setter> setterMethodMap;//時間配置
  
  
  static Map<Method, Setter> toSetters(SetterFactory setterFactory, Target<?> target,
                                       Set<Method> methods) {
    Map<Method, Setter> result = new LinkedHashMap<Method, Setter>();
    for (Method method : methods) {
      method.setAccessible(true);
      result.put(method, setterFactory.create(target, method));
    }
    return result;
  }

SetterFactoryspring-boot

public interface SetterFactory {

  /**
   * Returns a hystrix setter appropriate for the given target and method
   */
  HystrixCommand.Setter create(Target<?> target, Method method);

  /**
   * Default behavior is to derive the group key from {@link Target#name()} and the command key from
   * {@link Feign#configKey(Class, Method)}.
   */
  final class Default implements SetterFactory {

    @Override
    public HystrixCommand.Setter create(Target<?> target, Method method) {
      String groupKey = target.name();
      String commandKey = Feign.configKey(target.type(), method);
      return HystrixCommand.Setter
          .withGroupKey(HystrixCommandGroupKey.Factory.asKey(groupKey))
          .andCommandKey(HystrixCommandKey.Factory.asKey(commandKey));
    }
  }
}

feginui

public static String configKey(Class targetType, Method method) {
    StringBuilder builder = new StringBuilder();
    builder.append(targetType.getSimpleName());
    builder.append('#').append(method.getName()).append('(');
    for (Type param : method.getGenericParameterTypes()) {
      param = Types.resolve(targetType, targetType, param);
      builder.append(Types.getRawType(param).getSimpleName()).append(',');
    }
    if (method.getParameterTypes().length > 0) {
      builder.deleteCharAt(builder.length() - 1);
    }
    return builder.append(')').toString();
  }

對應上面的規則就能夠解釋了spa

hystrix.command.MyService#getLastTimeData(Map).execution.isolation.thread.timeoutInMilliseconds
其中MyService#getLastTimeData(Map)爲commandKey有configKey進行生成

hystrix生效.net

HystrixInvocationHandler.invoke->
newHystrixCommand->execute->toObservable->
addTimerListener(executionTimeoutInMilliseconds)->
ScheduledThreadPoolExecutor.scheduleAtFixedRate-call-TimerListener.tick->
timeoutRunnable.run->throw HystrixTimeoutException 
簡單來講就是建立一個ScheduledThreadPoolExecutor,超時時間後執行拋出異常操做,若是超時時間執行完成會吧這個Reference應用幹掉

ps

羣裏看到的問題,閒順便翻了下源碼。有錯誤請指出,避免誤人子弟

參考