實(shí)現(xiàn)高并發(fā)下的SpringCloud中Hystrix請求合并
????????????????????????????????點(diǎn)擊上方藍(lán)色字體,選擇“標(biāo)星公眾號”
優(yōu)質(zhì)文章,第一時間送達(dá)
66套java從入門到精通實(shí)戰(zhàn)課程分享
實(shí)現(xiàn)高并發(fā)下的SpringCloud中Hystrix請求合并
1、在pom.xml中引入maven包
???
????
??????org.springframework.cloud
??????spring-cloud-starter-netflix-hystrix
??????2.1.6.RELEASE
????
spring Boot 包引入,版本必須一致,否則啟動報錯。
??
????org.springframework.boot
????spring-boot-starter-parent
????2.1.12.RELEASE
????
??
2、添加啟動注解
@EnableCircuitBreaker該注解啟動hystrix,否則不生效。
@SpringBootApplication
@EnableScheduling
@EnableSwagger2
@EnableCaching
@EnableAsync
@ServletComponentScan
@EnableMqHandlerScan(packages?=?{"com.sxgw.pcops.im.client.mq.handler"})
//使用hystrix必須增加
@EnableCircuitBreaker
public?class?ClientApplication?{
??public?static?void?main(String[]?args)?{
????SpringApplication.run(ClientApplication.class,?args);
??}
}
3、請求接口Controller
@Api(value?=?"測試")
@RestController
@RequestMapping("/test")
@Slf4j
public?class?TestController?{
??@Autowired
??private?UserBatchServiceImpl?userBatchServiceImpl;
??@ApiOperation("測試請求合并")
??@PostMapping(value?=?"/userbyMerge/{id}")
??public?String?userbyMerge(@PathVariable?Long?id)??{
????String?ids?=?"";
???try?{
?????Future?userFu?=?this.userBatchServiceImpl.getUserById(id);
??????ids?=?userFu.get();
???}catch?(Exception?e){
?????e.printStackTrace();
???}
????return?ids;
??}
}
4、編寫請求合并邏輯
timerDelayInMilliseconds 該參數(shù)設(shè)置的是線程池中間間隔時間,如間隔5000ms則是一個線程池等待5s后執(zhí)行
/**
?*
?*?@author??
?*
?*/
@Component
public?class?UserBatchServiceImpl?{
????@HystrixCollapser(batchMethod?=?"getUserBatchById",scope=?com.netflix.hystrix.HystrixCollapser.Scope.GLOBAL,
????????????collapserProperties?=?{@HystrixProperty(name?="timerDelayInMilliseconds",value?=?"5000")})
????public?Future?getUserById(Long?id)?{
????????throw?new?RuntimeException("This?method?body?should?not?be?executed");
????}
????@HystrixCommand
????public?List?getUserBatchById(List?ids)?{
????????System.out.println("進(jìn)入批量處理方法"+ids);
????????List?ps?=?new?ArrayList();
????????for?(Long?id?:?ids)?{
????????????ps.add(id+"");
????????}
????????return?ps;
????}
}
5、測試時通過jmeter測試工具測試可以查看到效果。
版權(quán)聲明:本文為博主原創(chuàng)文章,遵循 CC 4.0 BY-SA 版權(quán)協(xié)議,轉(zhuǎn)載請附上原文出處鏈接和本聲明。
本文鏈接:
https://blog.csdn.net/qq_25891151/article/details/111386228
粉絲福利:Java從入門到入土學(xué)習(xí)路線圖
???

?長按上方微信二維碼?2 秒
感謝點(diǎn)贊支持下哈?
評論
圖片
表情
