部署架构

✅ 推荐架构

┌────────────────────────────────────┐
│  Nginx (反向代理 + 健康检查)        │
└──────────────┬─────────────────────┘
               ↓
┌──────────────────────────────────┐
│  Spring Cloud Gateway            │
│  (网关层限流)                     │
└──────────┬───────────────────────┘
           ↓
┌──────────────────────────────────┐
│  微服务集群                       │
│  (服务层限流 + 熔断)              │
│  ├─ Order Service (3实例)        │
│  ├─ Product Service (5实例)      │
│  └─ Payment Service (2实例)      │
└──────────┬───────────────────────┘
           ↓
┌──────────────────────────────────┐
│  Sentinel Dashboard (高可用)     │
│  ├─ Dashboard 1                  │
│  └─ Dashboard 2                  │
└──────────────────────────────────┘
           ↓
┌──────────────────────────────────┐
│  Nacos (配置中心 + 注册中心)      │
│  (规则持久化)                     │
└──────────────────────────────────┘

规则配置

1. 限流规则

// ✅ 推荐:分层限流
// 第一层:网关限流(总入口)
GatewayFlowRule gatewayRule = new GatewayFlowRule("order-service")
    .setCount(10000);

// 第二层:服务限流(服务级)
FlowRule serviceRule = new FlowRule();
serviceRule.setResource("orderService");
serviceRule.setCount(5000);

// 第三层:接口限流(接口级)
FlowRule apiRule = new FlowRule();
apiRule.setResource("orderCreate");
apiRule.setCount(1000);

2. 熔断规则

// ✅ 推荐:慢调用比例
DegradeRule rule = new DegradeRule();
rule.setResource("callProductService");
rule.setGrade(RuleConstant.DEGRADE_GRADE_RT);
rule.setCount(1000);              // RT > 1秒
rule.setSlowRatioThreshold(0.5);  // 慢调用比例50%
rule.setMinRequestAmount(10);     // 最少10个请求
rule.setStatIntervalMs(10000);    // 统计10秒
rule.setTimeWindow(10);           // 熔断10秒

3. 系统保护

// ✅ 推荐:多指标保护
SystemRule rule = new SystemRule();
rule.setHighestCpuUsage(0.8);     // CPU 80%
rule.setAvgRt(500);               // RT 500ms
rule.setMaxThread(50);            // 线程数50

配置清单

application.yml

spring:
  application:
    name: order-service
  cloud:
    # Nacos配置
    nacos:
      discovery:
        server-addr: nacos.example.com:8848
        namespace: production
      config:
        server-addr: nacos.example.com:8848
        namespace: production

    # Sentinel配置
    sentinel:
      transport:
        dashboard: sentinel.example.com:8080
        port: 8719
      eager: true

      # 规则持久化
      datasource:
        flow:
          nacos:
            server-addr: nacos.example.com:8848
            dataId: ${spring.application.name}-flow-rules
            groupId: SENTINEL_GROUP
            rule-type: flow
            namespace: production
        degrade:
          nacos:
            server-addr: nacos.example.com:8848
            dataId: ${spring.application.name}-degrade-rules
            groupId: SENTINEL_GROUP
            rule-type: degrade
            namespace: production

监控告警

Prometheus指标

management:
  endpoints:
    web:
      exposure:
        include: '*'
  metrics:
    export:
      prometheus:
        enabled: true
    tags:
      application: ${spring.application.name}
      env: production

告警规则

groups:
  - name: sentinel_critical
    rules:
      # 限流比例过高
      - alert: HighBlockRate
        expr: |
          rate(sentinel_block_qps[1m]) /
          (rate(sentinel_pass_qps[1m]) + rate(sentinel_block_qps[1m])) > 0.1
        for: 5m
        annotations:
          summary: "限流比例超过10%"

      # 熔断器开启
      - alert: CircuitBreakerOpen
        expr: sentinel_circuit_breaker_state == 1
        for: 1m
        annotations:
          summary: "熔断器已开启"

      # RT过高
      - alert: HighRT
        expr: sentinel_avg_rt > 1000
        for: 5m
        annotations:
          summary: "平均RT超过1秒"

代码规范

1. 资源命名

// ✅ 推荐:模块_操作
@SentinelResource("order_create")
@SentinelResource("product_query")
@SentinelResource("payment_pay")

// ❌ 避免:动态资源名
@SentinelResource(value = "/api/" + userId)  // 会产生大量资源

2. 降级处理

// ✅ 推荐:友好降级
@SentinelResource(
    value = "getProduct",
    blockHandler = "handleBlock",
    fallback = "handleFallback"
)
public Product getProduct(Long id) {
    return productService.getById(id);
}

public Product handleBlock(Long id, BlockException ex) {
    // 限流降级:返回缓存
    return productCache.get(id);
}

public Product handleFallback(Long id, Throwable ex) {
    // 异常降级:返回默认值
    return Product.builder().id(id).name("服务暂时不可用").build();
}

3. 异常处理

// ✅ 推荐:全局异常处理
@RestControllerAdvice
public class GlobalExceptionHandler {

