Sentinel限流资料入门详解

2024/9/26 23:03:25

本文主要是介绍Sentinel限流资料入门详解,对大家解决编程问题具有一定的参考价值,需要的程序猿们随着小编来一起学习吧!

概述

本文详细介绍了Sentinel限流资料,包括Sentinel的基本概念、主要功能和应用场景。文中还提供了Sentinel的安装、配置教程以及限流规则的详细解释和实战案例,帮助读者全面了解和应用Sentinel限流功能。

Sentinel简介

Sentinel是什么

Sentinel 是阿里巴巴开源的一款高可用的流量控制组件,旨在以最小代价地防护应用免受瞬时流量洪峰与公共故障的影响。Sentinel 通过将流量控制、系统保护、降级规则等纳入到统一的管理当中,支持丰富的应用场景,让系统变得更为稳定、可靠。

Sentinel 主要功能包括流量控制、系统保护和降级等。它的目标是提供一套简单易用的流量控制方案,保护应用免受瞬时流量洪峰的冲击,同时保证系统的稳定运行。

Sentinel的应用场景

电商场景

在电商场景中,例如双11或618大促活动中,访问量可能会突然激增,需要通过限流来保护系统,防止流量洪峰导致服务崩溃。具体实现可以通过以下代码示例进行:

import com.alibaba.csp.sentinel.annotation.SentinelResource;
import com.alibaba.csp.sentinel.slots.block.BlockException;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class OrderController {

    @GetMapping("/order/create")
    @SentinelResource(value = "order-create", blockHandler = "orderCreateBlockHandler")
    public String createOrder() {
        return "Order Created Successfully";
    }

    public String orderCreateBlockHandler(BlockException ex) {
        return "Order Creation Rate Limit Exceeded";
    }
}

微服务架构

微服务架构下,每个服务可能都有多种接口,这些接口的访问量可能会出现不均匀的情况。通过Sentinel可以对不同的接口进行灵活的限流策略配置,保障服务的稳定运行。例如,可以通过以下代码为每个微服务接口设置不同的限流策略:

import com.alibaba.csp.sentinel.annotation.SentinelResource;
import com.alibaba.csp.sentinel.slots.block.BlockException;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class MicroserviceController {

    @GetMapping("/microservice1")
    @SentinelResource(value = "microservice1", blockHandler = "microservice1BlockHandler")
    public String microservice1() {
        return "Service 1 Running";
    }

    public String microservice1BlockHandler(BlockException ex) {
        return "Microservice 1 Rate Limit Exceeded";
    }

    @GetMapping("/microservice2")
    @SentinelResource(value = "microservice2", blockHandler = "microservice2BlockHandler")
    public String microservice2() {
        return "Service 2 Running";
    }

    public String microservice2BlockHandler(BlockException ex) {
        return "Microservice 2 Rate Limit Exceeded";
    }
}

API接口

在API接口调用中,无论是公网开放的接口还是内部服务之间的接口调用,都可能需要做限流处理,防止恶意攻击或者流量不均衡引起的系统压力过大。例如:

import com.alibaba.csp.sentinel.annotation.SentinelResource;
import com.alibaba.csp.sentinel.slots.block.BlockException;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class ApiController {

    @GetMapping("/api/public")
    @SentinelResource(value = "publicApi", blockHandler = "publicApiBlockHandler")
    public String publicApi() {
        return "Public API Running";
    }

    public String publicApiBlockHandler(BlockException ex) {
        return "Public API Rate Limit Exceeded";
    }

    @GetMapping("/api/internal")
    @SentinelResource(value = "internalApi", blockHandler = "internalApiBlockHandler")
    public String internalApi() {
        return "Internal API Running";
    }

    public String internalApiBlockHandler(BlockException ex) {
        return "Internal API Rate Limit Exceeded";
    }
}

数据库操作

对于写操作密集的应用场景,数据库是瓶颈之一。为了避免数据库过载,可以通过Sentinel对数据库操作进行流控,保护数据库稳定运行。例如:

import com.alibaba.csp.sentinel.annotation.SentinelResource;
import com.alibaba.csp.sentinel.slots.block.BlockException;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class DatabaseController {

    @GetMapping("/database/write")
    @SentinelResource(value = "databaseWrite", blockHandler = "databaseWriteBlockHandler")
    public String databaseWrite() {
        return "Database Write Running";
    }

    public String databaseWriteBlockHandler(BlockException ex) {
        return "Database Write Rate Limit Exceeded";
    }
}
限流基本概念

什么是限流

限流是一种常见的流量控制手段,目的是限制通过系统的流量,防止系统被过高的请求压垮。限流策略可以基于多个维度,比如每秒通过的请求数(QPS)、最大并发数、请求访问频率等。

限流的作用和意义

限流的主要作用是在系统出现流量洪峰或流量不均衡时,保护系统不受过大压力的影响,保障系统正常运行。限流的意义在于,它能够帮助应用系统抵御恶意攻击或者因流量激增而导致的服务不可用。

限流的常见策略介绍

QPS限流

