【Spring Cloud、Sentinel】搭建Spirng Cloud项目(八):Spring Cloud使用Sentinel的@SentinelResource注解指定出现异常时的处理策略

一、环境准备

1、环境搭建,查看Spring Cloud专栏

二、介绍

1、在定义了资源点之后,我们可以通过Dashboard控制台页面来设置限流和降级策略来对资源点进行保护。同时还能通过@SentinelResource注解来指定出现异常时的处理策略。

2、@SentinelResource注解的blockHandler字段,指定发生Sentinel异常时的处理方法(即触发Sentinel五种类型规则时的处理逻辑)。fallback字段指定发生业务异常时的处理方法。当然,为了省事,可以不指定blockHandler,所有处理逻辑都放在fallback中。

3、参数介绍:图示

在这里插入图片描述

三、代码配置

1、新增SentinelBlockHandler类和SentinelFallback类

package com.cyun.sys.controller.sentinel;

import com.alibaba.csp.sentinel.slots.block.BlockException;

/**
 * 触发Sentinel五种类型规则时的处理逻辑
 *
 * @author He PanFu
 * @date 2022-03-16 11:24:17
 */
public class SentinelBlockHandler {

    public static String testBlockHandler(BlockException e) {
        return "sentinel限流";
    }
}

package com.cyun.sys.controller.sentinel;

/**
 * 指定发生业务异常时的处理方法
 *
 * @author He PanFu
 * @date 2022-03-16 11:24:32
 */
public class SentinelFallback {

    public static String testFallback() {
        return "方法错误";
    }
}

2、测试类使用

package com.cyun.sys.controller.sentinel;

import com.alibaba.csp.sentinel.annotation.SentinelResource;
import com.alibaba.csp.sentinel.slots.block.BlockException;
import lombok.RequiredArgsConstructor;
import org.springframework.security.access.annotation.Secured;
import org.springframework.security.access.prepost.PostAuthorize;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.security.RolesAllowed;

/**
 * 测试 controller
 *
 * @author He PanFu
 * @date 2022-03-12 17:53:02
 */
@RestController
@RequiredArgsConstructor
@RequestMapping("/test")
public class TestSentinelController {

    int i = 0;

    /**
     * 熔断测试
     *
     * @return 结果
     */
    @GetMapping("/sentinel1")
    @SentinelResource(value = "test_sentinel1", blockHandlerClass = SentinelBlockHandler.class, blockHandler = "testBlockHandler",
            fallbackClass = SentinelFallback.class, fallback = "testFallback")
    public String sentinel1() {
        if (i++ % 3 == 0) {
            throw new RuntimeException();
        }
        return "新增接口";
    }

    /**
     * 熔断测试
     *
     * @return 结果
     */
    @GetMapping("/sentinel2")
    @SentinelResource(value = "test_sentinel2", blockHandler = "testBlockHandler", fallback = "testFallback")
    public String sentinel2() {
        if (i++ % 3 == 0) {
            throw new RuntimeException();
        }
        return "新增接口";
    }

    /**
     * 熔断测试
     *
     * @return 结果
     */
    @GetMapping("/sentinel3")
    @SentinelResource(value = "test_sentinel3", blockHandler = "testBlockHandler", fallback = "testFallback",
            exceptionsToIgnore = {RuntimeException.class})
    public String sentinel3() {
        if (i++ % 3 == 0) {
            throw new RuntimeException();
        }
        return "新增接口";
    }

    /**
     * 熔断测试
     *
     * @return 结果
     */
    @GetMapping("/sentinel4")
    @SentinelResource(value = "test_sentinel4", fallback = "testFallback", defaultFallback = "testDefaultFallback")
    public String sentinel4() {
        if (i++ % 3 == 0) {
            throw new RuntimeException();
        }
        return "新增接口";
    }

    /**
     * 触发Sentinel五种类型规则时的处理逻辑
     *
     * @param e 异常
     * @return 结果
     */
    public String testBlockHandler(BlockException e) {
        return "sentinel限流";
    }

    /**
     * 指定发生业务异常时的处理方法
     *
     * @return 结果
     */
    public String testFallback(Throwable throwable) {
        return "方法错误";
    }

    /**
     * 指定发生业务异常时的默认处理方法
     *
     * @return 结果
     */
    public String testDefaultFallback() {
        return "默认:方法错误";
    }
}

3、Sentinel的Dashboard控制台配置

在这里插入图片描述
在这里插入图片描述

4、结果展示

  • 触发Sentinel五种类型规则时在这里插入图片描述

  • 发生业务异常时的处理方法在这里插入图片描述

四、问题总结

1、定义的SentinelBlockHandler类和SentinelFallback类中的方法必要满足对应要求,否则不会生效。特别注意对应函数需要static修饰。

2、@SentinelResource注解中blockHandler配置的方法中必须包含BlockException参数,否则不会生效。

在这里插入图片描述

五、拓展

本文参考链接:SENTINEL之@SENTINELRESOURCE注解的使用

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值