SpringCloudAlibaba之Sentinel简单使用

发布于:2024-04-27 ⋅ 阅读:(23) ⋅ 点赞:(0)

SpringCloudAlibaba之Sentinel简单使用

使用sentinel三部曲,

  1. 定义资源
  2. 定义规则
  3. 检验规则是否生效

sentinel入门

  1. 创建基础模块sentinel-boot-service
  • 引入依赖
<dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>

        <dependency>
            <groupId>com.alibaba.csp</groupId>
            <artifactId>sentinel-core</artifactId>
            <version>1.8.0</version>
</dependency>
  • 创建主类
@SpringBootApplication
public class SentinelBootServiceApplication {

    public static void main(String[] args) {
        SpringApplication.run(SentinelBootServiceApplication.class,args);
    }
}
  • 创建application.yml
server:
  port: 8080

资源定义

  1. 定义sentinel资源,sentinel中的资源可以是一段代码,一个方法,甚至是整个应用。sentinel可以保护整个应用

资源必须定义资源名称

  • 创建service/SentinelTestService
SphU(抛出异常方式)
public String sphuTest(){
        // 1.5.0 版本开始可以直接利用 try-with-resources 特性
        try(Entry ignored = SphU.entry("sphuTest")) {
            // 被保护的逻辑
            return "hello world";
        } catch (BlockException ex) {
            // 处理被流控的逻辑
            return "blocked";
        }
}

Sphu.entry(resourceName) 可以定义一段sentinel资源,当访问超出sentinel定义的规则后,Sphu.entry方法会抛出BlockException

所以可以在try语句块中定义sentinel的资源,也就是被保护的逻辑。在catch块中捕获BlockException,用来对降级进行处理

SphO(布尔类型方式)
public String sphoTest() {
        if(SphO.entry("sphoTest")){
            try {
                return "hello world";
            } finally{
                SphO.exit();
            }
        }else{
            //被限流了,逻辑处理
            return "被限流了";
        }
}

SphO.entry(resourceName) 方法可以返回一个boolean值,用于是否通过sentinel管控,必须与SphO.exit()搭配使用

@SentinelResource(注解的方式定义)
@SentinelResource使用前置条件

由于@SentinelResource是通过aop方式进行管控,所以需要引入aspectj模块

<dependency>
            <groupId>com.alibaba.csp</groupId>
            <artifactId>sentinel-annotation-aspectj</artifactId>
            <version>1.8.0</version>
</dependency>
  • SentinelResourceAspect注入到Spring容器中

sentinel-boot-service 的config包下创建SentinelConfig

@Configuration
public class SentinelConfig {

    //注入
    @Bean
    public SentinelResourceAspect sentinelResourceAspect(){
        return new SentinelResourceAspect();
    }
}
使用@SentinelResource定义资源
private static AtomicInteger atomicInteger = new AtomicInteger(0);

	@SentinelResource(value = "annotationTest",blockHandler = "blockHandler",fallback = "fallback")
    public String annotationTest(){
        int i = atomicInteger.incrementAndGet();
        if(i % 2 == 0){
            return "hello world";
        }else{
            throw new RuntimeException("something wrong");
        }
	}

blockHandler和fallback是两个概念

blockHandler是sentinel对资源进行限流或者熔断时的处理规则

fallback是资源抛出异常后,对异常捕获后的处理

定义blockHandler和fallback方法(方法名称和@sentinelResoruce的参数名称需要一直,并且方法必须是public修饰)
	public String blockHandler(BlockException ex){
        ex.printStackTrace();
        return "block handler";
    }

    public String fallback(){
        return "fallback";
    }

所以上述代码有三段逻辑,第一段正常逻辑返回hello world,第二段,抛出异常,被fallback捕获,返回fallback,第三段,被sentinel限流,被blockHandler处理,返回block handler

@SentinelResoruce参数释义
参数名称 类型 描述
value String 必需项,用于指定资源的名称。这个名称通常用于在 Sentinel Dashboard 中进行监控和管理。
entryType EntryType 可选项,指定 entry 的类型。默认为 EntryType.OUT,表示这是一个出站调用。
blockHandler String 可选项,指定处理 BlockException 的函数名称。当原方法被限流或熔断时,会调用这个函数进行异常处理。该函数的访问范围需要是 public,返回类型需要与原方法相匹配,参数类型需要和原方法相匹配并且最后加一个额外的参数,类型为 BlockException。默认情况下,这个函数需要和原方法在同一个类中。
blockHandlerClass Class<?> 可选项,当希望使用其他类的函数作为 blockHandler 时,可以指定这个类的 Class 对象。注意,对应的函数必须为 static 函数。
fallback String 可选项,指定回退函数名称,当原方法调用失败时,会调用这个回退函数。回退函数的签名(返回值类型、参数类型)需要与原方法一致。
fallbackClass Class<?> 可选项,当希望使用其他类的函数作为回退函数时,可以指定这个类的 Class 对象。同样,对应的函数必须为 static 函数。
resourceType int 可选项,用于指定资源的类型。这通常用于区分不同类型的资源,以便进行更精细的控制。
exceptionsToIgnore Class<? extends Throwable>[] 可选项,指定需要忽略的异常类型。当原方法抛出这些异常时,Sentinel 不会触发限流或熔断逻辑。

