Home > Java > javaTutorial > Java @SentinelResource example code analysis

Java @SentinelResource example code analysis

WBOY
Release: 2023-05-05 13:31:16
forward
1341 people have browsed it

Limit flow by resource name and add subsequent processing

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>
Copy after login

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 服务不可用");
    }
}
Copy after login

Java @SentinelResource example code analysis

##Graphic configuration and code relationship

Java @SentinelResource example code analysis

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 1

Click 1 time in 1 second, OK

Exceeded the above, clicked like crazy, returned self-defined current limiting processing information, current limiting occurred

Java @SentinelResource example code analysis

According to the Url address current limiting and subsequent processing

Limiting the current through the accessed URL will return the default current limiting processing information provided by Sentinel

Controller is modified to:

@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"));
    }
}
Copy after login

Test 2

Visit once

http://localhost:8401/rateLimit/byUrl

Normal

Java @SentinelResource example code analysis

Crazy click http://localhost:8401/rateLimit/ byUrl

Java @SentinelResource example code analysis

will return Sentinel’s own current limiting processing results

Problems faced by the above solution

1 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 logic

Create the CustomerBlockHandler class for customizing current-limiting processing logic

After 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,"按客户自定义限流处理逻辑");
    }
Copy after login
Customize general current limiting processing logic

blockHandlerClass = CustomerBlockHandler.class

blockHandler = handleException2

The above configuration: Find the handleException2 method in the CustomerBlockHandler class for back-up processing and define general current limiting processing logic

Test 3

Java @SentinelResource example code analysis

Java @SentinelResource example code analysis

After testing, we customized it

Java @SentinelResource example code analysis

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!

Related labels:
source:yisu.com
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template