Java Code Examples for com.alibaba.csp.sentinel.slots.block.degrade.DegradeRuleManager#loadRules()

The following examples show how to use com.alibaba.csp.sentinel.slots.block.degrade.DegradeRuleManager#loadRules() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: ExceptionCountDegradeDemo.java    From Sentinel-Dashboard-Nacos with Apache License 2.0 6 votes vote down vote up
private static void initDegradeRule() {
    List<DegradeRule> rules = new ArrayList<DegradeRule>();
    DegradeRule rule = new DegradeRule();
    rule.setResource(KEY);
    // set limit exception count to 4
    rule.setCount(4);
    rule.setGrade(RuleConstant.DEGRADE_GRADE_EXCEPTION_COUNT);
    /**
     * When degrading by {@link RuleConstant#DEGRADE_GRADE_EXCEPTION_COUNT}, time window
     * less than 60 seconds will not work as expected. Because the exception count is
     * summed by minute, when a short time window elapsed, the degradation condition
     * may still be satisfied.
     */
    rule.setTimeWindow(10);
    rules.add(rule);
    DegradeRuleManager.loadRules(rules);
}
 
Example 2
Source File: ExceptionCountDegradeDemo.java    From Sentinel with Apache License 2.0 6 votes vote down vote up
private static void initDegradeRule() {
    List<DegradeRule> rules = new ArrayList<DegradeRule>();
    DegradeRule rule = new DegradeRule();
    rule.setResource(KEY);
    // set limit exception count to 4
    rule.setCount(4);
    rule.setGrade(RuleConstant.DEGRADE_GRADE_EXCEPTION_COUNT);
    /**
     * When degrading by {@link RuleConstant#DEGRADE_GRADE_EXCEPTION_COUNT}, time window
     * less than 60 seconds will not work as expected. Because the exception count is
     * summed by minute, when a short time window elapsed, the degradation condition
     * may still be satisfied.
     */
    rule.setTimeWindow(10);
    rules.add(rule);
    DegradeRuleManager.loadRules(rules);
}
 
Example 3
Source File: SentinelAutoConfigurationTests.java    From spring-cloud-alibaba with Apache License 2.0 6 votes vote down vote up
@Before
public void setUp() {
	FlowRule rule = new FlowRule();
	rule.setGrade(RuleConstant.FLOW_GRADE_QPS);
	rule.setCount(0);
	rule.setResource("GET:" + flowUrl);
	rule.setLimitApp("default");
	rule.setControlBehavior(RuleConstant.CONTROL_BEHAVIOR_DEFAULT);
	rule.setStrategy(RuleConstant.STRATEGY_DIRECT);
	FlowRuleManager.loadRules(Arrays.asList(rule));

	DegradeRule degradeRule = new DegradeRule();
	degradeRule.setGrade(RuleConstant.DEGRADE_GRADE_EXCEPTION_COUNT);
	degradeRule.setResource("GET:" + degradeUrl);
	degradeRule.setCount(0);
	degradeRule.setTimeWindow(60);
	DegradeRuleManager.loadRules(Arrays.asList(degradeRule));
}
 
Example 4
Source File: ExceptionRatioDegradeDemo.java    From Sentinel-Dashboard-Nacos with Apache License 2.0 5 votes vote down vote up
private static void initDegradeRule() {
    List<DegradeRule> rules = new ArrayList<DegradeRule>();
    DegradeRule rule = new DegradeRule();
    rule.setResource(KEY);
    // set limit exception ratio to 0.1
    rule.setCount(0.1);
    rule.setGrade(RuleConstant.DEGRADE_GRADE_EXCEPTION_RATIO);
    rule.setTimeWindow(10);
    rule.setMinRequestAmount(20);
    rules.add(rule);
    DegradeRuleManager.loadRules(rules);
}
 
Example 5
Source File: RtDegradeDemo.java    From Sentinel-Dashboard-Nacos with Apache License 2.0 5 votes vote down vote up
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);
}
 
Example 6
Source File: ExceptionRatioDegradeDemo.java    From Sentinel with Apache License 2.0 5 votes vote down vote up
private static void initDegradeRule() {
    List<DegradeRule> rules = new ArrayList<DegradeRule>();
    DegradeRule rule = new DegradeRule();
    rule.setResource(KEY);
    // set limit exception ratio to 0.1
    rule.setCount(0.1);
    rule.setGrade(RuleConstant.DEGRADE_GRADE_EXCEPTION_RATIO);
    rule.setTimeWindow(10);
    rule.setMinRequestAmount(20);
    rules.add(rule);
    DegradeRuleManager.loadRules(rules);
}
 
Example 7
Source File: RtDegradeDemo.java    From Sentinel with Apache License 2.0 5 votes vote down vote up
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);
}
 
