Spring Coud

Feign-灵活的使用Hystrix熔断(自定义CommandK

2020-07-14  本文已影响0人  小胖学编程

Feign可以直接去集成Hystrix熔断。具体配置:Hystrix熔断&&Feign熔断

但是配置时,却不是很灵活,只是支持default类名#方法名()的配置。这就不能对类或者一组方法进行统一的配置。

源码改造:

@Configuration
public class MyHystrixFeign {

    @Bean
    @Scope("prototype")
    @ConditionalOnProperty(name = "feign.hystrix.enabled")
    public Feign.Builder feignHystrixBuilder() {
        return HystrixFeign.builder().setterFactory(new MyDefault());
    }

    /**
     * 修改源码{@link HystrixFeign.Builder#setterFactory}
     */
    final class MyDefault implements SetterFactory {

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

}

项目启动后,会遍历@Feign注解类中的每一个方法调用create生成Hystrix的配置key,也可以是线程池的key。
源码位置:feign.hystrix.HystrixInvocationHandler#toSetters如下图所示。

  /**
   * Process all methods in the target so that appropriate setters are created.
   * 为每一个方法增加熔断的配置(可以在代码中配置,也可以在yml中进行配置)。
   */
  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;
  }

yml的配置:

feign:
  # Dalston SR1(待定)之后的版本默认关闭hystrix对feign的支持,如果想要使用fallback功能这里必须启用。
  hystrix:
    enabled: true
#
#
#hystrix:
#  command:
#    default:
#      execution:
#        isolation:
#          thread:
#            timeoutInMilliseconds: 100000  # 调用者执行的超时时间默认是1s
#  threadpool:
#    default:
#      coreSize: 1 #核心线程数
#      maximumSize: 1 # 最大线程数
#      maxQueueSize: 0 # 不使用队列
#

## https://blog.csdn.net/Mr_rain/article/details/89472167
## Feign仅仅支持方法级别和default级别
hystrix:
  command:
    TestDemoApi1:   ## 支持类级别
     execution:
      isolation:
       thread:
        timeoutInMilliseconds: 4000  # 调用者执行的超时时间默认是1s

注:默认情况下,Feign-Hystrix会使用服务名作为CommandGroup,使用类名#方法名作为CommandKey。

而在yml配置:全局配置是default的配置,而实例配置为commandKey配置。

更多的配置,可以参考Hystrix(2)— 相关配置

推荐阅读

[享学Feign] 十二、Feign通过feign-hystrix模块使其拥有熔断、降级能力

上一篇 下一篇

猜你喜欢

热点阅读