天天看點

阿裡巴巴開源的限流器 Sentinel,輕松實作接口限流!

在日常開發中,限流功能時常被使用,用于對某些接口進行限流熔斷,譬如限制機關時間内接口通路次數;或者按照某種規則進行限流,如限制ip的機關時間通路次數等。

之前我們已經講過接口限流的工具類ratelimter可以實作令牌桶的限流,很明顯sentinel的功能更為全面和完善。來看一下sentinel的簡介:

Sentinel 介紹

随着微服務的流行,服務和服務之間的穩定性變得越來越重要。 Sentinel 以流量為切入點,從流量控制、熔斷降級、系統負載保護等多個次元保護服務的穩定性。

Sentinel 具有以下特征:

豐富的應用場景: Sentinel 承接了阿裡巴巴近 10 年的雙十一大促流量的核心場景,例如秒殺(即突發流量控制在系統容量可以承受的範圍)、消息削峰填谷、實時熔斷下遊不可用應用等。

完備的實時監控: Sentinel 同時提供實時的監控功能。您可以在控制台中看到接入應用的單台機器秒級資料,甚至 500 台以下規模的叢集的彙總運作情況。

廣泛的開源生态: Sentinel 提供開箱即用的與其它開源架構/庫的整合子產品,例如與 Spring Cloud、Dubbo、gRPC 的整合。您隻需要引入相應的依賴并進行簡單的配置即可快速地接入 Sentinel。

完善的 SPI 擴充點: Sentinel 提供簡單易用、完善的 SPI 擴充點。您可以通過實作擴充點,快速的定制邏輯。例如定制規則管理、适配資料源等。

來簡單使用一下Sentinel。

Sentinel包括服務端和用戶端,服務端有可視化界面,用戶端需引入jar後即可和服務端通信并完成限流功能。

啟動服務端的jar

https://github.com/alibaba/Sentinel/releases

在這個位址,下載下傳release的jar,然後啟動即可。

這個jar是個标準的Springboot應用,可以通過

java -jar sentinel-dashboard-1.6.0.jar

來啟動,這樣就是預設的設定,啟動在8080端口。

也可以加上一些自定義配置來啟動:

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

具體配置的解釋,可以到 GitHub 上看一下文檔。

不懂 Spring Boot 的看下這個,很全了:

https://github.com/javastacks/spring-boot-best-practice 這裡我們直接使用預設 java -jar sentinel-dashboard-1.6.0.jar 來啟動,之後通路localhost:8080。可以看到界面:
阿裡巴巴開源的限流器 Sentinel,輕松實作接口限流!
輸入賬号密碼sentinel後進入主界面
阿裡巴巴開源的限流器 Sentinel,輕松實作接口限流!
此時因為我們并沒有啟動用戶端,是以界面是空的。

啟動用戶端

建立一個Springboot項目,pom如下:

<?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>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.0.5.RELEASE</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>

    <groupId>com.maimeng.baobanq</groupId>
    <artifactId>baobanserver</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <packaging>jar</packaging>
    <name>baobanserver</name>
    <description>Demo project for Spring Boot</description>

    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
        <java.version>1.8</java.version>
        <spring-cloud.version>Finchley.SR1</spring-cloud.version>
    </properties>

    <dependencies>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>

        <!--sentinel-->
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-alibaba-sentinel</artifactId>
        </dependency>
        <!--sentinel end-->

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
    </dependencies>

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

            <dependency>
                <groupId>org.springframework.cloud</groupId>
                <artifactId>spring-cloud-alibaba-dependencies</artifactId>
                <version>0.2.2.RELEASE</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>      

需要注意引用的SpringCloud-alibaba的版本是0.2.2,目前的最新版,如果是Springboot2.x的項目,需要引0.2.x的。Springboot1.x的引0.1.x的。

Sentinel的用戶端依賴也很簡單,spring-cloud-starter-alibaba-sentinel加這一個引用即可。

之後在application.yml裡添加server的位址配置:

阿裡巴巴開源的限流器 Sentinel,輕松實作接口限流!

就是一個普通的controller接口。

之後啟動該項目。啟動後回到server的控制台界面

阿裡巴巴開源的限流器 Sentinel,輕松實作接口限流!
阿裡巴巴開源的限流器 Sentinel,輕松實作接口限流!
阿裡巴巴開源的限流器 Sentinel,輕松實作接口限流!

因為Sentinel采用延遲加載,隻有在主動發起一次請求後,才會被攔截并發送給服務端。如果想關閉這個延遲,就在上面的yml裡把eager的注釋放掉。

然後在簇點鍊路裡hello接口的流控那裡設定限流規則,将單機門檻值設為1.就代表一秒内最多隻能通過1次請求到達該hello接口。

阿裡巴巴開源的限流器 Sentinel,輕松實作接口限流!

之後再次連續通路hello接口。

阿裡巴巴開源的限流器 Sentinel,輕松實作接口限流!

發現已經被攔截了,限流已經生效。

這樣就完成了一次簡單的限流操作,并且能看到各接口的QPS的統計。

後續我們來研究叢集的限流、降級等功能。

版權聲明:本文為部落客原創文章,遵循 CC 4.0 BY-SA 版權協定,轉載請附上原文出處連結和本聲明。 本文連結: