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));
}
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));
}
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);
}
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));
}
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;
}
Aggregations