com.alibaba.csp.sentinel.slots.block.degrade.DegradeRule Java Examples

The following examples show how to use com.alibaba.csp.sentinel.slots.block.degrade.DegradeRule. 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: 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 #2
Source File: FileDataSourceDemo.java    From Sentinel-Dashboard-Nacos with Apache License 2.0 6 votes vote down vote up
private void listenRules() throws Exception {
    ClassLoader classLoader = getClass().getClassLoader();
    String flowRulePath = URLDecoder.decode(classLoader.getResource("FlowRule.json").getFile(), "UTF-8");
    String degradeRulePath = URLDecoder.decode(classLoader.getResource("DegradeRule.json").getFile(), "UTF-8");
    String systemRulePath = URLDecoder.decode(classLoader.getResource("SystemRule.json").getFile(), "UTF-8");

    // Data source for FlowRule
    FileRefreshableDataSource<List<FlowRule>> flowRuleDataSource = new FileRefreshableDataSource<>(
        flowRulePath, flowRuleListParser);
    FlowRuleManager.register2Property(flowRuleDataSource.getProperty());

    // Data source for DegradeRule
    FileRefreshableDataSource<List<DegradeRule>> degradeRuleDataSource
        = new FileRefreshableDataSource<>(
        degradeRulePath, degradeRuleListParser);
    DegradeRuleManager.register2Property(degradeRuleDataSource.getProperty());

    // Data source for SystemRule
    FileRefreshableDataSource<List<SystemRule>> systemRuleDataSource
        = new FileRefreshableDataSource<>(
        systemRulePath, systemRuleListParser);
    SystemRuleManager.register2Property(systemRuleDataSource.getProperty());
}
 
Example #3
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 #4
Source File: SentinelCircuitBreakerTest.java    From spring-cloud-alibaba with Apache License 2.0 6 votes vote down vote up
@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();
}
 
Example #5
Source File: ReactiveSentinelCircuitBreakerIntegrationTest.java    From spring-cloud-alibaba with Apache License 2.0 6 votes vote down vote up
@Bean
public Customizer<ReactiveSentinelCircuitBreakerFactory> slowCustomizer() {
	return factory -> {
		factory.configure(builder -> builder
				.rules(Collections.singletonList(new DegradeRule("slow_mono")
						.setGrade(RuleConstant.DEGRADE_GRADE_RT).setCount(100)
						.setTimeWindow(5))),
				"slow_mono");
		factory.configure(builder -> builder
				.rules(Collections.singletonList(new DegradeRule("slow_flux")
						.setGrade(RuleConstant.DEGRADE_GRADE_RT).setCount(100)
						.setTimeWindow(5))),
				"slow_flux");
		factory.configureDefault(id -> new SentinelConfigBuilder()
				.resourceName(id)
				.rules(Collections.singletonList(new DegradeRule(id)
						.setGrade(RuleConstant.DEGRADE_GRADE_EXCEPTION_COUNT)
						.setCount(0.5).setTimeWindow(10)))
				.build());
	};
}
 
Example #6
Source File: SentinelCircuitBreakerIntegrationTest.java    From spring-cloud-alibaba with Apache License 2.0 6 votes vote down vote up
@Bean
public Customizer<SentinelCircuitBreakerFactory> slowCustomizer() {
	String slowId = "slow";
	List<DegradeRule> rules = Collections.singletonList(
			new DegradeRule(slowId).setGrade(RuleConstant.DEGRADE_GRADE_RT)
					.setCount(100).setTimeWindow(10));
	return factory -> {
		factory.configure(builder -> builder.rules(rules), slowId);
		factory.configureDefault(id -> new SentinelConfigBuilder()
				.resourceName(id)
				.rules(Collections.singletonList(new DegradeRule(id)
						.setGrade(RuleConstant.DEGRADE_GRADE_EXCEPTION_COUNT)
						.setCount(0.5).setTimeWindow(10)))
				.build());
	};
}
 
Example #7
Source File: SentinelRecorder.java    From Sentinel with Apache License 2.0 6 votes vote down vote up
/**
 * register fastjson serializer deserializer class info
 */
public void init() {
    SerializeConfig.getGlobalInstance().getObjectWriter(NodeVo.class);
    SerializeConfig.getGlobalInstance().getObjectWriter(FlowRule.class);
    SerializeConfig.getGlobalInstance().getObjectWriter(SystemRule.class);
    SerializeConfig.getGlobalInstance().getObjectWriter(DegradeRule.class);
    SerializeConfig.getGlobalInstance().getObjectWriter(AuthorityRule.class);
    SerializeConfig.getGlobalInstance().getObjectWriter(ParamFlowRule.class);

    ParserConfig.getGlobalInstance().getDeserializer(NodeVo.class);
    ParserConfig.getGlobalInstance().getDeserializer(FlowRule.class);
    ParserConfig.getGlobalInstance().getDeserializer(SystemRule.class);
    ParserConfig.getGlobalInstance().getDeserializer(DegradeRule.class);
    ParserConfig.getGlobalInstance().getDeserializer(AuthorityRule.class);
    ParserConfig.getGlobalInstance().getDeserializer(ParamFlowRule.class);
}
 