QPS(Queries Per Second)代表每秒查询数,用于限制每秒钟通过系统请求的数量。QPS 限流是一种常见的限流策略,适用于大多数应用场景。例如:

import com.alibaba.csp.sentinel.annotation.SentinelResource;
import com.alibaba.csp.sentinel.slots.block.BlockException;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class QpsController {

    @GetMapping("/qps")
    @SentinelResource(value = "qps", blockHandler = "qpsBlockHandler")
    public String qps() {
        return "Hello, QPS";
    }

    public String qpsBlockHandler(BlockException ex) {
        return "QPS Limit Exceeded";
    }
}

并发数限流

并发数限流用于限制某个资源在同一时刻被访问的最大请求数量。这种策略适用于那些对并发负载敏感的场景。例如:

import com.alibaba.csp.sentinel.annotation.SentinelResource;
import com.alibaba.csp.sentinel.slots.block.BlockException;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class ConcurrentController {

    @GetMapping("/concurrent")
    @SentinelResource(value = "concurrent", blockHandler = "concurrentBlockHandler")
    public String concurrent() {
        return "Hello, Concurrent";
    }

    public String concurrentBlockHandler(BlockException ex) {
        return "Concurrent Limit Exceeded";
    }
}

累积式限流

累积式限流基于一段时间内的总请求量进行限流,适用于那些对一段时间内请求总量有严格控制的场景。例如:

import com.alibaba.csp.sentinel.annotation.SentinelResource;
import com.alibaba.csp.sentinel.slots.block.BlockException;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class CumulativeController {

    @GetMapping("/cumulative")
    @SentinelResource(value = "cumulative", blockHandler = "cumulativeBlockHandler")
    public String cumulative() {
        return "Hello, Cumulative";
    }

    public String cumulativeBlockHandler(BlockException ex) {
        return "Cumulative Limit Exceeded";
    }
}
Sentinel限流配置教程

如何安装Sentinel

首先,通过 Maven 或 Gradle 依赖管理工具将 Sentinel 添加到项目中。

<!-- Maven -->
<dependency>
    <groupId>com.alibaba.csp</groupId>
    <artifactId>sentinel-core</artifactId>
    <version>1.8.3</version>
</dependency>

<dependency>
    <groupId>com.alibaba.csp</groupId>
    <artifactId>sentinel-spring-boot-starter</artifactId>
    <version>1.8.3</version>
</dependency>
// Gradle
implementation 'com.alibaba.csp:sentinel-core:1.8.3'
implementation 'com.alibaba.csp:sentinel-spring-boot-starter:1.8.3'

如何在Spring Boot项目中集成Sentinel

在Spring Boot 项目中集成 Sentinel,需要在 application.propertiesapplication.yml 文件中配置 Sentinel 相关参数。

# application.properties
spring.application.name=sentinel-demo
server.port=8080

在启动类上添加 @EnableSentinel 注解,启用 Sentinel。

import com.alibaba.csp.sentinel.annotation.SentinelResource;
import com.alibaba.csp.sentinel.slots.block.BlockException;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

@SpringBootApplication
@EnableSentinel // 启用 Sentinel
public class SentinelDemoApplication {
    public static void main(String[] args) {
        SpringApplication.run(SentinelDemoApplication.class, args);
    }
}

Sentinel的限流规则配置方法

Sentinel 的限流规则可以通过 API 或者控制台配置。

通过 API 配置

可以通过 FlowRuleManager 类的方法批量加载或修改限流规则。

import com.alibaba.csp.sentinel.slots.block.flow.FlowRule;
import com.alibaba.csp.sentinel.slots.block.flow.FlowRuleManager;

public class FlowRuleConfig {
    public static void main(String[] args) {
        FlowRule rule = new FlowRule();
        rule.setResource("hello");
        rule.setCount(20);
        rule.setGrade(FlowRuleConstant.FLOW_GRADE_QPS);
        rule.setLimitApp("system");
        rule.setStrategy(FlowRuleConstant.STATEGY_ALL);
        FlowRuleManager.loadRules(Collections.singletonList(rule));
    }
}

通过控制台配置

Sentinel 提供了一个管理控制台,可以通过控制台界面来配置限流规则。控制台下载地址:https://github.com/alibaba/Sentinel/releases

Sentinel限流规则详解

流量控制规则

流量控制规则用于限制进入系统的流量,主要包括 QPS 限流、并发数限流和累积式限流三种类型。

  • QPS 限流:限制每秒钟通过系统的请求数量。
  • 并发数限流:限制某个资源在同一时刻被访问的最大请求数量。
  • 累积式限流:基于一段时间内的总请求量进行限流。

QPS 限流示例

import com.alibaba.csp.sentinel.annotation.SentinelResource;
import com.alibaba.csp.sentinel.slots.block.BlockException;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class QpsController {

    @GetMapping("/qps")
    @SentinelResource(value = "qps", blockHandler = "qpsBlockHandler")
    public String qps() {
        return "Hello, QPS";
    }

    public String qpsBlockHandler(BlockException ex) {
        return "QPS Limit Exceeded";
    }
}

