• @SentinelResource(4)


    按资源名称限流+后续处理

    Module:cloudalibaba-sentinel-service8401

    pom新增依赖

    1. <dependency>
    2. <groupId>com.atguigu.springcloudgroupId>
    3. <artifactId>cloud-api-commonartifactId>
    4. <version>${project.version}version>
    5. dependency>

    这个依赖来自自己的模板,这里的这个依赖就是去数据库查询的一部分业务处理

    新增Controller 

    1. @RestController
    2. public class RateLimitController
    3. {
    4. @GetMapping("/byResource")
    5. @SentinelResource(value = "byResource",blockHandler = "handleException")
    6. public CommonResult byResource()
    7. {
    8. return new CommonResult(200,"按资源名称限流测试OK",new Payment(2020L,"serial001"));
    9. }
    10. public CommonResult handleException(BlockException exception)
    11. {
    12. return new CommonResult(444,exception.getClass().getCanonicalName()+"\t 服务不可用");
    13. }
    14. }

     

    图形配置和代码关系 

    表示1秒钟内查询次数大于1,就跑到我们自定义的处流,限流 

     测试1

    1秒钟点击1下,OK

    超过上述,疯狂点击,返回了自己定义的限流处理信息,限流发生

    额外问题

    此时关闭问服务8401看看

    Sentinel控制台,流控规则消失了?????

    临时/持久? 

    按照Url地址限流+后续处理 

    通过访问的URL来限流,会返回Sentinel自带默认的限流处理信息

    Controller修改为:

    1. @RestController
    2. public class RateLimitController
    3. {
    4. @GetMapping("/byResource")
    5. @SentinelResource(value = "byResource",blockHandler = "handleException")
    6. public CommonResult byResource()
    7. {
    8. return new CommonResult(200,"按资源名称限流测试OK",new Payment(2020L,"serial001"));
    9. }
    10. public CommonResult handleException(BlockException exception)
    11. {
    12. return new CommonResult(444,exception.getClass().getCanonicalName()+"\t 服务不可用");
    13. }
    14. @GetMapping("/rateLimit/byUrl")
    15. @SentinelResource(value = "byUrl")
    16. public CommonResult byUrl()
    17. {
    18. return new CommonResult(200,"按url限流测试OK",new Payment(2020L,"serial002"));
    19. }
    20. }

    测试2

      访问一次

    http://localhost:8401/rateLimit/byUrl

    正常

    疯狂点击http://localhost:8401/rateLimit/byUrl 

    会返回Sentinel自带的限流处理结果 

    上面兜底方案面临的问题 

    1    系统默认的,没有体现我们自己的业务要求。
     
    2  依照现有条件,我们自定义的处理方法又和业务代码耦合在一块,不直观。
     
    3  每个业务方法都添加一个兜底的,那代码膨胀加剧。
     
    4  全局统一的处理方法没有体现。

    客户自定义限流处理逻辑 

    创建CustomerBlockHandler类用于自定义限流处理逻辑

    测试后我们自定义的出来了

    控制类增加新的业务 

    1. @GetMapping("/rateLimit/customerBlockHandler")
    2. @SentinelResource(value = "customerBlockHandler",
    3. blockHandlerClass = CustomerBlockHandler.class, blockHandler = "handleException2")
    4. public CommonResult customerBlockHandler()
    5. {
    6. return new CommonResult(200,"按客户自定义限流处理逻辑");
    7. }



    自定义通用的限流处理逻辑,
         blockHandlerClass = CustomerBlockHandler.class
         blockHandler = handleException2
         上述配置:找CustomerBlockHandler类里的handleException2方法进行兜底处理
    定义通用的限流处理逻辑


     

     测试3

     

     测试后我们自定义的出来了

     

  • 相关阅读:
    SpringBoot Security 单点登出清除所有业务系统的 token
    调整视频帧率、分辨率
    Docker学习笔记(三)
    【AI视野·今日CV 计算机视觉论文速览 第251期】Thu, 21 Sep 2023
    细谈MySQL的一下经典问题(一)
    Linux 学习笔记(1)——系统基本配置与开关机命令
    线程同步(解决线程安全问题)
    百度SEO优化不稳定的原因分析(提升网站排名的稳定性)
    4D毫米波雷达硬件系统架构
    k8s的网络插件Flannel和Calico(the hard way)
  • 原文地址:https://blog.csdn.net/m0_62436868/article/details/126448859