use of com.huawei.route.common.gray.label.entity.MatchRule in project Sermant by huaweicloud.
the class RouterUtil method getRoutes.
private static List<Route> getRoutes(Object[] arguments, Rule rule) {
Match match = rule.getMatch();
boolean isFullMatch = match.isFullMatch();
Map<String, List<MatchRule>> args = match.getArgs();
for (Entry<String, List<MatchRule>> entry : args.entrySet()) {
String key = entry.getKey();
if (!key.startsWith(GrayConstant.DUBBO_SOURCE_TYPE_PREFIX)) {
continue;
}
List<MatchRule> matchRuleList = entry.getValue();
for (MatchRule matchRule : matchRuleList) {
ValueMatch valueMatch = matchRule.getValueMatch();
List<String> values = valueMatch.getValues();
MatchStrategy matchStrategy = valueMatch.getMatchStrategy();
String arg = TypeStrategyChooser.INSTANCE.getValue(matchRule.getType(), key, arguments).orElse(null);
if (!isFullMatch && matchStrategy.isMatch(values, arg, matchRule.isCaseInsensitive())) {
// 如果不是全匹配,且匹配了一个,那么直接return
return rule.getRoute();
}
if (isFullMatch && !matchStrategy.isMatch(values, arg, matchRule.isCaseInsensitive())) {
// 如果是全匹配,且有一个不匹配,则继续下一个规则
return Collections.emptyList();
}
}
}
if (isFullMatch) {
// 如果是全匹配,走到这里,说明没有不匹配的,直接return
return rule.getRoute();
}
// 如果不是全匹配,走到这里,说明没有一个规则能够匹配上,则继续下一个规则
return Collections.emptyList();
}
use of com.huawei.route.common.gray.label.entity.MatchRule in project Sermant by huaweicloud.
the class RouterUtil method getValidRules.
/**
* 获取有效规则
*
* @param grayConfiguration 灰度配置
* @param targetService 目标服务
* @param path 请求路径
* @return 有效规则
*/
public static List<Rule> getValidRules(GrayConfiguration grayConfiguration, String targetService, String path) {
if (GrayConfiguration.isInValid(grayConfiguration)) {
return Collections.emptyList();
}
Map<String, List<Rule>> routeRule = grayConfiguration.getRouteRule();
if (CollectionUtils.isEmpty(routeRule) || CollectionUtils.isEmpty(routeRule.get(targetService))) {
return Collections.emptyList();
}
List<Rule> list = new ArrayList<Rule>();
for (Rule rule : routeRule.get(targetService)) {
if (!isValidRule(rule, CurrentInstance.getInstance().getAppName(), path)) {
continue;
}
// 去掉无效的规则
Map<String, List<MatchRule>> headerRules = rule.getMatch().getHeaders();
if (headerRules != null) {
removeInValidRules(headerRules);
rule.getMatch().setHeaders(headerRules);
}
Map<String, List<MatchRule>> paramRules = rule.getMatch().getParameters();
if (paramRules != null) {
removeInValidRules(paramRules);
rule.getMatch().setParameters(paramRules);
}
Map<String, List<MatchRule>> cookieRules = rule.getMatch().getCookie();
if (cookieRules != null) {
removeInValidRules(cookieRules);
rule.getMatch().setCookie(cookieRules);
}
// 去掉无效的路由
Iterator<Route> routeIterator = rule.getRoute().iterator();
while (routeIterator.hasNext()) {
if (isInValidRoute(routeIterator.next())) {
routeIterator.remove();
}
}
list.add(rule);
}
Collections.sort(list, new Comparator<Rule>() {
@Override
public int compare(Rule o1, Rule o2) {
return o1.getPrecedence() - o2.getPrecedence();
}
});
return list;
}
use of com.huawei.route.common.gray.label.entity.MatchRule in project Sermant by huaweicloud.
the class RouterUtil method getRoutes.
/**
* 获取路由
*
* @param list 规则列表
* @param request 请求
* @return 路由
*/
public static List<Route> getRoutes(List<Rule> list, Request request) {
// 支持headers,parameters和cookie三种参数匹配方式且允许同时配置,全匹配应满足所有三种匹配方式规则
for (Rule rule : list) {
Match match = rule.getMatch();
boolean isFullMatch = match.isFullMatch();
// headers 参数匹配
Map<String, List<MatchRule>> headerRules = match.getHeaders();
boolean isMatchByHeader = true;
if (headerRules != null) {
isMatchByHeader = isMatchHeaderRule(headerRules, isFullMatch, request);
}
// parameters 参数匹配
Map<String, List<MatchRule>> paramRules = match.getParameters();
boolean isMatchByParam = true;
if (paramRules != null) {
isMatchByParam = isMatchParamRule(paramRules, isFullMatch, request);
}
// cookie 参数匹配
Map<String, List<MatchRule>> cookieRules = match.getCookie();
boolean isMatchByCookie = true;
if (cookieRules != null) {
isMatchByCookie = isMatchCookieRule(cookieRules, isFullMatch, request);
}
if (headerRules == null && paramRules == null && cookieRules == null) {
continue;
}
// 全匹配需要三种配置都匹配上
if (isFullMatch && isMatchByHeader && isMatchByParam && isMatchByCookie) {
return rule.getRoute();
}
// 非全匹配只要其中一种规则匹配上即可
if (!isFullMatch && (isMatchByHeader || isMatchByParam || isMatchByCookie)) {
return rule.getRoute();
}
}
return Collections.emptyList();
}
use of com.huawei.route.common.gray.label.entity.MatchRule in project Sermant by huaweicloud.
the class RouterUtil method matchResultForParamAndCookie.
private static boolean matchResultForParamAndCookie(Map<String, List<MatchRule>> rules, boolean isFullMatch, Map<String, String> paramMap) {
for (Entry<String, List<MatchRule>> entry : rules.entrySet()) {
String key = entry.getKey();
if (!paramMap.containsKey(key)) {
if (isFullMatch) {
return false;
} else {
continue;
}
}
List<MatchRule> matchRuleList = entry.getValue();
for (MatchRule matchRule : matchRuleList) {
ValueMatch valueMatch = matchRule.getValueMatch();
List<String> values = valueMatch.getValues();
MatchStrategy matchStrategy = valueMatch.getMatchStrategy();
String arg = paramMap.get(key);
if (!isFullMatch && matchStrategy.isMatch(values, arg, matchRule.isCaseInsensitive())) {
// 如果不是全匹配,且匹配了一个,那么直接return
return true;
}
if (isFullMatch && !matchStrategy.isMatch(values, arg, matchRule.isCaseInsensitive())) {
// 如果是全匹配,且有一个不匹配,则继续下一个规则
return false;
}
}
}
// 执行到此处,如果是全匹配,则匹配成功,如果不是全匹配,则没有匹配到
return isFullMatch;
}
use of com.huawei.route.common.gray.label.entity.MatchRule in project Sermant by huaweicloud.
the class RouterUtil method isMatchHeaderRule.
private static boolean isMatchHeaderRule(Map<String, List<MatchRule>> headerRules, boolean isFullMatch, Request request) {
Map<String, Collection<String>> headers = request.headers();
for (Entry<String, List<MatchRule>> entry : headerRules.entrySet()) {
String key = entry.getKey();
if (!headers.containsKey(key) || headers.get(key).size() == 0) {
if (isFullMatch) {
return false;
} else {
continue;
}
}
List<MatchRule> headerMatchRuleList = entry.getValue();
for (MatchRule matchRule : headerMatchRuleList) {
ValueMatch valueMatch = matchRule.getValueMatch();
List<String> values = valueMatch.getValues();
MatchStrategy headerMatchStrategy = valueMatch.getMatchStrategy();
String arg = new ArrayList<String>(headers.get(key)).get(0);
if (!isFullMatch && headerMatchStrategy.isMatch(values, arg, matchRule.isCaseInsensitive())) {
// 如果不是全匹配,且匹配了一个,那么直接return
return true;
}
if (isFullMatch && !headerMatchStrategy.isMatch(values, arg, matchRule.isCaseInsensitive())) {
// 如果是全匹配,且有一个不匹配,则继续下一个规则
return false;
}
}
}
// 执行到此处,如果是全匹配,则匹配成功,如果不是全匹配,则没有匹配到
return isFullMatch;
}
Aggregations