這篇文章主要介紹Spring Cloud中Hystrix線程池隊(duì)列配置的示例分析,文中介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們一定要看完!
成都創(chuàng)新互聯(lián)專業(yè)為企業(yè)提供平安網(wǎng)站建設(shè)、平安做網(wǎng)站、平安網(wǎng)站設(shè)計(jì)、平安網(wǎng)站制作等企業(yè)網(wǎng)站建設(shè)、網(wǎng)頁設(shè)計(jì)與制作、平安企業(yè)網(wǎng)站模板建站服務(wù),十載平安做網(wǎng)站經(jīng)驗(yàn),不只是建網(wǎng)站,更提供有價(jià)值的思路和整體網(wǎng)絡(luò)服務(wù)。
先看一下正確的Hystrix配置姿勢(shì)。
application.yml:
hystrix: threadpool: default: coreSize: 200 #并發(fā)執(zhí)行的最大線程數(shù),默認(rèn)10 maxQueueSize: 1000 #BlockingQueue的最大隊(duì)列數(shù),默認(rèn)值-1 queueSizeRejectionThreshold: 800 #即使maxQueueSize沒有達(dá)到,達(dá)到queueSizeRejectionThreshold該值后,請(qǐng)求也會(huì)被拒絕,默認(rèn)值5
接下來編寫一個(gè)測(cè)試類,來驗(yàn)證幾種錯(cuò)誤配置,看看會(huì)出現(xiàn)什么情況。
測(cè)試類代碼(A調(diào)用方):
/** * @Author: XiongFeng * @Description: * @Date: Created in 11:12 2018/6/11 */ public class RepaymentHelperTest extends FundApplicationTests { @Autowired RepaymentHelper repaymentHelper; @Autowired private RouterFeign routerFeign; @Test public void hystrixTest() throws InterruptedException { for (int i = 0; i < 135; i++) { new Thread(new Runnable() { @Override public void run() { job(); } }).start(); } Thread.currentThread().join(); } public void job() { String repaymentNo = "xf1002"; String transNo = "T4324324234"; String reqNo = "xf1002"; String begintime = "20180831130030"; String endtime = "20180831130050"; TransRecQueryReqDto transRecQueryReqDto = new TransRecQueryReqDto(); transRecQueryReqDto.setTransNo(transNo); transRecQueryReqDto.setBeginTime(begintime); transRecQueryReqDto.setEndTime(endtime); transRecQueryReqDto.setReqNo(reqNo); Resp<List<TransRecDto>> queryTransRecListResp = routerFeign.queryTransRec(new Req<>(repaymentNo, "2018080200000002", null, null, transRecQueryReqDto)); System.out.println(String.format("獲取結(jié)果為:【%s】", JsonUtil.toJson(queryTransRecListResp))); } }
這個(gè)測(cè)試類的作用就是創(chuàng)建135個(gè)線程,通過RouterFeign類并發(fā)請(qǐng)求B服務(wù)方,看看請(qǐng)求結(jié)果是否出現(xiàn)異常。
Feign調(diào)用代碼:
@FeignClient(value = "${core.name}", fallbackFactory = RouterFeignBackFactory.class, path = "/router") public interface RouterFeign { /** * 代扣結(jié)果查詢 * @param transRecQueryReqDtoReq * @return */ @PostMapping("/queryTransRec") Resp<List<TransRecDto>> queryTransRec(@RequestBody Req<TransRecQueryReqDto> transRecQueryReqDtoReq); }
這個(gè)類,就是通過Feign方式去調(diào)用B服務(wù)方的客戶端
服務(wù)提供方代碼(B服務(wù)方):
/** * @Author: XiongFeng * @Description: * @Date: Created in 16:04 2018/5/24 */ @Api("還款服務(wù)") @RefreshScope @RestController @RequestMapping("/router") public class TestController { private static Logger logger = LoggerFactory.getLogger(TestController.class); // 計(jì)數(shù)器 private static AtomicInteger count = new AtomicInteger(1); @ApiOperation(value = "代扣結(jié)果查詢") @PostMapping("/queryTransRec") Resp<List<TransRecDto>> queryTransRec(@RequestBody Req<TransRecQueryReqDto> transRecQueryReqDtoReq) throws InterruptedException { System.out.println(String.format("查詢支付結(jié)果......計(jì)數(shù): %s", count.getAndAdd(1))); Thread.sleep(500); return Resp.success(RespStatus.SUCCESS.getDesc(), null); }
這個(gè)類的作用,就是一個(gè)服務(wù)提供方,計(jì)數(shù)并返回結(jié)果。
下面我們看一下幾種錯(cuò)誤的配置。
案例一(將核心線程數(shù)調(diào)低,最大隊(duì)列數(shù)調(diào)大一點(diǎn),但是隊(duì)列拒絕閾值設(shè)置小一點(diǎn)):
hystrix: threadpool: default: coreSize: 10 maxQueueSize: 1000 queueSizeRejectionThreshold: 20
此時(shí)的結(jié)果:
左窗口是B服務(wù)方,右窗口是A調(diào)用方。從結(jié)果可以看出,調(diào)用135次,成功32次左右,其余線程全部拋異常。
案例二(將核心線程數(shù)調(diào)低,最大隊(duì)列數(shù)調(diào)小一點(diǎn),但是隊(duì)列拒絕閾值設(shè)置大一點(diǎn)):
hystrix: threadpool: default: coreSize: 10 maxQueueSize: 15 queueSizeRejectionThreshold: 2000
此時(shí)的結(jié)果:
java.util.concurrent.RejectedExecutionException: Task java.util.concurrent.FutureTask@7d6d472b rejected from java.util.concurrent.ThreadPoolExecutor@17f8bcb7[Running, pool size = 3, active threads = 3, queued tasks = 15, completed tasks = 0]
左窗口是B服務(wù)方,右窗口是A調(diào)用方。從結(jié)果可以看出,調(diào)用135次,成功25次左右,其余線程全部拋異常。。
案例三(將核心線程數(shù)調(diào)低,最大隊(duì)列數(shù)調(diào)大一點(diǎn),但是隊(duì)列拒絕閾值不設(shè)置值):
hystrix: threadpool: default: coreSize: 10 maxQueueSize: 1500
此時(shí)的結(jié)果:
java.util.concurrent.RejectedExecutionException: Rejected command because thread-pool queueSize is at rejection threshold.
左窗口是B服務(wù)方,右窗口是A調(diào)用方。此時(shí)的結(jié)果和案例一的情況一樣,調(diào)用135次,成功47次左右,其余線程全部拋異常。報(bào)錯(cuò)跟案例一一樣
案例四(將核心線程數(shù)調(diào)低,最大隊(duì)列數(shù)不設(shè)值,但是隊(duì)列拒絕閾值設(shè)置的比較大):
hystrix: threadpool: default: coreSize: 10 queueSizeRejectionThreshold: 1000
此時(shí)的結(jié)果:
java.util.concurrent.RejectedExecutionException: Task java.util.concurrent.FutureTask@23d268ea rejected from java.util.concurrent.ThreadPoolExecutor@66d0e2f4[Running, pool size = 0, active threads = 0, queued tasks = 0, completed tasks = 0]
at java.util.concurrent.ThreadPoolExecutor$AbortPolicy.rejectedExecution(ThreadPoolExecutor.java:2063)
at java.util.concurrent.ThreadPoolExecutor.reject(ThreadPoolExecutor.java:830)
at java.util.concurrent.ThreadPoolExecutor.execute(ThreadPoolExecutor.java:1379)
at java.util.concurrent.AbstractExecutorService.submit(AbstractExecutorService.java:112)
左窗口是B服務(wù)方,右窗口是A調(diào)用方。此時(shí)的結(jié)果和案例二的情況一樣,調(diào)用135次,成功10次左右,其余線程全部拋異常。報(bào)錯(cuò)跟案例二一樣
下面來看一看正確的配置案例
案例一:將核心線程數(shù)調(diào)低,最大隊(duì)列數(shù)和隊(duì)列拒絕閾值的值都設(shè)置大一點(diǎn)):
hystrix: threadpool: default: coreSize: 10 maxQueueSize: 1500 queueSizeRejectionThreshold: 1000
此時(shí)的結(jié)果:
左窗口是B服務(wù)方,右窗口是A調(diào)用方。此時(shí)的結(jié)果就完全正常了,并發(fā)請(qǐng)求了135次,全部成功!
以上是“Spring Cloud中Hystrix線程池隊(duì)列配置的示例分析”這篇文章的所有內(nèi)容,感謝各位的閱讀!希望分享的內(nèi)容對(duì)大家有幫助,更多相關(guān)知識(shí),歡迎關(guān)注創(chuàng)新互聯(lián)行業(yè)資訊頻道!
網(wǎng)頁標(biāo)題:SpringCloud中Hystrix線程池隊(duì)列配置的示例分析
轉(zhuǎn)載注明:http://www.rwnh.cn/article4/jehooe.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供電子商務(wù)、網(wǎng)站排名、營銷型網(wǎng)站建設(shè)、網(wǎng)站內(nèi)鏈、網(wǎng)站制作、品牌網(wǎng)站制作
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請(qǐng)盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場,如需處理請(qǐng)聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來源: 創(chuàng)新互聯(lián)