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;
- loadBalance()
- getTarget()
- FeignClientFactoryBean.getObject()
-
FeignClient调用
- HystrixInvocationHandler.invoke()
- new HystrixCommand(setterMethodMap.get(method)) {…}.execute()
- HystrixInvocationHandler.invoke()
-
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
- A、@FeignClient(configuration = FeignConfig.class) ,重写Feign.Builder.SetterFactory,相关设置参数会写到setterMethodMap供上述代码调用;