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

The following examples show how to use com.alibaba.csp.sentinel.slots.block.flow.FlowRule#setCount() . 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: PaceFlowDemo.java    From Sentinel-Dashboard-Nacos 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 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: 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 4
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 5
Source File: FlowRuleEntity.java    From Sentinel-Dashboard-Nacos with Apache License 2.0 6 votes vote down vote up
@Override
public FlowRule toRule() {
    FlowRule flowRule = new FlowRule();
    flowRule.setCount(this.count);
    flowRule.setGrade(this.grade);
    flowRule.setResource(this.resource);
    flowRule.setLimitApp(this.limitApp);
    flowRule.setRefResource(this.refResource);
    flowRule.setStrategy(this.strategy);
    if (this.controlBehavior != null) {
        flowRule.setControlBehavior(controlBehavior);
    }
    if (this.warmUpPeriodSec != null) {
        flowRule.setWarmUpPeriodSec(warmUpPeriodSec);
    }
    if (this.maxQueueingTimeMs != null) {
        flowRule.setMaxQueueingTimeMs(maxQueueingTimeMs);
    }
    flowRule.setClusterMode(clusterMode);
    flowRule.setClusterConfig(clusterConfig);
    return flowRule;
}
 
Example 6
Source File: UserService.java    From sentinel-tutorial with Apache License 2.0 6 votes vote down vote up
public UserService(){
    // 定义热点限流的规则,对第一个参数设置 qps 限流模式,阈值为5
    FlowRule rule = new FlowRule();
    rule.setResource(USER_RES);
    // 限流类型,qps
    rule.setGrade(RuleConstant.FLOW_GRADE_QPS);
    // 设置阈值
    rule.setCount(5);
    // 限制哪个调用方
    rule.setLimitApp(RuleConstant.LIMIT_APP_DEFAULT);
    // 基于调用关系的流量控制
    rule.setStrategy(RuleConstant.STRATEGY_DIRECT);
    // 流控策略
    rule.setControlBehavior(RuleConstant.CONTROL_BEHAVIOR_DEFAULT);
    FlowRuleManager.loadRules(Collections.singletonList(rule));
}
 
Example 7
Source File: FlowRuleEntity.java    From Sentinel with Apache License 2.0 6 votes vote down vote up
@Override
public FlowRule toRule() {
    FlowRule flowRule = new FlowRule();
    flowRule.setCount(this.count);
    flowRule.setGrade(this.grade);
    flowRule.setResource(this.resource);
    flowRule.setLimitApp(this.limitApp);
    flowRule.setRefResource(this.refResource);
    flowRule.setStrategy(this.strategy);
    if (this.controlBehavior != null) {
        flowRule.setControlBehavior(controlBehavior);
    }
    if (this.warmUpPeriodSec != null) {
        flowRule.setWarmUpPeriodSec(warmUpPeriodSec);
    }
    if (this.maxQueueingTimeMs != null) {
        flowRule.setMaxQueueingTimeMs(maxQueueingTimeMs);
    }
    flowRule.setClusterMode(clusterMode);
    flowRule.setClusterConfig(clusterConfig);
    return flowRule;
}
 
Example 8
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 9
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 10
Source File: SentinelDubboConsumerFilterTest.java    From Sentinel with Apache License 2.0 5 votes vote down vote up
private void initFlowRule(String resource) {
    FlowRule flowRule = new FlowRule(resource);
    flowRule.setCount(1);
    flowRule.setGrade(RuleConstant.FLOW_GRADE_QPS);
    List<FlowRule> flowRules = new ArrayList<>();
    flowRules.add(flowRule);
    FlowRuleManager.loadRules(flowRules);
}
 
Example 11
Source File: FlowThreadDemo.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("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 12
Source File: PaceFlowDemo.java    From Sentinel with Apache License 2.0 5 votes vote down vote up
private static void initDefaultFlowRule() {
    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_DEFAULT means requests more than threshold will be rejected immediately.
    rule1.setControlBehavior(RuleConstant.CONTROL_BEHAVIOR_DEFAULT);

    rules.add(rule1);
    FlowRuleManager.loadRules(rules);
}
 
Example 13
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 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: WarmUpRateLimiterFlowDemo.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(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 16
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 17
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 18
Source File: PaceFlowDemo.java    From Sentinel-Dashboard-Nacos with Apache License 2.0 5 votes vote down vote up
private static void initDefaultFlowRule() {
    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_DEFAULT means requests more than threshold will be rejected immediately.
    rule1.setControlBehavior(RuleConstant.CONTROL_BEHAVIOR_DEFAULT);

    rules.add(rule1);
    FlowRuleManager.loadRules(rules);
}
 
Example 19
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 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));
}