Skip to content

Commit

Permalink
*修改兼容性
Browse files Browse the repository at this point in the history
Signed-off-by: provenceee <[email protected]>
  • Loading branch information
provenceee committed Jan 16, 2024
1 parent 6e284a1 commit 9841121
Show file tree
Hide file tree
Showing 19 changed files with 774 additions and 69 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -47,12 +47,12 @@ public class RouterConstant {
public static final String IS_METHOD_SUFFIX = "()";

/**
* 标签路由key前缀
* 流量路由key前缀
*/
public static final String ROUTER_KEY_PREFIX = "servicecomb.routeRule";

/**
* 标签路由全局规则key
* 流量路由全局规则key
*/
public static final String GLOBAL_ROUTER_KEY = "servicecomb.globalRouteRule";

Expand Down Expand Up @@ -156,6 +156,44 @@ public class RouterConstant {
*/
public static final String ZONE = "zone";

/**
* 标签路由key前缀
*/
public static final String TAG_KEY_PREFIX = "servicecomb.tagRule";

/**
* 标签路由全局规则key
*/
public static final String GLOBAL_TAG_KEY = "servicecomb.globalTagRule";

/**
* 泳道key前缀
*/
public static final String LANE_KEY_PREFIX = "servicecomb.laneRule";

/**
* 泳道全局规则key
*/
public static final String GLOBAL_LANE_KEY = "servicecomb.globalLaneRule";

/**
* 全量服务级兼容的key
*/
public static final List<String> COMPATIBILITY_KEY_LIST = Arrays.asList(ROUTER_KEY_PREFIX, TAG_KEY_PREFIX,
LANE_KEY_PREFIX);

/**
* 全局级兼容的key
*/
public static final List<String> GLOBAL_COMPATIBILITY_KEY_LIST = Arrays.asList(GLOBAL_ROUTER_KEY, GLOBAL_TAG_KEY,
GLOBAL_LANE_KEY);

/**
* 服务级兼容的key
*/
public static final List<String> SERVICE_COMPATIBILITY_KEY_LIST = Arrays.asList(ROUTER_KEY_PREFIX + POINT,
TAG_KEY_PREFIX + POINT, LANE_KEY_PREFIX + POINT);

private RouterConstant() {
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,20 @@ public void updateServiceRule(String serviceName, List<EntireRule> entireRules)
}
}

/**
* 按类型更新服务粒度的路由规则
*
* @param serviceName 服务名
* @param entireRule 规则
*/
public void updateServiceRule(String serviceName, EntireRule entireRule) {
Map<String, List<Rule>> ruleList = rules.computeIfAbsent(entireRule.getKind(),
key -> new ConcurrentHashMap<>());
ruleList.put(serviceName, entireRule.getRules());
LOGGER.info(String.format(Locale.ROOT, "Rule for %s has been updated: %s ", serviceName,
JSONObject.toJSONString(entireRule)));
}

/**
* 移除服务的路由规则
*
Expand All @@ -123,6 +137,20 @@ public void removeServiceRule(String serviceName) {
LOGGER.info(String.format(Locale.ROOT, "All rules for %s have been removed! ", serviceName));
}

/**
* 移除服务的路由规则
*
* @param serviceName 服务名
* @param kind 规则类型
*/
public void removeServiceRule(String serviceName, String kind) {
Map<String, List<Rule>> ruleList = rules.get(kind);
if (!CollectionUtils.isEmpty(ruleList)) {
ruleList.remove(serviceName);
}
LOGGER.info(String.format(Locale.ROOT, "%s rules for %s have been removed! ", kind, serviceName));
}

/**
* 重置路由规则
*
Expand All @@ -139,6 +167,26 @@ public void resetRouteRule(Map<String, List<EntireRule>> map) {
}
}

/**
* 重置路由规则
*
* @param kind 规则类型
* @param map 路由规则
*/
public void resetRouteRule(String kind, Map<String, EntireRule> map) {
if (CollectionUtils.isEmpty(map)) {
rules.remove(kind);
} else {
for (Map.Entry<String, EntireRule> ruleEntry : map.entrySet()) {
EntireRule entireRule = ruleEntry.getValue();
Map<String, List<Rule>> serviceRuleMap = rules.compute(kind, (key, value) -> new ConcurrentHashMap<>());
serviceRuleMap.put(ruleEntry.getKey(), entireRule.getRules());
}
}
LOGGER.info(String.format(Locale.ROOT, "Service rules have been updated: %s",
JSONObject.toJSONString(map)));
}

/**
* 重置全局路由规则
*
Expand All @@ -153,6 +201,21 @@ public void resetGlobalRule(List<EntireRule> list) {
JSONObject.toJSONString(list)));
}

/**
* 重置全局路由规则
*
* @param entireRule 路由规则
*/
public void resetGlobalRule(EntireRule entireRule) {
if (CollectionUtils.isEmpty(entireRule.getRules())) {
globalRules.remove(entireRule.getKind());
} else {
globalRules.put(entireRule.getKind(), entireRule.getRules());
}
LOGGER.info(String.format(Locale.ROOT, "Global rules have been updated: %s",
JSONObject.toJSONString(entireRule)));
}

/**
* 路由规则是否无效
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -258,59 +258,71 @@ public static void removeInvalidRules(List<EntireRule> list, boolean isReplaceDa
entireIterator.remove();
continue;
}
removeInvalidRules(entireRule.getKind(), entireRule.getRules(), isReplaceDash, isAppendPrefix);

Iterator<Rule> ruleIterator = entireRule.getRules().iterator();
while (ruleIterator.hasNext()) {
Rule rule = ruleIterator.next();
List<Route> routes = rule.getRoute();
// 去掉全是无效规则的配置
if (CollectionUtils.isEmpty(entireRule.getRules())) {
entireIterator.remove();
}
}
}

// 去掉没有配置路由的规则
if (CollectionUtils.isEmpty(routes)) {
LOGGER.warning("Routes are empty, rule will be removed.");
ruleIterator.remove();
continue;
}
/**
* 去掉无效的规则
*
* @param kind 规则类型
* @param rules 规则
* @param isReplaceDash 是否需要把"-"替换成"."
* @param isAppendPrefix 元数据的key值是否需要加上前缀
*/
public static void removeInvalidRules(String kind, List<Rule> rules, boolean isReplaceDash,
boolean isAppendPrefix) {
Iterator<Rule> ruleIterator = rules.iterator();
while (ruleIterator.hasNext()) {
Rule rule = ruleIterator.next();
List<Route> routes = rule.getRoute();

// 去掉无效的路由和修复同标签规则的路由
removeInvalidRoute(routes, kind, isReplaceDash, isAppendPrefix);
// 去掉没有配置路由的规则
if (CollectionUtils.isEmpty(routes)) {
LOGGER.warning("Routes are empty, rule will be removed.");
ruleIterator.remove();
continue;
}

List<Route> fallback = rule.getFallback();
if (!CollectionUtils.isEmpty(fallback)) {
// 去掉无效的路由和修复同标签规则的路由
removeInvalidRoute(fallback, kind, isReplaceDash, isAppendPrefix);
}
// 去掉无效的路由和修复同标签规则的路由
removeInvalidRoute(routes, kind, isReplaceDash, isAppendPrefix);

// 去掉全是无效路由的规则
if (CollectionUtils.isEmpty(routes)) {
LOGGER.warning("Routes are invalid, rule will be removed.");
ruleIterator.remove();
continue;
}
List<Route> fallback = rule.getFallback();
if (!CollectionUtils.isEmpty(fallback)) {
// 去掉无效的路由和修复同标签规则的路由
removeInvalidRoute(fallback, kind, isReplaceDash, isAppendPrefix);
}

if (RouterConstant.FLOW_MATCH_KIND.equals(kind)) {
// 去掉无效的规则
removeInvalidMatch(rule.getMatch());
// 去掉全是无效路由的规则
if (CollectionUtils.isEmpty(routes)) {
LOGGER.warning("Routes are invalid, rule will be removed.");
ruleIterator.remove();
continue;
}

// 无attachments规则,将headers规则更新到attachments规则
setAttachmentsByHeaders(rule.getMatch());
continue;
}
if (RouterConstant.FLOW_MATCH_KIND.equals(kind)) {
// 去掉无效的规则
removeInvalidMatch(rule.getMatch());

if (RouterConstant.TAG_MATCH_KIND.equals(kind)) {
// 去掉无效的规则
removeInvalidTagMatch(rule.getMatch(), isAppendPrefix);
continue;
}
// 无attachments规则,将headers规则更新到attachments规则
setAttachmentsByHeaders(rule.getMatch());
continue;
}

if (RouterConstant.LANE_MATCH_KIND.equals(kind)) {
// 去掉无效的规则
removeInvalidLaneMatch(rule.getMatch());
}
if (RouterConstant.TAG_MATCH_KIND.equals(kind)) {
// 去掉无效的规则
removeInvalidTagMatch(rule.getMatch(), isAppendPrefix);
continue;
}

// 去掉全是无效规则的配置
if (CollectionUtils.isEmpty(entireRule.getRules())) {
entireIterator.remove();
if (RouterConstant.LANE_MATCH_KIND.equals(kind)) {
// 去掉无效的规则
removeInvalidLaneMatch(rule.getMatch());
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@

package com.huaweicloud.sermant.router.config.handler;

import com.huaweicloud.sermant.core.service.dynamicconfig.common.DynamicConfigEvent;
import com.huaweicloud.sermant.router.common.constants.RouterConstant;
import com.huaweicloud.sermant.router.config.common.SafeConstructor;

import org.yaml.snakeyaml.Yaml;
Expand All @@ -27,7 +27,7 @@
* @author provenceee
* @since 2022-08-09
*/
public abstract class AbstractConfigHandler {
public abstract class AbstractConfigHandler implements AbstractHandler {
/**
* yaml
*/
Expand All @@ -40,19 +40,15 @@ public AbstractConfigHandler() {
this.yaml = new Yaml(new SafeConstructor(null));
}

/**
* 路由配置处理
*
* @param event 配置监听事件
* @param cacheName 缓存名
*/
public abstract void handle(DynamicConfigEvent event, String cacheName);

/**
* 是否需要处理
*
* @param key 配置key
* @param content 配置内容
* @return 是否需要处理
*/
public abstract boolean shouldHandle(String key);
@Override
public boolean shouldHandle(String key, String content) {
return RouterConstant.MATCH_KIND_LIST.stream().anyMatch(content::contains);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
/*
*
* * Copyright (C) 2024-2024 Huawei Technologies Co., Ltd. All rights reserved.
* *
* * Licensed under the Apache License, Version 2.0 (the "License");
* * you may not use this file except in compliance with the License.
* * You may obtain a copy of the License at
* *
* * http://www.apache.org/licenses/LICENSE-2.0
* *
* * Unless required by applicable law or agreed to in writing, software
* * distributed under the License is distributed on an "AS IS" BASIS,
* * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* * See the License for the specific language governing permissions and
* * limitations under the License.
*
*/

package com.huaweicloud.sermant.router.config.handler;

import com.huaweicloud.sermant.core.service.dynamicconfig.common.DynamicConfigEvent;

/**
* 处理器接口
*
* @author provenceee
* @since 2024-01-16
*/
public interface AbstractHandler {
/**
* 配置处理
*
* @param event 配置监听事件
* @param cacheName 缓存名
*/
void handle(DynamicConfigEvent event, String cacheName);

/**
* 是否需要处理
*
* @param key 配置key
* @param content 配置内容
* @return 是否需要处理
*/
boolean shouldHandle(String key, String content);
}
Original file line number Diff line number Diff line change
Expand Up @@ -69,8 +69,8 @@ public void handle(DynamicConfigEvent event, String cacheName) {
}

@Override
public boolean shouldHandle(String key) {
return RouterConstant.GLOBAL_ROUTER_KEY.equals(key);
public boolean shouldHandle(String key, String content) {
return super.shouldHandle(key, content) && RouterConstant.GLOBAL_ROUTER_KEY.equals(key);
}

private List<Map<String, Object>> getRule(DynamicConfigEvent event) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -77,8 +77,8 @@ public void handle(DynamicConfigEvent event, String cacheName) {
}

@Override
public boolean shouldHandle(String key) {
return RouterConstant.ROUTER_KEY_PREFIX.equals(key);
public boolean shouldHandle(String key, String content) {
return super.shouldHandle(key, content) && RouterConstant.ROUTER_KEY_PREFIX.equals(key);
}

private Map<String, List<Object>> getRouteRuleMap(DynamicConfigEvent event) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -73,8 +73,8 @@ public void handle(DynamicConfigEvent event, String cacheName) {
}

@Override
public boolean shouldHandle(String key) {
return key.startsWith(RouterConstant.ROUTER_KEY_PREFIX + POINT);
public boolean shouldHandle(String key, String content) {
return super.shouldHandle(key, content) && key.startsWith(RouterConstant.ROUTER_KEY_PREFIX + POINT);
}

private List<Map<String, Object>> getRule(DynamicConfigEvent event, String serviceName) {
Expand Down
Loading

0 comments on commit 9841121

Please sign in to comment.