在 Sentinel 中,资源是用于流量控制和熔断的基本单元。定义资源是为了能够对其进行限流、降级等操作。资源可以是一个方法、一个 URL、一个类或任何其他需要进行流量控制的对象。通过定义资源,Sentinel 可以为每个资源单独配置限流规则、降级策略等。
资源的定义方法
在 Sentinel 中,有多种方式可以定义资源:
1. 使用 @SentinelResource
注解
这是最常用的方法之一,适用于 Spring Boot 应用程序。通过在方法或类上添加 @SentinelResource
注解,可以方便地定义资源。
import com.alibaba.csp.sentinel.annotation.SentinelResource;
public class ResourceExample {
@SentinelResource(value = "resourceA")
public String resourceA() {
// 业务逻辑
return "Hello from resourceA";
}
@SentinelResource(value = "resourceB")
public String resourceB() {
// 业务逻辑
return "Hello from resourceB";
}
}
在上面的例子中,resourceA
和 resourceB
分别定义了两个资源。
2. 使用 SphU.entry
方法
对于非 Spring Boot 应用程序或不希望使用注解的情况下,可以使用 SphU.entry
方法来显式地定义资源。
import com.alibaba.csp.sentinel.Entry;
import com.alibaba.csp.sentinel.SphU;
import com.alibaba.csp.sentinel.Tracer;
import com.alibaba.csp.sentinel.slots.block.BlockException;
public class ResourceExample {
public String processRequest() {
try {
Entry entry = SphU.entry("resourceC");
// 业务逻辑
return "Hello from resourceC";
} catch (BlockException e) {
Tracer.trace(e, "Blocked by Sentinel");
return "Too many requests, please try again later.";
}
}
}
在上面的例子中,processRequest
方法中通过 SphU.entry("resourceC")
显式地定义了一个名为 resourceC
的资源。
3. 使用 Block
注解
除了 @SentinelResource
注解外,Sentinel 还提供了 @Block
注解,可以用于标记那些需要进行流量控制的方法或类。
import com.alibaba.csp.sentinel.annotation.Block;
import com.alibaba.csp.sentinel.annotation.SentinelResource;
public class ResourceExample {
@Block(value = "resourceD")
public String resourceD() {
// 业务逻辑
return "Hello from resourceD";
}
}
资源的使用方法
定义资源后,可以为其配置各种流量控制和降级策略。以下是一些常见的使用方法:
1. 配置限流规则
可以为定义的资源配置限流规则,例如设置每秒的最大请求数(QPS)。
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 ResourceConfig {
public static void initFlowRules() {
List<FlowRule> rules = new ArrayList<>();
FlowRule rule = new FlowRule();
rule.setResource("resourceA");
rule.setCount(20); // 每秒允许的请求数量
rule.setGrade(com.alibaba.csp.sentinel.slots.block.RuleConstant.FLOW_GRADE_QPS); // 限流模式: QPS
rules.add(rule);
FlowRuleManager.loadRules(rules);
}
}
2. 配置降级规则
可以为定义的资源配置降级规则,例如基于异常比例、异常数或响应时间进行降级。
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 ResourceConfig {
public static void initDegradeRules() {
List<DegradeRule> rules = new ArrayList<>();
DegradeRule rule = new DegradeRule();
rule.setResource("resourceA");
rule.setCount(0.1); // 异常比例阈值
rule.setGrade(com.alibaba.csp.sentinel.slots.block.degrade.DegradeRuleConstant.DEGRADE_GRADE_EXCEPTION_RATIO); // 降级模式: 异常比例
rule.setTimeWindow(10); // 时间窗口长度,单位秒
rules.add(rule);
DegradeRuleManager.loadRules(rules);
}
}
3. 配置热点参数限流
可以为定义的资源配置热点参数限流规则,即对请求中的某些参数进行限流。
import com.alibaba.csp.sentinel.slots.block.flow.param.ParamFlowRule;
import com.alibaba.csp.sentinel.slots.block.flow.param.ParamFlowRuleManager;
import java.util.ArrayList;
import java.util.List;
public class ResourceConfig {
public static void initHotParamRules() {
List<ParamFlowRule> rules = new ArrayList<>();
ParamFlowRule rule = new ParamFlowRule("resourceA")
.setParamIdx(0) // 设置第一个参数为热点参数
.setCount(5); // 设置限流阈值,每秒最多5次请求
rules.add(rule);
ParamFlowRuleManager.loadRules(rules);
}
}
总结
通过上述方法,你可以定义 Sentinel 中的资源,并为这些资源配置各种流量控制和降级策略。定义资源是使用 Sentinel 进行流量控制的基础,而合理配置规则则是实现有效流量控制的关键。根据你的具体需求,可以选择适合的方法来定义资源,并根据资源的特点来配置相应的规则。