FeignClient Hystrix 超時測試app
1.註解形式,能夠精確到服務,也能夠動態根據方法名稱設置超時測試
@FeignClient(name = "xxx-im", url = "${svc.xxx.im}",/*configuration=DisableHystrixConfiguration.class, */fallback = ProductClientHystrix.class)
public interface IProductService {ui
//
@RequestMapping(value = "/product/queryCategoriesCodeByProductCode", method = RequestMethod.POST)
String queryCategoriesCodeByProductCode(@RequestBody String productCode);url
@RequestMapping(value = "/product/queryMaxBasicPriceByPizzaSizeCode", method = RequestMethod.POST)
BigDecimal queryMaxBasicPriceByPizzaSizeCode(@RequestBody String pizzaSizeCode);
@RequestMapping(value = "/product/queryCategoriesCodeByProductCode", method = RequestMethod.POST)
String queryCategoriesCodeByProductCode2(@RequestBody String productCode);
}prototype
@Configurable
public class DisableHystrixConfiguration {對象
/*@Bean
@Scope ("prototype")
public Feign.Builder feignBuilder() {
return Feign.builder();ci
}*/
@Bean
public Feign.Builder feignHystrixBuilder() {
return HystrixFeign.builder().setterFactory(new SetterFactory() {
public HystrixCommand.Setter create(Target<?> target, Method method) {
String groupKey = target.name();
//@FeignClient(name = "xxx-im"
System.out.println(groupKey);
String commandKey = method.getName();
System.out.println("commandKey=="+commandKey);
//打印,爲每個方法生成一個對象,此時groupKey,commandKey沒用,能夠設置超時時間
/**
* xxx-im2
commandKey==queryCategoriesCodeByProductCode2
xxx-im2
commandKey==queryCategoriesCodeByProductCode
xxx-im2
commandKey==queryMaxBasicPriceByPizzaSizeCode
*/
int time = 6000;
if("queryCategoriesCodeByProductCode2".equals(commandKey)) {
time = 4000;
}
return HystrixCommand.Setter
.withGroupKey(HystrixCommandGroupKey.Factory.asKey(groupKey))// 控制 RemoteProductService 下,全部方法的Hystrix Configuration
.andCommandKey(HystrixCommandKey.Factory.asKey(commandKey))
.andCommandPropertiesDefaults(
HystrixCommandProperties.Setter().withExecutionTimeoutInMilliseconds(time) // 超時配置
);
}
});
}get
}it
2.配置文件形式(配置文件只能精確到方法級別配置)
feign.hystrix.enabled=true
#hystrix.command.commandKey.execution.isolation.thread.timeoutInMilliseconds=300000
hystrix.command.IProductService\#queryCategoriesCodeByProductCode(String).execution.isolation.thread.timeoutInMilliseconds=300000io
hystrix-core版本:1.5.6
feign-core版本:9.3.1