偶爾在spring4all,看到DiDi關於hystrix請求合併的一篇文章 Spring Cloud Hystrix的請求合併,查閱資料又整理了一下。spring
具體業務概念,什麼是請求合併?請求合併優缺點?能夠參考DiDi的文章,而後我把我使用過程當中的問題及解決方法寫出來bash
@HystrixCollapser(batchMethod = "testAll", collapserProperties = {
@HystrixProperty(name = "timerDelayInMilliseconds", value = "3000")
})
public Future<Demo> test(String param) {
return null;
}
@HystrixCommand
public List<Demo> testAll(List<String> params) {
logger.info("合併操做線程 --> {} --> params --> {}", Thread.currentThread().getName(), params);
return return restTemplate.getForObject("http://DEMO-SERVICE/demo?params={1}", List.class, StringUtils.join(params, ","));;
}
複製代碼
@RequestMapping("/test")
public SysDict test() throws ExecutionException, InterruptedException {
//開啓上下文TheardLocal
HystrixRequestContext context = HystrixRequestContext.initializeContext();
Future<Demo> demo1 = testService.test(RandomUtil.randomNumbers(5));
Future<Demo> demo2 = testService.test(RandomUtil.randomNumbers(5));
System.out.println(demo1.get());
System.out.println(demo2.get());
context.close();
return null;
}
複製代碼
@HystrixCollapser scope屬性
//全部線程的請求中的屢次服務請求進行合併。
Scope.GLOBAL;
//默認,對一次請求的屢次服務調用合併
Scope.REQUEST;
複製代碼
@HystrixCollapser(batchMethod = "testAll", scope = Scope.GLOBAL, collapserProperties = {
@HystrixProperty(name = "timerDelayInMilliseconds", value = "3000")
})
public Future<Demo> test(String param) {
return null;
}
複製代碼