Spring Cloud Alibaba-限流组件Sentinel认识并使用Sentinel实现接口限流

echo编辑整理,欢迎转载,转载请声明文章来源。欢迎添加echo微信(微信号:t2421499075) 交流学习。


Sentinel简介

说到限流,很多人可能熟悉Hystrix,但是比较可惜的是Netflix已经宣布对Hystrix停止更新。Sentinel就是一个限流组件,官方标题是:分布式系统的流量防卫兵,是面向于云原生微服务的高可用流控防护组件,主要以流量为切入点,从流量控制、熔断降级、系统自适应保护等多个维度来帮助用户保障微服务的稳定性。它和Hystrix是一个类似的东西,相比之下Sentinel功能更加的全面,而且由于Hystrix停止更新,所以已经在限流方面成为主流的组件。

Sentinel只能做限流吗?不是,我们后面会继续讲Sentinel的其他功能,这里从限流开始入手

Sentinel 具有以下特征

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

使用Sentinel实现接口限流

Sentinel分为两部分,服务端和客户端,服务端有可视化界面,客户端引入jar包后即可和服务端通信,和我们的nacos的这种使用的结构很类似。那我们要使用Sentinel,要先下载这个服务端的jar包并启动

  • 服务端的下载地址:https://github.com/alibaba/Sentinel/releases 在这里插入图片描述

最新的cloudalibaba要使用最新的sentinel版本

  • 启动命令:
java -jar sentinel-dashboard-1.8.0.jar

- 也可以使用以下命令做一些设置,特别是端口,微服务很可能需要很多端口
java -Dserver.port=10083 -Dcsp.sentinel.dashboard.server=localhost:10083 -Dproject.name=sentinel-dashboard-1.8.0.jar sentinel-dashboard.jar

出现如下日志,就证明启动成功了,我们就可以访问localhost:10083 在这里插入图片描述

访问界面如下: 在这里插入图片描述

  • 登录默认账密:sentinel sentinel

进入之后界面如下: 在这里插入图片描述

到这里我们的服务端准备就已经完成了,我们可以开始在我们的项目里面集成我们的Sentinel了

这里我们要创建一个客户端,然后集成sentinel,集成只需要在pom文件里面加入spring-cloud-starter-alibaba-sentinel即可,但是版本问题很多,如果不知道哪些版本可以兼容,直接使用我下面的xml配置即可

<?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 https://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.3.4.RELEASE</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>
    <groupId>com.echo</groupId>
    <artifactId>sentinel</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>sentinel</name>
    <description>Demo project for Spring Boot</description>

    <properties>
        <java.version>1.8</java.version>
        <spring-cloud-alibaba.version>2.2.3.RELEASE</spring-cloud-alibaba.version>
        <spring-cloud.version>Hoxton.SR1</spring-cloud.version>
    </properties>

    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        <dependency>
            <groupId>com.alibaba.cloud</groupId>
            <artifactId>spring-cloud-starter-alibaba-nacos-config</artifactId>
        </dependency>
        <dependency>
            <groupId>com.alibaba.cloud</groupId>
            <artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId>
        </dependency>
        <dependency>
            <groupId>com.alibaba.cloud</groupId>
            <artifactId>spring-cloud-starter-alibaba-sentinel</artifactId>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
            <exclusions>
                <exclusion>
                    <groupId>org.junit.vintage</groupId>
                    <artifactId>junit-vintage-engine</artifactId>
                </exclusion>
            </exclusions>
        </dependency>
    </dependencies>

    <dependencyManagement>
        <dependencies>
            <dependency>
                <groupId>com.alibaba.cloud</groupId>
                <artifactId>spring-cloud-alibaba-dependencies</artifactId>
                <version>${spring-cloud-alibaba.version}</version>
                <type>pom</type>
                <scope>import</scope>
            </dependency>
            <dependency>
                <groupId>org.springframework.cloud</groupId>
                <artifactId>spring-cloud-dependencies</artifactId>
                <version>${spring-cloud.version}</version>
                <type>pom</type>
                <scope>import</scope>
            </dependency>
        </dependencies>
    </dependencyManagement>

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>

</project>
  • 这个时候集成完成,我们在添加这个依赖的类里面添加一个test接口

在这里插入图片描述

注意:启动成功之后控制台并不会直接就有显示,要先访问一下我们建好的项目里面的接口,然后就可以看到sentinel的控制台里面有了我们创建的项目

在这里插入图片描述

我们看到的界面就是实时监控的界面,这个界面显示的就是我们的访问数据

设置流控,完成限流

直接打开簇点链路,然后找到我们的测试接口,点击流控。然后我们就可以新增一个流控规则

在这里插入图片描述

在这里插入图片描述

这里设置了一个QPS值为1的流控,我们可以连续访问接口来观看我们的接口返回有什么变化

在这里插入图片描述

很明显设置了流控的测试接口,如果QPS超过了阈值,肯定就直接给拒绝了访问。而实时监控也给我们显示了一样的图 在这里插入图片描述

这里不仅统计了总共的QPS,同时还给我们分了通过的QPS和拒绝的QPS

总结

  • Sentinel的整合比较简单
  • 关键是注意版本兼容
  • 控制台需要访问接口之后才能看到流控的规则界面
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章