9.9.6 Sentinel、Gateway协同治理

Sentinel、Gateway协同治理#

Sentinel 负责服务侧限流与熔断,Spring Cloud Gateway 负责统一入口与路由治理。二者配合实现“入口限流 + 服务级保护 + 规则动态下发 + 统一观测”。

协同原理草图#

文章图片

安装与运行(示例)#

  • Sentinel Dashboard(本地运行示例)
# 1) 下载 Dashboard
wget -O sentinel-dashboard.jar \
https://github.com/alibaba/Sentinel/releases/download/1.8.6/sentinel-dashboard-1.8.6.jar

# 2) 运行并指定登录账号
java -jar sentinel-dashboard.jar \
  --server.port=8858 \
  --spring.security.user.name=sentinel \
  --spring.security.user.password=sentinel

# 预期:浏览器访问 http://localhost:8858 登录成功

关键依赖与配置(Gateway + 服务侧)#

  • Gateway 侧依赖与配置(application.yml)
spring:
  cloud:
    gateway:
      routes:
        - id: api-user
          uri: http://127.0.0.1:8081
          predicates:
            - Path=/user/**
    sentinel:
      transport:
        dashboard: 127.0.0.1:8858
      datasource:
        nacos:
          nacos:
            server-addr: 127.0.0.1:8848
            dataId: gateway-flow-rules
            groupId: GATEWAY_GROUP
          rule-type: gw-flow
  • 服务侧依赖与配置(application.yml)
spring:
  cloud:
    sentinel:
      transport:
        dashboard: 127.0.0.1:8858
      datasource:
        nacos:
          nacos:
            server-addr: 127.0.0.1:8848
            dataId: service-flow-rules
            groupId: SERVICE_GROUP
          rule-type: flow
  • 服务侧资源注解与降级(示例)
@RestController
@RequestMapping("/user")
public class UserController {

    @GetMapping("/{id}")
    @SentinelResource(value = "getUser", blockHandler = "blockHandler")
    public String getUser(@PathVariable String id) {
        return "user:" + id;
    }

    public String blockHandler(String id, BlockException ex) {
        return "blocked user:" + id;
    }
}

Nacos 规则示例(可直接发布)#

  • Gateway 路由级限流规则(dataId: gateway-flow-rules)
[
  {
    "resource": "api-user",
    "resourceMode": 0,
    "grade": 1,
    "count": 5,
    "intervalSec": 1,
    "burst": 2
  }
]
  • 服务侧 QPS 规则(dataId: service-flow-rules)
[
  {
    "resource": "getUser",
    "limitApp": "default",
    "grade": 1,
    "count": 10,
    "strategy": 0,
    "controlBehavior": 0
  }
]

关键命令与效果验证#

  • 压测与观测
# 1) 网关入口压测
ab -n 200 -c 50 http://127.0.0.1:8080/user/1

# 2) 直接请求服务(验证服务侧保护)
ab -n 200 -c 50 http://127.0.0.1:8081/user/1

# 预期:网关入口出现限流响应;服务侧触发 blockHandler
  • 查看 Nacos 配置是否下发
# 查看 Nacos 配置是否存在(替换实际 namespaceId)
curl -s "http://127.0.0.1:8848/nacos/v1/cs/configs?dataId=gateway-flow-rules&group=GATEWAY_GROUP"

典型排错流程(含命令)#

1) 网关规则不生效

# 检查 Gateway 是否连接 Sentinel Dashboard
grep -R "sentinel" /var/log/gateway/*.log
# 检查 dataId/groupId 是否一致
curl -s "http://127.0.0.1:8848/nacos/v1/cs/configs?dataId=gateway-flow-rules&group=GATEWAY_GROUP"

2) 服务侧未保护

# 检查 @SentinelResource 是否生效
grep -R "SentinelResource" -n ./src/main/java

# 查看服务日志是否有 Sentinel 连接信息
grep -R "Sentinel" /var/log/service/*.log

3) 规则下发但被“误伤”

# 查看当前规则阈值
curl -s "http://127.0.0.1:8858/sentinel/api/gateway/flow/rules?app=gateway-app"
# 根据结果调整 count/intervalSec

练习与实践#

1) 练习一:入口限流 + 服务兜底
- 目标:Gateway QPS=5,服务 QPS=10
- 步骤:分别配置 gateway-flow-rules 与 service-flow-rules,使用 ab 压测验证限流发生在入口。
2) 练习二:灰度路由联动限流
- 目标:灰度版本流量 10%,限流阈值设为 2 QPS
- 步骤:Gateway 添加灰度路由 predicate,并为灰度路由单独配置 gw-flow 规则,压测验证灰度请求更早被限制。
3) 练习三:热点参数限流
- 目标:限制同一用户 ID 的请求
- 步骤:在服务侧配置热点参数规则,对 id 维度设置阈值,验证单一用户被限制而整体不受影响。