Spring boot 弹性4J隔板螺纹性能

Spring boot 弹性4J隔板螺纹性能,spring-boot,resilience4j,Spring Boot,Resilience4j,我正在使用Resilience4j进行大量测试,并监视线程的行为 我使用的是spring boot 2.2.5、resilience4j 1.4.0。Gatling(加载工具)、visualvm(分析线程) 我意识到: @CircuitBreaker(name = MyServiceCommand.BEAN_NAME, fallbackMethod = "fallback") @TimeLimiter(name = MyServiceCommand.BEAN_NAME) @Bulkhead(na

我正在使用Resilience4j进行大量测试,并监视线程的行为

我使用的是spring boot 2.2.5、resilience4j 1.4.0。Gatling(加载工具)、visualvm(分析线程)

我意识到:

@CircuitBreaker(name = MyServiceCommand.BEAN_NAME, fallbackMethod = "fallback")
@TimeLimiter(name = MyServiceCommand.BEAN_NAME)
@Bulkhead(name = MyServiceCommand.BEAN_NAME, type = Bulkhead.Type.THREADPOOL)
@Component
@AllArgsConstructor
@Slf4j
public class MyServiceCommand extends ResilienceAbstractCommand {

    protected static final String BEAN_NAME = "MyService";

    private SlowService slowService;

    public CompletableFuture<String> perform() {

        return CompletableFuture.completedFuture(slowService.get());
    }

    private CompletableFuture<String> fallback() {
        return CompletableFuture.completedFuture("fallback");
    }
}
  • 当我使用舱壁类型=信号灯时

    我意识到异步执行将使用默认的ExecutorService,在这种情况下,ForkJoinPool和此配置将起作用:

    maxConcurrentCalls, maxWaitDuration 
    
    如果您使用的是隔板类型=螺纹池,则上述配置将被忽略

  • 当我使用Bulked THREADPOOL时,这些配置将起作用:

    maxThreadPoolSize, coreThreadPoolSize, queueCapacity, keepAliveDuration
    
    这些配置将被忽略:

    maxConcurrentCalls, maxWaitDuration 
    
  • 我的代码:

    @CircuitBreaker(name = MyServiceCommand.BEAN_NAME, fallbackMethod = "fallback")
    @TimeLimiter(name = MyServiceCommand.BEAN_NAME)
    @Bulkhead(name = MyServiceCommand.BEAN_NAME, type = Bulkhead.Type.THREADPOOL)
    @Component
    @AllArgsConstructor
    @Slf4j
    public class MyServiceCommand extends ResilienceAbstractCommand {
    
        protected static final String BEAN_NAME = "MyService";
    
        private SlowService slowService;
    
        public CompletableFuture<String> perform() {
    
            return CompletableFuture.completedFuture(slowService.get());
        }
    
        private CompletableFuture<String> fallback() {
            return CompletableFuture.completedFuture("fallback");
        }
    }
    
    SlowService对另一个应用程序进行缓慢的网络调用

    我配置这些值只是为了在生产中进行测试 价值观会有所不同

    问题是:

    @CircuitBreaker(name = MyServiceCommand.BEAN_NAME, fallbackMethod = "fallback")
    @TimeLimiter(name = MyServiceCommand.BEAN_NAME)
    @Bulkhead(name = MyServiceCommand.BEAN_NAME, type = Bulkhead.Type.THREADPOOL)
    @Component
    @AllArgsConstructor
    @Slf4j
    public class MyServiceCommand extends ResilienceAbstractCommand {
    
        protected static final String BEAN_NAME = "MyService";
    
        private SlowService slowService;
    
        public CompletableFuture<String> perform() {
    
            return CompletableFuture.completedFuture(slowService.get());
        }
    
        private CompletableFuture<String> fallback() {
            return CompletableFuture.completedFuture("fallback");
        }
    }
    
    1-我的应用程序是I/O绑定的,我希望当有许多请求(40/s)时,由resilience4j创建的线程池达到maxThreadPoolSize数,但仅创建了3个线程(cpu核心数量-1) 我只看到了:

    隔板-MyService-1、隔板-MyService-2、隔板-MyService-3

    有什么问题吗?

    2-我进行了以下配置:

    resilience4j.circuitbreaker:
        configs:
            default:
                failureRateThreshold: 40
                waitDurationInOpenState: 20s
                minimumNumberOfCalls: 5
                slowCallRateThreshold: 30
                slowCallDurationThreshold: 300s
        instances:
            MyService:
                baseConfig: default
                ignoreExceptions:
                    - com.hotmart.display.commons.exception.BusinessException
    
    resilience4j.bulkhead:
        configs:
            default:
                maxConcurrentCalls: 800
                maxWaitDuration: 15000
        instances:
            MyService:
                baseConfig: default
    
    resilience4j.thread-pool-bulkhead:
        configs:
            default:
                maxThreadPoolSize: 10
                queueCapacity: 500
        instances:
            MyService:
                baseConfig: default
    
    resilience4j.timelimiter:
        configs:
            default:
                timeoutDuration: 300s
                cancelRunningFuture: true
        instances:
            MyService:
                baseConfig: default
    
        maxThreadPoolSize: 1
        coreThreadPoolSize: 1
        queueCapacity: 2
        keepAliveDuration: 3
    
    我将slowService配置为10秒,太慢了

    当时我做了3个请求,系统表现如下:

    • 一次只处理1,这是正常的,因为核心和maxThread是1
    • 请求已被查询,因为队列容量为2,所以可以
    但是,我希望队列中等待的请求只等待3ms,因为这是配置的时间

    如何限制线程在队列中等待的最长时间?

    [编辑][更新] 我做了更多的测试,并意识到:

    当一个线程忙时,如果所有coreThread都忙,则到达的另一个请求将启动一个新任务,直到到达coreThread

    如果池到达coreThread,每个新请求都将被放入队列中,队列已满后,新请求将创建新线程,直到重新启动maxPoolThread

    关于keepAliveDuration,线程将在配置的空闲时间后终止。coreThreadSize将处于空闲状态,仅终止多余的线程

    我所有的疑问都通过我的测试得到了解答

    现在我只想知道线程池和信号量之间的行为差异