SentinelResource保护资源

  1. SentinelResource限流
  2. 编写业务代码
    @RestController
    @RequestMapping("/test")
    public class TestController {
        @GetMapping(value = "/hello")
        @SentinelResource(value = "hello", blockHandler = "blockHandlerHello")
        public String hello() {
            return "Hello Sentinel";
        }
        public String blockHandlerHello(BlockException e){
            log.error("被限流了异常打印:", e);
            return "被限流了" ;
        }
    }
    
  3. 编写流控规则,并按照InitFunc的SPI配置
    public class FlowRuleInitFunc implements InitFunc {
        @Override
        public void init() throws Exception {
            List<FlowRule> rules = new ArrayList<>() ;
            var rule = new FlowRule() ;
            rule.setResource("hello") ;
            rule.setGrade(RuleConstant.FLOW_GRADE_QPS) ;
            rule.setCount(1) ;
            rule.setStrategy(RuleConstant.STRATEGY_DIRECT) ;
            rule.setLimitApp("default") ;
            rules.add(rule) ;
            FlowRuleManager.loadRules(rules);
        }
    }
    
  4. 访问http://localhost:8083/test/hello
SentinelResource 降级
  1. 在TestController中补充业务代码
    @RestController
    @RequestMapping("/test")
    public class TestController {
    
        @GetMapping(value = "/exception")
        @SentinelResource(value = "test_exception", fallback = "fallbackHello")
        public String exception(@RequestParam(value = "param", required = false) String param) {
            if (StringUtils.hasText(param)){
               int i = 1/0 ;
            }
            return "Hello Sentinel";
        }
        public String fallbackHello(Throwable e){
            log.error("被降级异常打印:", e);
            return "被降级了" ;
        }
    }
    
  2. 访问http://localhost:8083/test/exception?param=1
熔断配置
  1. 在FlowRuleInitFunc中补充熔断配置
    @Slf4j
    public class RuleLoadInitFunc implements InitFunc {
        @Override
        public void init() throws Exception {
            log.info("======> 手动加载限流规则.....");
            this.initDegradeRule();
        }
        /**
         * 30秒内,最少3个请求,如果有10% 的报错则触发10秒的熔断降级
         */
        private void initDegradeRule(){
            List<DegradeRule> rules = new ArrayList<>() ;
            DegradeRule rule = new DegradeRule("test_exception")
                .setGrade(CircuitBreakerStrategy.ERROR_RATIO.getType())
                .setCount(0.1) // Threshold is 70% error ratio
                .setMinRequestAmount(3) // 熔断触发的最小请求数
                .setStatIntervalMs(30000) // 30s 统计时长
                .setTimeWindow(10); // 熔断时长10秒
            rules.add(rule) ;
            DegradeRuleManager.loadRules(rules);
        }
    }
    
  2. 访问3次http://localhost:8083/test/exception?param=1
  3. 再次访问http://localhost:8083/test/exception发现服务已经处于熔断状态,10秒之后服务恢复正常
其他补充
  1. blockHandlerHello中的异常参数类型为BlockException,写成Exception则不生效
  2. fallbackHello中的异常参数必须是Throwable, 写成别的不生效
  3. @SentinelResource注解被 SentinelResourceAspect 切面拦截
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值