Search in sources :

Example 26 with DegradeRule

use of com.alibaba.csp.sentinel.slots.block.degrade.DegradeRule in project JHodgepodge by werwolfGu.

the class RtDegradeDemo method initDegradeRule.

private static void initDegradeRule() {
    List<DegradeRule> rules = new ArrayList<DegradeRule>();
    DegradeRule rule = new DegradeRule();
    rule.setResource(KEY);
    // set threshold rt, 10 ms
    rule.setCount(10);
    rule.setGrade(RuleConstant.DEGRADE_GRADE_RT);
    rule.setTimeWindow(10);
    rules.add(rule);
    DegradeRuleManager.loadRules(rules);
}
Also used : ArrayList(java.util.ArrayList) DegradeRule(com.alibaba.csp.sentinel.slots.block.degrade.DegradeRule)

Example 27 with DegradeRule

use of com.alibaba.csp.sentinel.slots.block.degrade.DegradeRule in project hummer-framework by hummer-team.

the class ListenerConfig method addSentinelConfigForUpdate.

private void addSentinelConfigForUpdate(Set<DegradeRule> degradeRules, Set<GatewayFlowRule> gatewayFlowRules, Set<ApiDefinition> apiDefinitions, DoorGoodConfig doorGoodConfig) {
    if (!PropertiesContainer.valueOf("csp.sentinel.enable", Boolean.class, false)) {
        return;
    }
    if (doorGoodConfig.getSentinelConfig() == null) {
        cleanSentinelConfig(degradeRules, gatewayFlowRules, apiDefinitions);
        return;
    }
    if (CollectionUtils.isNotEmpty(doorGoodConfig.getSentinelConfig().getDegradeRule2())) {
        Map<String, DegradeRule> degradeRuleMap = new ConcurrentHashMap<>(16);
        for (DegradeRule rule : degradeRules) {
            degradeRuleMap.put(rule.getResource(), rule);
        }
        for (DegradeRule rule : doorGoodConfig.getSentinelConfig().getDegradeRule2()) {
            degradeRuleMap.put(rule.getResource(), rule);
        }
        degradeRules.clear();
        degradeRules.addAll(degradeRuleMap.values());
    }
    if (CollectionUtils.isNotEmpty(doorGoodConfig.getSentinelConfig().getGatewayFlowRules())) {
        Map<String, GatewayFlowRule> gatewayFlowRuleMap = new ConcurrentHashMap<>(16);
        for (GatewayFlowRule rule : gatewayFlowRules) {
            gatewayFlowRuleMap.put(rule.getResource(), rule);
        }
        for (GatewayFlowRule rule : doorGoodConfig.getSentinelConfig().getGatewayFlowRules()) {
            gatewayFlowRuleMap.put(rule.getResource(), rule);
        }
        gatewayFlowRules.clear();
        gatewayFlowRules.addAll(gatewayFlowRuleMap.values());
    }
    if (CollectionUtils.isNotEmpty(doorGoodConfig.getSentinelConfig().getApiDefinitions())) {
        Map<String, ApiDefinition> apiDefinitionMap = new ConcurrentHashMap<>(16);
        for (ApiDefinition api : apiDefinitions) {
            apiDefinitionMap.put(api.getApiName(), api);
        }
        for (ApiDefinition api : doorGoodConfig.getSentinelConfig().getApiDefinitions()) {
            apiDefinitionMap.put(api.getApiName(), api);
        }
        apiDefinitions.clear();
        apiDefinitions.addAll(apiDefinitionMap.values());
    }
}
Also used : ApiDefinition(com.alibaba.csp.sentinel.adapter.gateway.common.api.ApiDefinition) GatewayFlowRule(com.alibaba.csp.sentinel.adapter.gateway.common.rule.GatewayFlowRule) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) DegradeRule(com.alibaba.csp.sentinel.slots.block.degrade.DegradeRule)

Example 28 with DegradeRule

use of com.alibaba.csp.sentinel.slots.block.degrade.DegradeRule in project hummer-framework by hummer-team.

the class ListenerConfig method addListener.

private void addListener(List<String> gatewayGroup, String groupId, long timeoutMillis, ConfigService configService, boolean isFirstLoading) throws NacosException {
    Set<DegradeRule> degradeRules = Sets.newConcurrentHashSet();
    Set<GatewayFlowRule> gatewayFlowRules = Sets.newConcurrentHashSet();
    Set<ApiDefinition> apiDefinitions = Sets.newConcurrentHashSet();
    for (String dataId : gatewayGroup) {
        Listener listener = getListener();
        String configVal = configService.getConfigAndSignListener(dataId, groupId, timeoutMillis, listener);
        if (!Strings.isNullOrEmpty(configVal)) {
            // refresh gateway config
            log.debug("refresh config done,config value is \n{}", configVal);
            DoorGoodConfig doorGoodConfig = JSON.parseObject(configVal, new TypeReference<DoorGoodConfig>() {
            });
            refreshGatewayRule(doorGoodConfig);
            listenerMap.put(dataId, ListenerEvent.builder().dataId(dataId).groupId(groupId).listener(listener).sentinelConfig(doorGoodConfig.getSentinelConfig()).build());
            if (isFirstLoading) {
                addSentinelConfigForInitLoad(degradeRules, gatewayFlowRules, apiDefinitions, doorGoodConfig);
            } else {
                // refresh sentinel config
                refreshSentinelConfig(doorGoodConfig);
            }
        }
    }
    firstLoadingSentinel(isFirstLoading, degradeRules, gatewayFlowRules, apiDefinitions);
}
Also used : Listener(com.alibaba.nacos.api.config.listener.Listener) DoorGoodConfig(com.hummer.doorgod.service.domain.configuration.DoorGoodConfig) ApiDefinition(com.alibaba.csp.sentinel.adapter.gateway.common.api.ApiDefinition) GatewayFlowRule(com.alibaba.csp.sentinel.adapter.gateway.common.rule.GatewayFlowRule) DegradeRule(com.alibaba.csp.sentinel.slots.block.degrade.DegradeRule)

Example 29 with DegradeRule

use of com.alibaba.csp.sentinel.slots.block.degrade.DegradeRule in project pig by pig-mesh.

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 30 with DegradeRule

use of com.alibaba.csp.sentinel.slots.block.degrade.DegradeRule in project spring-boot-student by wyh-spring-ecosystem-student.

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);
    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