Sentinel 基本使用 - 流量控制
一、流量控制概述
流量控制是 Sentinel 的核心功能之一,用于限制系统或服务的访问流量,防止过载。通过设置流量阈值,可以控制单位时间内的请求数量(QPS)或并发线程数。
二、流量控制规则(FlowRule)
流量控制规则定义了如何限制资源的访问流量,主要包括以下参数:
- 资源名称(resource):被保护的资源名称,如接口路径或方法名。
- 控制维度(grade):
QPS
:基于每秒查询数进行流量控制。THREAD
:基于并发线程数进行流量控制。
- 阈值(count):设置的流量限制值,如 QPS 为 10 表示每秒最多允许 10 个请求。
- 来源应用(limitApp):限制的来源应用,默认为
default
表示所有来源。
三、流量控制的基本使用步骤
1. 添加 Sentinel 依赖
在 Spring Boot 项目中添加 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.flow.FlowRule;
import com.alibaba.csp.sentinel.slots.block.flow.FlowRuleManager;
import java.util.ArrayList;
import java.util.List;
public class FlowRuleConfig {
public static void initFlowRules() {
List<FlowRule> rules = new ArrayList<>();
FlowRule rule = new FlowRule();
rule.setResource("service1_route"); // 资源名称
rule.setGrade(RuleConstant.FLOW_GRADE_QPS); // 控制维度(QPS)
rule.setCount(10); // QPS 阈值
rules.add(rule);
FlowRuleManager.loadRules(rules); // 加载规则
}
}
在应用启动时调用 initFlowRules
方法加载规则:
import org.springframework.boot.CommandLineRunner;
import org.springframework.stereotype.Component;
@Component
public class SentinelRunner implements CommandLineRunner {
@Override
public void run(String... args) throws Exception {
FlowRuleConfig.initFlowRules();
}
}
3. 在 Spring Cloud Gateway 中使用流量控制
Sentinel 与 Spring Cloud Gateway 集成时,可以通过配置路由规则实现流量控制:
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();
}
}
在 application.yml
中配置路由规则和 Sentinel 规则:
spring:
cloud:
gateway:
routes:
- id: service1_route
uri: http://service1
predicates:
- Path=/api/service1/**
filters:
- name: SentinelGatewayFilter
args:
resource: service1_route
在 Sentinel 控制台或代码中配置流量控制规则:
import com.alibaba.csp.sentinel.slots.block.RuleConstant;
import com.alibaba.csp.sentinel.slots.block.flow.FlowRule;
import com.alibaba.csp.sentinel.slots.block.flow.FlowRuleManager;
import java.util.ArrayList;
import java.util.List;
public class SentinelGatewayRuleConfig {
public static void initGatewayRules() {
List<FlowRule> rules = new ArrayList<>();
FlowRule rule = new FlowRule();
rule.setResource("service1_route"); // 资源名称
rule.setGrade(RuleConstant.FLOW_GRADE_QPS); // 控制维度(QPS)
rule.setCount(10); // QPS 阈值
rules.add(rule);
FlowRuleManager.loadRules(rules); // 加载规则
}
}
四、流量控制的应用场景
- 接口限流:限制某个接口的访问频率,防止恶意请求或突发流量。
- 服务保护:保护后端服务不被过载,确保服务的稳定性。
- 资源隔离:对不同的资源设置不同的流量限制,实现资源的隔离保护。
五、总结
- 流量控制规则:通过
FlowRule
定义流量限制的规则,包括资源名称、控制维度、阈值等。 - 集成方式:可以通过代码或配置文件配置流量控制规则,也可以在 Spring Cloud Gateway 中集成 Sentinel 实现流量控制。
- 应用场景:适用于接口限流、服务保护、资源隔离等场景,确保系统的稳定性和可靠性。
通过 Sentinel 的流量控制功能,可以有效保护系统免受过载的影响,提高系统的稳定性和可靠性。