跳到主要内容

Sentinel限流熔断集成

本文介绍如何集成 Sentinel 限流熔断功能(jeecg-boot-starter-sentinel)

前提:搭建 Sentinel 服务端

1. 启动 sentinel-dashboard 服务

找到 jeecg-server-cloud/jeecg-visual/jeecg-cloud-sentinel 项目下的类

com.alibaba.csp.sentinel.dashboard.JeecgSentinelApplication,右键运行

默认登录账号:sentinel/sentinel


第二步 集成 jeecg-boot-starter-sentinel

⚠️ v3.9.3+ 支持此starter 新版 Sentinel 已从 starter-cloud 中抽离为独立模块,需要显式引入依赖后才能使用。

1. 引入 jeecg-boot-starter-sentinel 依赖

<!-- 引入Sentinel限流熔断依赖 -->
<dependency>
<groupId>org.jeecgframework.boot3</groupId>
<artifactId>jeecg-boot-starter-sentinel</artifactId>
</dependency>

2. 开启 Feign Sentinel

application.yml 或 Nacos 配置中添加:

spring:
cloud:
sentinel:
transport:
dashboard: jeecg-boot-sentinel:9000
openfeign:
sentinel:
enabled: true # 默认 true

3. Feign 降级示例

配置 fallbackFactory 实现熔断降级:

@FeignClient(value = "jeecg-system", fallbackFactory = SysBaseAPIFallbackFactory.class)
public interface ISysBaseAPI {
// 调用失败时自动走 SysBaseAPIFallbackFactory
}

未配置 fallbackFactory 但返回类型为 Result 时,自动返回 Result.error()


模块功能

引入依赖后自动生效(无需额外配置),注册以下 Bean:

Bean说明
Feign.Builder自定义 Feign Builder,自动注入 fallback/fallbackFactory
BlockExceptionHandlerServlet 层限流降级,返回统一 JSON(HTTP 429)
RequestOriginParser基于请求参数 origin 或客户端 IP 的授权来源解析

限流降级响应

被 Sentinel 限流时返回 HTTP 429 + JSON:

BlockException 类型提示信息
FlowException访问频繁,请稍候再试
DegradeException系统降级
ParamFlowException热点参数限流
SystemBlockException系统规则限流或降级
AuthorityException授权规则不通过

请求来源解析

  • 优先取请求参数 origin
  • 参数为空时取 X-Forwarded-For 头(支持代理)
  • 最后 fallback 到 RemoteAddr

与 jeecg-boot-starter-cloud 的关系

微服务只需要 Nacos + Feign  → 只引 starter-cloud
微服务还需要限流熔断 → 再加 starter-sentinel

版本

组件版本
Spring Boot4.1.0
Spring Cloud2025.1.2
Spring Cloud Alibaba2025.1.0.0
Sentinel1.8.6+(BOM 管理)