How to realize the merging of Hystrix requests under High concurrency
How to achieve high concurrency in the Hystrix request merge, many novices are not very clear about this, in order to help you solve this problem, the following editor will explain in detail for you, people with this need can come to learn, I hope you can gain something.
Implement merging of Hystrix requests in SpringCloud under high concurrency
1. Introduce maven package into pom.xml
Org.springframework.cloud
Spring-cloud-starter-netflix-hystrix
2.1.6.RELEASE
The spring Boot package is introduced, and the version must be consistent, otherwise an error message will be started.
Org.springframework.boot
Spring-boot-starter-parent
2.1.12.RELEASE
2. Add startup comments
@ EnableCircuitBreaker the annotation starts hystrix, otherwise it will not take effect.
@ SpringBootApplication
@ EnableScheduling
@ EnableSwagger2
@ EnableCaching
@ EnableAsync
@ ServletComponentScan
@ EnableMqHandlerScan (packages = {"com.sxgw.pcops.im.client.mq.handler"})
/ / use of hystrix must be increased
@ EnableCircuitBreaker
Public class ClientApplication {
Public static void main (String [] args) {
SpringApplication.run (ClientApplication.class, args)
}
}
3. Request API Controller
@ Api (value = "test")
@ RestController
@ RequestMapping ("/ test")
@ Slf4j
Public class TestController {
@ Autowired
Private UserBatchServiceImpl userBatchServiceImpl
ApiOperation ("Test request merge")
@ 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. Write request merge logic
TimerDelayInMilliseconds this parameter sets the interval time between thread pools. For example, interval 5000ms means that a thread pool waits for 5 seconds before execution.
/ * *
*
* @ 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 ("enter batch processing method" + ids)
List ps = new ArrayList ()
For (Long id: ids) {
Ps.add (id+ "")
}
Return ps
}
}
5. During the test, you can see the effect through the jmeter test tool.
Is it helpful for you to read the above content? If you want to know more about the relevant knowledge or read more related articles, please follow the industry information channel, thank you for your support.