在微服务中集成 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:8848
dataId: ${spring.application.name}-sentinel
groupId: DEFAULT_GROUP
rule-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:8848 dataId: ${spring.application.name}-flow-rules groupId: SENTINEL_GROUP rule-type: flow ds2: nacos: server-addr: localhost:8848 dataId: ${spring.application.name}-degrade-rules groupId: SENTINEL_GROUP rule-type: degrade
总结
Sentinel 为微服务架构提供了强大的流量控制、熔断降级和系统保护能力。通过合理配置 Sentinel,可以显著提高分布式系统的稳定性和可靠性。与传统的 Hystrix 相比,Sentinel 提供了更丰富的控制维度、更直观的监控界面和更灵活的扩展能力,是现代微服务架构中不可或缺的组件。