Spring Cloud 断路器

来源:互联网 发布:苹果mac os x系统下载 编辑:程序博客网 时间:2024/04/27 19:23

1. Smaphore策略
参数配置:

    /**     * execution SEMAPHORE     */    @HystrixCommand(            groupKey = "Semaphore test",               //hystrix dashboard 中 ThreadPool的名字            commandKey = "Semaphore test",             //hystrix dashboard 中 Circuit的名字,如果使用SEMAPHORE策略,dashboard里面没有ThreadPool显示            commandProperties = {                    @HystrixProperty(name = "execution.isolation.strategy", value = "SEMAPHORE"),                    @HystrixProperty(name = "execution.isolation.semaphore.maxConcurrentRequests", value = "2")   //default 10,当隔离策略使用信号量的时候,当并发请求数达到此值,后续请求将被拒绝            }    )    public String helloSemaphore(){        return "ok";    }

Apache Bench测试:
ab -n 100 -c 3 -t 20 http://localhost:9000/ribbon/consumer/hystrix/semaphore
这里写图片描述

2.Thread策略

参数配置:

    /**     * circuitBreaker  断路器     */    @HystrixCommand(            fallbackMethod = "helloFallback",            groupKey = "custom CircuitBreaker test",               //hystrix dashboard 中 ThreadPool的名字            commandKey = "custom CircuitBreaker test",             //hystrix dashboard 中 Circuit的名字            commandProperties = {                   // @HystrixProperty(name = "execution.isolation.thread.timeoutInMilliseconds", value = "2000"),       //default 1000                    @HystrixProperty(name = "circuitBreaker.enabled", value = "true"),       //default true, 当服务命令请求失败,是否开启断路器来跟踪此服务健康指标和熔断服务请求                    @HystrixProperty(name = "circuitBreaker.requestVolumeThreshold", value = "20"), //default 20, 设置在滚动时间窗中,断路器熔断的最小请求数,在时间窗内(默认10s)内收到了19个请求,即使这19个请求都失败,也不断路(熔断器不打开)                    @HystrixProperty(name = "circuitBreaker.sleepWindowInMilliseconds", value = "3000"), //default 5000, 设置在断路器打开后的休眠时间窗,休眠时间窗结束后,断路器为“半开”状态                    @HystrixProperty(name = "circuitBreaker.errorThresholdPercentage", value = "20"), //default 50, 设置断路器打开的错误百分比条件。在滚动时间窗内,请求数超过requestVolumeThreshold,如果错误请求数>50%,断路器打开                    @HystrixProperty(name = "circuitBreaker.forceOpen", value = "false"),        //default false, 强制打开断路器                    @HystrixProperty(name = "circuitBreaker.forceClosed", value = "false")       //default false, 强制关闭断路器,如果forceOpen=true,此属性无效            },            threadPoolProperties = {                    @HystrixProperty(name = "coreSize", value = "10"), //default 10,执行命令线程池核心线程数                    @HystrixProperty(name = "maxQueueSize", value = "15"),     //default -1,线程池等待队列的最大大小,-1使用SynchronousQueue,否则使用LinkedBlockingQueue,不能动态刷新此配置                    @HystrixProperty(name = "queueSizeRejectionThreshold", value = "17"), //default 5, 队列拒绝阈值,LinkedBlockingQueue maxSize不能动态修改,通过此属性补充                    @HystrixProperty(name = "metrics.rollingStats.timeInMilliseconds", value = "10000"),    //default 10s,线程池滚动时间窗,统计线程池指标时间度量                    @HystrixProperty(name = "metrics.rollingStats.numBuckets", value = "10")        //default 10, 线程池滚动时间窗被划分的“桶”的数量            }    )    public String helloCircuitBreaker(){        long start = System.currentTimeMillis();        String result = restTemplate.getForEntity("http://HELLO-SERVICE/hello", String.class).getBody();        System.out.println("spend time: " + (System.currentTimeMillis() - start) + "ms");        return result;    }

Apache Bench测试:
ab -n 10 -c 5 -t 20 http://localhost:9000/ribbon/consumer/hystrix/circuitBreaker
这里写图片描述