行业资讯

SpringBoot 整合 Sentinel——流量控制与熔断降级

发布时间:2026/7/28 0:24:33
SpringBoot 整合 Sentinel——流量控制与熔断降级 Sentinel 是阿里巴巴开源的流量控制组件相比 Hystrix 功能更丰富、性能更好。支持限流、熔断、系统保护等多种能力。一、为什么选 Sentinel对比SentinelHystrix限流模式直接、冷启动、匀速线程池/信号量熔断策略慢比、异常比、异常数错误率控制台✅ 可视化配置❌ 需额外搭建规则持久化✅ Nacos 支持❌社区活跃度⭐⭐⭐⭐⭐⭐已停更二、集成 SentineldependencygroupIdcom.alibaba.cloud/groupIdartifactIdspring-cloud-starter-alibaba-sentinel/artifactId/dependencyspring:cloud:sentinel:transport:dashboard:localhost:8080# Sentinel 控制台eager:true三、流控规则RestControllerRequestMapping(/api)publicclassSeckillController{GetMapping(/seckill/{productId})SentinelResource(valueseckill,blockHandlerseckillBlockHandler,fallbackseckillFallback)publicResultVO?seckill(PathVariableLongproductId){// 秒杀逻辑returnResultVO.success(秒杀成功);}/** * 限流后的处理方法blockHandler 处理 */publicResultVO?seckillBlockHandler(LongproductId,BlockExceptione){returnResultVO.error(429,请求太频繁请稍后重试);}/** * 熔断降级后的处理方法fallback 处理 */publicResultVO?seckillFallback(LongproductId,Throwablee){returnResultVO.error(500,服务繁忙请稍后);}}四、熔断规则ConfigurationpublicclassSentinelRuleConfig{PostConstructpublicvoidinitRules(){// 流控规则每秒最多 100 个请求ListFlowRuleflowRulesnewArrayList();FlowRuleflowRulenewFlowRule();flowRule.setResource(seckill);flowRule.setGrade(RuleConstant.FLOW_GRADE_QPS);flowRule.setCount(100);flowRule.setControlBehavior(RuleConstant.CONTROL_BEHAVIOR_DEFAULT);flowRules.add(flowRule);FlowRuleManager.loadRules(flowRules);// 熔断规则10秒内异常比例超过50%则熔断ListDegradeRuledegradeRulesnewArrayList();DegradeRuledegradeRulenewDegradeRule();degradeRule.setResource(seckill);degradeRule.setGrade(RuleConstant.DEGRADE_GRADE_EXCEPTION_RATIO);degradeRule.setCount(0.5);degradeRule.setTimeWindow(10);degradeRules.add(degradeRule);DegradeRuleManager.loadRules(degradeRules);}}五、配置中心持久化ComponentpublicclassSentinelNacosDataSource{PostConstructpublicvoidinit()throwsException{ReadableDataSourceString,ListFlowRuledsnewNacosDataSource(localhost:8848,DEFAULT_GROUP,seckill-flow-rules,source-JSON.parseObject(source,newTypeReferenceListFlowRule(){}));FlowRuleManager.register2Property(ds.getProperty());}}六、热点参数限流GetMapping(/product/{id})SentinelResource(valueproduct,blockHandlerproductBlockHandler)publicResultVO?getProduct(PathVariableSentinelResourceParamLongid){returnResultVO.success(productService.getById(id));}// Nacos 中配置热点规则// 参数索引 0单机阈值 10统计时长 1 秒// 即对商品 ID 限流同一个商品每秒最多请求 10 次七、系统保护规则// 系统规则当 Load 或 RT 超过阈值时触发ListSystemRulesystemRulesnewArrayList();SystemRulerulenewSystemRule();rule.setHighestSystemLoad(5.0);// 系统 Loadrule.setAvgRt(1000);// 平均 RT 超过 1000msrule.setMaxQps(10000);// 总 QPS 限制SystemRuleManager.loadRules(systemRules);八、Feign 集成feign:sentinel:enabled:trueFeignClient(namestock-service,fallbackStockFallback.class)publicinterfaceStockClient{GetMapping(/stock/{productId})ResultVOIntegergetStock(PathVariableLongproductId);}ComponentpublicclassStockFallbackimplementsStockClient{OverridepublicResultVOIntegergetStock(LongproductId){returnResultVO.error(500,库存服务不可用);}}九、控制台# 下载并启动 Sentinel 控制台java-jarsentinel-dashboard-1.8.6.jar--server.port8080访问http://localhost:8080可以可视化配置限流和熔断规则。总结Sentinel 的核心功能 限流 → 控制 QPS防止系统被打满 熔断 → 下游服务挂了快速失败 降级 → 返回默认值保证核心业务可用 觉得有用的话点赞 关注【张老师技术栈】吧每周更新 Java/Python/MySQL 实战干货不让你白来。