SpringCloud Alibaba Sentinel实现熔断与限流

1、官网 

https://github.com/alibaba/Sentinel 

中文  介绍 · alibaba/Sentinel Wiki (github.com)

2、是什么: 一句话解释,之前我们讲解过的Hystrix

3、去哪下:https://github.com/alibaba/Sentinel/releases

4、能干嘛 

 

5、怎么玩

https://spring-cloud-alibaba-group.github.io/github-pages/greenwich/spring-cloud-alibaba.html

服务使用中的各种问题 :服务雪崩、服务降级、服务熔断、服务限流

安装Sentinel控制台

1、sentinel组件由2部分构成 后台和前端8080

下载地址  https://github.com/alibaba/Sentinel/releases 

下载到本地sentinel-dashboard-1.8.4.jar

前提 java8环境OK  8080端口没有被占用

命令行执行  java -jar sentinel-dashboard-1.8.4.jar

访问sentinel管理界面  http://localhost:8080 用户名密码均为sentinel  

 

初始化工程

1、新建Module  cloudalibaba-sentinel-service8401

2、pom文件

<dependencies>
        <!--SpringCloud ailibaba nacos -->
        <dependency>
            <groupId>com.alibaba.cloud</groupId>
            <artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId>
        </dependency>
        <!--SpringCloud ailibaba sentinel-datasource-nacos 后续做持久化用到-->
        <dependency>
            <groupId>com.alibaba.csp</groupId>
            <artifactId>sentinel-datasource-nacos</artifactId>
        </dependency>
        <!--SpringCloud ailibaba sentinel -->
        <dependency>
            <groupId>com.alibaba.cloud</groupId>
            <artifactId>spring-cloud-starter-alibaba-sentinel</artifactId>
        </dependency>
        <!--openfeign-->
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-openfeign</artifactId>
        </dependency>
        <!-- SpringBoot整合Web组件+actuator -->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-actuator</artifactId>
        </dependency>
        <!--日常通用jar包配置-->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-devtools</artifactId>
            <scope>runtime</scope>
            <optional>true</optional>
        </dependency>
        <dependency>
            <groupId>cn.hutool</groupId>
            <artifactId>hutool-all</artifactId>
            <version>4.6.3</version>
        </dependency>
        <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
            <optional>true</optional>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
</dependencies>

3、application.yml文件

server:
  port: 8401

spring:
  application:
    name: cloudalibaba-sentinel-service
  cloud:
    nacos:
      discovery:
        #Nacos服务注册中心地址
        server-addr: localhost:8848
    sentinel:
      transport:
        #配置Sentinel dashboard地址
        dashboard: localhost:8080
        #默认8719端口,假如被占用会自动从8719开始依次+1扫描,直至找到未被占用的端口
        port: 8719

management:
  endpoints:
    web:
      exposure:
        include: '*'

4、主启动类

@EnableDiscoveryClient
@SpringBootApplication
public class MainApp8401
{
    public static void main(String[] args) {
        SpringApplication.run(MainApp8401.class, args);
    }
}
 

5、业务类

@RestController
@Slf4j
public class FlowLimitController {

@GetMapping("/testA")
public String testA()
{

try { TimeUnit.SECONDS.sleep(800); } catch (InterruptedException e) { e.printStackTrace(); }

return "------testA";
}

@GetMapping("/testB")
public String testB()
{
log.info(Thread.currentThread().getName()+"\t"+"...testB");
return "------testB";
}

}

6、启动8401微服务后查看sentienl控制台,空空如也,啥都没有

     Sentinel采用的懒加载说明,执行一次访问即可  http://localhost:8401/testA     http://localhost:8401/testB

效果如图  sentinel8080正在监控微服务8401

  

 

 

 流控规则

 

 

 

 

 

1、直接--->快速失败 

如图表示一秒内查询1次就OK,超过1次,就直接快速失败,报默认错误,快速点击访问http://localhost:8401/testA,结果报错 Blocked by Sentinel (flow limiting)

 

 2、关联--->快速失败

 当与A关联的资源B达到阀值后,就限流A自己  B惹事,A挂了

设置效果
当关联资源/testB的qps阀值超过1时,就限流/testA的Rest访问地址,当关联资源到阈值后限制配置好的资源名

 postman模拟并发密集访问testB

 

3、预热 Warmup

 

 

公式:阈值除以coldFactor(默认值为3),经过预热时长后才会达到阈值

默认coldFactor为3,即请求 QPS 从 threshold / 3 开始,经预热时长逐渐升至设定的 QPS 阈值。

官网 限流 冷启动 · alibaba/Sentinel Wiki (github.com) 

配置如图 阀值为10+预热时长设置5秒。
系统初始化的阀值为10 / 3 约等于3,即阀值刚开始为3;然后过了5秒后阀值才慢慢升高恢复到10

 多次点击http://localhost:8401/testB  刚开始不行,后续慢慢OK

应用场景 如:秒杀系统在开启的瞬间,会有很多流量上来,很有可能把系统打死,预热方式就是把为了保护系统,可慢慢的把流量放进来,慢慢的把阀值增长到设置的阀值。

 

4、排队等待 

匀速排队,阈值必须设置为QPS

官网解释

 

匀速排队,让请求以均匀的速度通过,阀值类型必须设成QPS,否则无效。

设置含义:/testA每秒1次请求,超过的话就排队等待,等待的超时时间为20000毫秒。

 

 

posted @ 2022-07-20 15:47  雨韵蓝田  阅读(68)  评论(0编辑  收藏  举报