Search in sources :

Example 1 with ParamFlowItem

use of com.alibaba.csp.sentinel.slots.block.flow.param.ParamFlowItem 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 ParamFlowItem

use of com.alibaba.csp.sentinel.slots.block.flow.param.ParamFlowItem 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 3 with ParamFlowItem

use of com.alibaba.csp.sentinel.slots.block.flow.param.ParamFlowItem in project Spring-Cloud by zhao-staff-officer.

the class SentinelDemo1 method initRule.

public static void initRule() {
    // 流浪控制
    List<FlowRule> flowRules = new ArrayList<>();
    FlowRule rule_flow = new FlowRule();
    // 申明定义资源
    rule_flow.setRefResource("sentinel");
    // 限流阈值类型,QPS 或线程数模式
    // The threshold type of flow control (0: thread count, 1: QPS).
    rule_flow.setGrade(RuleConstant.FLOW_GRADE_QPS);
    // 流控针对的调用来源
    // rule.setLimitApp("");
    // 直接拒绝 / 排队等待 / 慢启动模式
    // 0. default(reject directly), 1. warm up, 2. rate limiter, 3. warm up + rate limiter
    // rule.setControlBehavior(0);
    // 限流阈值
    rule_flow.setCount(2);
    flowRules.add(rule_flow);
    FlowRuleManager.loadRules(flowRules);
    // 熔断降级规则
    List<DegradeRule> degraRules = new ArrayList<>();
    DegradeRule rule_degra = new DegradeRule();
    // 申明熔断资源
    rule_degra.setResource("sentinel");
    // 申明阀值
    rule_degra.setCount(1);
    rule_degra.setGrade(RuleConstant.DEGRADE_GRADE_RT);
    // 降级的时间,单位为 s
    rule_degra.setTimeWindow(10);
    degraRules.add(rule_degra);
    DegradeRuleManager.loadRules(degraRules);
    // 系统保护,此数据最后不要设置,是基于服务器整个控制
    List<SystemRule> sysRules = new ArrayList<>();
    SystemRule rule_sys = new SystemRule();
    // 当系统 load1 超过阈值,且系统当前的并发线程数超过系统容量时才会触发系统保护。系统容量由系统的 maxQps * minRt 计算得出。设定参考值一般是 CPU cores * 2.5。
    rule_sys.setHighestSystemLoad(100);
    // 所有入口流量的平均响应时间
    rule_sys.setAvgRt(1000);
    // 入口流量的最大并发数
    rule_sys.setMaxThread(1000);
    // 所有入口资源的 QPS
    rule_sys.setQps(-1);
    sysRules.add(rule_sys);
    SystemRuleManager.loadRules(sysRules);
    // 授权规则,黑白名单
    AuthorityRule authorRule = new AuthorityRule();
    authorRule.setResource("test");
    authorRule.setStrategy(RuleConstant.AUTHORITY_WHITE);
    authorRule.setLimitApp("appA,appB");
    AuthorityRuleManager.loadRules(Collections.singletonList(authorRule));
    // 热点参数限流
    ParamFlowRule rule = new ParamFlowRule("sentinel").setParamIdx(0).setCount(5);
    // 针对 int 类型的参数 PARAM_B,单独设置限流 QPS 阈值为 10,而不是全局的阈值 5.
    ParamFlowItem item = new ParamFlowItem().setObject(String.valueOf("param")).setClassType(int.class.getName()).setCount(10);
    rule.setParamFlowItemList(Collections.singletonList(item));
    ParamFlowRuleManager.loadRules(Collections.singletonList(rule));
}
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)

Aggregations

ParamFlowItem (com.alibaba.csp.sentinel.slots.block.flow.param.ParamFlowItem)3 ParamFlowRule (com.alibaba.csp.sentinel.slots.block.flow.param.ParamFlowRule)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 SystemRule (com.alibaba.csp.sentinel.slots.system.SystemRule)2 ArrayList (java.util.ArrayList)2 PostConstruct (javax.annotation.PostConstruct)1