微服务架构下限流熔断的必要性
微服务架构中,服务间调用链路复杂,单个下游服务的过载会通过调用链向上游传播,引发级联故障。限流控制进入服务的请求速率,熔断在故障达到阈值时快速切断调用,两者配合是高并发设计中保护系统可用性的核心手段。
Sentinel是阿里巴巴开源的流量治理组件,与Spring Boot集成简单、规则配置灵活、控制台功能完善,是Java微服务限流熔断的主流选择。本文覆盖Sentinel在Spring Boot项目中的完整集成方案,包括规则配置、集群限流、熔断降级、监控告警等生产级配置。
Spring Boot集成Sentinel核心依赖
Maven中引入sentinel-core和spring-cloud-starter-alibaba-sentinel依赖,application.yml中配置Sentinel Dashboard地址和Nacos数据源。Nacos作为规则持久化存储,确保应用重启后规则不丢失。
spring:
cloud:
sentinel:
transport:
dashboard: sentinel-dashboard:8080
port: 8719
datasource:
flow:
nacos:
server-addr: nacos:8848
namespace: sentinel-rules
data-id: ${spring.application.name}-flow-rules
rule-type: flow
degrade:
nacos:
server-addr: nacos:8848
namespace: sentinel-rules
data-id: ${spring.application.name}-degrade-rules
rule-type: degrade
限流规则配置:QPS与线程数两种模式
Sentinel支持QPS限流和线程数限流两种模式。QPS模式控制每秒请求数,适合API网关和入口服务;线程数模式控制并发执行数,适合下游服务保护。
@Configuration
public class SentinelRuleConfig {
@PostConstruct
public void initFlowRules() {
List<FlowRule> rules = new ArrayList<>();
// API接口QPS限流:每秒最多200个请求
FlowRule apiRule = new FlowRule()
.setResource("/api/orders")
.setGrade(RuleConstant.FLOW_GRADE_QPS)
.setCount(200)
.setControlBehavior(RuleConstant.CONTROL_BEHAVIOR_WARM_UP)
.setWarmUpPeriodSec(10);
rules.add(apiRule);
// 服务调用线程数限流:最多50个并发线程
FlowRule threadRule = new FlowRule()
.setResource("order-service")
.setGrade(RuleConstant.FLOW_GRADE_THREAD)
.setCount(50);
rules.add(threadRule);
FlowRuleManager.loadRules(rules);
}
}
WARM_UP(预热)模式是生产环境的推荐配置。冷启动期间,限流阈值逐步从 count/3 提升到 count,避免突发流量直接打满导致大量拒绝。
熔断降级:三种策略的选择
Sentinel提供慢调用比例、异常比例、异常数三种熔断策略。慢调用比例策略:RT超过阈值视为慢调用,比例超过阈值触发熔断。异常比例策略:异常比例超过阈值触发熔断。
@PostConstruct
public void initDegradeRules() {
List<DegradeRule> rules = new ArrayList<>();
// 慢调用比例熔断
DegradeRule slowCallRule = new DegradeRule("payment-service")
.setGrade(CircuitBreakerStrategy.SLOW_REQUEST_RATIO.getType())
.setCount(500)
.setSlowRatioThreshold(0.5)
.setTimeWindow(30)
.setMinRequestAmount(10)
.setStatIntervalMs(10000);
rules.add(slowCallRule);
DegradeRuleManager.loadRules(rules);
}
策略选择建议:对外部依赖服务使用慢调用比例策略,对内部服务调用使用异常比例策略。
@SentinelResource注解与降级处理
使用注解方式声明资源点和降级逻辑:
@SentinelResource(
value = "createOrder",
blockHandler = "createOrderBlockHandler",
fallback = "createOrderFallback"
)
public OrderResult createOrder(OrderRequest request) {
return orderClient.submit(request);
}
// 限流处理
public OrderResult createOrderBlockHandler(
OrderRequest req, BlockException ex) {
return OrderResult.fail("系统繁忙,请稍后重试");
}
// 熔断降级处理
public OrderResult createOrderFallback(
OrderRequest req, Throwable t) {
return OrderResult.fail("服务暂时不可用,已触发降级保护");
}
blockHandler处理限流触发(BlockException),fallback处理业务异常和熔断降级。两者分工明确,不应混用。
集群限流与监控告警
单机限流在多实例部署时无法精确控制总QPS。集群限流通过Token Server统一分配令牌,确保集群总QPS严格达标。
Sentinel Dashboard提供实时监控面板,但生产环境需要将指标接入Prometheus体系实现统一告警。关键告警规则:blocked_qps大于0持续5分钟触发告警(说明持续触发限流,需扩容或调整阈值);avg_rt大于P99基线的2倍持续3分钟触发告警。
限流熔断不是万能药,它是对抗故障扩散的最后一道防线。配置时要始终问自己:触发限流后,用户体验是什么?如果答案是”完全不可用”,说明限流策略还需要搭配降级方案和友好的用户提示。
原创文章,作者:小编,如若转载,请注明出处:https://www.yunthe.com/springboot-wei-fu-wu-xian-liu-rong-duan-shi-zhan-sentinel/