Search in sources :

Example 16 with GatewayFlowRule

use of com.alibaba.csp.sentinel.adapter.gateway.common.rule.GatewayFlowRule 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 17 with GatewayFlowRule

use of com.alibaba.csp.sentinel.adapter.gateway.common.rule.GatewayFlowRule 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 18 with GatewayFlowRule

use of com.alibaba.csp.sentinel.adapter.gateway.common.rule.GatewayFlowRule in project pig by pig-mesh.

the class GatewayFlowRuleEntity method toGatewayFlowRule.

public GatewayFlowRule toGatewayFlowRule() {
    GatewayFlowRule rule = new GatewayFlowRule();
    rule.setResource(resource);
    rule.setResourceMode(resourceMode);
    rule.setGrade(grade);
    rule.setCount(count);
    rule.setIntervalSec(calIntervalSec(interval, intervalUnit));
    rule.setControlBehavior(controlBehavior);
    if (burst != null) {
        rule.setBurst(burst);
    }
    if (maxQueueingTimeoutMs != null) {
        rule.setMaxQueueingTimeoutMs(maxQueueingTimeoutMs);
    }
    if (paramItem != null) {
        GatewayParamFlowItem ruleItem = new GatewayParamFlowItem();
        rule.setParamItem(ruleItem);
        ruleItem.setParseStrategy(paramItem.getParseStrategy());
        ruleItem.setFieldName(paramItem.getFieldName());
        ruleItem.setPattern(paramItem.getPattern());
        if (paramItem.getMatchStrategy() != null) {
            ruleItem.setMatchStrategy(paramItem.getMatchStrategy());
        }
    }
    return rule;
}
Also used : GatewayParamFlowItem(com.alibaba.csp.sentinel.adapter.gateway.common.rule.GatewayParamFlowItem) GatewayFlowRule(com.alibaba.csp.sentinel.adapter.gateway.common.rule.GatewayFlowRule)

Example 19 with GatewayFlowRule

use of com.alibaba.csp.sentinel.adapter.gateway.common.rule.GatewayFlowRule in project spring-boot-student by wyh-spring-ecosystem-student.

the class GatewayFlowRuleEntity method toGatewayFlowRule.

public GatewayFlowRule toGatewayFlowRule() {
    GatewayFlowRule rule = new GatewayFlowRule();
    rule.setResource(resource);
    rule.setResourceMode(resourceMode);
    rule.setGrade(grade);
    rule.setCount(count);
    rule.setIntervalSec(calIntervalSec(interval, intervalUnit));
    rule.setControlBehavior(controlBehavior);
    if (burst != null) {
        rule.setBurst(burst);
    }
    if (maxQueueingTimeoutMs != null) {
        rule.setMaxQueueingTimeoutMs(maxQueueingTimeoutMs);
    }
    if (paramItem != null) {
        GatewayParamFlowItem ruleItem = new GatewayParamFlowItem();
        rule.setParamItem(ruleItem);
        ruleItem.setParseStrategy(paramItem.getParseStrategy());
        ruleItem.setFieldName(paramItem.getFieldName());
        ruleItem.setPattern(paramItem.getPattern());
        if (paramItem.getMatchStrategy() != null) {
            ruleItem.setMatchStrategy(paramItem.getMatchStrategy());
        }
    }
    return rule;
}
Also used : GatewayParamFlowItem(com.alibaba.csp.sentinel.adapter.gateway.common.rule.GatewayParamFlowItem) GatewayFlowRule(com.alibaba.csp.sentinel.adapter.gateway.common.rule.GatewayFlowRule)

Example 20 with GatewayFlowRule

use of com.alibaba.csp.sentinel.adapter.gateway.common.rule.GatewayFlowRule in project spring-cloud-alibaba by longlylong.

the class NacosSentinelConfig method initGatewayRules.

private void initGatewayRules() {
    List<RouteDefinition> routes = gatewayProperties.getRoutes();
    Set<GatewayFlowRule> rules = new HashSet<>();
    for (RouteDefinition route : routes) {
        rules.add(new GatewayFlowRule(route.getId()).setCount(200).setIntervalSec(1).setBurst(100).setResourceMode(SentinelGatewayConstants.RESOURCE_MODE_ROUTE_ID));
    }
    GatewayRuleManager.loadRules(rules);
}
Also used : RouteDefinition(org.springframework.cloud.gateway.route.RouteDefinition) GatewayFlowRule(com.alibaba.csp.sentinel.adapter.gateway.common.rule.GatewayFlowRule)

Aggregations

GatewayFlowRule (com.alibaba.csp.sentinel.adapter.gateway.common.rule.GatewayFlowRule)21 GatewayParamFlowItem (com.alibaba.csp.sentinel.adapter.gateway.common.rule.GatewayParamFlowItem)15 HashSet (java.util.HashSet)11 Test (org.junit.Test)6 ApiDefinition (com.alibaba.csp.sentinel.adapter.gateway.common.api.ApiDefinition)2 DegradeRule (com.alibaba.csp.sentinel.slots.block.degrade.DegradeRule)2 HashMap (java.util.HashMap)2 ServerWebExchange (org.springframework.web.server.ServerWebExchange)2 AsyncEntry (com.alibaba.csp.sentinel.AsyncEntry)1 SentinelGatewayConstants (com.alibaba.csp.sentinel.adapter.gateway.common.SentinelGatewayConstants)1 GatewayApiDefinitionManager (com.alibaba.csp.sentinel.adapter.gateway.common.api.GatewayApiDefinitionManager)1 GatewayParamParser (com.alibaba.csp.sentinel.adapter.gateway.common.param.GatewayParamParser)1 GatewayRuleManager (com.alibaba.csp.sentinel.adapter.gateway.common.rule.GatewayRuleManager)1 RuleConstant (com.alibaba.csp.sentinel.slots.block.RuleConstant)1 Listener (com.alibaba.nacos.api.config.listener.Listener)1 DoorGoodConfig (com.hummer.doorgod.service.domain.configuration.DoorGoodConfig)1 InetAddress (java.net.InetAddress)1 InetSocketAddress (java.net.InetSocketAddress)1 Map (java.util.Map)1 Set (java.util.Set)1