一 背景描述
1.1 说明
1.定义一个接口,按照资源名和路径进行测试
2.需要先启动nacos,然后sentinel,然后启动app
3.这里使用@SentinelResource的默认处理策略,没有配置blockhandler处理策略
4.可以看到:
按路径限流 触发后 默认报:Blocked by Sentinel (flow limiting)
按资源限流 触发后 默认报: whitelable error page 页面 ,如果加blockhandler指定的处理逻辑,按指定逻辑进行显示。
1.2 按资源限流
1.代码的编写
@GetMapping("/byLu")
@SentinelResource(value = "byZiyuan")
public CommonResult byZiyuan()
{
return new CommonResult(200,"按资源名称限流测试OK",new Payment(2020L,"serial001"));
}
2.规则配置
3.访问,快速访问
快速访问后触发限流规则,这里报空白页错误,是因为@sentinelResource注解没有配置blockhandler指定的处理方法。
1.3 按路径测试
1.代码的编写
@GetMapping("/byLu")
@SentinelResource(value = "byZiyuan")
public CommonResult byZiyuan()
{
return new CommonResult(200,"按资源名称限流测试OK",new Payment(2020L,"serial001"));
}
2.配置按路径测试: 删除掉之前配置的资源规则,新建配置按路径测试
3.开始访问
4.快速访问后,报限流提示
1.4 按资源进行全局设置
1.定义全局工具类
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");
}
}
引用:
2.正常配置
3.正常访问
4.快速访问,触发限流规则