Sentinel: 分布式系統的流量防衛兵
Sentinel 是什么?
隨着微服務的流行,服務和服務之間的穩定性變得越來越重要。Sentinel 以流量為切入點,從流量控制、熔斷降級、系統負載保護等多個維度保護服務的穩定性。
官網:https://github.com/alibaba/Sentinel
中文官網:https://github.com/alibaba/Sentinel/wiki
Sentinel 具有以下特征:
- 豐富的應用場景:Sentinel 承接了阿里巴巴近 10 年的雙十一大促流量的核心場景,例如秒殺(即突發流量控制在系統容量可以承受的范圍)、消息削峰填谷、集群流量控制、實時熔斷下游不可用應用等。
- 完備的實時監控:Sentinel 同時提供實時的監控功能。您可以在控制台中看到接入應用的單台機器秒級數據,甚至 500 台以下規模的集群的匯總運行情況。
- 廣泛的開源生態:Sentinel 提供開箱即用的與其它開源框架/庫的整合模塊,例如與 Spring Cloud、Dubbo、gRPC 的整合。您只需要引入相應的依賴並進行簡單的配置即可快速地接入 Sentinel。
- 完善的 SPI 擴展點:Sentinel 提供簡單易用、完善的 SPI 擴展接口。您可以通過實現擴展接口來快速地定制邏輯。例如定制規則管理、適配動態數據源等。
Sentinel 的主要特性:

Sentinel 的開源生態:

