hystrix具体配置——演示线程池满了的熔断情况

已经找到了熔断的条件(rolling window内请求数阈值,失败数占所有请求书比例阈值),但是实在没有考虑到这个线程的因素的时候进行的。所以现在需要单独的看一下最大线程数对熔断的影响。
也就是这三个参数(当然也可以不设置后面的两个参数)
coreSize
This property sets the core thread-pool size.
maximumSize
Added in 1.5.9. This property sets the maximum thread-pool size. This is the maximum amount of concurrency that can be supported without starting to reject  HystrixCommand s. Please note that this setting only takes effect if you also set  allowMaximumSizeToDivergeFromCoreSize . Prior to 1.5.9, core and maximum sizes were always equal.
allowMaximumSizeToDivergeFromCoreSize
Added in 1.5.9. This property allows the configuration for  maximumSize  to take effect. That value can then be equal to, or higher, than  coreSize . Setting  coreSize < maximumSize  creates a thread pool which can sustain  maximumSize  concurrency, but will return threads to the system during periods of relative inactivity. (subject to  keepAliveTimeInMinutes )

在这里先要看一下hystrix的内部执行流程图

从上面的流程图中能够看出,如果当前没有熔断,因为线程池中的总数不够,那么这时候hystrix会直接降级走getFallBack()。

1、引入maven依赖

<!-- https://mvnrepository.com/artifact/com.netflix.hystrix/hystrix-core -->
        <dependency>
            <groupId>com.netflix.hystrix</groupId>
            <artifactId>hystrix-core</artifactId>
            <version>1.5.12</version>
        </dependency>

2、创建一个继承HystrixCommand的类

package com.xueyou.hystrixdemo.v4;

import com.netflix.hystrix.HystrixCommand;

public class CommandHelloworldWithFallBackThread extends HystrixCommand<String> {
    private String name;
    private int timeIndex;

    public CommandHelloworldWithFallBackThread(Setter setter, String name) {
        super(setter);
        this.name = name;
    }

    @Override
    protected String getFallback() {
        return "fall back " + name;
    }

    @Override
    protected String run() {
        try {
            Thread.currentThread().sleep(400);
        } catch (InterruptedException e) {
        }
        return "ok " + name;
    }

}

3、创建一个测试类,测试命令

package com.xueyou.hystrixdemo.v4;

import com.netflix.hystrix.*;

import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

public class HelloWorldFallBackThread {
    public static void main(String[] args) {
        Runnable runnable = new Runnable() {
            @Override
            public void run() {
                String s = new CommandHelloworldWithFallBackThread(HystrixCommand.Setter.withGroupKey(HystrixCommandGroupKey.Factory.asKey("ExampleGroup1"))
                        .andThreadPoolKey(HystrixThreadPoolKey.Factory.asKey("threadpoolwithfallback"))
                        .andThreadPoolPropertiesDefaults(HystrixThreadPoolProperties.Setter()
                                .withCoreSize(3)
                                .withMaximumSize(6)
                                .withAllowMaximumSizeToDivergeFromCoreSize(true))
                        .andCommandPropertiesDefaults(HystrixCommandProperties.Setter()
                                .withExecutionTimeoutInMilliseconds(1000)
                                .withCircuitBreakerSleepWindowInMilliseconds(5000)
                                .withCircuitBreakerErrorThresholdPercentage(50)
                                .withCircuitBreakerRequestVolumeThreshold(100))
                        , Thread.currentThread().getName()).execute();
                System.out.println(s);
                try {
                    Thread.currentThread().sleep(50);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                } finally {
                }
            }
        };
        ExecutorService executorService = Executors.newFixedThreadPool(10);
        int i = 100;
        while (i > 0) {
            executorService.submit(runnable);
/*            try {
                Thread.sleep(100);
            } catch (InterruptedException e) {
            }
            */
            i--;
        }
        executorService.shutdown();
    }
}

4、运行结果:

fall back pool-1-thread-8
fall back pool-1-thread-7
fall back pool-1-thread-5
fall back pool-1-thread-3
fall back pool-1-thread-8
fall back pool-1-thread-7
fall back pool-1-thread-5
fall back pool-1-thread-3
fall back pool-1-thread-7
fall back pool-1-thread-8
fall back pool-1-thread-5
fall back pool-1-thread-3
fall back pool-1-thread-7
fall back pool-1-thread-8
fall back pool-1-thread-5
fall back pool-1-thread-3
fall back pool-1-thread-7
fall back pool-1-thread-8
fall back pool-1-thread-5
fall back pool-1-thread-3
fall back pool-1-thread-7
fall back pool-1-thread-8
fall back pool-1-thread-5
fall back pool-1-thread-3
fall back pool-1-thread-7
fall back pool-1-thread-8
fall back pool-1-thread-5
fall back pool-1-thread-3
fall back pool-1-thread-7
fall back pool-1-thread-8
fall back pool-1-thread-5
fall back pool-1-thread-3
ok pool-1-thread-10
ok pool-1-thread-2
ok pool-1-thread-9
ok pool-1-thread-1
ok pool-1-thread-4
ok pool-1-thread-6
fall back pool-1-thread-4
fall back pool-1-thread-2
fall back pool-1-thread-6
fall back pool-1-thread-1
fall back pool-1-thread-4
fall back pool-1-thread-2
fall back pool-1-thread-6
fall back pool-1-thread-1
fall back pool-1-thread-4
fall back pool-1-thread-2
fall back pool-1-thread-6
fall back pool-1-thread-1
fall back pool-1-thread-4
fall back pool-1-thread-2
fall back pool-1-thread-1
fall back pool-1-thread-6
fall back pool-1-thread-2
fall back pool-1-thread-4
fall back pool-1-thread-6
fall back pool-1-thread-1
fall back pool-1-thread-4
fall back pool-1-thread-2
fall back pool-1-thread-1
fall back pool-1-thread-6
fall back pool-1-thread-6
fall back pool-1-thread-4
fall back pool-1-thread-2
fall back pool-1-thread-1
ok pool-1-thread-7
fall back pool-1-thread-2
fall back pool-1-thread-6
fall back pool-1-thread-1
ok pool-1-thread-8
ok pool-1-thread-5
ok pool-1-thread-3
ok pool-1-thread-9
ok pool-1-thread-10
fall back pool-1-thread-5
fall back pool-1-thread-3
fall back pool-1-thread-10
fall back pool-1-thread-9
fall back pool-1-thread-5
fall back pool-1-thread-3
fall back pool-1-thread-9
fall back pool-1-thread-10
fall back pool-1-thread-5
fall back pool-1-thread-3
fall back pool-1-thread-10
fall back pool-1-thread-9
fall back pool-1-thread-5
fall back pool-1-thread-3
fall back pool-1-thread-10
fall back pool-1-thread-9
fall back pool-1-thread-3
fall back pool-1-thread-5
fall back pool-1-thread-10
ok pool-1-thread-4
ok pool-1-thread-7
ok pool-1-thread-2
ok pool-1-thread-6
ok pool-1-thread-1

ok pool-1-thread-8

分析:
因为没有run()方法中出现的错误,所以所有的错误都是因为线程池中的线程都被占用了导致的。
能够看到最大的线程数是6个。也就是说能够约束当前的这个请求的线程池资源。

猜你喜欢

转载自blog.csdn.net/wild46cat/article/details/80783195