在一些情况下,不需要使用nacos等其他组件保存限流规则,且限流规则一般不改变。就可以使用配置文件的方式配置限流规则了。
限流参数解释: 对应的值可以在com.alibaba.csp.sentinel.slots.block.RuleConstant.java中查到
Field | 说明 | 默认值 |
---|---|---|
resource | 资源名,资源名是限流规则的作用对象 | |
count | 限流阈值 | |
grade | 限流阈值类型,QPS 或线程数模式 | QPS 模式 |
limitApp | 流控针对的调用来源 | default ,代表不区分调用来源 |
strategy | 判断的根据是资源自身,还是根据其它关联资源 (refResource ),还是根据链路入口 |
根据资源本身 |
controlBehavior | 流控效果(直接拒绝 / 排队等待 / 慢启动模式) | 直接拒绝 |
同一个资源可以同时有多个限流规则。
public final class RuleConstant {
public static final int FLOW_GRADE_THREAD = 0; //限流 基于线程数
public static final int FLOW_GRADE_QPS = 1; //限流 基于QPS
public static final int DEGRADE_GRADE_RT = 0; //降级 , 代表一秒内该资源的平均响应时间
/**
* Degrade by biz exception ratio in the current {@link IntervalProperty#INTERVAL} second(s).
*/
public static final int DEGRADE_GRADE_EXCEPTION_RATIO = 1; // 降级 异常比例
/**
* Degrade by biz exception count in the last 60 seconds.
*/
public static final int DEGRADE_GRADE_EXCEPTION_COUNT = 2;// 降级, 异常数
public static final int AUTHORITY_WHITE = 0;// 认证, 白名单
public static final int AUTHORITY_BLACK = 1;// 认证, 黑名单
public static final int STRATEGY_DIRECT = 0; //
public static final int STRATEGY_RELATE = 1;
public static final int STRATEGY_CHAIN = 2;
public static final int CONTROL_BEHAVIOR_DEFAULT = 0;// 限流行为,直接拒绝
public static final int CONTROL_BEHAVIOR_WARM_UP = 1;// 限流行为,WARM_UP
public static final int CONTROL_BEHAVIOR_RATE_LIMITER = 2;// 限流行为,匀速排队
public static final int CONTROL_BEHAVIOR_WARM_UP_RATE_LIMITER = 3;
public static final String LIMIT_APP_DEFAULT = "default";
public static final String LIMIT_APP_OTHER = "other";
public static final int DEFAULT_SAMPLE_COUNT = 2;
public static final int DEFAULT_WINDOW_INTERVAL_MS = 1000;
private RuleConstant() {}
}
0、依赖包
4.0.0
org.springframework.boot
spring-boot-starter-parent
2.0.6.RELEASE
com.mei
SpringBootSentinel
0.01
1.8
org.springframework.boot
spring-boot-starter
org.springframework.boot
spring-boot-starter-logging
org.springframework.boot
spring-boot-starter-log4j
1.3.8.RELEASE
com.alibaba.cloud
spring-cloud-starter-alibaba-sentinel
2.1.0.RELEASE
org.springframework.boot
spring-boot-starter-web
org.springframework.boot
spring-boot-maven-plugin
1、开启注解
package com.mei.config;
import com.alibaba.csp.sentinel.annotation.aspectj.SentinelResourceAspect;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
@Configuration
public class AopConfiguration {
@Bean
public SentinelResourceAspect sentinelResourceAspect() {
return new SentinelResourceAspect();
}
}
2、资源
package com.mei.controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.alibaba.csp.sentinel.annotation.SentinelResource;
import com.alibaba.csp.sentinel.slots.block.BlockException;
import com.alibaba.csp.sentinel.slots.block.RuleConstant;
@RestController
public class HelloController {
@RequestMapping("/helloworld3")
@SentinelResource(value="helloworld3",blockHandler="helloworld3Handler")
public String helloworld3() {
return "hello Sentinel333 !" ;
}
public static String helloworld3Handler(BlockException ex) {
System.out.println("Oops: " + ex.getClass().getCanonicalName());
return "系统限流了111....";
}
}
3、application.properties
spring.application.name=sentinel-test
server.port=8081
management.endpoints.web.exposure.include=*
spring.cloud.sentinel.transport.dashboard=localhost:8080
spring.cloud.sentinel.eager=true
spring.cloud.sentinel.datasource.ds1.file.file=classpath: flowrule.json
spring.cloud.sentinel.datasource.ds1.file.data-type=json
spring.cloud.sentinel.datasource.ds1.file.rule-type=flow
#spring.cloud.sentinel.datasource.ds2.file.file=classpath: degraderule.json
#spring.cloud.sentinel.datasource.ds2.file.data-type=json
#spring.cloud.sentinel.datasource.ds2.file.rule-type=degrade
4、配置限流规则
[
{
"resource": "helloworld3",
"controlBehavior": 2,
"count": 5,
"grade": 1,
"limitApp": "default",
"strategy": 0
}
]
4、启动应用,启动sentinel控制台
登录sentintel控制台,发现限流规则已经存在了。
快速访问测试, http://localhost:8081/helloworld3 , 确实起到了限流作用