Example 8
Source File: FooConsumerExceptionDegradeBootstrap.java    From Sentinel with Apache License 2.0 5 votes vote down vote up
public static void initExceptionFallback(int timewindow) {
    DegradeRule degradeRule = new DegradeRule(INTERFACE_RES_KEY)
            .setCount(0.5)
            .setGrade(RuleConstant.DEGRADE_GRADE_EXCEPTION_RATIO)
            .setTimeWindow(timewindow)
            .setMinRequestAmount(1);
    DegradeRuleManager.loadRules(Collections.singletonList(degradeRule));

}
 
Example 9
Source File: AppLifecycleBean.java    From Sentinel with Apache License 2.0 5 votes vote down vote up
void onStart(@Observes StartupEvent ev) {
    LOGGER.info("The application is starting...");
    FlowRule rule = new FlowRule()
            .setCount(1)
            .setGrade(RuleConstant.FLOW_GRADE_QPS)
            .setResource("GET:/hello/txt")
            .setLimitApp("default")
            .as(FlowRule.class);
    FlowRuleManager.loadRules(Arrays.asList(rule));

    SystemRule systemRule = new SystemRule();
    systemRule.setLimitApp("default");
    systemRule.setAvgRt(3000);
    SystemRuleManager.loadRules(Arrays.asList(systemRule));

    DegradeRule degradeRule1 = new DegradeRule("greeting1")
            .setCount(1)
            .setGrade(RuleConstant.DEGRADE_GRADE_EXCEPTION_COUNT)
            .setTimeWindow(10)
            .setMinRequestAmount(1);

    DegradeRule degradeRule2 = new DegradeRule("greeting2")
            .setCount(1)
            .setGrade(RuleConstant.DEGRADE_GRADE_EXCEPTION_COUNT)
            .setTimeWindow(10)
            .setMinRequestAmount(1);
    DegradeRuleManager.loadRules(Arrays.asList(degradeRule1, degradeRule2));
}
 
Example 10
Source File: SentinelDubboConsumerFilterTest.java    From Sentinel with Apache License 2.0 5 votes vote down vote up
private void initDegradeRule(String resource) {
    DegradeRule degradeRule = new DegradeRule(resource)
            .setCount(0.5)
            .setGrade(DEGRADE_GRADE_EXCEPTION_RATIO);
    List<DegradeRule> degradeRules = new ArrayList<>();
    degradeRules.add(degradeRule);
    degradeRule.setTimeWindow(1);
    DegradeRuleManager.loadRules(degradeRules);
}
 
Example 11
Source File: BaseTest.java    From Sentinel with Apache License 2.0 5 votes vote down vote up
private void cleanUpCstContext() throws NoSuchMethodException, InvocationTargetException, IllegalAccessException {
    ClusterBuilderSlot.getClusterNodeMap().clear();
    CtSph.resetChainMap();
    Method method = ContextUtil.class.getDeclaredMethod("resetContextMap");
    method.setAccessible(true);
    method.invoke(null, null);
    ContextUtil.exit();
    FlowRuleManager.loadRules(new ArrayList<>());
    DegradeRuleManager.loadRules(new ArrayList<>());
}
 
Example 12
Source File: SentinelCircuitBreaker.java    From spring-cloud-alibaba with Apache License 2.0 5 votes vote down vote up
private void applyToSentinelRuleManager() {
	if (this.rules == null || this.rules.isEmpty()) {
		return;
	}
	Set<DegradeRule> ruleSet = new HashSet<>(DegradeRuleManager.getRules());
	for (DegradeRule rule : this.rules) {
		if (rule == null) {
			continue;
		}
		rule.setResource(resourceName);
		ruleSet.add(rule);
	}
	DegradeRuleManager.loadRules(new ArrayList<>(ruleSet));
}
 
Example 13
Source File: ReactiveSentinelCircuitBreaker.java    From spring-cloud-alibaba with Apache License 2.0 5 votes vote down vote up
private void applyToSentinelRuleManager() {
	if (this.rules == null || this.rules.isEmpty()) {
		return;
	}
	Set<DegradeRule> ruleSet = new HashSet<>(DegradeRuleManager.getRules());
	for (DegradeRule rule : this.rules) {
		if (rule == null) {
			continue;
		}
		rule.setResource(resourceName);
		ruleSet.add(rule);
	}
	DegradeRuleManager.loadRules(new ArrayList<>(ruleSet));
}
 
Example 14
Source File: SentinelCircuitBreakerIntegrationTest.java    From spring-cloud-alibaba with Apache License 2.0 4 votes vote down vote up
@Before
public void setUp() {
	DegradeRuleManager.loadRules(new ArrayList<>());
}
 
Example 15
Source File: SentinelCircuitBreakerIntegrationTest.java    From spring-cloud-alibaba with Apache License 2.0 4 votes vote down vote up
@Before
public void tearDown() {
	DegradeRuleManager.loadRules(new ArrayList<>());
}
 
Example 16
Source File: SentinelCircuitBreakerTest.java    From spring-cloud-alibaba with Apache License 2.0 4 votes vote down vote up
@After
public void tearDown() {
	// Clear the rules.
	DegradeRuleManager.loadRules(new ArrayList<>());
}