Sentinel[超详细讲解]-1
定义一系列 规则 👺,对资源进行 保护 👺, 如果违反的了规则,则抛出异常,看是否有fallback兜底处理,如果没有则直接返回异常信息😎
1. 快速入门
1.1 引入 Sentinel 依赖
<dependency>
<groupId>com.alibaba.cloud</groupId>
<artifactId>spring-cloud-starter-alibaba-sentinel</artifactId>
</dependency>
1.2 配置连接
spring:
cloud:
sentinel:
transport:
dashboard: localhost:8080
1.3 创建资源
注解 @SentinelResource 创建
编码示例:
@SentinelResource("HelloWorld")
public void helloWorld() {
// 资源中的逻辑
System.out.println("hello world");
}
1.4 定义规则
Sentinel 提供了丰富的类库,可以定义各种规则,如:流控规则、熔断规则、热点规则、系统规则、授权规则等
编码示例:流量控制规则 (FlowRule)
// 定义流量控制规则
FlowRule rule = new FlowRule();
rule.setResource("orderQuery"); // 资源名称
rule.setGrade(RuleConstant.FLOW_GRADE_QPS); // 限流阈值类型(QPS/线程数)
rule.setCount(10); // 阈值(每秒10次)
rule.setLimitApp("default"); // 针对来源(default代表不区分来源)
rule.setControlBehavior(RuleConstant.CONTROL_BEHAVIOR_DEFAULT); // 流控效果(直接拒绝)
rule.setClusterMode(false); // 是否集群模式
// 加载规则
List<FlowRule> rules = new ArrayList<>();
rules.add(rule);
FlowRuleManager.loadRules(rules);
1.5 启动控制台
java -Dserver.port=8230 -jar sentinel-dashboard-1.8.6.jar
对应包 链接: 百度网盘 请输入提取码百度网盘为您提供文件的网络备份、同步和分享服务。空间大、速度快、安全稳固,支持教育网加速,支持手机端。注册使用百度网盘即可享受免费存储空间https://pan.baidu.com/s/1zc-usWQF_IIf-mxeZxhU1Q 提取码: cvp1 【若请没有联系我😘】