Introduction

To use Sentinel, you only need to complete 2 steps:

  • Define resources
  • Define rules
    These two steps don’t have to be synchronized. As long as the resources are defined, you can add rules as needed. Multiple rules can be applied to the same resource simultaneously.

Sentinel provides adaptions to popular frameworks as well. After introducing these adapters, services and methods provided by these frameworks are defined as resources by default.

Define Resource

You can use one of the following two methods to define resources.

"try" and "catch" mode

  1. Entry entry = null;
  2. try {
  3. entry = SphU.entry(KEY);
  4.  
  5. // Your business logic here.
  6. } catch (BlockException ex) {
  7. // Resource is rejected.
  8.  
  9. // Here to handle the block exception
  10. } finally {
  11. if (entry != null) {
  12. entry.exit();
  13. }
  14. }

Bool Mode

  1. if(SphO.entry("helloworld")){
  2. try {
  3. /**
  4. * code logic
  5. */
  6. } finally {
  7. SphO.exit();
  8. }
  9. }else{
  10. //resource is rejected
  11. /**
  12. * logic to handle exception
  13. */
  14. }
  15. }

For details, please refer to Adapters to popular frameworks.

Define Rules

Sentinel provides APIs for you to modify your rules, which can be integrated with various kinds of rule repository, such as configuration server and SQL.

Definition of Rules

There are 3 types of rules:flow control rules, circuit breaking rules, and system protection rules.

Flow control rules (FlowRule)

Definition:

key fields:

Field Description Default value
resource resource name, defined in entry()
count threshold
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

Multiple rules can be applied to the same resource.

API

API:FlowRuleManager.loadRules() can be used to configure flow control rules.

  1. private static void initFlowQpsRule() {
  2. List<FlowRule> rules = new ArrayList<FlowRule>();
  3. FlowRule rule1 = new FlowRule();
  4. rule1.setResource(KEY);
  5. // set limit qps to 20
  6. rule1.setCount(20);
  7. rule1.setGrade(RuleConstant.FLOW_GRADE_QPS);
  8. rule1.setLimitApp("default");
  9. rules.add(rule1);
  10. FlowRuleManager.loadRules(rules);
  11. }

For more details please refer to Flow control.

Circuit breaking rules (DegradeRule)

Key fields:

Field Description Default value
resource resource name
count threshold
grade by response time or exception ratio response time
timeWindow degrade time window

Multiple rules can be applied to the same resource.

API

API:DegradeRuleManager.loadRules() can be used to configure degradation rules.

  1. private static void initDegradeRule() {
  2. List<DegradeRule> rules = new ArrayList<DegradeRule>();
  3. DegradeRule rule = new DegradeRule();
  4. rule.setResource(KEY);
  5. // set threshold rt, 10 ms
  6. rule.setCount(10);
  7. rule.setGrade(RuleConstant.DEGRADE_GRADE_RT);
  8. rule.setTimeWindow(10);
  9. rules.add(rule);
  10. DegradeRuleManager.loadRules(rules);
  11. }

For more details, please refer to Circuit Break.

System protection rules (SystemRule)

Key factors

Field Description Default value
highestSystemLoad threshold of Load1 -1(not valid)
avgRt average response time -1(not valid)
maxThread concurrent thread count -1(not valid)

API

API:SystemRuleManager.loadRules()

  1. private static void initDegradeRule() {
  2. List<SystemRule> rules = new ArrayList<SystemRule>();
  3. SystemRule rule = new SystemRule();
  4. rule.setHighestSystemLoad(10);
  5. rules.add(rule);
  6. DegradeRuleManager.loadRules(rules);
  7. }

For more details, please refer to System Protection.

HTTP commands for rules

You can also use HTTP commands to configure, query and update Sentinel rules.

To use these commands, make sure that the following library has been introduced:

  1. <dependency>
  2. <groupId>com.alibaba.csp</groupId>
  3. <artifactId>sentinel-transport-simple-http</artifactId>
  4. <version>x.y.z</version>
  5. </dependency>

Query command

  1. curl http://localhost:8719/getRules?type=<XXXX>
  • type=flow for flow rules;
  • type=degrade for circuit breaking rules;
  • type=system for system protection rules.
    Rules will be returned in JSON format.

Modification command

  1. curl http://localhost:8719/setRules?type=<XXXX>&data=<DATA>

Integrate with rule repositories

DataSource is designed to integrate rules to customized repositories and make rules persistent.

For more details, you can refer to Dynamic Rules.

原文: https://github.com/alibaba/Sentinel/wiki/How-to-Use