Java Code Examples for com.alibaba.csp.sentinel.slots.block.flow.FlowRuleManager#loadRules()

The following examples show how to use com.alibaba.csp.sentinel.slots.block.flow.FlowRuleManager#loadRules() . 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: MonoSentinelOperatorIntegrationTest.java    From Sentinel with Apache License 2.0 6 votes vote down vote up
@Test
public void testEmitExceptionWhenFlowControlTriggered() {
    String resourceName = createResourceName("testEmitExceptionWhenFlowControlTriggered");
    FlowRuleManager.loadRules(Collections.singletonList(
        new FlowRule(resourceName).setCount(0)
    ));
    StepVerifier.create(Mono.error(new IllegalStateException("some"))
        .transform(new SentinelReactorTransformer<>(resourceName)))
        .expectError(BlockException.class)
        .verify();

    ClusterNode cn = ClusterBuilderSlot.getClusterNode(resourceName);
    assertNotNull(cn);
    assertEquals(0, cn.passQps(), 0.01);
    assertEquals(1, cn.blockRequest());

    FlowRuleManager.loadRules(new ArrayList<>());
}
 
Example 2
Source File: FluxSentinelOperatorTestIntegrationTest.java    From Sentinel-Dashboard-Nacos with Apache License 2.0 6 votes vote down vote up
@Test
public void testEmitMultipleValuesWhenFlowControlTriggered() {
    String resourceName = createResourceName("testEmitMultipleValuesWhenFlowControlTriggered");
    FlowRuleManager.loadRules(Collections.singletonList(
        new FlowRule(resourceName).setCount(0)
    ));
    StepVerifier.create(Flux.just(1, 3, 5)
        .map(e -> e * 2)
        .transform(new SentinelReactorTransformer<>(resourceName)))
        .expectError(BlockException.class)
        .verify();

    ClusterNode cn = ClusterBuilderSlot.getClusterNode(resourceName);
    assertNotNull(cn);
    assertEquals(0, cn.passQps(), 0.01);
    assertEquals(1, cn.blockRequest());

    FlowRuleManager.loadRules(new ArrayList<>());
}
 
Example 3
Source File: PaceFlowDemo.java    From Sentinel with Apache License 2.0 6 votes vote down vote up
private static void initPaceFlowRule() {
    List<FlowRule> rules = new ArrayList<FlowRule>();
    FlowRule rule1 = new FlowRule();
    rule1.setResource(KEY);
    rule1.setCount(count);
    rule1.setGrade(RuleConstant.FLOW_GRADE_QPS);
    rule1.setLimitApp("default");
    /*
     * CONTROL_BEHAVIOR_RATE_LIMITER means requests more than threshold will be queueing in the queue,
     * until the queueing time is more than {@link FlowRule#maxQueueingTimeMs}, the requests will be rejected.
     */
    rule1.setControlBehavior(RuleConstant.CONTROL_BEHAVIOR_RATE_LIMITER);
    rule1.setMaxQueueingTimeMs(20 * 1000);

    rules.add(rule1);
    FlowRuleManager.loadRules(rules);
}
 
Example 4
Source File: ReactorSphUTest.java    From Sentinel-Dashboard-Nacos with Apache License 2.0 6 votes vote down vote up
@Test
public void testReactorEntryNormalWhenFlowControlTriggered() {
    String resourceName = createResourceName("testReactorEntryNormalWhenFlowControlTriggered");
    FlowRuleManager.loadRules(Collections.singletonList(
        new FlowRule(resourceName).setCount(0)
    ));
    StepVerifier.create(ReactorSphU.entryWith(resourceName, Mono.just(60))
        .subscribeOn(Schedulers.elastic())
        .map(e -> e * 3))
        .expectError(BlockException.class)
        .verify();

    ClusterNode cn = ClusterBuilderSlot.getClusterNode(resourceName);
    assertNotNull(cn);
    assertEquals(0, cn.passQps(), 0.01);
    assertEquals(1, cn.blockRequest());

    FlowRuleManager.loadRules(new ArrayList<>());
}
 
