【问题标题】:@HystrixCollapser null pointer exception on collapsed method@HystrixCollapser 折叠方法上的空指针异常
【发布时间】: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


    【解决方案1】:

    我面临着完全相同的问题。请求未按批次分组。 但是问题是 HystrixRequestContext,因为这是您的请求本地的。

    请删除本地 HystrixRequestContext 创建并将其作为范围添加到折叠器。

    @HystrixCollapser(scope = com.netflix.hystrix.HystrixCollapser.Scope.GLOBAL, batchMethod =.......)

    瞧!!!

    【讨论】:

      猜你喜欢
      • 2020-09-11
      • 1970-01-01
      • 2013-09-17
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多