Sentinel 分為兩個部分:
- 核心庫(Java 客戶端)不依賴任何框架/庫,能夠運行於所有 Java 運行時環境,同時對 Dubbo / Spring Cloud 等框架也有較好的支持。
- 控制台(Dashboard)基於 Spring Boot 開發,打包后可以直接運行,不需要額外的 Tomcat 等應用容器。
Sentinel 重要概念
定義資源
資源 是 Sentinel 中的核心概念之一。最常用的資源是我們代碼中的 Java 方法。 當然,您也可以更靈活的定義你的資源,例如,把需要控制流量的代碼用 Sentinel API SphU.entry("HelloWorld") 和 entry.exit() 包圍起來即可。在下面的例子中,我們將 System.out.println("hello world"); 作為資源(被保護的邏輯),用 API 包裝起來。參考代碼如下:
1 public static void main(String[] args) { 2 // 配置規則. 3 initFlowRules(); 4 5 while (true) { 6 // 1.5.0 版本開始可以直接利用 try-with-resources 特性,自動 exit entry 7 try (Entry entry = SphU.entry("HelloWorld")) { 8 // 被保護的邏輯 9 System.out.println("hello world"); 10 } catch (BlockException ex) { 11 // 處理被流控的邏輯 12 System.out.println("blocked!"); 13 } 14 } 15 }
也可以通過我們提供的 注解支持模塊,來定義我們的資源,類似於下面的代碼:
1 @SentinelResource("HelloWorld") 2 public void helloWorld() { 3 // 資源中的邏輯 4 System.out.println("hello world"); 5 }
這樣,helloWorld() 方法就成了我們的一個資源。注意注解支持模塊需要配合 Spring AOP 或者 AspectJ 一起使用。
定義規則
接下來,通過流控規則來指定允許該資源通過的請求次數,例如下面的代碼定義了資源 HelloWorld 每秒最多只能通過 20 個請求。
1 private static void initFlowRules(){ 2 List<FlowRule> rules = new ArrayList<>(); 3 FlowRule rule = new FlowRule(); 4 rule.setResource("HelloWorld"); 5 rule.setGrade(RuleConstant.FLOW_GRADE_QPS); 6 // Set limit QPS to 20. 7 rule.setCount(20); 8 rules.add(rule); 9 FlowRuleManager.loadRules(rules); 10 }
完成上面 ,Sentinel 就能夠正常工作了。更多的信息可以參考官網。
Sentinel使用
案例架構圖如下:

搭建Sentinel控制台
1、下載sentienl的jar包,本例使用:sentinel-dashboard-1.7.2.jar,地址:https://github.com/alibaba/Sentinel/releases
2、使用java -jar命令啟動Sentinel控制台,注意:啟動 Sentinel 控制台需要 JDK 版本為 1.8 及以上版本。
命令格式:java -Dserver.port=8080 -Dcsp.sentinel.dashboard.server=localhost:8080 -Dproject.name=sentinel-dashboard -jar sentinel-dashboard.jar
如若8080端口沖突,可使用 -Dserver.port=新端口 進行設置。
命令:java -jar sentinel-dashboard-1.7.2.jar

3、訪問地址:http://localhost:8080/,8080為Sentinel的默認端口

4、輸入默認用戶名/密碼:sentinel/sentinel,進入首頁

搭建Sentinel客戶端
1、新建項目Spring Cloud項目(springcloud-sentinel-service8401)

2、編輯pom.xml文件,引入依賴
1 <!-- alibaba nacos sentinel --> 2 <dependency> 3 <groupId>com.alibaba.cloud</groupId> 4 <artifactId>spring-cloud-starter-alibaba-sentinel</artifactId> 5 </dependency>
完整pom,如下:
1 <?xml version="1.0" encoding="UTF-8"?> 2 <project xmlns="http://maven.apache.org/POM/4.0.0" 3 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 4 xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> 5 <parent> 6 <artifactId>test-springcloud</artifactId> 7 <groupId>com.test</groupId> 8 <version>1.0-SNAPSHOT</version> 9 </parent> 10 <modelVersion>4.0.0</modelVersion> 11 12 <artifactId>springcloud-sentinel-service8401</artifactId> 13 14 <dependencies> 15 16 <!-- alibaba nacos sentinel --> 17 <dependency> 18 <groupId>com.alibaba.cloud</groupId> 19 <artifactId>spring-cloud-starter-alibaba-sentinel</artifactId> 20 </dependency> 21 22 <!-- alibaba nacos discovery --> 23 <dependency> 24 <groupId>com.alibaba.cloud</groupId> 25 <artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId> 26 </dependency> 27 28 <!-- spring boot --> 29 <dependency> 30 <groupId>org.springframework.boot</groupId> 31 <artifactId>spring-boot-starter-web</artifactId> 32 </dependency> 33 <dependency> 34 <groupId>org.springframework.boot</groupId> 35 <artifactId>spring-boot-starter-actuator</artifactId> 36 </dependency> 37 <dependency> 38 <groupId>org.springframework.boot</groupId> 39 <artifactId>spring-boot-devtools</artifactId> 40 <scope>runtime</scope> 41 <optional>true</optional> 42 </dependency> 43 <dependency> 44 <groupId>org.projectlombok</groupId> 45 <artifactId>lombok</artifactId> 46 <optional>true</optional> 47 </dependency> 48 <dependency> 49 <groupId>org.springframework.boot</groupId> 50 <artifactId>spring-boot-starter-test</artifactId> 51 <scope>test</scope> 52 </dependency> 53 54 </dependencies> 55 </project>
3、編輯application.yml文件
1 # 端口 2 server: 3 port: 8401 4 5 spring: 6 application: 7 name: alibaba-sentinel-service 8 cloud: 9 nacos: 10 discovery: 11 server-addr: localhost:8848 12 sentinel: 13 transport: 14 # 配置Sentinel DashBoard地址 15 dashboard: localhost:8080 16 # 應用與Sentinel控制台交互的端口,應用本地會起一個該端口占用的HttpServer 17 # 默認8719端口,假如端口被占用,依次+1,直到找到未被占用端口 18 port: 8719 19 20 management: 21 endpoints: 22 web: 23 exposure: 24 include: '*'
4、編輯主啟動類
1 @SpringBootApplication 2 @EnableDiscoveryClient 3 public class SentinelMain8401 { 4 public static void main(String[] args) { 5 SpringApplication.run(SentinelMain8401.class, args); 6 } 7 }
5、編輯一個Controller
1 @RestController 2 public class FlowLimitController { 3 4 @GetMapping("/testA") 5 public String testA(){ 6 return "--------testA"; 7 } 8 9 @GetMapping("/testB") 10 public String testB(){ 11 return "--------testB"; 12 } 13 }
6、測試
1)啟動項目,啟動Nacos服務(【SpringCloud】Spring Cloud Alibaba 之 Nacos注冊中心(二十七))
啟動Sentinel控制台
2)在瀏覽器中訪問地址:http://localhost:8401/testA
3)查看Sentinel控制台-〉選擇alibaba-sentinel-service服務-〉實時監控-〉可以看到監控詳情

4)同時可以查看簇點鏈路

5)同時可以查看機器列表

