BAEL-3636 Add degrade and system protection rules configurations

This commit is contained in:
Amit Bhave 2021-04-04 21:49:59 +05:30
parent cf7705caee
commit 71119f2193
1 changed files with 32 additions and 2 deletions

View File

@ -2,8 +2,12 @@ package com.baeldung.spring.cloud.sentinel.config;
import com.alibaba.csp.sentinel.annotation.aspectj.SentinelResourceAspect; import com.alibaba.csp.sentinel.annotation.aspectj.SentinelResourceAspect;
import com.alibaba.csp.sentinel.slots.block.RuleConstant; 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 com.alibaba.csp.sentinel.slots.block.flow.FlowRule; import com.alibaba.csp.sentinel.slots.block.flow.FlowRule;
import com.alibaba.csp.sentinel.slots.block.flow.FlowRuleManager; import com.alibaba.csp.sentinel.slots.block.flow.FlowRuleManager;
import com.alibaba.csp.sentinel.slots.system.SystemRule;
import com.alibaba.csp.sentinel.slots.system.SystemRuleManager;
import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration; import org.springframework.context.annotation.Configuration;
@ -14,17 +18,25 @@ import java.util.List;
@Configuration @Configuration
public class SentinelAspectConfiguration { public class SentinelAspectConfiguration {
public static final String RESOURCE_NAME = "greeting";
@Bean @Bean
public SentinelResourceAspect sentinelResourceAspect() { public SentinelResourceAspect sentinelResourceAspect() {
return new SentinelResourceAspect(); return new SentinelResourceAspect();
} }
@PostConstruct @PostConstruct
public void initFlowRules() { public void init() {
initFlowRules();
initDegradeRules();
initSystemProtectionRules();
}
private void initFlowRules() {
List<FlowRule> flowRules = new ArrayList<>(); List<FlowRule> flowRules = new ArrayList<>();
FlowRule flowRule = new FlowRule(); FlowRule flowRule = new FlowRule();
// Defined resource // Defined resource
flowRule.setResource("greeting"); flowRule.setResource(RESOURCE_NAME);
flowRule.setGrade(RuleConstant.FLOW_GRADE_QPS); flowRule.setGrade(RuleConstant.FLOW_GRADE_QPS);
// number of requests that QPS can pass in a second // number of requests that QPS can pass in a second
flowRule.setCount(1); flowRule.setCount(1);
@ -32,4 +44,22 @@ public class SentinelAspectConfiguration {
FlowRuleManager.loadRules(flowRules); FlowRuleManager.loadRules(flowRules);
} }
private void initDegradeRules() {
List<DegradeRule> rules = new ArrayList<DegradeRule>();
DegradeRule rule = new DegradeRule();
rule.setResource(RESOURCE_NAME);
rule.setCount(10);
rule.setTimeWindow(10);
rules.add(rule);
DegradeRuleManager.loadRules(rules);
}
private void initSystemProtectionRules() {
List<SystemRule> rules = new ArrayList<>();
SystemRule rule = new SystemRule();
rule.setHighestSystemLoad(10);
rules.add(rule);
SystemRuleManager.loadRules(rules);
}
} }