Skip to content

Commit

Permalink
增加标签路由匹配fallback规则逻辑
Browse files Browse the repository at this point in the history
Signed-off-by: chengyouling <[email protected]>
  • Loading branch information
chengyouling committed Dec 18, 2023
1 parent 285c2a2 commit 6d8244e
Showing 1 changed file with 20 additions and 22 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -77,33 +77,31 @@ public AbstractRuleStrategy(String source, InstanceStrategy<I, Map<String, Strin
public List<I> getMatchInstances(String serviceName, List<I> instances, Rule rule, boolean isReplaceDash) {
// match set routes
RouteResult<?> result = RuleUtils.getTargetTags(rule.getRoute(), isReplaceDash);
if (!CollectionUtils.isEmpty(rule.getFallback())) {
if (result.isMatch()) {
// route命中标签,fallback有设置路由规则时,仅返回匹配的实例,如果存在直接返回
List<I> routeInstances = getInstances(getStrategy(result.isMatch()), result.getTags(), serviceName,
instances, false);
if (!CollectionUtils.isEmpty(routeInstances)) {
return routeInstances;
}
}

// route设置的目标标签未匹配实例时,fallback有设置路由,通过fallback路由目标标签实例
RouteResult<?> fallback = RuleUtils.getTargetTags(rule.getFallback(), isReplaceDash);
List<I> fallbackInstances = getInstances(getStrategy(fallback.isMatch()), fallback.getTags(), serviceName,
if (CollectionUtils.isEmpty(rule.getFallback())) {
return getInstances(getStrategy(result.isMatch()), result.getTags(), serviceName, instances, true);
}
if (result.isMatch()) {
// route命中标签,fallback有设置路由规则时,仅返回匹配的实例,如果存在直接返回
List<I> routeInstances = getInstances(getStrategy(result.isMatch()), result.getTags(), serviceName,
instances, false);

// fallback中设置了路由规则命中routeTag,且有对应的实例匹配则按fallback路由规则选中实例,返回对应实例
if (!CollectionUtils.isEmpty(fallbackInstances)) {
return fallbackInstances;
if (!CollectionUtils.isEmpty(routeInstances)) {
return routeInstances;
}
}

if (result.isMatch()) {
// 结合81行,如果result.isMatch()为true,说明未匹配到标签实例,直接返回所有实例。
return instances;
}
// route设置的目标标签未匹配实例时,fallback有设置路由,通过fallback路由目标标签实例
RouteResult<?> fallback = RuleUtils.getTargetTags(rule.getFallback(), isReplaceDash);
List<I> fallbackInstances = getInstances(getStrategy(fallback.isMatch()), fallback.getTags(), serviceName,
instances, false);

// fallback中设置了路由规则命中routeTag,且有对应的实例匹配则按fallback路由规则选中实例,返回对应实例
if (!CollectionUtils.isEmpty(fallbackInstances)) {
return fallbackInstances;
}

return getInstances(getStrategy(result.isMatch()), result.getTags(), serviceName, instances, true);
// 结合上面result.isMatch()为true逻辑,如果为true未能匹配实例,直接返回所有实例,反之通过missmatch处理
return result.isMatch() ? instances
: getInstances(getStrategy(result.isMatch()), result.getTags(), serviceName, instances, true);
}

@Override
Expand Down

0 comments on commit 6d8244e

Please sign in to comment.