Example 5
Source File: ReactorSphUTest.java    From Sentinel with Apache License 2.0 6 votes vote down vote up
@Test
public void testReactorEntryNormalWhenFlowControlTriggered() {
    String resourceName = createResourceName("testReactorEntryNormalWhenFlowControlTriggered");
    FlowRuleManager.loadRules(Collections.singletonList(
        new FlowRule(resourceName).setCount(0)
    ));
    StepVerifier.create(ReactorSphU.entryWith(resourceName, Mono.just(60))
        .subscribeOn(Schedulers.elastic())
        .map(e -> e * 3))
        .expectError(BlockException.class)
        .verify();

    ClusterNode cn = ClusterBuilderSlot.getClusterNode(resourceName);
    assertNotNull(cn);
    assertEquals(0, cn.passQps(), 0.01);
    assertEquals(1, cn.blockRequest());

    FlowRuleManager.loadRules(new ArrayList<>());
}
 
Example 6
Source File: SentinelAnnotationIntegrationTest.java    From Sentinel with Apache License 2.0 6 votes vote down vote up
@Test
public void testDefaultFallbackWithSingleParam() {
    assertThat(fooService.anotherFoo(1)).isEqualTo("Hello for 1");
    String resourceName = "apiAnotherFooWithDefaultFallback";
    ClusterNode cn = ClusterBuilderSlot.getClusterNode(resourceName);
    assertThat(cn).isNotNull();
    assertThat(cn.passQps()).isPositive();

    // Default fallback should take effect.
    assertThat(fooService.anotherFoo(5758)).isEqualTo(FooUtil.FALLBACK_DEFAULT_RESULT);
    assertThat(cn.exceptionQps()).isPositive();
    assertThat(cn.blockQps()).isZero();

    FlowRuleManager.loadRules(Collections.singletonList(
        new FlowRule(resourceName).setCount(0)
    ));
    // Default fallback should also take effect for BlockException.
    assertThat(fooService.anotherFoo(5758)).isEqualTo(FooUtil.FALLBACK_DEFAULT_RESULT);
    assertThat(cn.blockQps()).isPositive();
}
 
Example 7
Source File: WarmUpRateLimiterFlowDemo.java    From Sentinel with Apache License 2.0 5 votes vote down vote up
private static void initFlowRule() {
    List<FlowRule> rules = new ArrayList<FlowRule>();
    FlowRule rule1 = new FlowRule();
    rule1.setResource(KEY);
    rule1.setCount(20);
    rule1.setGrade(RuleConstant.FLOW_GRADE_QPS);
    rule1.setLimitApp("default");
    rule1.setControlBehavior(RuleConstant.CONTROL_BEHAVIOR_WARM_UP_RATE_LIMITER);
    rule1.setWarmUpPeriodSec(10);
    rule1.setMaxQueueingTimeMs(100);

    rules.add(rule1);
    FlowRuleManager.loadRules(rules);
}
 