Example #8
Source File: MyConfiguration.java    From spring-cloud-alibaba with Apache License 2.0 6 votes vote down vote up
@Bean
public Customizer<ReactiveSentinelCircuitBreakerFactory> slowCustomizer() {
	return factory -> {
		factory.configure(builder -> builder.rules(Collections.singletonList(
				new DegradeRule("slow_mono").setGrade(RuleConstant.DEGRADE_GRADE_RT)
						.setCount(100).setTimeWindow(5))),
				"slow_mono");
		factory.configure(builder -> builder.rules(Collections.singletonList(
				new DegradeRule("slow_flux").setGrade(RuleConstant.DEGRADE_GRADE_RT)
						.setCount(100).setTimeWindow(5))),
				"slow_flux");
		factory.configureDefault(id -> new SentinelConfigBuilder().resourceName(id)
				.rules(Collections.singletonList(new DegradeRule(id)
						.setGrade(RuleConstant.DEGRADE_GRADE_EXCEPTION_COUNT)
						.setCount(0.5).setTimeWindow(10)))
				.build());
	};
}
 
Example #9
Source File: FileDataSourceDemo.java    From Sentinel with Apache License 2.0 6 votes vote down vote up
private void listenRules() throws Exception {
    ClassLoader classLoader = getClass().getClassLoader();
    String flowRulePath = URLDecoder.decode(classLoader.getResource("FlowRule.json").getFile(), "UTF-8");
    String degradeRulePath = URLDecoder.decode(classLoader.getResource("DegradeRule.json").getFile(), "UTF-8");
    String systemRulePath = URLDecoder.decode(classLoader.getResource("SystemRule.json").getFile(), "UTF-8");

    // Data source for FlowRule
    FileRefreshableDataSource<List<FlowRule>> flowRuleDataSource = new FileRefreshableDataSource<>(
        flowRulePath, flowRuleListParser);
    FlowRuleManager.register2Property(flowRuleDataSource.getProperty());

    // Data source for DegradeRule
    FileRefreshableDataSource<List<DegradeRule>> degradeRuleDataSource
        = new FileRefreshableDataSource<>(
        degradeRulePath, degradeRuleListParser);
    DegradeRuleManager.register2Property(degradeRuleDataSource.getProperty());

    // Data source for SystemRule
    FileRefreshableDataSource<List<SystemRule>> systemRuleDataSource
        = new FileRefreshableDataSource<>(
        systemRulePath, systemRuleListParser);
    SystemRuleManager.register2Property(systemRuleDataSource.getProperty());
}
 
Example #10
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 #11
Source File: RuleTypeTests.java    From spring-cloud-alibaba with Apache License 2.0 6 votes vote down vote up
@Test
public void testGetByClass() {
	assertThat(RuleType.getByClass(Object.class).isPresent())
			.isEqualTo(Boolean.FALSE);
	assertThat(RuleType.getByClass(AbstractRule.class).isPresent())
			.isEqualTo(Boolean.FALSE);
	assertThat(RuleType.getByClass(FlowRule.class).isPresent())
			.isEqualTo(Boolean.TRUE);
	assertThat(RuleType.getByClass(DegradeRule.class).isPresent())
			.isEqualTo(Boolean.TRUE);
	assertThat(RuleType.getByClass(ParamFlowRule.class).isPresent())
			.isEqualTo(Boolean.TRUE);
	assertThat(RuleType.getByClass(SystemRule.class).isPresent())
			.isEqualTo(Boolean.TRUE);
	assertThat(RuleType.getByClass(AuthorityRule.class).isPresent())
			.isEqualTo(Boolean.TRUE);
}
 
Example #12
Source File: SentinelApiClient.java    From Sentinel with Apache License 2.0 5 votes vote down vote up
public List<DegradeRuleEntity> fetchDegradeRuleOfMachine(String app, String ip, int port) {
    List<DegradeRule> rules = fetchRules(ip, port, DEGRADE_RULE_TYPE, DegradeRule.class);
    if (rules != null) {
        return rules.stream().map(rule -> DegradeRuleEntity.fromDegradeRule(app, ip, port, rule))
            .collect(Collectors.toList());
    } else {
        return null;
    }
}
 
Example #13
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 #14
Source File: ServiceApplication.java    From spring-cloud-alibaba with Apache License 2.0 5 votes vote down vote up
@Bean
public Customizer<SentinelCircuitBreakerFactory> defaultConfig() {
	return factory -> {
		factory.configureDefault(
				id -> new SentinelConfigBuilder().resourceName(id)
						.rules(Collections.singletonList(new DegradeRule(id)
								.setGrade(RuleConstant.DEGRADE_GRADE_RT).setCount(100)
								.setTimeWindow(10)))
						.build());
	};
}
 
