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的整合比較簡單
  • 關鍵是注意版本兼容
  • 控制檯需要訪問接口之後才能看到流控的規則界面
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章