天天看點

Spring Cloud Alibaba(四):Spring Cloud 使用 Sentinel 實作限流

本文為 Spring Cloud Alibaba 系列 的第四篇文章,系列文章持續更新,點此 檢視更多

本示例代碼基于之前的 m01 子產品代碼改造,項目啟動依賴同 m01 子產品,讀者可以閱讀此博文 《使用 Nacos + Dubbo 實作的遠端服務調用》 了解 m01 子產品代碼功能。

本示例完整代碼 點此檢視;

Sentinel 介紹

Sentinel 是面向分布式服務架構的高可用流量防護元件,随着微服務的流行,服務和服務之間的穩定性變得越來越重要。 Sentinel 以流量為切入點,從流量控制、熔斷降級、系統負載保護等多個次元保護服務的穩定性。

Sentinel 具有以下特征:

  • 豐富的應用場景: Sentinel 承接了阿裡巴巴近 10 年的雙十一大促流量的核心場景,例如秒殺(即突發流量控制在系統容量可以承受的範圍)、消息削峰填谷、實時熔斷下遊不可用應用等。
  • 完備的實時監控: Sentinel 同時提供實時的監控功能。您可以在控制台中看到接入應用的單台機器秒級資料,甚至 500 台以下規模的叢集的彙總運作情況。
  • 廣泛的開源生态: Sentinel 提供開箱即用的與其它開源架構/庫的整合子產品,例如與 Spring Cloud、Dubbo、gRPC 的整合。您隻需要引入相應的依賴并進行簡單的配置即可快速地接入 Sentinel。
  • 完善的 SPI 擴充點: Sentinel 提供簡單易用、完善的 SPI 擴充點。您可以通過實作擴充點,快速的定制邏輯。例如定制規則管理、适配資料源等。

本文将介紹如何在 Spring Cloud 項目中使用 Sentinel 進行限流。

Sentinel 控制台安裝與啟動

擷取控制台

可以從 release 頁面 下載下傳最新版本的控制台 jar 包。

也可以從最新版本的源碼自行建構 Sentinel 控制台:

  • 下載下傳 控制台 工程
  • 使用以下指令将代碼打包成一個 fat jar:

    mvn clean package

啟動控制台

Sentinel 控制台是一個标準的 Spring Boot 應用,以 Spring Boot 的方式運作 jar 包即可,本例中筆者下載下傳的為

sentinel-dashboard-1.7.1

,啟動指令如下:

java -Dserver.port=8019 -Dcsp.sentinel.dashboard.server=localhost:8019 -Dproject.name=sentinel-dashboard -jar sentinel-dashboard-1.7.1.jar
           

若啟動時端口沖突,可使用

-Dserver.port=新端口

指定使用其他端口。

此處筆者使用 8019 端口啟動 Sentinel 控制台,通路 http://localhost:8019 ,使用者及密碼預設都為

sentinel

,登入成功進入如下界面:

Spring Cloud Alibaba(四):Spring Cloud 使用 Sentinel 實作限流

Spring Cloud 內建 Sentinel

建立項目

複制之前 m01 子產品中的代碼,修各子產品名稱及相應依賴子產品,如下:

修改前子產品名 修改後子產品名
m01 m03
m01-nacos-dubbo-api m03-nacos-dubbo-sentinel-api
m01-nacos-dubbo-consumer m03-nacos-dubbo-sentinel-consumer
m01-nacos-dubbo-provider m03-nacos-dubbo-sentinel-provider

添加依賴

m03/pom.xml

<properties>

節點下添加屬性表示使用的 sentinel 版本

<properties>
    ......
    <!-- 指定使用的 sentinel 版本 -->
    <sentinel.version>1.7.1</sentinel.version>
    ......
</properties>
           
<!-- spring-cloud sentinel 支援 -->
<dependency>
    <groupId>com.alibaba.cloud</groupId>
    <artifactId>spring-cloud-starter-alibaba-sentinel</artifactId>
    <version>${spring-cloud-alibaba.version}</version>
