【发布时间】:2018-08-19 18:53:34
【问题描述】:
您好,我对简单折叠的方法有疑问: 我的控制器:
@GetMapping(path = "/collapsed/{id}")
public Car getFromServiceCollapsed(@PathVariable Integer id) throws Exception{
Car result = carService.getCollapsed(id).get();
return result;
}
和服务:
@HystrixCollapser(batchMethod = "getCollapsedCars",
collapserProperties = {
@HystrixProperty(name = "timerDelayInMilliseconds", value = "100"),
@HystrixProperty(name = "requestCache.enabled", value = "true")
},
collapserKey = "getCollapsed")
public Future<Car> getCollapsed(Integer id){
return null;
}
@HystrixCommand(groupKey = "getCircuitService",
threadPoolKey = "getCircuitService",
threadPoolProperties = {
@HystrixProperty(name = "coreSize", value = "10"),
@HystrixProperty(name = "maximumSize", value = "14"),
@HystrixProperty(name = "maxQueueSize", value = "20")
},
commandProperties = {
@HystrixProperty(name = "execution.isolation.strategy", value = "THREAD"),
@HystrixProperty(name = "execution.isolation.thread.timeoutInMilliseconds", value = "2000"),
@HystrixProperty(name = "execution.timeout.enabled", value = "true")
},
fallbackMethod = "getDefault")
private List<Car> getCollapsedCars(List<Integer> ids){
LOGGER.debug("Collapse for {}", ids.size());
return ids.stream().map(this::getCar).collect(Collectors.toList());
}
调用控制器时抛出异常:
java.lang.NullPointerException: null
at com.netflix.hystrix.HystrixCollapser$3.call(HystrixCollapser.java:398) ~[hystrix-core-1.5.12.jar:1.5.12]
at com.netflix.hystrix.HystrixCollapser$3.call(HystrixCollapser.java:382) ~[hystrix-core-1.5.12.jar:1.5.12]
但是,当我在每个控制器调用上使用初始化 HystrixRequestContext 修改控制器时,它开始工作但不是那么好 - 服务方法 getCollapsedCars 总是记录:“Collapse for 1” - 但我用 1 毫秒的延迟一个一个地发送 20 个 RQ。
修改后的控制器:
@GetMapping(path = "/collapsed/{id}")
public Car getFromServiceCollapsed(@PathVariable Integer id) throws Exception{
HystrixRequestContext context = HystrixRequestContext.initializeContext();
Car result = carService.getCollapsed(id).get();
context.shutdown();
return result;
}
如何使折叠的命令工作?
【问题讨论】:
标签: spring spring-cloud-netflix hystrix