SpringCloudAlibaba之Sentinel简单使用

简介: 高频率刷新,可以看到hello world、fallback、block handler都会出现。

使用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参数释义


image.png


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

完整的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阈值)

目录
相关文章
|
10月前
|
SpringCloudAlibaba 监控 Dubbo
SpringCloudAliBaba篇 之 Sentinel:图解分布式系统的流量防卫兵(上)
SpringCloudAliBaba篇 之 Sentinel:图解分布式系统的流量防卫兵
221 0
|
1月前
|
SpringCloudAlibaba Rust 监控
SpringCloudAlibaba:5.1Sentinel的基本使用
SpringCloudAlibaba:5.1Sentinel的基本使用
39 0
|
10月前
|
缓存 SpringCloudAlibaba Linux
SpringCloudAliBaba篇 之 Sentinel:图解分布式系统的流量防卫兵(下)
SpringCloudAliBaba篇 之 Sentinel:图解分布式系统的流量防卫兵
87 0
|
11月前
|
存储 SpringCloudAlibaba NoSQL
九.SpringCloudAlibaba极简入门-持久化Sentinel限流规则
在前两章节我们学习了通过[Sentinel的限流和熔断机制](https://blog.csdn.net/u014494148/article/details/105484410)来保护微服务,提高系统的可用性,但是有一个问题,我们在Sentinel配置了限流,熔断策略,默认情况下Sentinel的数据是基于内存存储,当客户端断开,或者Sentinel重启数据就会丢失,这不是我们愿意看到的。所有我们需要的Sentinel做数据持久。 Sentinel 中支持5种持久化的方式:file、redis、nacos、zk和apollo,本片文章针对于Nacos进行持久化配置。
|
11月前
|
SpringCloudAlibaba Java Sentinel
十四.SpringCloudAlibaba极简入门-Sentinel对Gateway网关进行限流
服务网关在微服务架构中充当了请求访问入口的角色,是非常重要的一个部分,在高并发的系统中我们通常会在网关层通过流控降级等手段把多余的请求拒绝在外来防止微服务被高并发请求打垮,在之前我们有讨论过《服务网关Spring Cloud Gateway》和 《Sentinel流控》,一个是服务网关,一个是流控降级,本篇文章要讨论的是如何使用Sentinel对Gateway进行流控
|
11月前
|
SpringCloudAlibaba 负载均衡 Java
八.SpringCloudAlibaba极简入门-Sentinel兼容Feign进行服务熔断
Spring Cloud Alibaba是Spring Cloud的一个子项目,OpenFeign是Spring Cloud的客户端负载均衡器,使用Spring Cloud Alibaba依然可以很方便的集成OpenFeign,如果要使用OpenFeign作为服务客户端负载均衡,那么我们需要考虑OpenFeign开启Sentinel进行服务熔断降级。
|
11月前
|
SpringCloudAlibaba 双11 Sentinel
七.SpringCloudAlibaba极简入门-Sentinel熔断
在上一章节我们探讨了Sentinel的流控(限流)功能,Sentinel除了流控还提供了服务熔断和降级机制,服务之间的调用关系错综复杂,微服务的调用链上的某些服务资源不稳定(宕机,异常,超时)可能会导致可能请求的失败和请求的堆积,调用链产生连锁反应可能会导致整个微服务架构瘫痪。服务熔断降级机制是保障高可用的重要措施之一。
|
11月前
|
消息中间件 SpringCloudAlibaba 监控
六.SpringCloudAlibaba极简入门-Sentinel限流
限流 , 限制流量,这里的流量我们可以理解成请求数量,其实就是限制服务器的请求并发数量,为什么要这么做?如果不做限流,那么在大量并发请求下我们的服务器会慢慢的变慢然后顶不住压力而挂掉(类似堵车)。并不是说并发越大越好,有的时候我们的项目规模和业务决定了我们不需要那么大的并发性,当大量的并发请求访问到服务器时我们需要把部分请求拒绝在外,这个是流量限制 - 限流。 熔断机制在在《Spring Cloud 极简入门》中有详细的解释,熔断机制是对服务调用链路的保护机制,如果链路上的某个服务不可访问,调用超时,发生异常等,服务会进行发熔断,触发降级返回托底数据。简单理解就是当服务器不可访问,可以返回一
|
11月前
|
SpringCloudAlibaba Go Sentinel
「SpringCloudAlibaba系列」分布式限流框架Sentinel基本引用|
分布式限流框架Sentinel基本引用 Sentinel的基本引用 使用Sentinel的核心库实现限流,主要分以下几个步骤: 定义资源 定义限流规则 校验规则是否生效
|
7天前
|
监控 Java Sentinel
使用Sentinel进行服务调用的熔断和限流管理(SpringCloud2023实战)
Sentinel是面向分布式、多语言异构化服务架构的流量治理组件,主要以流量为切入点,从流量路由、流量控制、流量整形、熔断降级、系统自适应过载保护、热点流量防护等多个维度来帮助开发者保障微服务的稳定性。
26 3