Search in sources :

Example 6 with ParamFlowRule

use of com.alibaba.csp.sentinel.slots.block.flow.param.ParamFlowRule in project learn-simple by muggle0.

the class SentileConfig method initRules.

@PostConstruct
private void initRules() throws Exception {
    FlowRule rule1 = new FlowRule();
    rule1.setResource("test.hello");
    rule1.setGrade(RuleConstant.FLOW_GRADE_QPS);
    // 每秒调用最大次数为 1 次
    rule1.setCount(1);
    List<FlowRule> rules = new ArrayList<>();
    // DegradeRuleManager.loadRules(List< DegradeRule > rules); // 修改降级规则
    rules.add(rule1);
    // 将控制规则载入到 Sentinel
    // AuthorityRuleManager
    FlowRuleManager.loadRules(rules);
    /* ReadableDataSource<String, List<FlowRule>> flowRuleDataSource = new NacosDataSource<>(remoteAddress, groupId, dataId,
            source -> JSON.parseObject(source, new TypeReference<List<FlowRule>>() {
            }));*/
    // FlowRuleManager.register2Property(flowRuleDataSource.getProperty());
    DegradeRule rule = new DegradeRule();
    List<DegradeRule> rules1 = new ArrayList<>();
    rule.setResource("test.hello");
    rule.setCount(0.01);
    // 秒级RT
    rule.setGrade(RuleConstant.DEGRADE_GRADE_RT);
    rule.setTimeWindow(10);
    rules1.add(rule);
    DegradeRuleManager.loadRules(rules1);
    ParamFlowRule paramFlowRule = new ParamFlowRule("resourceName").setParamIdx(0).setCount(5);
    // 单独设置限流 QPS,设置param 参数限流规则
    ParamFlowItem item = new ParamFlowItem().setObject("param").setClassType(int.class.getName()).setCount(10);
    paramFlowRule.setParamFlowItemList(Collections.singletonList(item));
    ParamFlowRuleManager.loadRules(Collections.singletonList(paramFlowRule));
    SystemRule systemRule = new SystemRule();
    systemRule.setHighestCpuUsage(0.8);
    systemRule.setAvgRt(10);
    systemRule.setQps(10);
    systemRule.setMaxThread(10);
    systemRule.setHighestSystemLoad(2.5);
    SystemRuleManager.loadRules(Collections.singletonList(systemRule));
    AuthorityRule authorityRule = new AuthorityRule();
    authorityRule.setStrategy(RuleConstant.AUTHORITY_BLACK);
    authorityRule.setLimitApp("127.0.0.1");
    authorityRule.setResource("test.hello");
    AuthorityRuleManager.loadRules(Collections.singletonList(authorityRule));
// ContextUtil.enter("xxx", "origin");
}
Also used : AuthorityRule(com.alibaba.csp.sentinel.slots.block.authority.AuthorityRule) ParamFlowItem(com.alibaba.csp.sentinel.slots.block.flow.param.ParamFlowItem) ParamFlowRule(com.alibaba.csp.sentinel.slots.block.flow.param.ParamFlowRule) ArrayList(java.util.ArrayList) SystemRule(com.alibaba.csp.sentinel.slots.system.SystemRule) ParamFlowRule(com.alibaba.csp.sentinel.slots.block.flow.param.ParamFlowRule) FlowRule(com.alibaba.csp.sentinel.slots.block.flow.FlowRule) DegradeRule(com.alibaba.csp.sentinel.slots.block.degrade.DegradeRule) PostConstruct(javax.annotation.PostConstruct)

Example 7 with ParamFlowRule

use of com.alibaba.csp.sentinel.slots.block.flow.param.ParamFlowRule in project Sentinel by alibaba.

the class ParamFlowQpsDemo method initParamFlowRules.

private static void initParamFlowRules() {
    // QPS mode, threshold is 5 for every frequent "hot spot" parameter in index 0 (the first arg).
    ParamFlowRule rule = new ParamFlowRule(RESOURCE_KEY).setParamIdx(0).setGrade(RuleConstant.FLOW_GRADE_QPS).setCount(5);
    // We can set threshold count for specific parameter value individually.
    // Here we add an exception item. That means: QPS threshold of entries with parameter `PARAM_B` (type: int)
    // in index 0 will be 10, rather than the global threshold (5).
    ParamFlowItem item = new ParamFlowItem().setObject(String.valueOf(PARAM_B)).setClassType(int.class.getName()).setCount(10);
    rule.setParamFlowItemList(Collections.singletonList(item));
    ParamFlowRuleManager.loadRules(Collections.singletonList(rule));
}
Also used : ParamFlowItem(com.alibaba.csp.sentinel.slots.block.flow.param.ParamFlowItem) ParamFlowRule(com.alibaba.csp.sentinel.slots.block.flow.param.ParamFlowRule)

Example 8 with ParamFlowRule

use of com.alibaba.csp.sentinel.slots.block.flow.param.ParamFlowRule in project Sentinel by alibaba.

the class GatewayFlowSlot method checkGatewayParamFlow.

