【线上踩坑】Spring Cloud Hystrix线程池不足

现象:

昨天突然线上很多接口获取失败,通过 kibana发现大量异常,具体异常信息:

...into fallback. Rejected command because thread-pool queueSize is at rejection threshold.

异常代码出处:

@FeignClient(name = "api", fallbackFactory = LoadBalancingFallbackFactory.class)
public interface LoadBalancingFeignClient {

    @PostMapping(value = "/api/loadBalancing/server")
    Result currentServer();

}

@Slf4j
@Component
public class LoadBalancingFallbackFactory implements FallbackFactory<LoadBalancingFeignClient> {

    @Override
    public LoadBalancingFeignClient create(Throwable throwable) {
        final String msg = throwable.getMessage();
        return () -> {
            log.error("loadBalancingFeignClient currentServer into fallback. {}", msg);
            return Result.error();
        };****
    }

}

原因:

看到这里已经很明显了,是由于hystrix线程池不够用,直接熔断导致的。项目apollo配置:

hystrix.command.default.execution.isolation.thread.timeoutInMilliseconds = 3500
hystrix.threadpool.default.maxQueueSize = 60
hystrix.threadpool.default.queueSizeRejectionThreshold = 40

hystrix参数简析:

maxQueueSize:线程池大小,默认为-1,创建的队列是SynchronousQueue,如果设置大于0则根据其大小创建LinkedBlockingQueue。

queueSizeRejectionThreshold:动态控制线程池队列的上限,即使maxQueueSize没有达到,达到queueSizeRejectionThreshold该值后,请求也会被拒绝,默认值5

相关源码:

hystrix-core-1.5.12-sources.jar!/com/netflix/hystrix/strategy/concurrency/HystrixContextScheduler.java

    private class HystrixContextSchedulerWorker extends Worker {

        private final Worker worker;

        private HystrixContextSchedulerWorker(Worker actualWorker) {
            this.worker = actualWorker;
        }

        @Override
        public void unsubscribe() {
            worker.unsubscribe();
        }

        @Override
        public boolean isUnsubscribed() {
            return worker.isUnsubscribed();
        }

        @Override
        public Subscription schedule(Action0 action, long delayTime, TimeUnit unit) {
            if (threadPool != null) {
                if (!threadPool.isQueueSpaceAvailable()) {
                    throw new RejectedExecutionException("Rejected command because thread-pool queueSize is at rejection threshold.");
                }
            }
            return worker.schedule(new HystrixContexSchedulerAction(concurrencyStrategy, action), delayTime, unit);
        }

        @Override
        public Subscription schedule(Action0 action) {
            if (threadPool != null) {
                if (!threadPool.isQueueSpaceAvailable()) {
                    throw new RejectedExecutionException("Rejected command because thread-pool queueSize is at rejection threshold.");
                }
            }
            return worker.schedule(new HystrixContexSchedulerAction(concurrencyStrategy, action));
        }

    }

解决办法:

  • 适当调大Hystrix线程队列参数
  • 动态水平扩容服务
  • 优化下游服务,减少服务响应时间


【线上踩坑】系列主要是用于简单记录工作中实际遇到的线上问题,如需要更深入的学习和了解,可以直接联系作者。


file


file

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Hystrix是一种在分布式系统中处理延迟和故障的库。它通过使用断路器模式来防止级联故障,并提供了一个备用机制,以便在出现故障时继续执行。 在Spring Cloud中,我们可以通过配置Hystrix线程池来提供更好的控制和管理。以下是配置Hystrix线程池的步骤: 1. 添加Hystrix依赖:在pom.xml文件中添加以下依赖: ``` <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-netflix-hystrix</artifactId> </dependency> ``` 2. 配置线程池:在应用程序的配置文件中,添加以下属性来配置Hystrix线程池: ``` hystrix: threadpool: default: coreSize: 10 maximumSize: 20 maxQueueSize: 50 queueSizeRejectionThreshold: 30 ``` - coreSize线程池的核心线程数,用于处理请求的线程数。 - maximumSize线程池的最大线程数,用于处理请求的最大线程数。 - maxQueueSize线程池的最大队列大小,用于等待处理的请求数。 - queueSizeRejectionThreshold线程池的拒绝阈值,当队列已满时拒绝请求。 3. 添加@EnableCircuitBreaker注解:在应用程序主类上添加@EnableCircuitBreaker注解,启用Hystrix断路器功能。 ``` @SpringBootApplication @EnableCircuitBreaker public class Application { public static void main(String[] args) { SpringApplication.run(Application.class, args); } } ``` 通过以上配置,我们可以更好地控制Hystrix的执行和线程池的大小,以确保系统在出现故障时具有更好的容错性和弹性。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值