Search in sources :

Example 16 with DegradeRule

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

the class CircuitBreakerRuleConverter method createRule.

private DegradeRule createRule(boolean isSlowRule, CircuitBreakerRule resilienceRule) {
    final DegradeRule degradeRule = new DegradeRule();
    degradeRule.setResource(resilienceRule.getName());
    degradeRule.setMinRequestAmount(resilienceRule.getMinimumNumberOfCalls());
    // CSE默认均为1分钟
    degradeRule.setTimeWindow((int) (CircuitBreakerRule.DEFAULT_WAIT_DURATION_IN_OPEN_STATUS_MS / CommonConst.S_MS_UNIT));
    degradeRule.setStatIntervalMs((int) resilienceRule.getParsedSlidingWindowSize());
    if (isSlowRule) {
        degradeRule.setGrade(RuleConstant.DEGRADE_GRADE_RT);
        degradeRule.setSlowRatioThreshold(resilienceRule.getSlowCallRateThreshold() / CommonConst.PERCENT);
        degradeRule.setCount(resilienceRule.getParsedSlowCallDurationThreshold());
    } else {
        degradeRule.setGrade(RuleConstant.DEGRADE_GRADE_EXCEPTION_RATIO);
        degradeRule.setCount(resilienceRule.getFailureRateThreshold() / CommonConst.PERCENT);
    }
    return degradeRule;
}
Also used : DegradeRule(com.alibaba.csp.sentinel.slots.block.degrade.DegradeRule)

Example 17 with DegradeRule

use of com.alibaba.csp.sentinel.slots.block.degrade.DegradeRule in project spring-cloud-alibaba by alibaba.

the class ReactiveSentinelCircuitBreaker method applyToSentinelRuleManager.

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

Example 18 with DegradeRule

use of com.alibaba.csp.sentinel.slots.block.degrade.DegradeRule in project spring-cloud-alibaba by alibaba.

the class SentinelCircuitBreaker method applyToSentinelRuleManager.

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

Example 19 with DegradeRule

use of com.alibaba.csp.sentinel.slots.block.degrade.DegradeRule in project spring-cloud-alibaba by alibaba.

the class SentinelCircuitBreakerTest method testCreateDirectlyThenRun.

@Test
public void testCreateDirectlyThenRun() {
    // Create a circuit breaker without any circuit breaking rules.
    CircuitBreaker cb = new SentinelCircuitBreaker("testSentinelCreateDirectlyThenRunA");
    assertThat(cb.run(() -> "Sentinel")).isEqualTo("Sentinel");
    assertThat(DegradeRuleManager.hasConfig("testSentinelCreateDirectlyThenRunA")).isFalse();
    CircuitBreaker cb2 = new SentinelCircuitBreaker("testSentinelCreateDirectlyThenRunB", Collections.singletonList(new DegradeRule("testSentinelCreateDirectlyThenRunB").setCount(100).setTimeWindow(10)));
    assertThat(cb2.run(() -> "Sentinel")).isEqualTo("Sentinel");
    assertThat(DegradeRuleManager.hasConfig("testSentinelCreateDirectlyThenRunB")).isTrue();
}
Also used : CircuitBreaker(org.springframework.cloud.client.circuitbreaker.CircuitBreaker) DegradeRule(com.alibaba.csp.sentinel.slots.block.degrade.DegradeRule) Test(org.junit.Test)

Example 20 with DegradeRule

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

the class SlowRatioCircuitBreakerDemo method initDegradeRule.

private static void initDegradeRule() {
    List<DegradeRule> rules = new ArrayList<>();
    DegradeRule rule = new DegradeRule(KEY).setGrade(CircuitBreakerStrategy.SLOW_REQUEST_RATIO.getType()).setCount(50).setTimeWindow(10).setSlowRatioThreshold(0.6).setMinRequestAmount(100).setStatIntervalMs(20000);
    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)

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