欢迎来到代码驿站!

JAVA代码

当前位置:首页 > 软件编程 > JAVA代码

Spring Cloud Hystrix线程池不足的解决方法

时间:2020-10-29 11:18:25|栏目:JAVA代码|点击:

现象:

昨天突然线上很多接口获取失败,通过 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线程队列参数
  • 动态水平扩容服务
  • 优化下游服务,减少服务响应时间

上一篇:简单理解Java的垃圾回收机制与finalize方法的作用

栏    目:JAVA代码

下一篇:SpringCloud消息总线Bus配置中心实现过程解析

本文标题:Spring Cloud Hystrix线程池不足的解决方法

本文地址:http://www.codeinn.net/misctech/17006.html

推荐教程

广告投放 | 联系我们 | 版权申明

重要申明:本站所有的文章、图片、评论等,均由网友发表或上传并维护或收集自网络,属个人行为,与本站立场无关。

如果侵犯了您的权利,请与我们联系,我们将在24小时内进行处理、任何非本站因素导致的法律后果,本站均不负任何责任。

联系QQ:914707363 | 邮箱:codeinn#126.com(#换成@)

Copyright © 2020 代码驿站 版权所有