微服务保护方案的基础使用(Sentinel)
·
保护方案
1.熔断 (Circuit Breaker)
依赖服务 异常/超时/慢调用 达到阈值(如错误率 > 50%)
防止雪崩:当依赖服务故障时,快速失败,避免调用方资源耗尽
2.降级(Degradation / Fallback)
保证核心功能可用:牺牲非核心功能,返回兜底数据
主动或被动:熔断、限流、系统负载高、手动开关等
3.超时 (Timeout)
4.线程隔离 (Thread Isolation)
5.限流 (Rate Limiting)
导依赖
<!--sentinel-->
<dependency>
<groupId>com.alibaba.cloud</groupId>
<artifactId>spring-cloud-starter-alibaba-sentinel</artifactId>
</dependency>
yml
spring:
application:
name: your-service
cloud:
sentinel:
http-method-specify: true #区分get,update,delete
# 启用 Sentinel(默认 true,可省略)
enabled: true
# 连接 Sentinel Dashboard(关键!)
transport:
dashboard: localhost:8080 # Sentinel 控制台地址
port: 8719 # 本地启动的 HTTP 端口(默认 8719,用于 Dashboard 拉取信息)
# Web 接口资源名前缀(可选,默认 /)
web-context-unify: false # 设为 false 可按 Controller 方法生成资源名(推荐)
# Feign 支持(如果用了 Feign)
feign:
enabled: true # 默认 true,启用 Feign 熔断
# 数据源持久化(如推送到 Nacos,可选)
datasource:
# 示例:从 Nacos 加载流控规则
ds1:
nacos:
server-addr: localhost:8848
data-id: ${spring.application.name}-sentinel-rules
group-id: DEFAULT_GROUP
data-type: json
rule-type: flow
openfeign开启对Sentinel的支持
spring:
cloud:
sentinel:
enabled: true
# 启用 Feign 的 Sentinel 支持
feign:
enabled: true
@FeignClient(name="item-service",path = "/items",fallbackFactory= 降级类名.class)
1.定义一个降级类实现FallbackFactory接口
2.重写create方法
3.重写降级方法
更多推荐


所有评论(0)