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