初始化示範工程
前提啟動nacos,Sentinel成功
建立cloudalibaba-sentinel-service8401
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">
<parent>
<artifactId>cloud2020</artifactId>
<groupId>com.atguigu.springcloud</groupId>
<version>1.0-SNAPSHOT</version>
</parent>
<modelVersion>4.0.0</modelVersion>
<artifactId>cloudalibaba-sentinel-service8401</artifactId>
<properties>
<maven.compiler.source>8</maven.compiler.source>
<maven.compiler.target>8</maven.compiler.target>
</properties>
<dependencies>
<!--SpringCloud ailibaba nacos -->
<dependency>
<groupId>com.alibaba.cloud</groupId>
<artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId>
</dependency>
<!--SpringCloud ailibaba sentinel -->
<dependency>
<groupId>com.alibaba.cloud</groupId>
<artifactId>spring-cloud-starter-alibaba-sentinel</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-openfeign</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-devtools</artifactId>
<scope>runtime</scope>
<optional>true</optional>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<optional>true</optional>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
</project>
yml檔案
rver:
port: 8401
spring:
application:
name: cloudalibaba-sentinel-service
cloud:
nacos:
discovery:
#Nacos服務注冊中心位址
server-addr: localhost:8848
sentinel:
transport:
#配置Sentinel dashboard位址
dashboard: localhost:8080
#預設8719端口,假如被占用會自動從8719開始依次+1掃描,直至找到未被占用的端口
port: 8719
management:
endpoints:
web:
exposure:
include: '*'
sentinel:
enabled: true
主啟動類
@EnableDiscoveryClient
@SpringBootApplication
public class MainApp8401
{
public static void main(String[] args) {
SpringApplication.run(MainApp8401.class, args);
}
}
業務類
@RestController
public class FlowLimitController
{
@GetMapping("/testA")
public String testA()
{
return "------testA";
}
@GetMapping("/testB")
public String testB()
{
return "------testB";
}
}
測試1
通路http://localhost:8401/testA通路http://localhost:8401/testB![]()
SpringCloud AlibabaSentinel實作熔斷與限流(1) 通路http://localhost:8080![]()
SpringCloud AlibabaSentinel實作熔斷與限流(1) ![]()
SpringCloud AlibabaSentinel實作熔斷與限流(1)
流控規則
基本介紹
流控模式
直接(預設)
直接->快速失敗
配置及說明
表示1秒鐘内查詢1次就是OK,若超過次數1,就直接-快速失敗,報預設錯誤
測試2
快速點選通路http://localhost:8401/testA![]()
SpringCloud AlibabaSentinel實作熔斷與限流(1)
關聯
設定效果
當關聯資源/testB的qps閥值超過1時,就限流/testA的Rest通路位址,當關聯資源到門檻值後限制配置好的資源名
測試3
postman發送請求http://localhost:8401/testB![]()
SpringCloud AlibabaSentinel實作熔斷與限流(1) postman裡建立多線程集合組将通路位址添加進新新線程組![]()
SpringCloud AlibabaSentinel實作熔斷與限流(1) run![]()
SpringCloud AlibabaSentinel實作熔斷與限流(1) ![]()
SpringCloud AlibabaSentinel實作熔斷與限流(1) 大批量線程高并發通路B,導緻A失效了
點選通路http://localhost:8401/testA
運作後發現testA挂了
結果:
Blocked by Sentinel (flow limiting)
鍊路
多個請求調用了同一個微服務
流控效果
直接->快速失敗(預設的流控處理)
直接失敗,抛出異常
Blocked by Sentinel (flow limiting)
源碼
com.alibaba.csp.sentinel.slots.block.flow.controller.DefaultController
預熱
說明:公式:門檻值除以coldFactor(預設值為3),經過預熱時長後才會達到門檻值
官網
預設coldFactor為3,即請求 QPS 從 threshold / 3 開始,經預熱時長逐漸升至設定的 QPS 門檻值。
預設 coldFactor 為 3,即請求QPS從(threshold / 3) 開始,經多少預熱時長才逐漸升至設定的 QPS 門檻值。
測試4
案例,閥值為10+預熱時長設定5秒。
系統初始化的閥值為10 / 3 約等于3,即閥值剛開始為3;然後過了5秒後閥值才慢慢升高恢複到10
![]()
SpringCloud AlibabaSentinel實作熔斷與限流(1) 多次點選http://localhost:8401/testB
剛開始不行,後續慢慢OK
應用場景
如:秒殺系統在開啟的瞬間,會有很多流量上來,很有可能把系統打死,預熱方式就是把為了保護系統,可慢慢的把流量放進來,慢慢的把閥值增長到設定的閥值。
排隊等待
勻速排隊,讓請求以均勻的速度通過,閥值類型必須設成QPS,否則無效。
設定含義:/testA每秒1次請求,超過的話就排隊等待,等待的逾時時間為20000毫秒。
官網
https://github.com/alibaba/Sentinel/wiki/%E6%B5%81%E9%87%8F%E6%8E%A7%E5%88%B6
com.alibaba.csp.sentinel.slots.block.flow.controller.RateLimiterController