Feign Hystrix (HystrixCommonKey) 设置单独接口的超时时间和FallBack

Feign设置单独接口的超时时间和FallBack(HystrixCommonKey)

  • HystrixCommonKey生成方法:类名#方法名(入参类型)
final class Default implements SetterFactory {

    @Override
    public HystrixCommand.Setter create(Target<?> target, Method method) {
      String groupKey = target.name();
      // 在这里生产HystrixCommonKey
      String commandKey = Feign.configKey(target.type(), method);
      return HystrixCommand.Setter
          .withGroupKey(HystrixCommandGroupKey.Factory.asKey(groupKey))
          .andCommandKey(HystrixCommandKey.Factory.asKey(commandKey));
    }
  }复制代码
  • 配置以下:
hystrix:
  threadpool:
    default:
      # 核心线程池大小 默认10
      coreSize: 20
      # 最大最大线程池大小
      maximumSize: 30
      # 此属性容许maximumSize的配置生效。 那么该值能够等于或高于coreSize。 设置coreSize <maximumSize会建立一个线程池,该线程池能够支持maximumSize并发,但在相对不活动期间将向系统返回线程。 (以keepAliveTimeInMinutes为准)
      allowMaximumSizeToDivergeFromCoreSize: true
      # 请求等待队列
      maxQueueSize: 10
      # 队列大小拒绝阀值 在还未超过请求等待队列时也会拒绝的大小
      queueSizeRejectionThreshold: 10
  command:
    LimitCheckApi#rcsLimitCheck(RpcRequest): #default全局有效 默认值为 commonKey commonKey生成方法在 Feign.configKey(target.type(), method) 中
      fallback:
        enabled: true
      execution:
        timeout:
          #若是enabled设置为false,则请求超时交给ribbon控制,为true,则超时做为熔断根据
          enabled: true
        isolation:
          #隔离策略,有THREAD和SEMAPHORE
          #THREAD - 它在单独的线程上执行,并发请求受线程池中的线程数量的限制
          #SEMAPHORE - 它在调用线程上执行,并发请求受到信号量计数的限制
          #对比:https://www.cnblogs.com/java-synchronized/p/7927726.html
          thread:
            timeoutInMilliseconds: 800 #断路器超时时间,默认1000ms
    LimitCheckApi#testTimeOutFallBack(long):
      fallback:
        enabled: true
      execution:
        timeout:
            #若是enabled设置为false,则请求超时交给ribbon控制,为true,则超时做为熔断根据
            enabled: true
        isolation:
          #隔离策略,有THREAD和SEMAPHORE
          #THREAD - 它在单独的线程上执行,并发请求受线程池中的线程数量的限制
          #SEMAPHORE - 它在调用线程上执行,并发请求受到信号量计数的限制
          #对比:https://www.cnblogs.com/java-synchronized/p/7927726.html
          thread:
            timeoutInMilliseconds: 800 #断路器超时时间,默认1000ms

feign:
  hystrix:
    enabled: true复制代码

欢迎加入做者的微信公众号:html

相关文章
相关标签/搜索