1. 程式人生 > 其它 >alibaba sentinel簡單實踐

alibaba sentinel簡單實踐

alibaba sentinel是一套流控元件,有限流、熔斷、降級等功能。由一個管理端dashboard和放入需要使用的應用的攔截元件組成。前者是個jar包、啟起來就行了,後者可以通過starter方便的整合到springboot應用裡。dashboard呼叫應用開的一個埠(8720)傳送限流配置給攔截元件,攔截元件通過dashboard的埠上報應用的流量情況。是雙向的一個通訊。
1、從官網下載sentinel-dashboard-1.8.2.jar,然後啟動dashboard管理端:java -jar -Dserver.port=9100 sentinel-dashboard-1.8.2.jar
2、用idea建立一個springboot專案並整合sentinel真是方便,配置好建立嚮導start.aliyun.com,勾選spring web和alibaba sentinel兩個依賴就行了。不用自己費心去搞pom.xml檔案和各種依賴的搭配。
pom.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>
    <groupId>com.wangan</groupId>
    <artifactId>springbootone</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>springbootone</name>
    <description>springbootone</description>

    <properties>
        <java.version>1.8</java.version>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
        <spring-boot.version>2.1.17.RELEASE</spring-boot.version>
        <spring-cloud-alibaba.version>2.1.2.RELEASE</spring-cloud-alibaba.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-sentinel</artifactId>
        </dependency>

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

    <dependencyManagement>
        <dependencies>
            <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-dependencies</artifactId>
                <version>${spring-boot.version}</version>
                <type>pom</type>
                <scope>import</scope>
            </dependency>
            <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>
        </dependencies>
    </dependencyManagement>

    <build>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <version>3.8.1</version>
                <configuration>
                    <source>1.8</source>
                    <target>1.8</target>
                    <encoding>UTF-8</encoding>
                </configuration>
            </plugin>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
                <version>2.1.17.RELEASE</version>
                <configuration>
                    <mainClass>com.wangan.springbootone.SpringbootoneApplication</mainClass>
                </configuration>
                <executions>
                    <execution>
                        <id>repackage</id>
                        <goals>
                            <goal>repackage</goal>
                        </goals>
                    </execution>
                </executions>
            </plugin>
        </plugins>
    </build>

</project>

連application.properties檔案都給自動生成了一份:

# 應用名稱
spring.application.name=springbootone
# Sentinel 控制檯地址
spring.cloud.sentinel.transport.dashboard=localhost:9100
# 取消Sentinel控制檯懶載入
# 預設情況下 Sentinel 會在客戶端首次呼叫的時候進行初始化,開始向控制檯傳送心跳包
# 配置 sentinel.eager=true 時,取消Sentinel控制檯懶載入功能
spring.cloud.sentinel.eager=true
# 如果有多套網路,又無法正確獲取本機IP,則需要使用下面的引數設定當前機器可被外部訪問的IP地址,供admin控制檯使用
# spring.cloud.sentinel.transport.client-ip=
# 應用服務 WEB 訪問埠
server.port=8080

寫一個Controller測試一下:

import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

@Slf4j
@RestController
@RequestMapping("limit")
public class LimitController {

    @RequestMapping(value = "test", method = RequestMethod.GET)
    public String test(){
        log.info("LimitController test介面");
        return "success";
    }
}

3、到dashboard上配置一下流控規則,比如設定上面的介面GET limit/test的單機閾值QPS=1,這樣當介面超過這個QPS會返回狀態http 429 Too Many Requests response body: "Blocked by Sentinel (flow limiting)"
還能看到比較直觀的實時監控的視覺化流控圖。

上面的過程屬於快速上手的實踐,有個問題在於springboot應用重啟之後,流控規則就沒了,看來流控規則在dashboard沒有持久化儲存,攔截元件側也是隻放在記憶體裡的,所以我們的架構中應該還少個配置持久化放在哪裡的問題,配合阿里家的另一款開源元件————配置中心nacos即可解決。