Search in sources :

Example 11 with ApiPathPredicateItem

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

the class GatewayConfiguration method initCustomizedApis.

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 12 with ApiPathPredicateItem

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

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

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

the class ApiDefinitionEntity method fromApiDefinition.

public static ApiDefinitionEntity fromApiDefinition(String app, String ip, Integer port, ApiDefinition apiDefinition) {
    ApiDefinitionEntity entity = new ApiDefinitionEntity();
    entity.setApp(app);
    entity.setIp(ip);
    entity.setPort(port);
    entity.setApiName(apiDefinition.getApiName());
    Set<ApiPredicateItemEntity> predicateItems = new LinkedHashSet<>();
    entity.setPredicateItems(predicateItems);
    Set<ApiPredicateItem> apiPredicateItems = apiDefinition.getPredicateItems();
    if (apiPredicateItems != null) {
        for (ApiPredicateItem apiPredicateItem : apiPredicateItems) {
            ApiPredicateItemEntity itemEntity = new ApiPredicateItemEntity();
            predicateItems.add(itemEntity);
            ApiPathPredicateItem pathPredicateItem = (ApiPathPredicateItem) apiPredicateItem;
            itemEntity.setPattern(pathPredicateItem.getPattern());
            itemEntity.setMatchStrategy(pathPredicateItem.getMatchStrategy());
        }
    }
    return entity;
}
Also used : LinkedHashSet(java.util.LinkedHashSet) ApiPathPredicateItem(com.alibaba.csp.sentinel.adapter.gateway.common.api.ApiPathPredicateItem) ApiPredicateItem(com.alibaba.csp.sentinel.adapter.gateway.common.api.ApiPredicateItem)

Example 15 with ApiPathPredicateItem

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

the class ApiDefinitionEntity method fromApiDefinition.

public static ApiDefinitionEntity fromApiDefinition(String app, String ip, Integer port, ApiDefinition apiDefinition) {
    ApiDefinitionEntity entity = new ApiDefinitionEntity();
    entity.setApp(app);
    entity.setIp(ip);
    entity.setPort(port);
    entity.setApiName(apiDefinition.getApiName());
    Set<ApiPredicateItemEntity> predicateItems = new LinkedHashSet<>();
    entity.setPredicateItems(predicateItems);
    Set<ApiPredicateItem> apiPredicateItems = apiDefinition.getPredicateItems();
    if (apiPredicateItems != null) {
        for (ApiPredicateItem apiPredicateItem : apiPredicateItems) {
            ApiPredicateItemEntity itemEntity = new ApiPredicateItemEntity();
            predicateItems.add(itemEntity);
            ApiPathPredicateItem pathPredicateItem = (ApiPathPredicateItem) apiPredicateItem;
            itemEntity.setPattern(pathPredicateItem.getPattern());
            itemEntity.setMatchStrategy(pathPredicateItem.getMatchStrategy());
        }
    }
    return entity;
}
Also used : LinkedHashSet(java.util.LinkedHashSet) ApiPathPredicateItem(com.alibaba.csp.sentinel.adapter.gateway.common.api.ApiPathPredicateItem) ApiPredicateItem(com.alibaba.csp.sentinel.adapter.gateway.common.api.ApiPredicateItem)

Aggregations

ApiPathPredicateItem (com.alibaba.csp.sentinel.adapter.gateway.common.api.ApiPathPredicateItem)18 ApiPredicateItem (com.alibaba.csp.sentinel.adapter.gateway.common.api.ApiPredicateItem)18 ApiDefinition (com.alibaba.csp.sentinel.adapter.gateway.common.api.ApiDefinition)13 LinkedHashSet (java.util.LinkedHashSet)10 HashSet (java.util.HashSet)6 JSONArray (com.alibaba.fastjson.JSONArray)1 JSONObject (com.alibaba.fastjson.JSONObject)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