java

关注公众号 jb51net

关闭
首页 > 软件编程 > java > springboot sentinel限流

Spring Boot4使用Sentinel进行限流的操作方法

作者:enjoy编程

Sentinel是阿里巴巴开源的流量控制组件,用于保障微服务架构的稳定性,它提供了多种规则类型,本文还介绍了如何在Spring Boot 4中集成Sentinel,以及Sentinel在不同版本的Spring Boot和Spring Cloud中的兼容性情况,感兴趣的朋友跟随小编一起看看吧

Sentinel介绍

随着微服务的流行,服务和服务之间的稳定性变得越来越重要。Sentinel 是面向分布式、多语言异构化服务架构的流量治理组件,主要以流量为切入点,从流量路由、流量控制、流量整形、熔断降级、系统自适应过载保护、热点流量防护等多个维度来帮助开发者保障微服务的稳定性。

Sentinel 社区正在将流量治理相关标准抽出到 OpenSergo spec 中,Sentinel 作为流量治理标准实现

Sentinel规则类型

针对Sentinel的规则重新学习下,详细使用文档参见 官方文档

规则类型核心作用维度典型应用场景触发后果作用
流控规则 (Flow)限制流量 (QPS/线程数)秒杀限流、防止突发流量冲垮系统请求被拒绝/排队等待防刷、防过载
熔断规则 (Degrade)依赖稳定性 (响应时间/异常)调用第三方接口超时、数据库慢查询暂停请求,快速失败(熔断)防雪崩、防依赖故障
热点规则 (Param)参数粒度 (特定参数值)某个热门商品被疯狂刷,其他商品正常访问限制特定参数的访问频率防热点数据击穿
系统规则 (System)整体负载 (CPU/Load/RT)大促期间防止机器过载,保护系统基线拒绝部分请求,保护机器不挂防机器挂掉
授权规则 (Auth)来源控制 (黑白名单)防止某个恶意 IP 或非法应用调用接口允许或拒绝请求防非法调用

流控规则(Flow Control)

最常用的规则,主要用于限流,有如下2种模式

除了简单的直接拒绝,它还有两种高级策略:

熔断规则 (Circuit Breaker)

关注服务的质量。当它发现某个服务调用“不健康”时,会直接切断连接,防止连锁故障(雪崩)。
Sentinel 支持三种策略:

熔断是“宁可错杀一百,不可放过一个”的保护机制。触发后,所有请求直接走“降级逻辑”(fallback),不再调用下游真实服务,直到经过一段“冷却时间”后尝试恢复

热点规则 (Hotspot Param)

流控规则的高级版,它能把限流的粒度细化到方法参数级别。
通常情况下,流控是对整个接口(如 /getProduct)限流。但热点规则可以做到:当参数 id=1 的时候限流 100 QPS,而 id=2 的时候不限流

关注的是“不均匀”。在实际场景中,可能只有某个特定的“爆款”商品(热点 key)流量巨大,导致数据库压力大。如果对整个接口限流,会误伤其他正常的商品查询。热点规则就是为了解决这种“贫富不均”的问题

系统规则 (System Protection)

全局视角的规则,它不针对某个具体的接口,而是针对整个应用实例的系统指标。
它监控的指标包括:

授权规则 (Authority Rule)

黑白名单机制,用于控制“谁可以调用”。
它通过 SentinelContext 中的 origin(来源标识)来判断。通常用来识别调用方的服务名、IP 地址等。 关注的是“身份”,可以使用黑名单 或白名单限制

Spring boot 4如何集成Sentinel?

本次采用@SentinelResource + AOP 方式集成 Sentinel

添加依赖

 <dependency>
            <groupId>com.alibaba.csp</groupId>
            <artifactId>sentinel-core</artifactId>
            <version>1.8.9</version>
        </dependency>
        <dependency>
            <groupId>com.alibaba.csp</groupId>
            <artifactId>sentinel-parameter-flow-control</artifactId>
            <version>1.8.9</version>
        </dependency>
        <dependency>
            <groupId>com.alibaba.csp</groupId>
            <artifactId>sentinel-annotation-aspectj</artifactId>
            <version>1.8.9</version>
        </dependency>
        <dependency>
            <groupId>com.alibaba.csp</groupId>
            <artifactId>sentinel-transport-simple-http</artifactId>
            <version>1.8.9</version>
        </dependency>

编写相关的测试代码

SentinelConfig类

@Configuration
public class SentinelConfig {
    @PostConstruct
    public void initFlowRules(){
        List<FlowRule> rules = new ArrayList<>();
        FlowRule rule = new FlowRule();
        rule.setResource("HelloWorld");
        rule.setGrade(RuleConstant.FLOW_GRADE_QPS);
        // Set limit QPS to 20.
        rule.setCount(10);
        rules.add(rule);
        FlowRuleManager.loadRules(rules);
    }
    @PostConstruct
    public void initDegradeRules() {
        List<DegradeRule> rules = new ArrayList<>();
        DegradeRule rule = new DegradeRule();
        rule.setResource("HelloWorld");
        rule.setGrade(RuleConstant.DEGRADE_GRADE_RT); // RT threshold degrade strategy
        rule.setCount(200); // Max response time (ms)
        rule.setTimeWindow(10); // Recovery timeout period in seconds
        rules.add(rule);
        DegradeRuleManager.loadRules(rules);
    }
    @Bean
    public SentinelResourceAspect sentinelResourceAspect() {
        return new SentinelResourceAspect();
    }
}

