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


    初始化演示工程

    前提启动nacos,Sentinel成功

    新建cloudalibaba-sentinel-service8401

    pom文件

    1. "1.0" encoding="UTF-8"?>
    2. <project xmlns="http://maven.apache.org/POM/4.0.0"
    3. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    4. xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    5. <parent>
    6. <artifactId>cloud2020artifactId>
    7. <groupId>com.atguigu.springcloudgroupId>
    8. <version>1.0-SNAPSHOTversion>
    9. parent>
    10. <modelVersion>4.0.0modelVersion>
    11. <artifactId>cloudalibaba-sentinel-service8401artifactId>
    12. <properties>
    13. <maven.compiler.source>8maven.compiler.source>
    14. <maven.compiler.target>8maven.compiler.target>
    15. properties>
    16. <dependencies>
    17. <dependency>
    18. <groupId>com.alibaba.cloudgroupId>
    19. <artifactId>spring-cloud-starter-alibaba-nacos-discoveryartifactId>
    20. dependency>
    21. <dependency>
    22. <groupId>com.alibaba.cloudgroupId>
    23. <artifactId>spring-cloud-starter-alibaba-sentinelartifactId>
    24. dependency>
    25. <dependency>
    26. <groupId>org.springframework.cloudgroupId>
    27. <artifactId>spring-cloud-starter-openfeignartifactId>
    28. dependency>
    29. <dependency>
    30. <groupId>org.springframework.bootgroupId>
    31. <artifactId>spring-boot-starter-webartifactId>
    32. dependency>
    33. <dependency>
    34. <groupId>org.springframework.bootgroupId>
    35. <artifactId>spring-boot-starter-actuatorartifactId>
    36. dependency>
    37. <dependency>
    38. <groupId>org.springframework.bootgroupId>
    39. <artifactId>spring-boot-devtoolsartifactId>
    40. <scope>runtimescope>
    41. <optional>trueoptional>
    42. dependency>
    43. <dependency>
    44. <groupId>org.projectlombokgroupId>
    45. <artifactId>lombokartifactId>
    46. <optional>trueoptional>
    47. dependency>
    48. <dependency>
    49. <groupId>org.springframework.bootgroupId>
    50. <artifactId>spring-boot-starter-testartifactId>
    51. <scope>testscope>
    52. dependency>
    53. dependencies>
    54. project>

    yml文件

    1. rver:
    2. port: 8401
    3. spring:
    4. application:
    5. name: cloudalibaba-sentinel-service
    6. cloud:
    7. nacos:
    8. discovery:
    9. #Nacos服务注册中心地址
    10. server-addr: localhost:8848
    11. sentinel:
    12. transport:
    13. #配置Sentinel dashboard地址
    14. dashboard: localhost:8080
    15. #默认8719端口,假如被占用会自动从8719开始依次+1扫描,直至找到未被占用的端口
    16. port: 8719
    17. management:
    18. endpoints:
    19. web:
    20. exposure:
    21. include: '*'
    22. sentinel:
    23. enabled: true

    主启动类

    1. @EnableDiscoveryClient
    2. @SpringBootApplication
    3. public class MainApp8401
    4. {
    5. public static void main(String[] args) {
    6. SpringApplication.run(MainApp8401.class, args);
    7. }
    8. }

    业务类

    1. @RestController
    2. public class FlowLimitController
    3. {
    4. @GetMapping("/testA")
    5. public String testA()
    6. {
    7. return "------testA";
    8. }
    9. @GetMapping("/testB")
    10. public String testB()
    11. {
    12. return "------testB";
    13. }
    14. }

     测试1

    访问http://localhost:8401/testA

     访问http://localhost:8401/testB

     访问http://localhost:8080

     流控规则

    基本介绍

    流控模式 

    直接(默认)

    直接->快速失败

    配置及说明

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

     测试2

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

    关联

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

    测试3

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

    postman里新建多线程集合组

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

    run 

    大批量线程高并发访问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),经过预热时长后才会达到阈值

    官网

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

     

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

    测试4

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

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

    刚开始不行,后续慢慢OK

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

    排队等待

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

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

    官网 

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

     

     

    源码 

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

     

  • 相关阅读:
    共享单车之数据存储
    [附源码]Python计算机毕业设计Django冬奥会网上商城
    HarmonyOS hsp制作与引用
    一文掌握GitHub Actions基本概念与配置
    如何保证 HTTPS 证书的有效性?
    请问一下就是业务概念模型和业务逻辑模型有啥关系
    【Redis】深入探索 Redis 的数据类型 —— 列表 List
    Keras深度学习实战(29)——长短时记忆网络详解与实现
    16位数码管驱动及键盘控制芯片CH456
    动态规划18(Leetcode300最长递归子序列)
  • 原文地址:https://blog.csdn.net/m0_62436868/article/details/126433092