Example 8
Source File: SentinelFeignTests.java    From spring-cloud-alibaba with Apache License 2.0 5 votes vote down vote up
@Before
public void setUp() {
	FlowRule rule1 = new FlowRule();
	rule1.setGrade(RuleConstant.FLOW_GRADE_QPS);
	rule1.setCount(0);
	rule1.setResource("GET:http://test-service/echo/{str}");
	rule1.setLimitApp("default");
	rule1.setControlBehavior(RuleConstant.CONTROL_BEHAVIOR_DEFAULT);
	rule1.setStrategy(RuleConstant.STRATEGY_DIRECT);
	FlowRule rule2 = new FlowRule();
	rule2.setGrade(RuleConstant.FLOW_GRADE_QPS);
	rule2.setCount(0);
	rule2.setResource("GET:http://foo-service/echo/{str}");
	rule2.setLimitApp("default");
	rule2.setControlBehavior(RuleConstant.CONTROL_BEHAVIOR_DEFAULT);
	rule2.setStrategy(RuleConstant.STRATEGY_DIRECT);
	FlowRule rule3 = new FlowRule();
	rule3.setGrade(RuleConstant.FLOW_GRADE_QPS);
	rule3.setCount(0);
	rule3.setResource("GET:http://bar-service/bar");
	rule3.setLimitApp("default");
	rule3.setControlBehavior(RuleConstant.CONTROL_BEHAVIOR_DEFAULT);
	rule3.setStrategy(RuleConstant.STRATEGY_DIRECT);
	FlowRule rule4 = new FlowRule();
	rule4.setGrade(RuleConstant.FLOW_GRADE_QPS);
	rule4.setCount(0);
	rule4.setResource("GET:http://baz-service/baz");
	rule4.setLimitApp("default");
	rule4.setControlBehavior(RuleConstant.CONTROL_BEHAVIOR_DEFAULT);
	rule4.setStrategy(RuleConstant.STRATEGY_DIRECT);
	FlowRuleManager.loadRules(Arrays.asList(rule1, rule2, rule3, rule4));
}
 
Example 9
Source File: SentinelWebFluxIntegrationTest.java    From Sentinel with Apache License 2.0 5 votes vote down vote up
private void configureRulesFor(String resource, int count, String limitApp) {
    FlowRule rule = new FlowRule()
        .setCount(count)
        .setGrade(RuleConstant.FLOW_GRADE_QPS);
    rule.setResource(resource);
    if (StringUtil.isNotBlank(limitApp)) {
        rule.setLimitApp(limitApp);
    }
    FlowRuleManager.loadRules(Collections.singletonList(rule));
}
 
Example 10
Source File: CommonFilterMethodTest.java    From Sentinel with Apache License 2.0 5 votes vote down vote up
private void configureRulesFor(String resource, int count, String limitApp) {
    FlowRule rule = new FlowRule()
            .setCount(count)
            .setGrade(RuleConstant.FLOW_GRADE_QPS);
    rule.setResource(resource);
    if (StringUtil.isNotBlank(limitApp)) {
        rule.setLimitApp(limitApp);
    }
    FlowRuleManager.loadRules(Collections.singletonList(rule));
}
 
Example 11
Source File: WarmUpFlowDemo.java    From Sentinel with Apache License 2.0 5 votes vote down vote up
private static void initFlowRule() {
    List<FlowRule> rules = new ArrayList<FlowRule>();
    FlowRule rule1 = new FlowRule();
    rule1.setResource(KEY);
    rule1.setCount(20);
    rule1.setGrade(RuleConstant.FLOW_GRADE_QPS);
    rule1.setLimitApp("default");
    rule1.setControlBehavior(RuleConstant.CONTROL_BEHAVIOR_WARM_UP);
    rule1.setWarmUpPeriodSec(10);

    rules.add(rule1);
    FlowRuleManager.loadRules(rules);
}
 
Example 12
Source File: SentinelAnnotationIntegrationTest.java    From Sentinel with Apache License 2.0 5 votes vote down vote up
@Test
public void testFallbackWithNoParams() throws Exception {
    assertThat(fooService.fooWithFallback(1)).isEqualTo("Hello for 1");
    String resourceName = "apiFooWithFallback";
    ClusterNode cn = ClusterBuilderSlot.getClusterNode(resourceName);
    assertThat(cn).isNotNull();
    assertThat(cn.passQps()).isPositive();

    // Fallback should be ignored for this.
    try {
        fooService.fooWithFallback(5758);
        fail("should not reach here");
    } catch (IllegalAccessException e) {
        assertThat(cn.exceptionQps()).isZero();
    }

    // Fallback should take effect.
    assertThat(fooService.fooWithFallback(5763)).isEqualTo("eee...");
    assertThat(cn.exceptionQps()).isPositive();
    assertThat(cn.blockQps()).isZero();

    FlowRuleManager.loadRules(Collections.singletonList(
        new FlowRule(resourceName).setCount(0)
    ));
    // Fallback should not take effect for BlockException, as blockHandler is configured.
    assertThat(fooService.fooWithFallback(2221)).isEqualTo("Oops, 2221");
    assertThat(cn.blockQps()).isPositive();
}
 
