Sentinel + Feign 整合与规则持久化
约 568 字大约 2 分钟
布欧-Lewyon
2026-05-15
首页 › Spring Cloud › 服务容错
Feign + Sentinel 整合
feign:
sentinel:
enabled: true # 开启 Feign 的 Sentinel 支持Feign Fallback 降级
@FeignClient(
name = "user-service",
path = "/api/users",
fallback = UserFeignClientFallback.class // 降级处理
)
public interface UserFeignClient {
@GetMapping("/{id}")
Result<User> getUser(@PathVariable("id") Long id);
}@Component
public class UserFeignClientFallback implements UserFeignClient {
@Override
public Result<User> getUser(Long id) {
log.warn("user-service 不可用,返回降级数据");
return Result.error("用户服务暂时不可用");
}
}Feign FallbackFactory(获取异常原因)
@FeignClient(
name = "product-service",
path = "/api/products",
fallbackFactory = ProductFeignClientFallbackFactory.class
)
public interface ProductFeignClient {
@GetMapping("/{id}")
Result<Product> getProduct(@PathVariable("id") Long id);
@PutMapping("/{id}/stock")
Result<Void> deductStock(@PathVariable("id") Long id,
@RequestParam("quantity") Integer quantity);
}@Component
public class ProductFeignClientFallbackFactory
implements FallbackFactory<ProductFeignClient> {
@Override
public ProductFeignClient create(Throwable cause) {
return new ProductFeignClient() {
@Override
public Result<Product> getProduct(Long id) {
log.error("商品服务调用失败: {}", cause.getMessage());
return Result.error("商品服务暂时不可用");
}
@Override
public Result<Void> deductStock(Long id, Integer quantity) {
log.error("扣减库存失败: {}", cause.getMessage());
return Result.error("库存服务暂时不可用");
}
};
}
}Sentinel 规则持久化到 Nacos
Sentinel 控制台配置的规则存储在内存中,重启丢失。通过 Nacos 持久化规则。
spring:
cloud:
sentinel:
datasource:
# 流控规则数据源
flow:
nacos:
server-addr: localhost:8848
data-id: ${spring.application.name}-sentinel-flow
group-id: DEFAULT_GROUP
data-type: json
rule-type: flow
# 降级规则数据源
degrade:
nacos:
server-addr: localhost:8848
data-id: ${spring.application.name}-sentinel-degrade
group-id: DEFAULT_GROUP
data-type: json
rule-type: degrade
# 热点参数规则
param-flow:
nacos:
server-addr: localhost:8848
data-id: ${spring.application.name}-sentinel-param-flow
group-id: DEFAULT_GROUP
data-type: json
rule-type: param-flow# Nacos → user-service-sentinel-flow
[
{
"resource": "getUser",
"grade": 1,
"count": 10,
"limitApp": "default",
"strategy": 0,
"controlBehavior": 0
},
{
"resource": "createOrder",
"grade": 1,
"count": 5,
"limitApp": "default",
"strategy": 0,
"controlBehavior": 0
}
]# Nacos → user-service-sentinel-degrade
[
{
"resource": "getUser",
"grade": 0,
"count": 200,
"timeWindow": 10,
"minRequestAmount": 5,
"slowRatioThreshold": 0.5
},
{
"resource": "createOrder",
"grade": 1,
"count": 0.1,
"timeWindow": 10
}
]完整配置总览
spring:
cloud:
sentinel:
transport:
dashboard: localhost:8858
eager: true
datasource:
flow:
nacos:
server-addr: ${spring.cloud.nacos.config.server-addr}
data-id: ${spring.application.name}-sentinel-flow
group-id: SENTINEL_GROUP
data-type: json
rule-type: flow
degrade:
nacos:
server-addr: ${spring.cloud.nacos.config.server-addr}
data-id: ${spring.application.name}-sentinel-degrade
group-id: SENTINEL_GROUP
data-type: json
rule-type: degrade
feign:
sentinel:
enabled: true小结
feign.sentinel.enabled=true开启 Feign + Sentinel 整合。fallback提供降级处理,fallbackFactory获取降级原因。- Sentinel 规则持久化到 Nacos:配置
datasource.flow/datasource.degrade指定 Nacos Data ID。 - 规则 JSON 数组格式,重启不丢失。
- 推荐将流控、降级、热点参数三种规则都持久化到 Nacos。