HelloWorldService

@Service
public class HelloWorldService {
    @SentinelResource(value = "HelloWorld",
        entryType = EntryType.IN,
        exceptionsToIgnore = {IllegalStateException.class},
        blockHandler = "handleBlock",
        fallback = "handleFallback")
    public String sayHello(String name) {
        return "Hello, " + name;
    }
    public String handleBlock(String name, BlockException ex) {
        return "Request blocked by Sentinel: " + ex.getClass().getSimpleName();
    }
    public String handleFallback(String name, Throwable t) {
        return "Request failed and handled by fallback: " + t.getClass().getSimpleName();
    }
}

HelloWorldController

@RestController
@RequestMapping("/api")
@Tag(name = "登录接口")
public class HelloWorldController {
    @Autowired
    private HelloWorldService helloWorldService;
    @GetMapping("/hello")
    @Operation(summary = "hello")
    public String sayHello(@RequestParam(value = "name", defaultValue = "World") String name) {
        return helloWorldService.sayHello(name);
    }
}
@SpringBootTest
@Slf4j
public class SentinelTest {
    @Autowired
    HelloWorldService helloWorldService;
    private AtomicInteger successCount = new AtomicInteger(0);
    private AtomicInteger blockCount = new AtomicInteger(0);
    private AtomicInteger fallbackCount = new AtomicInteger(0);
    @BeforeEach
    public void setUp() throws Exception {
        // Reset counters before each test
        successCount.set(0);
        blockCount.set(0);
        fallbackCount.set(0);
    }
    @Test
    public void testRateLimitingAndDegradation() throws InterruptedException {
        log.info("准备测试...............");
        TimeUnit.SECONDS.sleep(5);
        log.info("开始测试...............");
        int numberOfThreads = 30; // Number of concurrent threads
        ExecutorService executorService = Executors.newFixedThreadPool(numberOfThreads);
        CountDownLatch latch = new CountDownLatch(numberOfThreads);
        for (int i = 0; i < numberOfThreads; i++) {
            executorService.submit(() -> {
                try {
                    String result = helloWorldService.sayHello("TestUser");
                    if (result.contains("blocked")) {
                        blockCount.incrementAndGet();
                    } else if (result.contains("failed")) {
                        fallbackCount.incrementAndGet();
                    } else {
                        successCount.incrementAndGet();
                    }
                } catch (Exception ex) {
                    System.out.println(ex.getMessage());
                    fallbackCount.incrementAndGet();
                } finally {
                    latch.countDown();
                }
            });
        }
        latch.await(); // Wait until all threads have completed
        log.info("Success count: " + successCount.get());
        log.info("Blocked count: " + blockCount.get());
        log.info("Fallback count: " + fallbackCount.get());
        // Assuming the flow rule is set to allow up to 20 requests per second
        assert successCount.get() <= 20 : "Number of successful requests should not exceed 20";
        assert blockCount.get() > 0 : "Some requests should be blocked due to rate limiting";
        assert fallbackCount.get() == 0 : "No requests should go to fallback under normal conditions";
        executorService.shutdown();
    }
}

启动程序

启动sentinel-dashboard

java -Dserver.port=8858 -Dcsp.sentinel.dashboard.server=localhost:8858 -Dproject.name=sentinel-dashboard -Dsentinel.dashboard.auth.username=sentinel -Dsentinel.dashboard.auth.password=123456  -Dserver.servlet.session.timeout=7200 -jar sentinel-dashboard-1.8.9.jar

启动Springboot

注意在启动时需要添加VM参数

-Dcsp.sentinel.dashboard.server=localhost:8858

界面展示

通过界面可以针对规则进行修改,保存后可以立即生效

附录

限流常见配置参数速查表

概念说明适用场景
QPS每秒查询率大促抢购、防止刷单
线程数并发占用线程数保护慢接口不占用所有线程池
快速失败超过阈值直接报错默认策略,简单粗暴
Warm Up预热模式,阈值缓慢升高系统刚启动,防止瞬间高流量压垮
排队等待请求匀速通过,处理不急的请求订单创建等需要削峰填谷的场景

Token Server Cluster 部署方式对比

特性嵌入模式 (Embedded)独立模式 (Alone)
部署成本 (利用现有资源) (需要独立机器)
隔离性差 (与业务争抢资源) (完全隔离)
适用场景中小规模集群、对成本敏感超大规模集群、核心中间件、全局限流
容灾能力依赖应用集群的稳定性 (通常有主从热备)
运维难度简单较复杂 (需维护 Server 集群)

与springboot及springcloud版本对应关系

Spring Boot 版本对应的 Spring Cloud Alibaba (SCA) 版本对应的 Sentinel 版本状态
2.3.x2.2.x.RELEASE1.7.x / 1.8.0旧版,已不再主推
2.4.x, 2.5.x, 2.6.x, 2.7.x2021.x (如 2021.0.5.0)1.8.5 / 1.8.6目前最主流的生产环境组合
3.0.x, 3.1.x2022.0.0.0 (or 2022.0.0.0-RC2)1.8.6 (有限支持)注意:原生 Web 适配器不支持,需用 Gateway 或特定适配

到此这篇关于Spring Boot4使用Sentinel进行限流的操作方法的文章就介绍到这了,更多相关springboot sentinel限流内容请搜索脚本之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持脚本之家!

您可能感兴趣的文章:
阅读全文