Search in sources :

Example 1 with SystemRule

use of com.alibaba.csp.sentinel.slots.system.SystemRule 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 2 with SystemRule

use of com.alibaba.csp.sentinel.slots.system.SystemRule in project hummer-framework by hummer-team.

the class GatewayContextInit method registerSentinelSystemRuleConfig.

private void registerSentinelSystemRuleConfig() {
    // sentinel system config
    if (PropertiesContainer.valueOf("system.sentinel.sys.rule.enable", Boolean.class, true)) {
        SystemRule sysRule = new SystemRule();
        sysRule.setMaxThread(PropertiesContainer.valueOfInteger("system.sentinel.max.thread", 1000));
        sysRule.setHighestCpuUsage(PropertiesContainer.valueOf("system.sentinel.max.cpu.use", Double.class, 0.85));
        sysRule.setQps(PropertiesContainer.valueOfInteger("system.sentinel.max.qps", 10000));
        sysRule.setResource("system.sentinel");
        // register sys rule.
        SystemRuleManager.loadRules(Collections.singletonList(sysRule));
        log.debug("init sentinel rule config done.");
    }
}
Also used : SystemRule(com.alibaba.csp.sentinel.slots.system.SystemRule)

Example 3 with SystemRule

use of com.alibaba.csp.sentinel.slots.system.SystemRule in project spring-boot-student by wyh-spring-ecosystem-student.

the class SystemRuleEntity method toRule.

@Override
public SystemRule toRule() {
    SystemRule rule = new SystemRule();
    rule.setHighestSystemLoad(highestSystemLoad);
    rule.setAvgRt(avgRt);
    rule.setMaxThread(maxThread);
    rule.setQps(qps);
    rule.setHighestCpuUsage(highestCpuUsage);
    return rule;
}
Also used : SystemRule(com.alibaba.csp.sentinel.slots.system.SystemRule)

Example 4 with SystemRule

use of com.alibaba.csp.sentinel.slots.system.SystemRule in project novel-cloud by 201206030.

the class GatewayConfiguration method initSystemRule.

/**
 * 系统自适应限流规则
 */
private void initSystemRule() {
    List<SystemRule> rules = new ArrayList<>();
    SystemRule rule = new SystemRule();
    // max load is 3
    rule.setHighestSystemLoad(3.0);
    // max cpu usage is 60%
    rule.setHighestCpuUsage(0.6);
    // max avg rt of all request is 10 ms
    rule.setAvgRt(10);
    // max total qps is 20
    rule.setQps(20);
    // max parallel working thread is 10
    rule.setMaxThread(10);
    rules.add(rule);
    SystemRuleManager.loadRules(Collections.singletonList(rule));
}
Also used : SystemRule(com.alibaba.csp.sentinel.slots.system.SystemRule)

Example 5 with SystemRule

use of com.alibaba.csp.sentinel.slots.system.SystemRule in project Sentinel by alibaba.

the class SystemGuardDemo method initSystemRule.

private static void initSystemRule() {
    List<SystemRule> rules = new ArrayList<SystemRule>();
    SystemRule rule = new SystemRule();
    // max load is 3
    rule.setHighestSystemLoad(3.0);
    // max cpu usage is 60%
    rule.setHighestCpuUsage(0.6);
    // max avg rt of all request is 10 ms
    rule.setAvgRt(10);
    // max total qps is 20
    rule.setQps(20);
    // max parallel working thread is 10
    rule.setMaxThread(10);
    rules.add(rule);
    SystemRuleManager.loadRules(Collections.singletonList(rule));
}
Also used : ArrayList(java.util.ArrayList) SystemRule(com.alibaba.csp.sentinel.slots.system.SystemRule)

Aggregations

SystemRule (com.alibaba.csp.sentinel.slots.system.SystemRule)13 ArrayList (java.util.ArrayList)3 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 ParamFlowItem (com.alibaba.csp.sentinel.slots.block.flow.param.ParamFlowItem)2 ParamFlowRule (com.alibaba.csp.sentinel.slots.block.flow.param.ParamFlowRule)2 Test (org.junit.Test)2 Context (com.alibaba.csp.sentinel.context.Context)1 ClusterNode (com.alibaba.csp.sentinel.node.ClusterNode)1 DefaultNode (com.alibaba.csp.sentinel.node.DefaultNode)1 PostConstruct (javax.annotation.PostConstruct)1