Sentinel

概述

  1. 官网:https://sentinelguard.io/zh-cn/
  2. GitHub:https://github.com/alibaba/Sentinel/wiki/主页

Sentinel的特征

  1. 丰富的应用场景:Sentinel 承接了阿里巴巴近 10 年的双十一大促流量的核心场景,例如:秒杀(即突发流量控制在系统容量可以承受的范围)、消息削峰填谷、集群流量控制、实时熔断下游不可用应用等。
  2. 完备的实时监控:Sentinel 同时提供实时的监控功能。您可以在控制台中看到接入应用的单台机器秒级数据,甚至 500 台以下规模的集群的汇总运行情况。
  3. 广泛的开源生态:Sentinel 提供开箱即用的与其它开源框架/库的整合模块,例如与 Spring Cloud、Apache Dubbo、gRPC、Quarkus 的整合。您只需要引入相应的依赖并进行简单的配置即可快速地接入 Sentinel。同时 Sentinel 提供 Java/Go/C++ 等多语言的原生实现。
  4. 完善的 SPI 扩展机制:Sentinel 提供简单易用、完善的 SPI 扩展接口。您可以通过实现扩展接口来快速地定制逻辑。例如定制规则管理、适配动态数据源等。
image.png

安装Sentinel

Sentinel 分为两个部分:

  1. 核心库(Java 客户端):不依赖任何框架/库,能够运行于所有 Java 运行时环境,同时对 Dubbo / Spring Cloud 等框架也有较好的支持。
  2. 控制台(Dashboard):基于 Spring Boot 开发,打包后可以直接运行,不需要额外的 Tomcat 等应用容器。

下载

下载Dashboard:https://github.com/alibaba/Sentinel/tags

image.png

启动

前提:JAVA环境正确、8080端口未被占用。

java -jar sentinel-dashboard-1.8.8.jar
image.png

访问Dashboard

  1. 访问地址:http://localhost:8080
  2. 账号、密码都是:sentinel
image.pngimage.png

整合Sentinel入门案例

前提

  1. 成功启动Nacos
  2. 成功启动Sentinel Dashboard

新建微服务8401

新建模块cloudalibaba-sentinel-service8401,作为微服务提供者被 Sentinel 管控。

新建模块

image.png

引入依赖

<?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">
    <modelVersion>4.0.0</modelVersion>
    <parent>
        <groupId>com.sun.cloud</groupId>
        <artifactId>cloud2025</artifactId>
        <version>1.0-SNAPSHOT</version>
    </parent>
 
    <artifactId>cloudalibaba-sentinel-service8401</artifactId>
 
    <properties>
        <maven.compiler.source>21</maven.compiler.source>
        <maven.compiler.target>21</maven.compiler.target>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    </properties>
 
    <dependencies>
        <!--SpringCloud alibaba sentinel -->
        <dependency>
            <groupId>com.alibaba.cloud</groupId>
            <artifactId>spring-cloud-starter-alibaba-sentinel</artifactId>
        </dependency>
        <!--nacos-discovery-->
        <dependency>
            <groupId>com.alibaba.cloud</groupId>
            <artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId>
        </dependency>
        <!-- 引入自己定义的api通用包 -->
        <dependency>
            <groupId>com.sun.cloud</groupId>
            <artifactId>cloud-api-commons</artifactId>
            <version>1.0-SNAPSHOT</version>
        </dependency>
        <!--SpringBoot通用依赖模块-->
        <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>
        <!--hutool-->
        <dependency>
            <groupId>cn.hutool</groupId>
            <artifactId>hutool-all</artifactId>
        </dependency>
        <!--lombok-->
        <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
            <scope>provided</scope>
        </dependency>
        <!--test-->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
    </dependencies>
 
    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>
 
</project>

修改yaml

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

主启动类

package com.sun.cloud;
 
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
 
@EnableDiscoveryClient
@SpringBootApplication
public class Main8401 {
    public static void main(String[] args) {
        SpringApplication.run(Main8401.class, args);
    }
}

业务类

创建FlowLimitController,用于接口测试。

package com.sun.cloud.controller;
 
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
 
@RestController
public class FlowLimitController {
 
    @GetMapping("/testA")
    public String testA() {
        return "------testA";
    }
 
    @GetMapping("/testB")
    public String testB() {
        return "------testB";
    }
}

测试接口

image.png

查看sentienl控制台

  1. 启动8401服务后,查看sentienl控制台。
  2. 注意:Sentinel采用的懒加载,想对某个接口进行限流和降级等操作,一定要先访问一下接口,使Sentinel检测出相应的接口。
image.pngimage.png

流控规则

Sentinel能够对流量进行控制,主要是监控应用的QPS流量或者并发线程数等指标,如果达到指定的阈值时,就会对流量进行控制,以避免服务被瞬时的高并发流量击垮,保证服务的高可靠性。

image.png

资源名

资源的唯一名称,默认就是请求的接口路径,可以自行修改,但是要保证唯一。

针对来源

具体针对某个微服务进行限流,默认值为default,表示不区分来源,全部限流。

阈值类型

QPS表示通过QPS进行限流,并发线程数表示通过并发线程数限流。

单机阈值

与阈值类型组合使用。如果阈值类型选择的是QPS,表示当调用接口的QPS达到阈值时,进行限流操作。如果阈值类型选择的是并发线程数,则表示当调用接口的并发线程数达到阈值时,进行限流操作。

是否集群

选中则表示集群环境,不选中则表示非集群环境。

流控模式

直接

  • 默认的流控模式,当接口达到限流条件时,直接开启限流功能。
  • 下面的设置表示:1秒钟内查询1次就是OK,若超过次数1,就直接-快速失败,报默认错误。
image.png

1秒内访问多次,则快速失败。

image.png

关联

  • 当关联的资源达到阈值时,就限流自己。
  • 下面的配置表示:当关联资源/testB的QPS阀值超过1时,就限流/testA的Rest访问地址,当关联资源到阈值后限制配置好的资源名,B惹事,A挂了。
image.png

使用Apifox对http://localhost:8401/testB进行压测,然后对http://localhost:8401/testA进行访问,始终不能成功。

image.pngimage.png

相关文章

评论区