在微服務中集成 Sentinel
1. 添加依賴
對于 Spring Cloud 項目,首先需要添加 Sentinel 的依賴:
<!-- Spring Cloud Alibaba Sentinel -->
<dependency><groupId>com.alibaba.cloud</groupId><artifactId>spring-cloud-starter-alibaba-sentinel</artifactId><version>2.2.5.RELEASE</version>
</dependency><!-- Sentinel 數據源擴展(如使用 Nacos 作為規則配置中心) -->
<dependency><groupId>com.alibaba.csp</groupId><artifactId>sentinel-datasource-nacos</artifactId><version>1.8.0</version>
</dependency>
2. 配置 Sentinel
在 application.yml 中添加基本配置:
spring:cloud:sentinel:transport:dashboard: localhost:8080 # Sentinel 控制臺地址port: 8719 # 本地啟動的 HTTP Server 端口eager: true # 是否立即初始化datasource:ds1:nacos:server-addr: localhost:8848dataId: ${spring.application.name}-sentinelgroupId: DEFAULT_GROUPrule-type: flow
3. 啟動 Sentinel 控制臺
從?GitHub Release?下載最新版本的控制臺 jar 包,然后運行:
java -Dserver.port=8080 -Dcsp.sentinel.dashboard.server=localhost:8080 -Dproject.name=sentinel-dashboard -jar sentinel-dashboard.jar
Sentinel 核心功能實踐
1. 流量控制
流量控制是 Sentinel 最基本的功能,可以限制某個資源的訪問量:
@GetMapping("/resource")
@SentinelResource(value = "protected-resource", blockHandler = "handleBlock")
public String getResource() {return "Protected Resource";
}public String handleBlock(BlockException ex) {return "Request blocked by Sentinel";
}
在控制臺中可以為 "protected-resource" 配置流控規則,如設置 QPS 閾值為 100。
2. 熔斷降級
Sentinel 提供了三種熔斷策略:
-
慢調用比例:當響應時間超過閾值的請求比例達到設定值時觸發熔斷
-
異常比例:當異常請求比例達到閾值時觸發熔斷
-
異常數:當異常數達到閾值時觸發熔斷
@GetMapping("/unstable-api") @SentinelResource(value = "unstable-api", fallback = "fallbackMethod") public String unstableApi() {if (Math.random() > 0.5) {throw new RuntimeException("Random error");}return "Success"; }public String fallbackMethod(Throwable t) {return "Fallback response"; }
3. 系統自適應保護
Sentinel 可以根據系統的負載動態調整入口流量:
// 在配置類中添加系統規則 @PostConstruct public void initSystemRule() {List<SystemRule> rules = new ArrayList<>();SystemRule rule = new SystemRule();rule.setHighestSystemLoad(4.0); // 當系統 load1 超過 4 時觸發保護rule.setMaxThread(1000); // 最大線程數rule.setQps(500); // 全局 QPS 閾值rules.add(rule);SystemRuleManager.loadRules(rules); }
高級特性
1. 熱點參數限流
可以對特定參數值進行細粒度限流:
@GetMapping("/hot") @SentinelResource(value = "hot-resource",blockHandler = "handleHotBlock") public String hotEndpoint(@RequestParam String id) {return "Hot data for " + id; }
然后在控制臺中配置參數 id 的特定值(如 "123")的限流規則。
2. 集群流控
當應用有多個實例時,可以使用集群流控來限制整個集群的流量:
// 配置集群流控規則 FlowRule rule = new FlowRule(); rule.setResource("cluster-resource"); rule.setGrade(RuleConstant.FLOW_GRADE_QPS); rule.setCount(1000); rule.setClusterMode(true); // 開啟集群模式 FlowRuleManager.loadRules(Collections.singletonList(rule));
3. 規則持久化
為了避免規則在應用重啟后丟失,可以將規則持久化到 Nacos、Zookeeper 或 Apollo:
spring:cloud:sentinel:datasource:ds1:nacos:server-addr: localhost:8848dataId: ${spring.application.name}-flow-rulesgroupId: SENTINEL_GROUPrule-type: flowds2:nacos:server-addr: localhost:8848dataId: ${spring.application.name}-degrade-rulesgroupId: SENTINEL_GROUPrule-type: degrade
總結
Sentinel 為微服務架構提供了強大的流量控制、熔斷降級和系統保護能力。通過合理配置 Sentinel,可以顯著提高分布式系統的穩定性和可靠性。與傳統的 Hystrix 相比,Sentinel 提供了更豐富的控制維度、更直觀的監控界面和更靈活的擴展能力,是現代微服務架構中不可或缺的組件。