并发数限流示例

import com.alibaba.csp.sentinel.annotation.SentinelResource;
import com.alibaba.csp.sentinel.slots.block.BlockException;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class ConcurrentController {

    @GetMapping("/concurrent")
    @SentinelResource(value = "concurrent", blockHandler = "concurrentBlockHandler")
    public String concurrent() {
        return "Hello, Concurrent";
    }

    public String concurrentBlockHandler(BlockException ex) {
        return "Concurrent Limit Exceeded";
    }
}

累积式限流示例

import com.alibaba.csp.sentinel.annotation.SentinelResource;
import com.alibaba.csp.sentinel.slots.block.BlockException;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class CumulativeController {

    @GetMapping("/cumulative")
    @SentinelResource(value = "cumulative", blockHandler = "cumulativeBlockHandler")
    public String cumulative() {
        return "Hello, Cumulative";
    }

    public String cumulativeBlockHandler(BlockException ex) {
        return "Cumulative Limit Exceeded";
    }
}

资源准备规则

资源准备规则用于定义哪些资源需要进行限流,每条资源准备规则都会有一个资源名,通过这个名称来识别和匹配。

资源准备规则示例

import com.alibaba.csp.sentinel.annotation.SentinelResource;
import com.alibaba.csp.sentinel.slots.block.BlockException;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class ResourceController {

    @GetMapping("/resource")
    @SentinelResource(value = "myResource", blockHandler = "resourceBlockHandler")
    public String resource() {
        return "Hello, Resource";
    }

    public String resourceBlockHandler(String args, BlockException ex) {
        return "Resource Limit Exceeded";
    }
}

系统保护规则

系统保护规则用于监控 CPU 使用率、线程数、平均响应时间等指标,当这些指标超出预设阈值时,Sentinel 会自动触发流量控制,防止系统过载。例如:

import com.alibaba.csp.sentinel.annotation.SentinelResource;
import com.alibaba.csp.sentinel.slots.block.BlockException;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class SystemProtectionController {

    @GetMapping("/system")
    @SentinelResource(value = "system")
    public String system() {
        return "Hello, System";
    }
}
Sentinel限流实战案例

实战场景模拟

假设我们有一个电商应用,其中有一个重要的接口 /order/create,在大促期间可能会有大量请求涌入。我们需要对这个接口进行 QPS 限流,确保其每秒最多只能被调用20次。

代码示例

import com.alibaba.csp.sentinel.annotation.SentinelResource;
import com.alibaba.csp.sentinel.slots.block.BlockException;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class OrderController {

    @GetMapping("/order/create")
    @SentinelResource(value = "order-create", blockHandler = "orderCreateBlockHandler")
    public String createOrder() {
        return "Order Created Successfully";
    }

    public String orderCreateBlockHandler(BlockException ex) {
        return "Order Creation Rate Limit Exceeded";
    }
}

限流效果观察

/order/create 接口的访问量超过每秒20次时,Sentinel 会触发限流,返回 "Order Creation Rate Limit Exceeded"。可以通过增加压测工具模拟大促流量,观察限流效果和系统的稳定表现。

调整限流参数优化效果

根据实际业务需求和系统性能,对限流参数进行调整。例如,将每秒请求限制从20次增加到30次,观察系统在高并发情况下的性能表现。

代码示例

import com.alibaba.csp.sentinel.annotation.SentinelResource;
import com.alibaba.csp.sentinel.slots.block.BlockException;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class OrderController {

    @GetMapping("/order/create")
    @SentinelResource(value = "order-create", blockHandler = "orderCreateBlockHandler")
    public String createOrder() {
        return "Order Created Successfully";
    }

    public String orderCreateBlockHandler(BlockException ex) {
        return "Order Creation Rate Limit Exceeded";
    }
}
常见问题与解答

Sentinel限流过程中容易遇到的问题

  1. 限流策略配置不当:如果限流策略配置不合理,可能导致系统性能下降。
  2. 流量洪峰期间系统响应缓慢:在流量洪峰期间,系统响应时间可能会变长。
  3. 限流规则更新慢:如果限流规则更新不及时,可能会导致系统在流量洪峰期间无法有效保护自身。

解决方案和建议

  1. 合理配置限流策略:根据实际情况灵活配置限流策略。
  2. 增加资源预留:在流量洪峰期间,增加一定的资源预留,以应对突发流量。
  3. 实时监控和调整:实时监控系统性能指标,并根据需要调整限流规则。

常见配置误区及注意事项

  1. 过分依赖限流:过分依赖限流可能会导致系统过于保守,影响用户体验。
  2. 忽略系统性能:忽略系统性能可能会导致限流策略失效。
  3. 不合理的资源预留:资源预留过多或过少都会影响系统的稳定性和性能。

Sentinel 是一个功能强大且灵活的流量控制组件,通过合理的配置和使用,可以有效保护应用系统免受流量洪峰的影响,确保系统的稳定运行。



这篇关于Sentinel限流资料入门详解的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!


扫一扫关注最新编程教程