circuitBreaker.requestVolumeThreshold没有按预期工作

时间:2016-11-06 21:43:37

标签: spring spring-cloud spring-cloud-netflix

我正在测试Spring Cloud断路器,而我却错过了实际的" circuitBreaker.requestVolumeThreshold"参数实际上有用......看看我的例子......

@HystrixCommand(
            fallbackMethod = "invokeMicroServiceFallback",
            commandProperties = {
                @HystrixProperty(name = "execution.isolation.thread.timeoutInMilliseconds",
                        value = "30000"),
                @HystrixProperty(name = "circuitBreaker.requestVolumeThreshold",
                        value = "2"),
                @HystrixProperty(name = "circuitBreaker.sleepWindowInMilliseconds",
                        value = "500"),
                @HystrixProperty(name = "metrics.rollingStats.timeInMilliseconds",
                        value = "180000")
            },
            threadPoolProperties = {
                @HystrixProperty(name = "coreSize", value = "30"),
                @HystrixProperty(name = "metrics.rollingStats.timeInMilliseconds",
                        value = "180000")
            })
    public void invokeMicroService() {
        final RestTemplate restTemplate = new RestTemplate();

        final ServiceInstance serviceInstance = loadBalancer.choose("personsService");
        if (serviceInstance != null) {
            System.out.println("Invoking instance at URL: "+serviceInstance.getUri());
            System.out.println("Result :"+
                    restTemplate.getForObject(serviceInstance.getUri()+"/persons",
                            String.class));
        } else {
            System.out.println("Service is down...");
            throw new IllegalStateException("PersonsService is not running!");
        }
    }

    public void invokeMicroServiceFallback() {
        System.out.println("Waiting for circuit-breaker to close again...");
    }

如果我关闭了personsService并在循环中调用了invokeMicroService,那么我有很多输出,如:

首先:

  Invoking instance at URL: <URL at my service>;
  "Waiting for circuit-breaker to close again..."
过了一段时间后,又重复了一遍:

  Service is down...
  "Waiting for circuit-breaker to close again..."

circuitBreaker.requestVolumeThreshold实际上在这里做什么? 为什么我尝试访问人员服务的次数超过两次?

提前感谢...

1 个答案:

答案 0 :(得分:0)

来自维基https://github.com/Netflix/Hystrix/wiki/Configuration#circuitBreaker.requestVolumeThreshold

  

circuitBreaker.requestVolumeThreshold

     

此属性设置滚动窗口中的最小请求数   这会使电路跳闸。

     

例如,如果值为20,则仅接收到19个请求   在滚动窗口(比如一个10秒的窗口)电路将   即使全部失败,也不会开放。