Search in sources :

Example 11 with ApiDefinition

use of com.alibaba.csp.sentinel.adapter.gateway.common.api.ApiDefinition in project Sentinel by alibaba.

the class UpdateGatewayApiDefinitionGroupCommandHandler method parseJson.

/**
 * Parse json data to set of {@link ApiDefinition}.
 *
 * Since the predicateItems of {@link ApiDefinition} is set of interface,
 * here we parse predicateItems to {@link ApiPathPredicateItem} temporarily.
 */
private Set<ApiDefinition> parseJson(String data) {
    Set<ApiDefinition> apiDefinitions = new HashSet<>();
    JSONArray array = JSON.parseArray(data);
    for (Object obj : array) {
        JSONObject o = (JSONObject) obj;
        ApiDefinition apiDefinition = new ApiDefinition((o.getString("apiName")));
        Set<ApiPredicateItem> predicateItems = new HashSet<>();
        JSONArray itemArray = o.getJSONArray("predicateItems");
        if (itemArray != null) {
            predicateItems.addAll(itemArray.toJavaList(ApiPathPredicateItem.class));
        }
        apiDefinition.setPredicateItems(predicateItems);
        apiDefinitions.add(apiDefinition);
    }
    return apiDefinitions;
}
Also used : JSONObject(com.alibaba.fastjson.JSONObject) ApiDefinition(com.alibaba.csp.sentinel.adapter.gateway.common.api.ApiDefinition) JSONArray(com.alibaba.fastjson.JSONArray) JSONObject(com.alibaba.fastjson.JSONObject) ApiPathPredicateItem(com.alibaba.csp.sentinel.adapter.gateway.common.api.ApiPathPredicateItem) ApiPredicateItem(com.alibaba.csp.sentinel.adapter.gateway.common.api.ApiPredicateItem) HashSet(java.util.HashSet)

Example 12 with ApiDefinition

use of com.alibaba.csp.sentinel.adapter.gateway.common.api.ApiDefinition in project matecloud by matevip.

the class SentinelConfig method initCustomizedApis.

/**
 * 样例,可以扩展使用
 * 文档参见:https://github.com/alibaba/Sentinel/wiki/%E7%BD%91%E5%85%B3%E9%99%90%E6%B5%81
 */
private void initCustomizedApis() {
    Set<ApiDefinition> definitions = new HashSet<>();
    ApiDefinition api1 = new ApiDefinition("some_customized_api").setPredicateItems(new HashSet<ApiPredicateItem>() {

        {
            add(new ApiPathPredicateItem().setPattern("/ahas"));
            add(new ApiPathPredicateItem().setPattern("/product/**").setMatchStrategy(SentinelGatewayConstants.URL_MATCH_STRATEGY_PREFIX));
        }
    });
    ApiDefinition api2 = new ApiDefinition("another_customized_api").setPredicateItems(new HashSet<ApiPredicateItem>() {

        {
            add(new ApiPathPredicateItem().setPattern("/**").setMatchStrategy(SentinelGatewayConstants.URL_MATCH_STRATEGY_PREFIX));
        }
    });
    definitions.add(api1);
    definitions.add(api2);
    GatewayApiDefinitionManager.loadApiDefinitions(definitions);
}
Also used : ApiDefinition(com.alibaba.csp.sentinel.adapter.gateway.common.api.ApiDefinition) ApiPathPredicateItem(com.alibaba.csp.sentinel.adapter.gateway.common.api.ApiPathPredicateItem) ApiPredicateItem(com.alibaba.csp.sentinel.adapter.gateway.common.api.ApiPredicateItem) HashSet(java.util.HashSet)

Example 13 with ApiDefinition

use of com.alibaba.csp.sentinel.adapter.gateway.common.api.ApiDefinition 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 14 with ApiDefinition

use of com.alibaba.csp.sentinel.adapter.gateway.common.api.ApiDefinition 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 15 with ApiDefinition

use of com.alibaba.csp.sentinel.adapter.gateway.common.api.ApiDefinition in project RuoYi-Cloud-Plus by JavaLionLi.

the class ApiDefinitionEntity method toApiDefinition.

public ApiDefinition toApiDefinition() {
    ApiDefinition apiDefinition = new ApiDefinition();
    apiDefinition.setApiName(apiName);
    Set<ApiPredicateItem> apiPredicateItems = new LinkedHashSet<>();
    apiDefinition.setPredicateItems(apiPredicateItems);
    if (predicateItems != null) {
        for (ApiPredicateItemEntity predicateItem : predicateItems) {
            ApiPathPredicateItem apiPredicateItem = new ApiPathPredicateItem();
            apiPredicateItems.add(apiPredicateItem);
            apiPredicateItem.setMatchStrategy(predicateItem.getMatchStrategy());
            apiPredicateItem.setPattern(predicateItem.getPattern());
        }
    }
    return apiDefinition;
}
Also used : LinkedHashSet(java.util.LinkedHashSet) ApiDefinition(com.alibaba.csp.sentinel.adapter.gateway.common.api.ApiDefinition) ApiPathPredicateItem(com.alibaba.csp.sentinel.adapter.gateway.common.api.ApiPathPredicateItem) ApiPredicateItem(com.alibaba.csp.sentinel.adapter.gateway.common.api.ApiPredicateItem)

Aggregations

ApiDefinition (com.alibaba.csp.sentinel.adapter.gateway.common.api.ApiDefinition)16 ApiPathPredicateItem (com.alibaba.csp.sentinel.adapter.gateway.common.api.ApiPathPredicateItem)13 ApiPredicateItem (com.alibaba.csp.sentinel.adapter.gateway.common.api.ApiPredicateItem)13 HashSet (java.util.HashSet)6 LinkedHashSet (java.util.LinkedHashSet)5 GatewayFlowRule (com.alibaba.csp.sentinel.adapter.gateway.common.rule.GatewayFlowRule)2 DegradeRule (com.alibaba.csp.sentinel.slots.block.degrade.DegradeRule)2 WebExchangeApiMatcher (com.alibaba.csp.sentinel.adapter.gateway.sc.api.matcher.WebExchangeApiMatcher)1 JSONArray (com.alibaba.fastjson.JSONArray)1 JSONObject (com.alibaba.fastjson.JSONObject)1 Listener (com.alibaba.nacos.api.config.listener.Listener)1 DoorGoodConfig (com.hummer.doorgod.service.domain.configuration.DoorGoodConfig)1 HashMap (java.util.HashMap)1 ConcurrentHashMap (java.util.concurrent.ConcurrentHashMap)1 Test (org.junit.Test)1 RequestPath (org.springframework.http.server.RequestPath)1 ServerHttpRequest (org.springframework.http.server.reactive.ServerHttpRequest)1 ServerWebExchange (org.springframework.web.server.ServerWebExchange)1