Example 13
Source File: SentinelAnnotationIntegrationTest.java    From Sentinel-Dashboard-Nacos with Apache License 2.0 5 votes vote down vote up
@Test
public void testFallbackWithNoParams() throws Exception {
    assertThat(fooService.fooWithFallback(1)).isEqualTo("Hello for 1");
    String resourceName = "apiFooWithFallback";
    ClusterNode cn = ClusterBuilderSlot.getClusterNode(resourceName);
    assertThat(cn).isNotNull();
    assertThat(cn.passQps()).isPositive();

    // Fallback should be ignored for this.
    try {
        fooService.fooWithFallback(5758);
        fail("should not reach here");
    } catch (IllegalAccessException e) {
        assertThat(cn.exceptionQps()).isZero();
    }

    // Fallback should take effect.
    assertThat(fooService.fooWithFallback(5763)).isEqualTo("eee...");
    assertThat(cn.exceptionQps()).isPositive();
    assertThat(cn.blockQps()).isZero();

    FlowRuleManager.loadRules(Collections.singletonList(
        new FlowRule(resourceName).setCount(0)
    ));
    // Fallback should not take effect for BlockException, as blockHandler is configured.
    assertThat(fooService.fooWithFallback(2221)).isEqualTo("Oops, 2221");
    assertThat(cn.blockQps()).isPositive();
}
 
Example 14
Source File: FooConsumerBootstrap.java    From Sentinel with Apache License 2.0 5 votes vote down vote up
private static void initFlowRule() {
    FlowRule flowRule = new FlowRule();
    flowRule.setResource(RES_KEY);
    flowRule.setCount(5);
    flowRule.setGrade(RuleConstant.FLOW_GRADE_THREAD);
    flowRule.setLimitApp("default");
    FlowRuleManager.loadRules(Collections.singletonList(flowRule));
}
 
Example 15
Source File: ClientFilterTest.java    From Sentinel with Apache License 2.0 4 votes vote down vote up
@After
public void cleanUp() {
    FlowRuleManager.loadRules(null);
    ClusterBuilderSlot.resetClusterNodes();
}
 
Example 16
Source File: FooProviderBootstrap.java    From Sentinel-Dashboard-Nacos with Apache License 2.0 4 votes vote down vote up
private static void initFlowRule() {
    FlowRule flowRule = new FlowRule(INTERFACE_RES_KEY)
        .setCount(10)
        .setGrade(RuleConstant.FLOW_GRADE_QPS);
    FlowRuleManager.loadRules(Collections.singletonList(flowRule));
}
 
Example 17
Source File: SentinelWebFluxIntegrationTest.java    From Sentinel with Apache License 2.0 4 votes vote down vote up
@Before
public void setUp() {
    FlowRuleManager.loadRules(new ArrayList<>());
    ClusterBuilderSlot.resetClusterNodes();
}
 
Example 18
Source File: SentinelGrpcServerInterceptorTest.java    From Sentinel-Dashboard-Nacos with Apache License 2.0 4 votes vote down vote up
@After
public void cleanUp() {
    FlowRuleManager.loadRules(null);
    ClusterBuilderSlot.getClusterNodeMap().clear();
}
 
Example 19
Source File: SentinelAnnotationIntegrationTest.java    From Sentinel with Apache License 2.0 4 votes vote down vote up
@After
public void tearDown() throws Exception {
    FlowRuleManager.loadRules(new ArrayList<FlowRule>());
    ClusterBuilderSlot.resetClusterNodes();
}
 
Example 20
Source File: EtcdDataSourceTest.java    From Sentinel with Apache License 2.0 4 votes vote down vote up
@Before
public void setUp() {
    SentinelConfig.setConfig(EtcdConfig.END_POINTS, endPoints);
    FlowRuleManager.loadRules(new ArrayList<>());
}