这个注解允许开发者对方法进行细粒度的流量控制,同时提供了异常处理和回退机制,确保系统的可靠性和稳定性。通过合理配置这些参数,可以更加精准地控制应用的流量,避免由于流量过大或异常导致的系统崩溃。

完整的service代码(资源定义)
@Service
public class SentinelTestService {

    private static AtomicInteger atomicInteger = new AtomicInteger(0);

    public String sphuTest(){
        // 1.5.0 版本开始可以直接利用 try-with-resources 特性
        try(Entry ignored = SphU.entry("sphuTest")) {
            // 被保护的逻辑
            return "hello world";
        } catch (BlockException ex) {
            // 处理被流控的逻辑
            return "blocked";
        }
    }

    public String sphoTest() {
        if(SphO.entry("sphoTest")){
            try {
                return "hello world";
            } finally{
                SphO.exit();
            }
        }else{
            //被限流了,逻辑处理
            return "被限流了";
        }
    }

    @SentinelResource(value = "annotationTest",blockHandler = "blockHandler",fallback = "fallback")
    public String annotationTest(){
        int i = atomicInteger.incrementAndGet();
        if(i % 2 == 0){
            return "hello world";
        }else{
            throw new RuntimeException("something wrong");
        }
    }

    public String blockHandler(BlockException ex){
        ex.printStackTrace();
        return "block handler";
    }

    public String fallback(){
        return "fallback";
    }
}

定义sentinel规则

sentinel的规则有很多种,此文章示例,仅用流控来测试sentinel定义的资源信息

在config/SentinelRuleConfig下分别创建上面三个资源的流控规则,如下

@Configuration
public class SentinelRuleConfig {
    @PostConstruct
    public void init(){
        List<FlowRule> rules = new ArrayList<>();
        FlowRule rule = new FlowRule();
        //定义规则适用的资源名称
        rule.setResource("sphuTest");
        rule.setGrade(RuleConstant.FLOW_GRADE_QPS);
        //定义每秒被保护的代码块最多运行2次
        rule.setCount(2);
        rules.add(rule);


        FlowRule sphoTestRule = new FlowRule();
        //定义规则适用的资源名称
        sphoTestRule.setResource("sphoTest");
        sphoTestRule.setGrade(RuleConstant.FLOW_GRADE_QPS);
        //定义每秒被保护的代码块最多运行2次
        sphoTestRule.setCount(2);
        rules.add(sphoTestRule);


        FlowRule annotationTestRule = new FlowRule();
        //定义规则适用的资源名称
        annotationTestRule.setResource("annotationTest");
        annotationTestRule.setGrade(RuleConstant.FLOW_GRADE_QPS);
        //定义每秒被保护的代码块最多运行2次
        annotationTestRule.setCount(2);
        rules.add(annotationTestRule);
        FlowRuleManager.loadRules(rules);
    }
}
  • setResource:设置资源名称
  • setGrade:设置流控的类型,此处通过QPS,即每秒的访问量
  • setCount: 设置每秒访问量的阈值,当大于2时,触发sentinel流控

校验规则是否生效

  • 新建入口SentinelTestController,并分别创建三个uri对应三个资源的访问
public class SentinelTestController {

    @Resource
    private SentinelTestService sentinelTestService;

    @GetMapping("/sphuTest")
    public String sphuTest(){
        return sentinelTestService.sphuTest();
    }

    @GetMapping("/sphoTest")
    public String sphoTest(){
        return sentinelTestService.sphoTest();
    }

    @GetMapping("/annotationTest")
    public String annotationTest(){
        return sentinelTestService.annotationTest();
    }
}
  • 启动sentinel-boot-service项目模块,并通过浏览器访问
  1. sphuTest
  • 访问sphuTest,可以看到正常返回hello world
http://localhost:8080/sentinel/sphuTest
-> hello world
  • 快速刷新sphuTest,可以看到blockedhello world出现
http://localhost:8080/sentinel/sphuTest
-> hello world
-> blocked    
  1. sphotest
  • 访问sphOTest,可以看到正常返回hello world
http://localhost:8080/sentinel/sphoTest
-> hello world
  • 快速刷新sphoTest,可以看到被限流了hello world出现
http://localhost:8080/sentinel/sphoTest
-> hello world
-> 被限流了
  1. annotationTest
  • 低频率刷新annotationTest,可以看到hello world和fallback交替出现,这里由于业务逻辑是执行一次,抛一次异常,异常捕获被fallback执行,所以交替出现
http://localhost:8080/sentinel/annotationTest
-> hello world
-> fallback
-> hello world
-> fallback
-> hello world
-> fallback
  • 高频率刷新,可以看到hello worldfallbackblock handler都会出现。

在高频率刷新中,可能会执行正常逻辑(未限流,未异常情况),可能执行到fallback逻辑(异常情况),block handler(达到设定的流控QPS阈值)


网站公告

今日签到

点亮在社区的每一天
去签到