Controller增加代码
@GetMapping("/rateLimit/customBlockHandler")
@SentinelResource(value="customBlockHandler", blockHandlerClass = CustomBlockHandler.class, blockHandler = "handlerException2")
public CommonResult byUrl(){
return new CommonResult(200,"按客户自定义进行限流,测试通过",new Payment(2020L,"serial002"));
}
流控资源名
流控的资源名可以选用GetMapping中的value,也可以使用SentinelResource中的value
抽取BlockException处理降低偶合度
可以声明一个类,集中处理
处理方法要声明为静态方法,这样不需要实例化了
public class CustomBlockHandler {
public static CommonResult handlerException1(BlockException exception){
return new CommonResult(431,"按照客户自定义【异常】, global handler Exception -------1");
}
public static CommonResult handlerException2(BlockException exception){
return new CommonResult(431,"按照客户自定义【异常】, global handler Exception --------2");
}
}