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

1.1 业务类

  1. package com.atguigu.speingcloud.controller;
  2. import com.alibaba.csp.sentinel.annotation.SentinelResource;
  3. import com.alibaba.csp.sentinel.slots.block.BlockException;
  4. import com.atguigu.springcloud.entities.CommonResult;
  5. import com.atguigu.springcloud.entities.Payment;
  6. import org.springframework.web.bind.annotation.GetMapping;
  7. import org.springframework.web.bind.annotation.RestController;
  8. @RestController
  9. public class RateLimitController {
  10. @GetMapping("/byResource")
  11. @SentinelResource(value = "byResource",blockHandler = "handleException")
  12. public CommonResult byResource() {
  13. return new CommonResult(200,"按资源名称限流测试OK",new Payment(2020L,"serial001"));
  14. }
  15. public CommonResult handleException(BlockException exception) {
  16. return new CommonResult(444,exception.getClass().getCanonicalName()+"\t 服务不可用");
  17. }
  18. @GetMapping("/rateLimit/byUrl")
  19. @SentinelResource(value = "byUrl")
  20. public CommonResult byUrl()
  21. {
  22. return new CommonResult(200,"按url限流测试OK",new Payment(2020L,"serial002"));
  23. }
  24. }

1.2 配置

image.png

1.3 测试

(1)测试一

http://localhost:8401/byResource

每秒点击一次
image.png
每秒点击超过一次
image.png

(2)测试二

http://localhost:8401/rateLimit/byUrl

每秒点击一次
image.png
每秒点击多次
image.png

1.4 注意:

  1. 添加@SentinelResource注解后需要重启项目
  2. 关闭8401项目后,sentinel控制台流控规则消失(临时)

2. 按照URL地址限流+后续处理

2.1 配置

image.png

2.2 测试

(1)测试一

http://localhost:8401/rateLimit/byUrl

每秒点击一次
image.png
每秒点击超过一次:会返回Sentinel自带的限流处理结果 Blocked by Sentinel (flow limiting)
image.png

(2)测试二

http://localhost:8401/byResource

每秒点击一次
image.png
每秒点击多次
image.png

3. 上面兜底方案面临的问题

**

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

4. 自定义限流处理逻辑

4.1 自定义限流处理类

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

  1. package com.atguigu.speingcloud.myhandler;
  2. import com.alibaba.csp.sentinel.slots.block.BlockException;
  3. import com.atguigu.springcloud.entities.CommonResult;
  4. import com.atguigu.springcloud.entities.Payment;
  5. public class CustomerBlockHandler {
  6. public static CommonResult handlerException(BlockException exception) {
  7. return new CommonResult(4444,"按客戶自定义,global handlerException----1");
  8. }
  9. public static CommonResult handlerException2(BlockException exception) {
  10. return new CommonResult(4444,"按客戶自定义,global handlerException----2");
  11. }
  12. }

4.2 controller

  1. @RestController
  2. public class RateLimitController {
  3. ...
  4. @GetMapping("/rateLimit/customerBlockHandler")
  5. @SentinelResource(value = "customerBlockHandler",
  6. blockHandlerClass = CustomerBlockHandler.class,//<-------- 自定义限流处理类
  7. blockHandler = "handlerException2")//<-----------
  8. public CommonResult customerBlockHandler()
  9. {
  10. return new CommonResult(200,"按客戶自定义",new Payment(2020L,"serial003"));
  11. }
  12. }

4.3 配置

image.png

4.4 测试

http://localhost:8401/rateLimit/customerBlockHandler

每秒访问一次
image.png
每秒访问吵过一次
image.png

5. @SentinelResource注解属性说明

@SentinelResource 注解

注意:注解方式埋点不支持 private 方法。

@SentinelResource 用于定义资源,并提供可选的异常处理和 fallback 配置项。 @SentinelResource 注解包含以下属性:

  • value:资源名称,必需项(不能为空)
  • entryType:entry 类型,可选项(默认为 EntryType.OUT)
  • blockHandler / blockHandlerClass: blockHandler 对应处理 BlockException 的函数名称,可选项。blockHandler 函数访问范围需要是 public,返回类型需要与原方法相匹配,参数类型需要和原方法相匹配并且最后加一个额外的参数,类型为 BlockException。blockHandler 函数默认需要和原方法在同一个类中。若希望使用其他类的函数,则可以指定 blockHandlerClass 为对应的类的 Class 对象,注意对应的函数必需为 static 函数,否则无法解析。
  • fallback /fallbackClass:fallback 函数名称,可选项,用于在抛出异常的时候提供 fallback 处理逻辑。fallback 函数可以针对所有类型的异常(除了exceptionsToIgnore里面排除掉的异常类型)进行处理。fallback 函数签名和位置要求:
    • 返回值类型必须与原函数返回值类型一致;
    • 方法参数列表需要和原函数一致,或者可以额外多一个 Throwable 类型的参数用于接收对应的异常。
    • fallback 函数默认需要和原方法在同一个类中。若希望使用其他类的函数,则可以指定 fallbackClass 为对应的类的 Class 对象,注意对应的函数必需为 static 函数,否则无法解析。
  • defaultFallback(since 1.6.0):默认的 fallback 函数名称,可选项,通常用于通用的 fallback 逻辑(即可以用于很多服务或方法)。默认 fallback 函数可以针对所有类型的异常(除了exceptionsToIgnore里面排除掉的异常类型)进行处理。若同时配置了 fallback 和 defaultFallback,则只有 fallback 会生效。defaultFallback 函数签名要求:
    • 返回值类型必须与原函数返回值类型一致;
    • 方法参数列表需要为空,或者可以额外多一个 Throwable 类型的参数用于接收对应的异常。
    • defaultFallback 函数默认需要和原方法在同一个类中。若希望使用其他类的函数,则可以指定 fallbackClass 为对应的类的 Class 对象,注意对应的函数必需为 static 函数,否则无法解析。
  • exceptionsToIgnore(since 1.6.0):用于指定哪些异常被排除掉,不会计入异常统计中,也不会进入 fallback 逻辑中,而是会原样抛出。

5.1 Sentinel主要有三个核心Api:

  1. SphU定义资源
  2. Tracer定义统计
  3. ContextUtil定义了上下文