Java Code Examples for com.alibaba.csp.sentinel.slots.block.flow.FlowRule#setResource()

The following examples show how to use com.alibaba.csp.sentinel.slots.block.flow.FlowRule#setResource() . 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: 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 3
Source File: HttpServerHandlerTest.java    From Sentinel with Apache License 2.0 6 votes vote down vote up
@Test
public void testFetchActiveRuleCommandSomeFlowRules() {
    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");
    rules.add(rule1);
    FlowRuleManager.loadRules(rules);

    String httpRequestStr = "GET /getRules?type=flow HTTP/1.1" + CRLF
                          + "Host: localhost:8719" + CRLF
                          + CRLF;

    // body json
    /*
    String expectedBody = "[{\"clusterMode\":false,\"controlBehavior\":0,\"count\":20.0"
            + ",\"grade\":1,\"limitApp\":\"default\",\"maxQueueingTimeMs\":500"
            + ",\"resource\":\"key\",\"strategy\":0,\"warmUpPeriodSec\":10}]";
    */
    String expectedBody = JSON.toJSONString(rules);

    processSuccess(httpRequestStr, expectedBody);
}
 
Example 4
Source File: PullConsumerDemo.java    From Sentinel with Apache License 2.0 6 votes vote down vote up
private static void initFlowControlRule() {
    FlowRule rule = new FlowRule();
    rule.setResource(KEY);
    // Indicates the interval between two adjacent requests is 200 ms.
    rule.setCount(5);
    rule.setGrade(RuleConstant.FLOW_GRADE_QPS);
    rule.setLimitApp("default");

    // Enable rate limiting (uniform). This can ensure fixed intervals between two adjacent calls.
    // In this example, intervals between two incoming calls (message consumption) will be 200 ms constantly.
    rule.setControlBehavior(RuleConstant.CONTROL_BEHAVIOR_RATE_LIMITER);
    // If more requests are coming, they'll be put into the waiting queue.
    // The queue has a queueing timeout. Requests that may exceed the timeout will be immediately blocked.
    // In this example, the max timeout is 5s.
    rule.setMaxQueueingTimeMs(5 * 1000);
    FlowRuleManager.loadRules(Collections.singletonList(rule));
}
 
Example 5
Source File: PullConsumerDemo.java    From Sentinel-Dashboard-Nacos with Apache License 2.0 6 votes vote down vote up
private static void initFlowControlRule() {
    FlowRule rule = new FlowRule();
    rule.setResource(KEY);
    // Indicates the interval between two adjacent requests is 200 ms.
    rule.setCount(5);
    rule.setGrade(RuleConstant.FLOW_GRADE_QPS);
    rule.setLimitApp("default");

    // Enable rate limiting (uniform). This can ensure fixed intervals between two adjacent calls.
    // In this example, intervals between two incoming calls (message consumption) will be 200 ms constantly.
    rule.setControlBehavior(RuleConstant.CONTROL_BEHAVIOR_RATE_LIMITER);
    // If more requests are coming, they'll be put into the waiting queue.
    // The queue has a queueing timeout. Requests that may exceed the timeout will be immediately blocked.
    // In this example, the max timeout is 5s.
    rule.setMaxQueueingTimeMs(5 * 1000);
    FlowRuleManager.loadRules(Collections.singletonList(rule));
}
 
Example 6
Source File: FlowThreadDemo.java    From Sentinel-Dashboard-Nacos 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("methodA");
    // set limit concurrent thread for 'methodA' to 20
    rule1.setCount(20);
    rule1.setGrade(RuleConstant.FLOW_GRADE_THREAD);
    rule1.setLimitApp("default");

    rules.add(rule1);
    FlowRuleManager.loadRules(rules);
}
 
Example 7
Source File: ClientFilterTest.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 8
Source File: FlowQpsDemo.java    From Sentinel-Dashboard-Nacos with Apache License 2.0 5 votes vote down vote up
private static void initFlowQpsRule() {
    List<FlowRule> rules = new ArrayList<FlowRule>();
    FlowRule rule1 = new FlowRule();
    rule1.setResource(KEY);
    // set limit qps to 20
    rule1.setCount(20);
    rule1.setGrade(RuleConstant.FLOW_GRADE_QPS);
    rule1.setLimitApp("default");
    rules.add(rule1);
    FlowRuleManager.loadRules(rules);
}
 
Example 9
Source File: FlowQpsDemo.java    From Sentinel with Apache License 2.0 5 votes vote down vote up
private static void initFlowQpsRule() {
    List<FlowRule> rules = new ArrayList<FlowRule>();
    FlowRule rule1 = new FlowRule();
    rule1.setResource(KEY);
    // set limit qps to 20
    rule1.setCount(20);
    rule1.setGrade(RuleConstant.FLOW_GRADE_QPS);
    rule1.setLimitApp("default");
    rules.add(rule1);
    FlowRuleManager.loadRules(rules);
}
 
Example 10
Source File: SentinelSpringMvcIntegrationTest.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: CommonFilterTest.java    From Sentinel-Dashboard-Nacos 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 13
Source File: CommonFilterMethodTest.java    From Sentinel-Dashboard-Nacos 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 14
Source File: SentinelWebFluxIntegrationTest.java    From Sentinel-Dashboard-Nacos 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 15
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 16
Source File: SentinelStart.java    From blog with BSD 2-Clause "Simplified" License 5 votes vote down vote up
private static void initFlowRules() {
	List<FlowRule> rules = new ArrayList<>();
	FlowRule rule = new FlowRule();
	rule.setResource("HelloWorld");
	rule.setGrade(RuleConstant.FLOW_GRADE_QPS);
	// Set limit QPS to 20.
	rule.setCount(20);
	rules.add(rule);
	FlowRuleManager.loadRules(rules);
}
 
Example 17
Source File: FooProviderBootstrap.java    From Sentinel-Dashboard-Nacos 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(10);
    flowRule.setGrade(RuleConstant.FLOW_GRADE_QPS);
    flowRule.setLimitApp("default");
    FlowRuleManager.loadRules(Collections.singletonList(flowRule));
}
 
Example 18
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 19
Source File: FooProviderBootstrap.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(10);
    flowRule.setGrade(RuleConstant.FLOW_GRADE_QPS);
    flowRule.setLimitApp("default");
    FlowRuleManager.loadRules(Collections.singletonList(flowRule));
}
 
Example 20
Source File: FooConsumerBootstrap.java    From Sentinel-Dashboard-Nacos 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));
}