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