Sentinel 基本使用 - 熔断降级
一、熔断降级概述
熔断降级是 Sentinel 的重要功能,当系统资源出现异常(如高延迟、高错误率)时,自动触发降级策略,保护系统稳定性。
二、熔断降级规则(DegradeRule)
熔断降级规则定义了触发降级的条件和降级行为,主要包括以下参数:
- 资源名称(resource):被保护的资源名称
- 降级策略(grade):
ERROR_RATIO
:异常比例降级(错误请求数/总请求数超过阈值时触发)ERROR_COUNT
:异常数降级(错误请求数超过阈值时触发)SLOW_RATIO
:慢调用比例降级(响应时间超过阈值的比例超过设定值时触发)
- 阈值(count):触发降级的阈值
- 熔断时长(timeWindow):降级持续的时间(毫秒)
- 最小请求数(minRequestAmount):统计周期内至少需要多少请求才会触发降级
三、熔断降级的基本使用步骤
1. 添加 Sentinel 依赖
<dependency>
<groupId>com.alibaba.cloud</groupId>
<artifactId>spring-cloud-starter-alibaba-sentinel</artifactId>
</dependency>
2. 配置熔断降级规则(代码方式)
import com.alibaba.csp.sentinel.slots.block.RuleConstant;
import com.alibaba.csp.sentinel.slots.block.degrade.DegradeRule;
import com.alibaba.csp.sentinel.slots.block.degrade.DegradeRuleManager;
import java.util.ArrayList;
import java.util.List;
public class DegradeRuleConfig {
public static void initDegradeRules() {
List<DegradeRule> rules = new ArrayList<>();
// 异常比例降级规则
DegradeRule rule1 = new DegradeRule();
rule1.setResource("service1_route"); // 资源名称
rule1.setGrade(RuleConstant.DEGRADE_GRADE_EXCEPTION_RATIO); // 异常比例降级
rule1.setCount(0.5); // 异常比例阈值(50%)
rule1.setTimeWindow(10000); // 熔断时长10秒
rule1.setMinRequestAmount(5); // 最小请求数
rules.add(rule1);
// 异常数降级规则
DegradeRule rule2 = new DegradeRule();
rule2.setResource("service2_route");
rule2.setGrade(RuleConstant.DEGRADE_GRADE_EXCEPTION_COUNT);
rule2.setCount(10); // 异常数阈值
rule2.setTimeWindow(15000);
rule2.setMinRequestAmount(3);
rules.add(rule2);
DegradeRuleManager.loadRules(rules); // 加载规则
}
}
在应用启动时加载规则:
import org.springframework.boot.CommandLineRunner;
import org.springframework.stereotype.Component;
@Component
public class SentinelRunner implements CommandLineRunner {
@Override
public void run(String... args) throws Exception {
DegradeRuleConfig.initDegradeRules();
}
}
3. 在 Spring Cloud Gateway 中使用熔断降级
在 application.yml
中配置路由和 Sentinel 规则:
spring:
cloud:
gateway:
routes:
- id: service1_route
uri: http://service1
predicates:
- Path=/api/service1/**
filters:
- name: SentinelGatewayFilter
args:
resource: service1_route
4. 自定义降级处理(可选)
可以自定义熔断时的响应内容:
import com.alibaba.csp.sentinel.adapter.gateway.sc.callback.BlockRequestHandler;
import org.springframework.http.HttpStatus;
import org.springframework.http.MediaType;
import org.springframework.web.reactive.function.server.ServerResponse;
import reactor.core.publisher.Mono;
public class CustomBlockHandler implements BlockRequestHandler {
@Override
public Mono<ServerResponse> handleRequest(ServerRequest request, Throwable throwable) {
return ServerResponse.status(HttpStatus.SERVICE_UNAVAILABLE)
.contentType(MediaType.APPLICATION_JSON)
.bodyValue("{\"code\":503,\"message\":\"服务暂时不可用,请稍后再试\"}");
}
}
配置自定义处理器:
import com.alibaba.csp.sentinel.adapter.gateway.sc.SentinelGatewayFilter;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
@Configuration
public class SentinelGatewayConfig {
@Bean
public SentinelGatewayFilter sentinelGatewayFilter() {
return new SentinelGatewayFilter();
}
@Bean
public BlockRequestHandler customBlockHandler() {
return new CustomBlockHandler();
}
}
四、熔断降级的应用场景
- 异常比例降级:当服务错误率过高时自动降级
- 异常数降级:当服务错误次数过多时自动降级
- 慢调用比例降级:当服务响应变慢时自动降级
五、总结
- 熔断降级规则:通过
DegradeRule
定义降级条件和行为 - 集成方式:可以通过代码或配置文件配置熔断降级规则
- 应用场景:适用于保护系统稳定性,防止异常或慢调用影响整体系统
- 自定义处理:可以自定义熔断时的响应内容,提供更好的用户体验
通过 Sentinel 的熔断降级功能,可以在系统出现异常或性能下降时自动保护系统,提高系统的稳定性和可靠性。