有一次在生產環境,忽然出現了不少筆還款單被掛起,後來排查緣由,發現是內部系統調用時出現了Hystrix調用異常。在開發過程當中,由於核心線程數設置的比較大,沒有出現這種異常。放到了測試環境,偶爾有出現這種狀況,後來在網上查找解決方案,網上的方案是調整maxQueueSize屬性就行了,當時調整了一下,確實有所改善。可沒想到在生產環境跑了一段時間後卻又出現這種了狀況,此時我第一想法就是去查看maxQueueSize屬性,但是maxQueueSize屬性是設置值了。當時就比較納悶了,爲何maxQueueSize屬性不起做用,後來經過查看官方文檔發現Hystrix還有一個queueSizeRejectionThreshold屬性,這個屬性是控制隊列最大閾值的,而Hystrix默認只配置了5個,所以就算咱們把maxQueueSize的值設置再大,也是不起做用的。兩個屬性必須同時配置java
application.yml:git
hystrix:
threadpool:
default:
coreSize: 200 #併發執行的最大線程數,默認10
maxQueueSize: 1000 #BlockingQueue的最大隊列數,默認值-1
queueSizeRejectionThreshold: 800 #即便maxQueueSize沒有達到,達到queueSizeRejectionThreshold該值後,請求也會被拒絕,默認值5
複製代碼
/**
* @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("獲取結果爲:【%s】", JsonUtil.toJson(queryTransRecListResp)));
}
}
複製代碼
@FeignClient(value = "${core.name}", fallbackFactory = RouterFeignBackFactory.class, path = "/router")
public interface RouterFeign {
/**
* 代扣結果查詢
* @param transRecQueryReqDtoReq
* @return
*/
@PostMapping("/queryTransRec")
Resp<List<TransRecDto>> queryTransRec(@RequestBody Req<TransRecQueryReqDto> transRecQueryReqDtoReq);
}
複製代碼
/**
* @Author: XiongFeng
* @Description:
* @Date: Created in 16:04 2018/5/24
*/
@Api("還款服務")
@RefreshScope
@RestController
@RequestMapping("/router")
public class TestController {
private static Logger logger = LoggerFactory.getLogger(TestController.class);
// 計數器
private static AtomicInteger count = new AtomicInteger(1);
@ApiOperation(value = "代扣結果查詢")
@PostMapping("/queryTransRec")
Resp<List<TransRecDto>> queryTransRec(@RequestBody Req<TransRecQueryReqDto> transRecQueryReqDtoReq) throws InterruptedException {
System.out.println(String.format("查詢支付結果......計數: %s", count.getAndAdd(1)));
Thread.sleep(500);
return Resp.success(RespStatus.SUCCESS.getDesc(), null);
}
複製代碼
hystrix:
threadpool:
default:
coreSize: 10
maxQueueSize: 1000
queueSizeRejectionThreshold: 20
複製代碼
hystrix:
threadpool:
default:
coreSize: 10
maxQueueSize: 15
queueSizeRejectionThreshold: 2000
複製代碼
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]
複製代碼
hystrix:
threadpool:
default:
coreSize: 10
maxQueueSize: 1500
複製代碼
java.util.concurrent.RejectedExecutionException: Rejected command because thread-pool queueSize is at rejection threshold.
複製代碼
hystrix:
threadpool:
default:
coreSize: 10
queueSizeRejectionThreshold: 1000
複製代碼
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)
複製代碼
hystrix:
threadpool:
default:
coreSize: 10
maxQueueSize: 1500
queueSizeRejectionThreshold: 1000
複製代碼
Spring Hystrix 官方文檔github
原文地址:www.seifon.cn/2018/12/08/…bash