    @ExceptionHandler(FlowException.class)
    public Result handleFlowException(FlowException e) {
        return Result.error(429, "请求过于频繁,请稍后重试");
    }

    @ExceptionHandler(DegradeException.class)
    public Result handleDegradeException(DegradeException e) {
        return Result.error(503, "服务暂时不可用,请稍后重试");
    }
}

运维流程

1. 变更流程

提出变更需求
    ↓
编写变更方案
    ↓
技术Review
    ↓
灰度验证
    ↓
全量发布
    ↓
监控观察
    ↓
变更总结

2. 灰度发布

# 1. 先更新1个实例
kubectl scale deployment order-service --replicas=1
# 更新规则
# 观察5分钟

# 2. 逐步扩容
kubectl scale deployment order-service --replicas=3
# 观察10分钟

# 3. 全量发布
kubectl scale deployment order-service --replicas=10

3. 回滚方案

# 1. 规则回滚
# 在Nacos中恢复旧规则

# 2. 应用回滚
kubectl rollout undo deployment order-service

# 3. 验证
curl http://localhost:8080/health

性能优化

1. JVM参数

-Xms2g -Xmx2g
-XX:+UseG1GC
-XX:MaxGCPauseMillis=200
-XX:+HeapDumpOnOutOfMemoryError
-XX:HeapDumpPath=/logs/heapdump.hprof

2. 线程池

server:
  tomcat:
    threads:
      max: 200
      min-spare: 50
    accept-count: 100

3. 规则优化

// 控制规则数量 < 100条
// 合并相似规则
// 定期清理无用规则

安全加固

1. Dashboard认证

# Dashboard启动参数
-Dsentinel.dashboard.auth.username=admin
-Dsentinel.dashboard.auth.password=your_strong_password

# 启用HTTPS
server.port=8443
server.ssl.enabled=true
server.ssl.key-store=classpath:keystore.p12
server.ssl.key-store-password=password

2. 规则权限

// 配置规则审批流程
// 只允许管理员修改规则
@PreAuthorize("hasRole('ADMIN')")
public Result updateRule(FlowRule rule) {
    // ...
}

测试验证

1. 限流测试

# 压测工具
wrk -t10 -c100 -d60s http://localhost:8080/order/create

# 验证限流生效
# 查看Dashboard监控
# 查看应用日志

2. 熔断测试

# 故障注入
curl -X POST http://localhost:8080/fault/inject?resource=productService&delay=5000

# 观察熔断生效
# 查看熔断日志
# 验证降级逻辑

3. 压测报告

测试场景:订单创建接口
限流阈值:1000 QPS
压测并发:100
持续时间:60秒

结果:
- 通过QPS:1000
- 限流QPS:100
- 成功率:90.9%
- 平均RT:50ms
- 限流生效:✅

应急预案

1. 限流过度

# 立即调整规则
# 在Nacos中增大阈值
# 或临时关闭限流

2. 熔断误判

# 调整熔断阈值
# 增大慢调用时间
# 增大慢调用比例

3. 全局降级

# 配置降级开关
sentinel:
  enabled: false  # 紧急关闭

检查清单

上线前检查

  • Dashboard部署完成
  • 配置Nacos持久化
  • 配置限流规则
  • 配置熔断规则
  • 配置监控告警
  • 限流压测验证
  • 熔断故障注入测试
  • 降级逻辑验证
  • 应急预案准备
  • 文档编写完成

运行中检查

  • 每日查看Dashboard监控
  • 每周review规则配置
  • 每月review告警记录
  • 每季度压测验证
  • 定期演练应急预案

总结

生产环境最佳实践:

  1. 分层防护:网关 → 服务 → 接口三层防护
  2. 规则持久化:使用Nacos持久化规则
  3. 监控告警:完善的监控告警体系
  4. 优雅降级:友好的降级提示
  5. 灰度发布:规则变更先灰度验证
  6. 应急预案:准备降级开关和回滚方案

关键原则

  • 宁可限流,不可崩溃
  • 宁可降级,不可雪崩
  • 宁可保守,不可激进
  • 宁可监控,不可盲目

终极目标

  • 系统稳定性 > 99.99%
  • 限流误杀率 < 1%
  • 故障恢复时间 < 5分钟
  • 用户体验影响最小化

🎉 全系列完结

恭喜你完成了Sentinel从入门到精通的全部37篇文章学习!

回顾学习内容:

  • 基础入门:流量控制本质、Sentinel核心概念
  • 流量控制:4种限流算法、3种流控效果、3种流控策略
  • 熔断降级:熔断原理、3种熔断策略、优雅降级、防雪崩
  • 进阶特性:系统保护、热点限流、黑白名单、集群流控、网关流控、动态配置
  • 框架集成:Spring Boot、Spring Cloud、Dubbo、Gateway
  • 架构原理:核心架构、Slot Chain、滑动窗口、规则管理
  • 生产实践:Dashboard部署、监控告警、性能调优、故障排查、最佳实践

下一步行动

  1. 在项目中实践应用
  2. 持续优化规则配置
  3. 建立监控告警体系
  4. 定期review和演练

祝你在生产环境中游刃有余!