</dependency>

<!-- Dubbo 內建 Sentinel -->
<dependency>
    <groupId>com.alibaba.csp</groupId>
    <artifactId>sentinel-apache-dubbo-adapter</artifactId>
    <version>${sentinel.version}</version>
</dependency>
           

m03-nacos-dubbo-sentinel-provider/pom.xml

<!-- spring-cloud sentinel 支援 -->
<dependency>
    <groupId>com.alibaba.cloud</groupId>
    <artifactId>spring-cloud-starter-alibaba-sentinel</artifactId>
</dependency>

<!-- Dubbo 內建 Sentinel -->
<dependency>
    <groupId>com.alibaba.csp</groupId>
    <artifactId>sentinel-apache-dubbo-adapter</artifactId>
</dependency>
           

說明

  • 添加

    spring-cloud-starter-alibaba-sentinel

    依賴後,會将項目所有的 web 接口定義為資源,可在 sentinel 控制台的

    簇點鍊路

    中對資源進行流控等操作;
  • 添加

    sentinel-apache-dubbo-adapter

    依賴後,會将目前服務提供的 Dubbo 接口實作方法定義為資源,可在 sentinel 控制台的

    簇點鍊路

    中對資源進行流控等操作,若讀者不需要對 Dubbo 服務實作接口進行流控,可不添加此依賴;
  • 讀者亦可通過

    @SentinelResource

    注解将指定的方法定義為資源,注意該方法不能為 private 修飾,若有興趣了解此注解的詳細用法,可參閱 @SentinelResource 注解

指定 Sentinel 控制台資訊

m03-nacos-dubbo-sentinel-provider

下的

application.yml

中添加配置指定

Sentinel

控制台位址:

spring:
  cloud:
    sentinel:
      transport:
        dashboard: localhost:8019
           

此配置值請讀者根據自己 sentinel-dashboard 服務的 IP 和端口位址進行修改;

啟動項目

依次啟動

m03-nacos-dubbo-sentinel-provider

m03-nacos-dubbo-sentinel-consumer

子產品。

通路 consumer 得到類似如下結果證明項目啟動成功:

添加流控規則

登入 Sentinel 控制台,在

m03-nacos-dubbo-sentinel-provider

>

簇點鍊路

,如下圖:

Spring Cloud Alibaba(四):Spring Cloud 使用 Sentinel 實作限流

上圖紅框标注的資源便是本示例中的 Dubbo 服務實作接口,點選

流控

按鈕添加流控規則

Spring Cloud Alibaba(四):Spring Cloud 使用 Sentinel 實作限流

為了友善看到限流效果,此處筆者将 單機門檻值 設定為 1,門檻值類型 為 QPS,表示每秒隻允許一個請求,超過的請求直接傳回失敗,流控規則可在

流控規則

清單中進行檢視與管理

Spring Cloud Alibaba(四):Spring Cloud 使用 Sentinel 實作限流

測試規則

通過浏覽器通路如下位址:

http://localhost:8021/hello?name=Shawearn
           

未超過門檻值時傳回結果如下:

超過門檻值時通路便會被快速失敗掉:

Spring Cloud Alibaba(四):Spring Cloud 使用 Sentinel 實作限流

結束語

至此,Spring Cloud 使用 Sentinel 實作限流便完成了,然而本示例畢竟隻是一個最簡單的入門示例,使用的也隻是 sentinel 的部分功能。例如規則持久化、動态資料源、異常處理等功能都還未涉及。本示例通過 sentinel-dashboard 配置規則雖然友善,但配置的規則并沒有被持久化,一旦項目重新開機,配置的規則就丢失了,這邊需要修改 sentinel-dashboard 中規則存儲的邏輯使規則持久化,這部分内容筆者會在後面逐一介紹。

繼續閱讀