美文网首页
Sentinel注解@SentinelResource

Sentinel注解@SentinelResource

作者: CodeYang | 来源:发表于2021-09-09 09:20 被阅读0次

@SentinelResource注解支持

注解支持

自定义全局限流处理类

  1. 新建全局处理类 CustomerBlockHandler
/**
 * 定义可以全局使用
 */
public class CustomerBlockHandler {

    public static String handlerException(BlockException e){
        return "4444 , 按用户自定义,Global,handlerException ";
    }

    public static String handlerException2(BlockException e){
        return "445 , 按用户自定义,Global,handlerException2";
    }
}
  1. 编写 RateLimitController 控制类,主要是 customerBlockHandler()方法,其他方法参考
/**
 * @SentinelResource 注解使用
 */
@RestController
public class RateLimitController {
    /**
     * 配置全局 处理类
     * @return
     */
    @GetMapping(value = "/rateLimit/customerBlockHandler")
    @SentinelResource(value = "customerBlockHandler",
            blockHandlerClass = CustomerBlockHandler.class,
            blockHandler = "handlerException2")
    public String customerBlockHandler(){
        return "200 , 按用户自定义成功访问";
    }
}
  1. 新建流控规则,选择按资源名称限流,如果按URL限流就会走默认的错误提示
image.png image.png
  1. 测试访问,如果限流就进入配置的 CustomerBlockHandler类的handlerException2方法
image.png

相关文章

网友评论

      本文标题:Sentinel注解@SentinelResource

      本文链接:https://www.haomeiwen.com/subject/cjhuiltx.html