Search in sources :

Example 1 with DegradeRule

use of com.alibaba.csp.sentinel.slots.block.degrade.DegradeRule in project Sentinel by alibaba.

the class AppLifecycleBean method onStart.

void onStart(@Observes StartupEvent ev) {
    LOGGER.info("The application is starting...");
    // Only for test here. Actually it's recommended to configure rules via data-source.
    FlowRule rule1 = new FlowRule().setCount(1).setGrade(RuleConstant.FLOW_GRADE_QPS).setResource("GET:/hello/txt").setLimitApp("default").as(FlowRule.class);
    FlowRule rule2 = new FlowRule("greeting2").setCount(1).setGrade(RuleConstant.FLOW_GRADE_QPS).as(FlowRule.class);
    FlowRuleManager.loadRules(Arrays.asList(rule1, rule2));
    DegradeRule degradeRule1 = new DegradeRule("greeting1").setCount(1).setGrade(CircuitBreakerStrategy.ERROR_COUNT.getType()).setTimeWindow(5).setStatIntervalMs(10000).setMinRequestAmount(1);
    DegradeRuleManager.loadRules(Arrays.asList(degradeRule1));
}
Also used : FlowRule(com.alibaba.csp.sentinel.slots.block.flow.FlowRule) DegradeRule(com.alibaba.csp.sentinel.slots.block.degrade.DegradeRule)

Example 2 with DegradeRule

use of com.alibaba.csp.sentinel.slots.block.degrade.DegradeRule in project Sentinel by alibaba.

the class FooConsumerExceptionDegradeBootstrap method initExceptionFallback.

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));
}
Also used : DegradeRule(com.alibaba.csp.sentinel.slots.block.degrade.DegradeRule)

Example 3 with DegradeRule

use of com.alibaba.csp.sentinel.slots.block.degrade.DegradeRule in project Sentinel by alibaba.

the class ExceptionRatioCircuitBreakerDemo method initDegradeRule.

private static void initDegradeRule() {
    List<DegradeRule> rules = new ArrayList<>();
    DegradeRule rule = new DegradeRule(KEY).setGrade(CircuitBreakerStrategy.ERROR_RATIO.getType()).setCount(0.5d).setStatIntervalMs(30000).setMinRequestAmount(50).setTimeWindow(10);
    rules.add(rule);
    DegradeRuleManager.loadRules(rules);
    System.out.println("Degrade rule loaded: " + rules);
}
Also used : ArrayList(java.util.ArrayList) DegradeRule(com.alibaba.csp.sentinel.slots.block.degrade.DegradeRule)

Example 4 with DegradeRule

use of com.alibaba.csp.sentinel.slots.block.degrade.DegradeRule in project Sentinel by alibaba.

the class ResponseTimeCircuitBreakerTest method testMaxSlowRatioThreshold.

@Test
public void testMaxSlowRatioThreshold() {
    String resource = "testMaxSlowRatioThreshold";
    DegradeRule rule = new DegradeRule("resource").setCount(10).setGrade(RuleConstant.DEGRADE_GRADE_RT).setMinRequestAmount(3).setSlowRatioThreshold(1).setStatIntervalMs(5000).setTimeWindow(5);
    rule.setResource(resource);
    DegradeRuleManager.loadRules(Collections.singletonList(rule));
    assertTrue(entryAndSleepFor(resource, 20));
    assertTrue(entryAndSleepFor(resource, 20));
    assertTrue(entryAndSleepFor(resource, 20));
    // should be blocked, cause 3/3 requests' rt is bigger than max rt.
    assertFalse(entryAndSleepFor(resource, 20));
    sleep(1000);
    assertFalse(entryAndSleepFor(resource, 20));
    sleep(4000);
    assertTrue(entryAndSleepFor(resource, 20));
}
Also used : DegradeRule(com.alibaba.csp.sentinel.slots.block.degrade.DegradeRule) AbstractTimeBasedTest(com.alibaba.csp.sentinel.test.AbstractTimeBasedTest) Test(org.junit.Test)

Example 5 with DegradeRule

use of com.alibaba.csp.sentinel.slots.block.degrade.DegradeRule in project Sentinel by alibaba.

the class DegradeRuleEntity method toRule.

@Override
public DegradeRule toRule() {
    DegradeRule rule = new DegradeRule();
    rule.setResource(resource);
    rule.setLimitApp(limitApp);
    rule.setCount(count);
    rule.setTimeWindow(timeWindow);
    rule.setGrade(grade);
    if (minRequestAmount != null) {
        rule.setMinRequestAmount(minRequestAmount);
    }
    if (slowRatioThreshold != null) {
        rule.setSlowRatioThreshold(slowRatioThreshold);
    }
    if (statIntervalMs != null) {
        rule.setStatIntervalMs(statIntervalMs);
    }
    return rule;
}
Also used : DegradeRule(com.alibaba.csp.sentinel.slots.block.degrade.DegradeRule)

Aggregations

DegradeRule (com.alibaba.csp.sentinel.slots.block.degrade.DegradeRule)34 ArrayList (java.util.ArrayList)12 FlowRule (com.alibaba.csp.sentinel.slots.block.flow.FlowRule)9 Test (org.junit.Test)4 List (java.util.List)3 ApiDefinition (com.alibaba.csp.sentinel.adapter.gateway.common.api.ApiDefinition)2 GatewayFlowRule (com.alibaba.csp.sentinel.adapter.gateway.common.rule.GatewayFlowRule)2 AuthorityRule (com.alibaba.csp.sentinel.slots.block.authority.AuthorityRule)2 ParamFlowItem (com.alibaba.csp.sentinel.slots.block.flow.param.ParamFlowItem)2 ParamFlowRule (com.alibaba.csp.sentinel.slots.block.flow.param.ParamFlowRule)2 SystemRule (com.alibaba.csp.sentinel.slots.system.SystemRule)2 AbstractTimeBasedTest (com.alibaba.csp.sentinel.test.AbstractTimeBasedTest)2 SentinelDegradeRule (com.github.howinfun.sentinel.pojo.SentinelDegradeRule)2 SentinelFlowRule (com.github.howinfun.sentinel.pojo.SentinelFlowRule)2 HashSet (java.util.HashSet)2 RuleData (org.apache.shenyu.common.dto.RuleData)2 SentinelHandle (org.apache.shenyu.common.dto.convert.rule.SentinelHandle)2 EntryType (com.alibaba.csp.sentinel.EntryType)1 DegradeRuleManager (com.alibaba.csp.sentinel.slots.block.degrade.DegradeRuleManager)1 FlowRuleManager (com.alibaba.csp.sentinel.slots.block.flow.FlowRuleManager)1