forked from sermant-io/Sermant
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request sermant-io#1317 from provenceee/global-router
增加全局路由规则
- Loading branch information
Showing
13 changed files
with
232 additions
and
13 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
75 changes: 75 additions & 0 deletions
75
...vice/src/main/java/com/huaweicloud/sermant/router/config/handler/GlobalConfigHandler.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,75 @@ | ||
/* | ||
* Copyright (C) 2023-2023 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; | ||
import com.huaweicloud.sermant.core.service.dynamicconfig.common.DynamicConfigEventType; | ||
import com.huaweicloud.sermant.core.utils.StringUtils; | ||
import com.huaweicloud.sermant.router.common.constants.RouterConstant; | ||
import com.huaweicloud.sermant.router.common.utils.CollectionUtils; | ||
import com.huaweicloud.sermant.router.config.cache.ConfigCache; | ||
import com.huaweicloud.sermant.router.config.entity.RouterConfiguration; | ||
import com.huaweicloud.sermant.router.config.entity.Rule; | ||
import com.huaweicloud.sermant.router.config.utils.RuleUtils; | ||
|
||
import com.alibaba.fastjson.JSONArray; | ||
import com.alibaba.fastjson.JSONObject; | ||
|
||
import java.util.Collections; | ||
import java.util.List; | ||
import java.util.Map; | ||
|
||
/** | ||
* 全局标签路由 | ||
* | ||
* @author provenceee | ||
* @since 2023-09-20 | ||
*/ | ||
public class GlobalConfigHandler extends AbstractConfigHandler { | ||
@Override | ||
public void handle(DynamicConfigEvent event, String cacheName) { | ||
RouterConfiguration configuration = ConfigCache.getLabel(cacheName); | ||
if (event.getEventType() == DynamicConfigEventType.DELETE) { | ||
configuration.resetGlobalRules(Collections.emptyList()); | ||
RuleUtils.initMatchKeys(configuration); | ||
return; | ||
} | ||
List<Rule> rules = JSONArray.parseArray(JSONObject.toJSONString(getRuleMap(event)), Rule.class); | ||
RuleUtils.removeInvalidRules(rules); | ||
if (CollectionUtils.isEmpty(rules)) { | ||
configuration.resetGlobalRules(Collections.emptyList()); | ||
} else { | ||
rules.sort((o1, o2) -> o2.getPrecedence() - o1.getPrecedence()); | ||
configuration.resetGlobalRules(rules); | ||
} | ||
RuleUtils.initMatchKeys(configuration); | ||
} | ||
|
||
@Override | ||
public boolean shouldHandle(String key) { | ||
return RouterConstant.GLOBAL_ROUTER_KEY.equals(key); | ||
} | ||
|
||
private List<Map<String, Object>> getRuleMap(DynamicConfigEvent event) { | ||
String content = event.getContent(); | ||
if (StringUtils.isBlank(content)) { | ||
return Collections.emptyList(); | ||
} | ||
Map<String, List<Map<String, Object>>> ruleMap = yaml.load(content); | ||
return ruleMap.get(RouterConstant.GLOBAL_ROUTER_KEY); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.