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;
}
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);
}
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());
}
}
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);
}
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;
}
Aggregations