• @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

     

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

     

  • 相关阅读:
    【Mybatis小白从0到90%精讲】09:Mybatis动态SQL:if、where、set标签
    rust trait对象
    基于STM32的蓝牙小车(虚拟串口模拟)的Proteus仿真
    Linux Redis 源码安装
    客服回复差评的话术模板
    【ES专题】ElasticSearch搜索进阶
    树结构工具-TreeUtil使用
    洛谷-P1255-数楼梯
    【李航统计学习笔记】第十章:隐马尔科夫模型
    微信小程序项目源码ssm校园跑腿+后台管理系统|前后分离VUE含论文+PPT+源码
  • 原文地址:https://blog.csdn.net/m0_62436868/article/details/126448859