1. 概述
正如其名稱所示,Sentinel 是一款強大的微服務守護者。它提供流量控制、併發限制、斷路器和自適應系統保護等功能,以確保微服務的可靠性。它是由阿里巴巴集團積極維護的開源組件。此外,它也是 Spring Cloud Circuit Breaker 的官方組成部分。
在本教程中,我們將探討 Sentinel 的主要功能。此外,我們還將看到如何使用它、其註解支持以及其監控儀表盤的示例。
2. 功能
- 支持多線程處理
- 提供靈活的配置選項
- 集成強大的日誌記錄功能
- 支持多種數據源連接
- 具備完善的錯誤處理機制
- 提供友好的 API 接口
- 支持自定義插件擴展
2.1. 流量控制
Sentinel 控制隨機請求的速率,以避免微服務的過載。 從而確保我們的服務不會因流量激增而崩潰。 它支持多種流量整形策略。 這些策略會在查詢每秒數 (QPS) 過高時,自動調整流量到適當的形狀。
以下是一些流量整形策略:
- 直接拒絕模式 – 當每秒請求數超過設定的閾值時,它會自動拒絕進一步的請求
- 漸温啓動模式 – 如果有突然的流量激增,該模式確保請求計數逐漸增加,直至達到上限
2.2. 回放機制與降級
當一個服務同步調用另一個服務時,存在另一個服務可能因為某種原因不可用的可能性。在這種情況下,線程會一直處於阻塞狀態,等待其他服務響應。這可能導致資源耗盡,調用服務也無法處理進一步的請求。 這被稱為級聯效應,可能導致我們整個微服務架構崩潰。
為了防止這種情況發生,回放機制應運而生。它會立即阻止所有後續對其他服務的調用。在超時期後,部分請求會通過。如果它們成功,則回放機制恢復正常流程。否則,超時期會重新開始。
Sentinel 採用最大併發限制的原則來實現回放機制。通過限制併發線程數,它減少了不穩定資源的影響。
Sentinel 還對不穩定資源進行降級。當資源的響應時間過高時,在指定的時間窗口內,所有對該資源的調用都會被拒絕。從而防止調用變得非常緩慢,導致級聯效應。
2.3. 適應性系統保護
Sentinel 在系統負載過高時保護我們的服務器。它使用 load1(系統負載)作為指標來啓動流量控制。請求將在以下條件下被阻止:
- 當前系統負載 (load1) > 閾值 (highestSystemLoad);
- 當前併發請求(線程數)> 估計容量(最小響應時間 * 最大 QPS)
3. 如何使用
This section provides instructions on how to use the product.
3.1 Installation
- Download the installer from our website: https://www.example.com/download
- Run the installer and follow the on-screen instructions.
- Once the installation is complete, you can start using the product.
3.2 Basic Usage
- Launch the application.
- The main window will appear.
- Click on the "File" menu and select "New" to create a new document.
- You can now start adding content to your document.
3.3 Advanced Features
- Customization: You can customize the product's appearance by changing the theme and font settings.
- Plugins: The product supports plugins that extend its functionality. You can find a list of available plugins on our website: https://www.example.com/plugins
- Settings: Access the settings menu to configure various options, such as data storage location and synchronization settings.
3.1. 添加 Maven 依賴
在我們的 Maven 項目中,需要在 <pom.xml> 中添加 <em/>sentinel-core</em/> 依賴:
<dependency>
<groupId>com.alibaba.csp</groupId>
<artifactId>sentinel-core</artifactId>
<version>1.8.0</version>
</dependency>3.2. 定義資源
讓我們使用 Sentinel API 在一個 try-catch 塊中,結合相應的業務邏輯定義我們的資源:
try (Entry entry = SphU.entry("HelloWorld")) {
// Our business logic here.
System.out.println("hello world");
} catch (BlockException e) {
// Handle rejected request.
}此 try-catch 塊,資源名稱為“HelloWorld”,作為我們業務邏輯的入口點,並由 Sentinel 進行保護。
3.3. 定義流程控制規則
這些規則控制着我們資源的流程,例如閾值計數或控制行為——例如,直接拒絕或啓動緩慢。 讓我們使用 <em >FlowRuleManager.loadRules()</em > 方法來配置流程規則:
List<FlowRule> flowRules = new ArrayList<>();
FlowRule flowRule = new FlowRule();
flowRule.setResource(RESOURCE_NAME);
flowRule.setGrade(RuleConstant.FLOW_GRADE_QPS);
flowRule.setCount(1);
flowRules.add(flowRule);
FlowRuleManager.loadRules(flowRules);該規則定義了我們的資源“RESOURCE_NAME”最多可以響應一個請求,且頻率限制為每秒一個請求。
3.4. 定義降級規則
使用降級規則,我們可以配置斷路器的請求閾值計數、恢復超時以及其他設置。
讓我們使用 <em >DegradeRuleManager.loadRules()</em> 來配置降級規則。
List<DegradeRule> rules = new ArrayList<DegradeRule>();
DegradeRule rule = new DegradeRule();
rule.setResource(RESOURCE_NAME);
rule.setCount(10);
rule.setTimeWindow(10);
rules.add(rule);
DegradeRuleManager.loadRules(rules);這條規則規定,當我們的資源 RESOURCE_NAME 無法服務 10 個請求(閾值計數)時,斷路器將斷開。所有後續對該資源的請求將在 10 秒(時間窗口)內被 Sentinel 阻止。
3.5. 定義系統保護規則
使用系統保護規則,我們可以配置並確保自適應系統保護(閾值包括 load1、平均響應時間、併發線程數)。 讓我們使用 SystemRuleManager.loadRules() 方法來配置系統規則:
List<SystemRule> rules = new ArrayList<>();
SystemRule rule = new SystemRule();
rule.setHighestSystemLoad(10);
rules.add(rule);
SystemRuleManager.loadRules(rules);該規則規定,對於我們的系統而言,最高系統負載為每秒10個請求。如果當前負載超過該閾值,所有後續請求將被阻止。
4. 註解支持
Sentinel 還提供基於方面(Aspect-Oriented)的註解支持,用於定義資源。
首先,我們將添加 Maven 依賴 sentinel-annotation-aspectj:
<dependency>
<groupId>com.alibaba.csp</groupId>
<artifactId>sentinel-annotation-aspectj</artifactId>
<version>1.8.0</version>
</dependency>然後,我們向我們的配置類中添加了 @Configuration</em/>,以將 sentinel 方面註冊為 Spring 容器中的一個 Bean:
@Configuration
public class SentinelAspectConfiguration {
@Bean
public SentinelResourceAspect sentinelResourceAspect() {
return new SentinelResourceAspect();
}
}@SentinelResource 指代資源定義。它具有諸如 value 屬性,用於定義資源名稱。 fallback 屬性定義了回退方法名稱。當斷路器斷開連接時,此回退方法定義了我們程序中的替代流程。 讓我們使用 @SentinelResource 註解定義資源:
@SentinelResource(value = "resource_name", fallback = "doFallback")
public String doSomething(long i) {
return "Hello " + i;
}
public String doFallback(long i, Throwable t) {
// Return fallback value.
return "fallback";
}這定義了名為 resource_name, 以及回退方法。
5. 監控儀表盤
Sentinel 還提供了一個監控儀表盤。 通過此儀表盤,我們可以監控客户端並動態配置規則。 我們可以實時查看對我們定義資源的傳入流量量。
5.1. 啓動儀表盤
首先,我們需要下載 Sentinel Dashboard jar 包。然後,可以使用以下命令啓動儀表盤:
java -Dserver.port=8080 -Dcsp.sentinel.dashboard.server=localhost:8080 -Dproject.name=sentinel-dashboard -jar sentinel-dashboard.jar當儀表盤應用程序啓動後,我們可以按照下一部分中的步驟連接我們的應用程序。
5.2. 準備我們的應用程序
讓我們將 sentinel-transport-simple-http 依賴項添加到我們的 pom.xml 文件中:
<dependency>
<groupId>com.alibaba.csp</groupId>
<artifactId>sentinel-transport-simple-http</artifactId>
<version>1.8.0</version>
</dependency>5.3. 將我們的應用程序連接到儀表板
啓動應用程序時,我們需要添加儀表板的 IP 地址:
-Dcsp.sentinel.dashboard.server=consoleIp:port現在,每當調用任何資源時,儀表盤將接收來自我們應用程序的心跳信號:
我們還可以使用儀表盤動態地修改流程、降級和系統規則。
6. 結論
在本文中,我們瞭解了阿里巴巴 Sentinel 流量控制、斷路器和自適應系統保護的主要功能。