Sentinel 基本使用 - 资源与规则

Sentinel 基本使用 - 资源与规则

一、资源定义
  1. 什么是资源

    • 资源是被 Sentinel 保护的对象,通常是一个接口、方法或代码块。
    • 在 Spring Cloud Gateway 中,资源通常是路由路径(如 /api/user/**)。
  2. 如何定义资源

    • 自动识别:Sentinel 会自动识别 Spring MVC 的 @RequestMapping 注解标记的方法。
    • 手动定义:可以通过 SphU.entry("resourceName") 手动定义资源(适用于非 Web 场景)。
    • Gateway 资源:在 Spring Cloud Gateway 中,资源通常是路由的 ID(如 service1_route)。
二、规则类型

Sentinel 支持以下几种规则:

  1. 流量控制规则(FlowRule)

    • 作用:控制资源的访问流量(如 QPS、线程数)。
    • 关键参数
      • resource:资源名称
      • grade:控制维度(QPSTHREAD
      • count:阈值
      • limitApp:来源应用(default 表示所有来源)
  2. 熔断降级规则(DegradeRule)

    • 作用:当资源出现异常或响应慢时自动降级。
    • 关键参数
      • resource:资源名称
      • grade:降级策略(ERROR_RATIOERROR_COUNTSLOW_RATIO
      • count:阈值
      • timeWindow:熔断时长(毫秒)
  3. 系统保护规则(SystemRule)

    • 作用:保护整个系统的稳定性(如 CPU 使用率、负载)。
    • 关键参数
      • highestSystemLoad:最高系统负载
      • avgRt:平均响应时间
      • maxThread:最大线程数
三、基本使用示例
1. 流量控制规则(FlowRule)
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); // 加载规则
    }
}
2. 熔断降级规则(DegradeRule)
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 rule = new DegradeRule();
        rule.setResource("service1_route"); // 资源名称
        rule.setGrade(RuleConstant.DEGRADE_GRADE_EXCEPTION_RATIO); // 降级策略(异常比例)
        rule.setCount(0.5); // 异常比例阈值(50%)
        rule.setTimeWindow(10000); // 熔断时长(10秒)
        rules.add(rule);
        DegradeRuleManager.loadRules(rules); // 加载规则
    }
}
3. 在 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();
    }
}
四、规则管理
  1. 动态配置
    • 可以通过 Sentinel Dashboard 或配置中心(如 Nacos)动态调整规则。
  2. 规则加载
    • Sentinel 会定期从配置中心加载最新的规则。
五、总结
  • 资源:被 Sentinel 保护的对象(如接口、路由路径)。
  • 规则:控制流量、熔断降级、系统保护的策略。
  • 使用方式
    • 手动定义资源(SphU.entry
    • 自动识别资源(Spring MVC、Gateway)
    • 动态配置规则(Dashboard 或配置中心)

Sentinel 的核心是通过 资源 + 规则 的组合实现对系统的保护。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值