private void checkGatewayParamFlow(ResourceWrapper resourceWrapper, int count, Object... args) throws BlockException {
    if (args == null) {
        return;
    }
    List<ParamFlowRule> rules = GatewayRuleManager.getConvertedParamRules(resourceWrapper.getName());
    if (rules == null || rules.isEmpty()) {
        return;
    }
    for (ParamFlowRule rule : rules) {
        // Initialize the parameter metrics.
        ParameterMetricStorage.initParamMetricsFor(resourceWrapper, rule);
        if (!ParamFlowChecker.passCheck(resourceWrapper, rule, count, args)) {
            String triggeredParam = "";
            if (args.length > rule.getParamIdx()) {
                Object value = args[rule.getParamIdx()];
                triggeredParam = String.valueOf(value);
            }
            throw new ParamFlowException(resourceWrapper.getName(), triggeredParam, rule);
        }
    }
}
Also used : ParamFlowRule(com.alibaba.csp.sentinel.slots.block.flow.param.ParamFlowRule) ParamFlowException(com.alibaba.csp.sentinel.slots.block.flow.param.ParamFlowException)

Example 9 with ParamFlowRule

use of com.alibaba.csp.sentinel.slots.block.flow.param.ParamFlowRule in project Sentinel by alibaba.

the class JsonSerializeTest method paramFlowRuleSerializeTest.

@Test
public void paramFlowRuleSerializeTest() {
    ParamFlowRuleEntity emptyRule = new ParamFlowRuleEntity();
    Assert.assertTrue("{}".equals(JSON.toJSONString(emptyRule)));
    ParamFlowRuleEntity paramFlowRule = new ParamFlowRuleEntity();
    ParamFlowRule rule = new ParamFlowRule();
    rule.setClusterConfig(new ParamFlowClusterConfig());
    rule.setResource("rs").setLimitApp("default");
    paramFlowRule.setRule(rule);
    Assert.assertTrue("{\"rule\":{\"burstCount\":0,\"clusterConfig\":{\"fallbackToLocalWhenFail\":false,\"sampleCount\":10,\"thresholdType\":0,\"windowIntervalMs\":1000},\"clusterMode\":false,\"controlBehavior\":0,\"count\":0.0,\"durationInSec\":1,\"grade\":1,\"limitApp\":\"default\",\"maxQueueingTimeMs\":0,\"paramFlowItemList\":[],\"resource\":\"rs\"}}".equals(JSON.toJSONString(paramFlowRule)));
}
Also used : ParamFlowRule(com.alibaba.csp.sentinel.slots.block.flow.param.ParamFlowRule) ParamFlowClusterConfig(com.alibaba.csp.sentinel.slots.block.flow.param.ParamFlowClusterConfig) ParamFlowRuleEntity(com.alibaba.csp.sentinel.dashboard.datasource.entity.rule.ParamFlowRuleEntity) Test(org.junit.Test)

Example 10 with ParamFlowRule

use of com.alibaba.csp.sentinel.slots.block.flow.param.ParamFlowRule in project spring-boot-student by wyh-spring-ecosystem-student.

the class JsonSerializeTest method paramFlowRuleSerializeTest.

@Test
public void paramFlowRuleSerializeTest() {
    ParamFlowRuleEntity emptyRule = new ParamFlowRuleEntity();
    Assert.assertTrue("{}".equals(JSON.toJSONString(emptyRule)));
    ParamFlowRuleEntity paramFlowRule = new ParamFlowRuleEntity();
    ParamFlowRule rule = new ParamFlowRule();
    rule.setClusterConfig(new ParamFlowClusterConfig());
    rule.setResource("rs").setLimitApp("default");
    paramFlowRule.setRule(rule);
    Assert.assertTrue("{\"rule\":{\"burstCount\":0,\"clusterConfig\":{\"fallbackToLocalWhenFail\":false,\"sampleCount\":10,\"thresholdType\":0,\"windowIntervalMs\":1000},\"clusterMode\":false,\"controlBehavior\":0,\"count\":0.0,\"durationInSec\":1,\"grade\":1,\"limitApp\":\"default\",\"maxQueueingTimeMs\":0,\"paramFlowItemList\":[],\"resource\":\"rs\"}}".equals(JSON.toJSONString(paramFlowRule)));
}
Also used : ParamFlowRule(com.alibaba.csp.sentinel.slots.block.flow.param.ParamFlowRule) ParamFlowClusterConfig(com.alibaba.csp.sentinel.slots.block.flow.param.ParamFlowClusterConfig) ParamFlowRuleEntity(com.alibaba.csp.sentinel.dashboard.datasource.entity.rule.ParamFlowRuleEntity) Test(org.junit.Test)

Aggregations

ParamFlowRule (com.alibaba.csp.sentinel.slots.block.flow.param.ParamFlowRule)11 ParamFlowClusterConfig (com.alibaba.csp.sentinel.slots.block.flow.param.ParamFlowClusterConfig)3 ParamFlowItem (com.alibaba.csp.sentinel.slots.block.flow.param.ParamFlowItem)3 ArrayList (java.util.ArrayList)3 Test (org.junit.Test)3 ClusterParamMetric (com.alibaba.csp.sentinel.cluster.flow.statistic.metric.ClusterParamMetric)2 ParamFlowRuleEntity (com.alibaba.csp.sentinel.dashboard.datasource.entity.rule.ParamFlowRuleEntity)2 AuthorityRule (com.alibaba.csp.sentinel.slots.block.authority.AuthorityRule)2 DegradeRule (com.alibaba.csp.sentinel.slots.block.degrade.DegradeRule)2 FlowRule (com.alibaba.csp.sentinel.slots.block.flow.FlowRule)2 SystemRule (com.alibaba.csp.sentinel.slots.system.SystemRule)2 ParamFlowException (com.alibaba.csp.sentinel.slots.block.flow.param.ParamFlowException)1 HashSet (java.util.HashSet)1 ConcurrentHashMap (java.util.concurrent.ConcurrentHashMap)1 PostConstruct (javax.annotation.PostConstruct)1