一、前言
接下来是开展一系列的 SpringCloud 的学习之旅,从传统的模块之间调用,一步步的升级为 SpringCloud 模块之间的调用,此篇文章为第十五篇,即介绍 SpringCloud Alibaba 的 @SentinelResource 注解。
二、简介
这个注解用于标记需要被流量控制、熔断降级以及系统保护等功能覆盖的方法或类。这个注解允许开发者自定义资源名称,并且可以配置异常处理逻辑和 fallback函数。
接下来我们分别使用按照资源名称限流和按照 url 地址限流分别来演示下。首先在 cloudalibaba-sentinel-service8401 模块新建一个 RateLimitController类,用于下面的测试。
三、按资源名称限流
在 RateLimitController中创建一个方法,并指定限流时所调用的方法,代码如下:
java
@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 服务不可用");
}
}
在 sentinel的管理界面根据资源名称配置限流规则,如下图:

输入 http://localhost:8401/byResource,进行测试,当 1s访问一次时,效果如下:

当频繁点击时效果如下,达到了我们的效果。

四、按照 url 地址限流
在 RateLimitController中创建一个方法,不指定发生限流时所调用的方法,代码如下:
java
@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"));
}
}
在 sentinel 的管理界面根据 url地址配置限流规则,如下图:

输入 http://localhost:8401/rateLimit/byUrl,进行测试,当 1s访问一次时,效果如下:

当频繁点击时效果如下,出现的是默认的限流返回消息,达到了我们的效果。

五、临时节点问题
此时,关闭我们的 cloudalibaba-sentinel-service8401模块,并等待一会,然后刷新我们
sentinel 管理界面,可以看到,节点全部消失了,证明我们所配置的这些限流规则均为临时节点。

六、兜底方案面临的问题
虽然限流发生时,我们可以指定兜底的方案,也可以不指定使用系统默认的兜底方案,但是也不是很好,总结起来有下面四点不好的地方:
**1、**若使用系统默认的,则没有体现我们自己的业务要求。
**2、**依照现有条件,我们自定义的处理方法又和业务代码耦合在一个类中,不直观。
**3、**每个业务方法都添加一个兜底的,那代码膨胀加剧。
**4、**全局统一的处理方法没有体现。
七、自定义限流处理
为了解决上面提出的这四点问题,我们可以采用自定义的限流处理逻辑,首先创建一个自定义处理的 hanler 类,代码如下:
java
package com.springcloud.myhandler;
import com.alibaba.csp.sentinel.slots.block.BlockException;
import com.springcloud.entities.CommonResult;
public class CustomerBlockHandler {
public static CommonResult handlerException(BlockException exception) {
return new CommonResult(4444, "按客戶自定义,global handlerException----1");
}
public static CommonResult handlerException2(BlockException exception) {
return new CommonResult(4444, "按客戶自定义,global handlerException----2");
}
}
在 RateLimitController 中创建一个测试方法,其中 blockHandlerClass 属性指定了兜底方法的类,blockHandler属性指定了兜底的方法,代码如下:
java
@GetMapping("/rateLimit/customerBlockHandler")
@SentinelResource(value = "customerBlockHandler",
blockHandlerClass = CustomerBlockHandler.class,
blockHandler = "handlerException2")
public CommonResult customerBlockHandler()
{
return new CommonResult(200,"按客戶自定义",new Payment(2020L,"serial003"));
}
打开 sentinel的管理界面,配置对应的限流规则,如下:

输入 http://localhost:8401/rateLimit/customerBlockHandler,进行测试,当 1s访问一次时,效果如下:

当频繁点击时效果如下,出现的是我们自定义的限流返回消息,达到了我们的效果。