Example #15
Source File: SentinelCircuitBreaker.java    From spring-cloud-alibaba with Apache License 2.0 5 votes vote down vote up
public SentinelCircuitBreaker(String resourceName, EntryType entryType,
		List<DegradeRule> rules) {
	Assert.hasText(resourceName, "resourceName cannot be blank");
	Assert.notNull(rules, "rules should not be null");
	this.resourceName = resourceName;
	this.entryType = entryType;
	this.rules = Collections.unmodifiableList(rules);

	applyToSentinelRuleManager();
}
 
Example #16
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 #17
Source File: ReactiveSentinelCircuitBreaker.java    From spring-cloud-alibaba with Apache License 2.0 5 votes vote down vote up
public ReactiveSentinelCircuitBreaker(String resourceName, EntryType entryType,
		List<DegradeRule> rules) {
	Assert.hasText(resourceName, "resourceName cannot be blank");
	Assert.notNull(rules, "rules should not be null");
	this.resourceName = resourceName;
	this.entryType = entryType;
	this.rules = Collections.unmodifiableList(rules);

	applyToSentinelRuleManager();
}
 
Example #18
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 #19
Source File: SentinelConfigBuilder.java    From spring-cloud-alibaba with Apache License 2.0 5 votes vote down vote up
@Override
public SentinelCircuitBreakerConfiguration build() {
	Assert.hasText(resourceName, "resourceName cannot be empty");
	List<DegradeRule> rules = Optional.ofNullable(this.rules)
			.orElse(new ArrayList<>());

	EntryType entryType = Optional.ofNullable(this.entryType).orElse(EntryType.OUT);
	return new SentinelCircuitBreakerConfiguration()
			.setResourceName(this.resourceName).setEntryType(entryType)
			.setRules(rules);
}
 
Example #20
Source File: DegradeRuleEntity.java    From Sentinel with Apache License 2.0 5 votes vote down vote up
public static DegradeRuleEntity fromDegradeRule(String app, String ip, Integer port, DegradeRule rule) {
    DegradeRuleEntity entity = new DegradeRuleEntity();
    entity.setApp(app);
    entity.setIp(ip);
    entity.setPort(port);
    entity.setResource(rule.getResource());
    entity.setLimitApp(rule.getLimitApp());
    entity.setCount(rule.getCount());
    entity.setTimeWindow(rule.getTimeWindow());
    entity.setGrade(rule.getGrade());
    return entity;
}
 
Example #21
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 #22
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 #23
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 #24
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 #25
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 #26
Source File: DegradeRuleEntity.java    From Sentinel with Apache License 2.0 5 votes vote down vote up
@Override
public DegradeRule toRule() {
    DegradeRule rule = new DegradeRule();
    rule.setResource(resource);
    rule.setLimitApp(limitApp);
    rule.setCount(count);
    rule.setTimeWindow(timeWindow);
    rule.setGrade(grade);
    return rule;
}
 
Example #27
Source File: SentinelApiClient.java    From Sentinel-Dashboard-Nacos with Apache License 2.0 5 votes vote down vote up
public List<DegradeRuleEntity> fetchDegradeRuleOfMachine(String app, String ip, int port) {
    List<DegradeRule> rules = fetchRules(ip, port, DEGRADE_RULE_TYPE, DegradeRule.class);
    if (rules != null) {
        return rules.stream().map(rule -> DegradeRuleEntity.fromDegradeRule(app, ip, port, rule))
            .collect(Collectors.toList());
    } else {
        return null;
    }
}
 
Example #28
Source File: DegradeRuleEntity.java    From Sentinel-Dashboard-Nacos with Apache License 2.0 5 votes vote down vote up
@Override
public DegradeRule toRule() {
    DegradeRule rule = new DegradeRule();
    rule.setResource(resource);
    rule.setLimitApp(limitApp);
    rule.setCount(count);
    rule.setTimeWindow(timeWindow);
    rule.setGrade(grade);
    return rule;
}
 
Example #29
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 #30
Source File: DegradeRuleEntity.java    From Sentinel-Dashboard-Nacos with Apache License 2.0 5 votes vote down vote up
public static DegradeRuleEntity fromDegradeRule(String app, String ip, Integer port, DegradeRule rule) {
    DegradeRuleEntity entity = new DegradeRuleEntity();
    entity.setApp(app);
    entity.setIp(ip);
    entity.setPort(port);
    entity.setResource(rule.getResource());
    entity.setLimitApp(rule.getLimitApp());
    entity.setCount(rule.getCount());
    entity.setTimeWindow(rule.getTimeWindow());
    entity.setGrade(rule.getGrade());
    return entity;
}