-
Notifications
You must be signed in to change notification settings - Fork 8.1k
How to Use
There are mainly 2 steps to use Sentinel:
- Define resource
- Define rules
These two steps don’t have to be synchronized. As long as the resources are defined, you can add rules as necessary. Resources can be applied to multiple rules simultaneously.
Sentinel provides adaptions to popular frameworks as well. After introducing these adapters, services and methods provided by these framework are defined as resources by default.
Entry entry = null;
try{
entry = SphU.entry("hellowrold");
/**
* code logic
*/
} catch (BlockException e1) {
//resource is rejected
/**
* logic to handle exception
*/
}finally{
if(entry != null){
entry.exit();
}
}
}
if(SphO.entry("helloworld")){
try {
/**
* code logic
*/
} finally {
SphO.exit();
}
}else{
//resource is rejected
/**
* logic to handle exception
*/
}
}
Details please check: Adapters to popular frameworks
Sentinel provides APIs for users to modify their rules which can be integrated with various kinds of rule repository, such as configuration server, sql, etc.
There are 3 kinds:Flow control rule、Circuit breaking rule以及System protection rule。
key fields:
field | description | default value |
---|---|---|
resource | resource name, defined in entry() | |
count | thredhold | |
grade | calculated by QPS or concurrent thread count | QPS |
limitApp | refer to specified caller | default |
strategy | by resource itself or other resource (refResource),or entry (refResource) | resource itself |
controlBehavior | reject directly,queue,slow start up | reject directly |
one resource can apply more than 1 rules
API:FlowRuleManager.loadRules() can be used to configure rules.
private static void initFlowQpsRule() {
List<FlowRule> rules = new ArrayList<FlowRule>();
FlowRule rule1 = new FlowRule();
rule1.setResource(KEY);
// set limit qps to 20
rule1.setCount(20);
rule1.setGrade(RuleConstant.FLOW_GRADE_QPS);
rule1.setLimitApp("default");
rules.add(rule1);
FlowRuleManager.loadRules(rules);
}
More details please refer to: Flow control
Key fields:
field | description | default value |
---|---|---|
resource | resource name | |
count | thredhold | |
grade | by response time or exception ratio | response time |
timeWindow | degrade time window |
one resource can apply more than 1 rules
API:DegradeRuleManager.loadRules() can be used to configure rules.
private static void initDegradeRule() {
List<DegradeRule> rules = new ArrayList<DegradeRule>();
DegradeRule rule = new DegradeRule();
rule.setResource(KEY);
// set threshold rt, 10 ms
rule.setCount(10);
rule.setGrade(RuleConstant.DEGRADE_GRADE_RT);
rule.setTimeWindow(10);
rules.add(rule);
DegradeRuleManager.loadRules(rules);
}
More details can refer to :Circuit Break
规则包含下面几个重要的属性:
field | 说明 | 默认值 |
---|---|---|
highestSystemLoad | 最大的Load1 | -1(不生效) |
avgRt | 所有入口流量的平均相应时间 | -1(不生效) |
maxThread | 入口流量的最大并发数 | -1(不生效) |
理解上面规则的定义之后,我们可以通过调用API:SystemRuleManager.loadRules()来硬编码的定义流量控制规则
private static void initDegradeRule() {
List<SystemRule> rules = new ArrayList<SystemRule>();
SystemRule rule = new SystemRule();
rule.setHighestSystemLoad(10);
rules.add(rule);
DegradeRuleManager.loadRules(rules);
}
更多详情可以参考:系统负载保护
运行下面命令,则会返回现有生效的规则:
curl http://localhost:8719/getRules?type=<XXXX>
其中,type=flow
以 JSON 格式返回现有的限流塑形规则;degrade 则返回现有生效的降级规则列表;system 则返回系统保护规则。
我们同时也可以通过下面命令,来修改已有规则:
curl http://localhost:8719/setRules?type=<XXXX>&data=<DATA>
其中,type 可以输入 flow
、degrade
等方式来制定更改的规则种类,data
则是对应的 JSON 格式的规则。
上面的规则配置,都是出于内存状态的。即如果应用发生了重启,这个规则就会失效。我们提供了开放的接口。你可以通过实现 DataSource 的方式,来自定义自己的持久化规则。通常我们的建议有:
- 整合 Diamond,动态的实时刷新配置规则
- 结合 SQL、GitHub 等来实现该规则
- 配合 Dashboard 使用
更多的详情请参见: 动态规则配置
-
文档
-
Documents
- Read Me
- Introduction
- How to Use
- How it Works
- Flow Control
- Parameter Flow Control
- Cluster Flow Control
- API Gateway Flow Control
- Circuit Breaking
- Adaptive System Protection
- Metrics
- General Configuration
- Dynamic Rule Configuration
- Dashboard
- Integrations with open-source frameworks
- Contribution Guideline