feign.hystrix.enabled开启后client timeout参数设置

openfeign: feign.hystrix.enabled = true

feign.hystrix.enabled = true 标识开启hystrix

1、springcloud版本

<dependency>
    <groupId>org.springframework.cloud</groupId>
    <artifactId>spring-cloud-dependencies</artifactId>
    <version>Hoxton.SR11</version>
    <type>pom</type>
    <scope>import</scope>
</dependency>
<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-dependencies</artifactId>
    <version>2.3.5.RELEASE</version>
    <type>pom</type>
    <scope>import</scope>
</dependency>

2、yml配置

feign:
  hystrix:
    enabled: true	
hystrix:
  command:
    default:
      execution:
        isolation:
          thread:
            timeoutInMilliseconds: 3000
        thread:
          timeoutInMilliseconds: 1000    

3、代码分析

  • @EnableFeignClients

    • 会加载FeignClientsRegistrar到spring容器且执行registerBeanDefinitions方法;
    • 调用registerFeignClients扫描解析所有@FeignClient的类解析成BeanDefinition到容器,且参数feignClientsRegistrarFactoryBean是FeignClientFactoryBean对象;
  • 实例化FeignClient

    • FeignClientFactoryBean.getObject()
      • getTarget()
        • loadBalance()
          • … 最终return FeignInvocationHandler;
  • FeignClient调用

    • HystrixInvocationHandler.invoke()
      • new HystrixCommand(setterMethodMap.get(method)) {…}.execute()
  • hystrix.command.default相关参数生效顺序 D A B C 啥都没配置 默认值
    源码如下

    	public HystrixDynamicProperty<T> build() {
            if (properties.size() < 1) throw new IllegalArgumentException();
            if (properties.size() == 1) return properties.get(0);
            List<HystrixDynamicProperty<T>> reversed = 
                    new ArrayList<HystrixDynamicProperty<T>>(properties);
            Collections.reverse(reversed);
            ChainProperty<T> current = null;
            for (HystrixDynamicProperty<T> p : reversed) {
                if (current == null) {
                    current = new ChainProperty<T>(p);
                }
                else {
                    current = new ChainProperty<T>(p, current);
                }
            }
            return new ChainHystrixProperty<T>(current);
        }
    
    • A、@FeignClient(configuration = FeignConfig.class) ,重写Feign.Builder.SetterFactory,相关设置参数会写到setterMethodMap供上述代码调用;
      @FeignClient(value = "service-X", configuration = configX.class, fallbackFactory = ServiceCallFailBack.class)
      
    • B、定义全局的Feign.Builder
      @Configuration(proxyBeanMethods = false)
       @ConditionalOnClass({ HystrixCommand.class, HystrixFeign.class })
      public class FeignConfig {
          @Bean
          @Scope("prototype")
          @ConditionalOnProperty(name = "feign.hystrix.enabled")
          public Feign.Builder feignHystrixBuilder() {
              HystrixFeign.Builder builder = HystrixFeign.builder();
              SetterFactory setterFactory= new SetterFactory(){
                  @Override
                  public HystrixCommand.Setter create(Target<?> target, Method method) {
                      String groupKey = target.name();
                      String commandKey = Feign.configKey(target.type(), method);
                      HystrixThreadPoolProperties.Setter setter = HystrixThreadPoolProperties.Setter().withCoreSize(100).withMaxQueueSize(300);
                      HystrixCommandProperties.Setter setter1 = HystrixCommandProperties.Setter().withExecutionTimeoutInMilliseconds(8000);
                      return HystrixCommand.Setter
                              .withGroupKey(HystrixCommandGroupKey.Factory.asKey(groupKey))
                              .andCommandKey(HystrixCommandKey.Factory.asKey(commandKey))
                              .andThreadPoolPropertiesDefaults(setter)
                              .andCommandPropertiesDefaults(setter1);
      
      
                  }
              };
              builder.setterFactory(setterFactory);
              return builder;
          }
      }
      
    • C、配置文件读取第一种
       hystrix:
      	 command:
      	    default:
      	      execution:
      	        isolation:
      	          thread:
      	            timeoutInMilliseconds: 3000
      	        thread:
      	          timeoutInMilliseconds: 1000
      
    • D、配置文件读取第二中
       hystrix:
      	  command:
      	    ServiceClient#getMethod(Integer):
      	      execution:
      	        isolation:
      	          thread:
      	            timeoutInMilliseconds: 5000
      	        thread:
      	          timeoutInMilliseconds: 5000
       
      
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值