我现在用的环境是springboot:2.3.1、springCloud:Hoxtom.SR6,然后引入的是hystrix依赖是spring-cloud-starter-netflix-hystrix
-
方法上加上@HystrixCommand
@RequestMapping("/getToOrderMemberAll") @HystrixCommand(commandProperties = {@HystrixProperty(name = "execution.isolation.thread.timeoutInMilliseconds", value = "4000")}) public List<String> getToOrderMemberAll() throws Exception { System.out.println("memberFegin 开始调用member工程。。。" + count++); return memberFeign.getToOrderMemberAll(); }
- 全局配置yml文件
hystrix: command: default: #也可以针对多个服务 execution: isolation: thread: timeoutInMilliseconds: 4000 # 设置hystrix的超时时间为4000ms
- 全部配置config
@Bean // @Scope("prototype") // @ConditionalOnMissingBean // @ConditionalOnProperty(name = "feign.hystrix.enabled") public Feign.Builder feignHystrixBuilder() { return HystrixFeign.builder().setterFactory((target, 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)) // .andCommandKey(HystrixCommandKey.Factory.asKey(groupKey)) // .andCommandKey(HystrixCommandKey.Factory.asKey(target.type().getSimpleName())) .andCommandPropertiesDefaults(HystrixCommandProperties.Setter() .withExecutionTimeoutInMilliseconds(4000) .withExecutionIsolationStrategy(HystrixCommandProperties.ExecutionIsolationStrategy.THREAD)); }); } //.andCommandKey(HystrixCommandKey.Factory.asKey(commandKey)) //默认的接口方式 //.andCommandKey(HystrixCommandKey.Factory.asKey(groupKey)) //service-id方式 //.andCommandKey(HystrixCommandKey.Factory.asKey(target.type().getSimpleName())); //Feign Client Name方式 //不知道用什么的 统一用第一个就好
最后,可能是我引入的openfeign的依赖,提供者那边接口中sleep了2s,这边的三种方式都不行,后来在官方文档中看到hystrix中关于ribbon的配置,然后就在yml文件中加入了下面的配置,至此,熔断超时设置全部ok!
service-member://提供者服务名
ribbon:
# NIWSServerListClassName: com.netflix.loadbalancer.ConfigurationBasedServerList
# listOfServers: https://example1.com,http://example2.com
ConnectTimeout: 2000
ReadTimeout: 4000
MaxTotalHttpConnections: 500
MaxConnectionsPerHost: 100