Spring Cloud Hystrix線程池不足的解決方法
現(xiàn)象:
昨天突然線上很多接口獲取失敗,通過 kibana發(fā)現(xiàn)大量異常,具體異常信息:
...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(); };**** } }
原因:
看到這里已經(jīng)很明顯了,是由于hystrix線程池不夠用,直接熔斷導(dǎo)致的。
項(xiàng)目apollo配置:
hystrix.command.default.execution.isolation.thread.timeoutInMilliseconds = 3500 hystrix.threadpool.default.maxQueueSize = 60 hystrix.threadpool.default.queueSizeRejectionThreshold = 40
hystrix參數(shù)簡析:
maxQueueSize:線程池大小,默認(rèn)為-1,創(chuàng)建的隊(duì)列是SynchronousQueue,如果設(shè)置大于0則根據(jù)其大小創(chuàng)建LinkedBlockingQueue。
queueSizeRejectionThreshold:動態(tài)控制線程池隊(duì)列的上限,即使maxQueueSize沒有達(dá)到,達(dá)到queueSizeRejectionThreshold該值后,請求也會被拒絕,默認(rèn)值5
相關(guān)源碼:
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)); } }
解決辦法:
- 適當(dāng)調(diào)大Hystrix線程隊(duì)列參數(shù)
- 動態(tài)水平擴(kuò)容服務(wù)
- 優(yōu)化下游服務(wù),減少服務(wù)響應(yīng)時間
以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
- Spring Cloud Hystrix異常處理方法詳解
- SpringCloud斷路器Hystrix原理及用法解析
- SpringCloud項(xiàng)目集成Feign、Hystrix過程解析
- SpringCloud之熔斷監(jiān)控Hystrix Dashboard的實(shí)現(xiàn)
- SpringCloud之熔斷器Hystrix的實(shí)現(xiàn)
- SpringCloud Hystrix-Dashboard儀表盤的實(shí)現(xiàn)
- SpringCloud微服務(wù)之Hystrix組件實(shí)現(xiàn)服務(wù)熔斷的方法
- Spring Cloud 的 Hystrix.功能及實(shí)踐詳解
- Springcloud hystrix服務(wù)熔斷和dashboard如何實(shí)現(xiàn)
相關(guān)文章
詳解SpringCloud Finchley Gateway 統(tǒng)一異常處理
這篇文章主要介紹了詳解SpringCloud Finchley Gateway 統(tǒng)一異常處理,非常具有實(shí)用價值,需要的朋友可以參考下2018-10-10基于SpringBoot實(shí)現(xiàn)圖片上傳與顯示
這篇文章主要為大家詳細(xì)介紹了基于SpringBoot實(shí)現(xiàn)圖片上傳與顯示,文中示例代碼介紹的非常詳細(xì),具有一定的參考價值,感興趣的小伙伴們可以參考一下2018-08-08Java中private關(guān)鍵字詳細(xì)用法實(shí)例以及解釋
這篇文章主要給大家介紹了關(guān)于Java中private關(guān)鍵字詳細(xì)用法實(shí)例以及解釋的相關(guān)資料,在Java中private是一種訪問修飾符,它可以用來控制類成員的訪問權(quán)限,文中將用法介紹的非常詳細(xì),需要的朋友可以參考下2024-01-01Java泛型機(jī)制與反射原理相關(guān)知識總結(jié)
今天帶大家學(xué)習(xí)的是關(guān)于Java進(jìn)階的相關(guān)知識,文章圍繞著Java泛型機(jī)制與反射原理展開,文中有非常詳細(xì)的介紹及代碼示例,需要的朋友可以參考下2021-06-06Java調(diào)用第三方http接口的四種方式總結(jié)
這篇文章主要給大家介紹了關(guān)于Java調(diào)用第三方http接口的四種方式,在實(shí)際開發(fā)中我們經(jīng)常會與第三方公司進(jìn)行合作,接入第三方接口,文中給出了詳細(xì)的代碼實(shí)例,需要的朋友可以參考下2023-08-08