Search in sources :

Example 31 with DegradeRule

use of com.alibaba.csp.sentinel.slots.block.degrade.DegradeRule 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)

Example 32 with DegradeRule

use of com.alibaba.csp.sentinel.slots.block.degrade.DegradeRule in project study-by-myself by Howinfun.

the class SentinelRulesGenerateConfig method onApplicationEvent.

@Override
public void onApplicationEvent(ContextRefreshedEvent contextRefreshedEvent) {
    SentinelRulesProperties sentinelRulesProperties = this.applicationContext.getBean(SentinelRulesProperties.class);
    List<SentinelFlowRule> flowRuleList = sentinelRulesProperties.getFlowRuleList();
    List<SentinelDegradeRule> degradeRuleList = sentinelRulesProperties.getDegradeRuleList();
    final List<FlowRule> flowRules = new ArrayList<>();
    final List<DegradeRule> degradeRules = new ArrayList<>();
    // 处理流控规则
    flowRuleList.forEach(sentinelFlowRule -> {
        FlowRule flowRule = new FlowRule();
        BeanUtils.copyProperties(sentinelFlowRule, flowRule);
        flowRules.add(flowRule);
    });
    // 处理熔断规则
    degradeRuleList.forEach(sentinelDegradeRule -> {
        DegradeRule degradeRule = new DegradeRule();
        BeanUtils.copyProperties(sentinelDegradeRule, degradeRule);
        degradeRules.add(degradeRule);
    });
    FlowRuleManager.loadRules(flowRules);
    DegradeRuleManager.loadRules(degradeRules);
}
Also used : SentinelDegradeRule(com.github.howinfun.sentinel.pojo.SentinelDegradeRule) SentinelRulesProperties(com.github.howinfun.sentinel.properties.SentinelRulesProperties) ArrayList(java.util.ArrayList) SentinelFlowRule(com.github.howinfun.sentinel.pojo.SentinelFlowRule) FlowRule(com.alibaba.csp.sentinel.slots.block.flow.FlowRule) SentinelFlowRule(com.github.howinfun.sentinel.pojo.SentinelFlowRule) DegradeRule(com.alibaba.csp.sentinel.slots.block.degrade.DegradeRule) SentinelDegradeRule(com.github.howinfun.sentinel.pojo.SentinelDegradeRule)

Example 33 with DegradeRule

use of com.alibaba.csp.sentinel.slots.block.degrade.DegradeRule in project RuoYi-Cloud-Plus by JavaLionLi.

the class DegradeRuleEntity method toRule.

@Override
public DegradeRule toRule() {
    DegradeRule rule = new DegradeRule();
    rule.setResource(resource);
    rule.setLimitApp(limitApp);
    rule.setCount(count);
    rule.setTimeWindow(timeWindow);
    rule.setGrade(grade);
    if (minRequestAmount != null) {
        rule.setMinRequestAmount(minRequestAmount);
    }
    if (slowRatioThreshold != null) {
        rule.setSlowRatioThreshold(slowRatioThreshold);
    }
    if (statIntervalMs != null) {
        rule.setStatIntervalMs(statIntervalMs);
    }
    return rule;
}
Also used : DegradeRule(com.alibaba.csp.sentinel.slots.block.degrade.DegradeRule)

Example 34 with DegradeRule

use of com.alibaba.csp.sentinel.slots.block.degrade.DegradeRule in project Sermant by huaweicloud.

the class DegradeRuleVo method toRule.

@Override
public DegradeRule toRule() {
    DegradeRule rule = new DegradeRule();
    rule.setResource(resource);
    rule.setLimitApp(limitApp);
    rule.setCount(count);
    rule.setTimeWindow(timeWindow);
    rule.setGrade(grade);
    if (minRequestAmount != null) {
        rule.setMinRequestAmount(minRequestAmount);
    }
    if (slowRatioThreshold != null) {
        rule.setSlowRatioThreshold(slowRatioThreshold);
    }
    if (statIntervalMs != null) {
        rule.setStatIntervalMs(statIntervalMs);
    }
    return rule;
}
Also used : DegradeRule(com.alibaba.csp.sentinel.slots.block.degrade.DegradeRule)

Aggregations

DegradeRule (com.alibaba.csp.sentinel.slots.block.degrade.DegradeRule)34 ArrayList (java.util.ArrayList)12 FlowRule (com.alibaba.csp.sentinel.slots.block.flow.FlowRule)9 Test (org.junit.Test)4 List (java.util.List)3 ApiDefinition (com.alibaba.csp.sentinel.adapter.gateway.common.api.ApiDefinition)2 GatewayFlowRule (com.alibaba.csp.sentinel.adapter.gateway.common.rule.GatewayFlowRule)2 AuthorityRule (com.alibaba.csp.sentinel.slots.block.authority.AuthorityRule)2 ParamFlowItem (com.alibaba.csp.sentinel.slots.block.flow.param.ParamFlowItem)2 ParamFlowRule (com.alibaba.csp.sentinel.slots.block.flow.param.ParamFlowRule)2 SystemRule (com.alibaba.csp.sentinel.slots.system.SystemRule)2 AbstractTimeBasedTest (com.alibaba.csp.sentinel.test.AbstractTimeBasedTest)2 SentinelDegradeRule (com.github.howinfun.sentinel.pojo.SentinelDegradeRule)2 SentinelFlowRule (com.github.howinfun.sentinel.pojo.SentinelFlowRule)2 HashSet (java.util.HashSet)2 RuleData (org.apache.shenyu.common.dto.RuleData)2 SentinelHandle (org.apache.shenyu.common.dto.convert.rule.SentinelHandle)2 EntryType (com.alibaba.csp.sentinel.EntryType)1 DegradeRuleManager (com.alibaba.csp.sentinel.slots.block.degrade.DegradeRuleManager)1 FlowRuleManager (com.alibaba.csp.sentinel.slots.block.flow.FlowRuleManager)1