Home >Java >javaTutorial >Java @SentinelResource example code analysis
Module:cloudalibaba-sentinel-service8401
Pom new dependency
<dependency><!-- 引入自己定义的api通用包,可以使用Payment支付Entity --> <groupId>com.atguigu.springcloud</groupId> <artifactId>cloud-api-common</artifactId> <version>${project.version}</version> </dependency>
This dependency comes from your own template, here This dependency is part of the business processing of database query
New Controller
@RestController public class RateLimitController { @GetMapping("/byResource") @SentinelResource(value = "byResource",blockHandler = "handleException") public CommonResult byResource() { return new CommonResult(200,"按资源名称限流测试OK",new Payment(2020L,"serial001")); } public CommonResult handleException(BlockException exception) { return new CommonResult(444,exception.getClass().getCanonicalName()+"\t 服务不可用"); } }##Graphic configuration and code relationship means that the number of queries within 1 second is greater than 1, then it will go to our custom flow and limit the flow Test 1Click 1 time in 1 second, OKExceeded the above, clicked like crazy, returned self-defined current limiting processing information, current limiting occurred According to the Url address current limiting and subsequent processing
@RestController public class RateLimitController { @GetMapping("/byResource") @SentinelResource(value = "byResource",blockHandler = "handleException") public CommonResult byResource() { return new CommonResult(200,"按资源名称限流测试OK",new Payment(2020L,"serial001")); } public CommonResult handleException(BlockException exception) { return new CommonResult(444,exception.getClass().getCanonicalName()+"\t 服务不可用"); } @GetMapping("/rateLimit/byUrl") @SentinelResource(value = "byUrl") public CommonResult byUrl() { return new CommonResult(200,"按url限流测试OK",new Payment(2020L,"serial002")); } }Test 2Visit oncehttp://localhost:8401/rateLimit/byUrlNormal Crazy click http://localhost:8401/rateLimit/ byUrl will return Sentinel’s own current limiting processing resultsProblems faced by the above solution1 The system defaults, no Reflect our own business requirements. 2 According to the existing conditions, our customized processing method is coupled with the business code, which is not intuitive. 3 If a caveat is added to each business method, the code bloat will increase. 4 The global unified processing method is not reflected. Customer-defined current-limiting processing logicCreate the CustomerBlockHandler class for customizing current-limiting processing logicAfter testing, we customized it Add new business to the control class
@GetMapping("/rateLimit/customerBlockHandler") @SentinelResource(value = "customerBlockHandler", blockHandlerClass = CustomerBlockHandler.class, blockHandler = "handleException2") public CommonResult customerBlockHandler() { return new CommonResult(200,"按客户自定义限流处理逻辑"); }Customize general current limiting processing logic
blockHandlerClass = CustomerBlockHandler.classblockHandler = handleException2The above configuration: Find the handleException2 method in the CustomerBlockHandler class for back-up processing and define general current limiting processing logicTest 3 After testing, we customized it
The above is the detailed content of Java @SentinelResource example code analysis. For more information, please follow other related articles on the PHP Chinese website!