SpringCloud AlibabaSentinel实现熔断与限流(1)

简介: 初始化演示工程前提启动nacos,Sentinel成功新建cloudalibaba-sentinel-service8401

初始化演示工程



前提启动nacos,Sentinel成功


新建cloudalibaba-sentinel-service8401


pom文件


<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <parent>
        <artifactId>cloud2020</artifactId>
        <groupId>com.atguigu.springcloud</groupId>
        <version>1.0-SNAPSHOT</version>
    </parent>
    <modelVersion>4.0.0</modelVersion>
    <artifactId>cloudalibaba-sentinel-service8401</artifactId>
    <properties>
        <maven.compiler.source>8</maven.compiler.source>
        <maven.compiler.target>8</maven.compiler.target>
    </properties>
    <dependencies>
        <!--SpringCloud ailibaba nacos -->
        <dependency>
            <groupId>com.alibaba.cloud</groupId>
            <artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId>
        </dependency>
        <!--SpringCloud ailibaba sentinel -->
        <dependency>
            <groupId>com.alibaba.cloud</groupId>
            <artifactId>spring-cloud-starter-alibaba-sentinel</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-openfeign</artifactId>
        </dependency>
        <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>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-devtools</artifactId>
            <scope>runtime</scope>
            <optional>true</optional>
        </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>
</project>


yml文件


rver:
  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: '*'
    sentinel:
      enabled: true


主启动类


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


业务类


@RestController
public class FlowLimitController
{
    @GetMapping("/testA")
    public String testA()
    {
        return "------testA";
    }
    @GetMapping("/testB")
    public String testB()
    {
        return "------testB";
    }
}


测试1


访问http://localhost:8401/testA


789c51c5ba3d434695a68d81f8a8b007.png

访问http://localhost:8401/testB


f6cc13a486134eb28b72d891c85ced6d.png

访问http://localhost:8080


ba32fdf1930042568e37e2108582d1b2.png


流控规则



基本介绍


ba79df7146324bbc9ac03aec736406cc.png8fc12f9f1eed4ef489743c32daf0ea3a.png


流控模式



直接(默认)

直接->快速失败

配置及说明

表示1秒钟内查询1次就是OK,若超过次数1,就直接-快速失败,报默认错误

f23709072e64421fbdf2667af5449e58.png


测试2


快速点击访问http://localhost:8401/testA


bbc9624e79bd4309b1a66ef09810b4db.png


关联


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


92dd2db36b5a44f087a1d55d15b862cf.png


测试3


postman发送请求http://localhost:8401/testB



e88c92ed57a649d7b7121165be98185b.png


postman里新建多线程集合组


94ec8f3fc8664b928bf4fbbc2ef093d2.png


将访问地址添加进新新线程组


e2d8f57037914280b5291e2296a6e0e6.png


run


7756d584407d4b45b52beccf896910e0.png


大批量线程高并发访问B,导致A失效了

点击访问http://localhost:8401/testA

运行后发现testA挂了

结果:

Blocked by Sentinel (flow limiting)


链路


多个请求调用了同一个微服务

http://t.csdn.cn/sUOy9


流控效果



直接->快速失败(默认的流控处理)

直接失败,抛出异常

Blocked by Sentinel (flow limiting)


源码

com.alibaba.csp.sentinel.slots.block.flow.controller.DefaultController


预热


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

官网

6db62bdd7b2949e2a67f5fad1543e61a.png


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


457775ac8bf04cfabb380e94779a8fe5.png

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


测试4


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


cc8a9e9a30b14291b9c1f582a5362283.png


多次点击http://localhost:8401/testB

刚开始不行,后续慢慢OK


应用场景


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


排队等待


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

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


9b593dfc262344ada0eff280b4ac7589.png


官网


https://github.com/alibaba/Sentinel/wiki/%E6%B5%81%E9%87%8F%E6%8E%A7%E5%88%B6

f1668773869342da817a4488c439cbcf.png


源码


com.alibaba.csp.sentinel.slots.block.flow.controller.RateLimiterController

34c19671132146fe96f66c9e6533c4e6.png




相关文章
|
1天前
|
监控 Java 开发者
Spring Cloud中的服务熔断与降级
Spring Cloud中的服务熔断与降级
|
1天前
|
负载均衡 监控 Java
深入理解Spring Boot与Spring Cloud的整合方式
深入理解Spring Boot与Spring Cloud的整合方式
|
5天前
|
消息中间件 Java 应用服务中间件
Spring Cloud学习之-什么是Spring Cloud?
Spring Cloud学习之-什么是Spring Cloud?
|
6天前
|
Java API 数据格式
Spring三兄弟:Spring、Spring Boot、Spring Cloud的100个常用注解大盘点
Spring三兄弟:Spring、Spring Boot、Spring Cloud的100个常用注解大盘点
|
7天前
|
自然语言处理 监控 开发者
springCloud之Sentinel流量路由、流量控制、流量整形、熔断降级
springCloud之Sentinel流量路由、流量控制、流量整形、熔断降级
10 0
|
7天前
springCloud之服务降级熔断Hystrix、OpenFeign
springCloud之服务降级熔断Hystrix、OpenFeign
12 0
|
11天前
|
监控 Java Sentinel
使用Sentinel进行服务调用的熔断和限流管理(SpringCloud2023实战)
Sentinel是面向分布式、多语言异构化服务架构的流量治理组件,主要以流量为切入点,从流量路由、流量控制、流量整形、熔断降级、系统自适应过载保护、热点流量防护等多个维度来帮助开发者保障微服务的稳定性。
30 3
|
18天前
|
监控 Java API
深入解析 Spring Cloud Sentinel:分布式系统流量控制与熔断降级的全面指南
深入解析 Spring Cloud Sentinel:分布式系统流量控制与熔断降级的全面指南
26 0
深入解析 Spring Cloud Sentinel:分布式系统流量控制与熔断降级的全面指南
|
2月前
|
Java 数据安全/隐私保护 Sentinel
微服务学习 | Spring Cloud 中使用 Sentinel 实现服务限流
微服务学习 | Spring Cloud 中使用 Sentinel 实现服务限流
|
2月前
|
算法 NoSQL API
SpringCloud&Gateway网关限流
SpringCloud&Gateway网关限流
122 7