From 85db59ec384c30fc3637b6fb93cdd77cd164a819 Mon Sep 17 00:00:00 2001 From: Steve Rao Date: Thu, 12 Jan 2023 00:08:49 +0800 Subject: [PATCH] refactor: rename some core classes about routing (#3082) refactor: rename some core classes about routing --- .../cloud/examples/ConsumerApplication.java | 10 +--- .../routing/rule/HeaderRoutingRule.java | 7 +-- .../commons/governance/routing/rule/Rule.java | 51 +++++++++++++++---- .../routing/rule/UrlRoutingRule.java | 26 +++------- .../pom.xml | 5 ++ .../filter/AbstractXdsResolveFilter.java | 2 - .../filter/impl/RoutingXdsResolveFilter.java | 7 ++- .../cloud/governance/istio/util/ConvUtil.java | 18 +++---- .../OpenSergoTrafficRouterParser.java | 27 ++++------ .../pom.xml | 2 + .../feign/RoutingFeignInterceptor.java | 7 ++- .../ribbon/RoutingLoadBalanceRule.java | 32 +++++------- .../routing/util/ConditionMatchUtil.java | 19 ++++--- .../cloud/routing/util/LoadBalanceUtil.java | 44 +++++++++------- .../cloud/routing/util/RequestContext.java | 49 ------------------ .../routing/web/WebMvcAutoConfiguration.java | 42 --------------- .../cloud/routing/web/WebMvcConfig.java | 37 -------------- .../cloud/routing/web/WebMvcInterceptor.java | 46 ----------------- .../main/resources/META-INF/spring.factories | 1 - 19 files changed, 132 insertions(+), 300 deletions(-) delete mode 100644 spring-cloud-alibaba-starters/spring-cloud-starter-alibaba-governance-routing/src/main/java/com/alibaba/cloud/routing/util/RequestContext.java delete mode 100644 spring-cloud-alibaba-starters/spring-cloud-starter-alibaba-governance-routing/src/main/java/com/alibaba/cloud/routing/web/WebMvcAutoConfiguration.java delete mode 100644 spring-cloud-alibaba-starters/spring-cloud-starter-alibaba-governance-routing/src/main/java/com/alibaba/cloud/routing/web/WebMvcConfig.java delete mode 100644 spring-cloud-alibaba-starters/spring-cloud-starter-alibaba-governance-routing/src/main/java/com/alibaba/cloud/routing/web/WebMvcInterceptor.java diff --git a/spring-cloud-alibaba-examples/governance-example/label-routing-example/consumer-example/src/main/java/com/alibaba/cloud/examples/ConsumerApplication.java b/spring-cloud-alibaba-examples/governance-example/label-routing-example/consumer-example/src/main/java/com/alibaba/cloud/examples/ConsumerApplication.java index 3320cb7ca..e9232dbeb 100644 --- a/spring-cloud-alibaba-examples/governance-example/label-routing-example/consumer-example/src/main/java/com/alibaba/cloud/examples/ConsumerApplication.java +++ b/spring-cloud-alibaba-examples/governance-example/label-routing-example/consumer-example/src/main/java/com/alibaba/cloud/examples/ConsumerApplication.java @@ -95,17 +95,14 @@ public class ConsumerApplication { labelRouteData.setDefaultRouteVersion("v1"); Rule routeRule = new HeaderRoutingRule(); - routeRule.setType("header"); routeRule.setCondition("="); routeRule.setKey("tag"); routeRule.setValue("gray"); - Rule routeRule1 = new UrlRoutingRule.Parameter(); - routeRule1.setType("parameter"); + Rule routeRule1 = new UrlRoutingRule.ParameterRoutingRule(); routeRule1.setCondition(">"); routeRule1.setKey("id"); routeRule1.setValue("10"); Rule routeRule2 = new UrlRoutingRule.PathRoutingRule(); - routeRule2.setType("path"); routeRule2.setCondition("="); routeRule2.setValue("/router-test"); routeRules.add(routeRule); @@ -140,17 +137,14 @@ public class ConsumerApplication { labelRouteData.setDefaultRouteVersion("v1"); Rule routeRule = new HeaderRoutingRule(); - routeRule.setType("header"); routeRule.setCondition("="); routeRule.setKey("tag"); routeRule.setValue("gray"); - Rule routeRule1 = new UrlRoutingRule.Parameter(); - routeRule1.setType("parameter"); + Rule routeRule1 = new UrlRoutingRule.ParameterRoutingRule(); routeRule1.setCondition(">"); routeRule1.setKey("id"); routeRule1.setValue("10"); Rule routeRule2 = new UrlRoutingRule.PathRoutingRule(); - routeRule2.setType("path"); routeRule2.setCondition("="); routeRule2.setValue("/router-test"); routeRules.add(routeRule); diff --git a/spring-cloud-alibaba-starters/spring-cloud-alibaba-commons/src/main/java/com/alibaba/cloud/commons/governance/routing/rule/HeaderRoutingRule.java b/spring-cloud-alibaba-starters/spring-cloud-alibaba-commons/src/main/java/com/alibaba/cloud/commons/governance/routing/rule/HeaderRoutingRule.java index cafeb59b9..eefafdfb7 100644 --- a/spring-cloud-alibaba-starters/spring-cloud-alibaba-commons/src/main/java/com/alibaba/cloud/commons/governance/routing/rule/HeaderRoutingRule.java +++ b/spring-cloud-alibaba-starters/spring-cloud-alibaba-commons/src/main/java/com/alibaba/cloud/commons/governance/routing/rule/HeaderRoutingRule.java @@ -24,7 +24,7 @@ import java.util.Objects; */ public class HeaderRoutingRule implements Rule { - private String type; + private final String type = "header"; private String condition; @@ -67,11 +67,6 @@ public class HeaderRoutingRule implements Rule { return this.type; } - @Override - public void setType(String type) { - this.type = type; - } - @Override public boolean equals(Object o) { if (this == o) { diff --git a/spring-cloud-alibaba-starters/spring-cloud-alibaba-commons/src/main/java/com/alibaba/cloud/commons/governance/routing/rule/Rule.java b/spring-cloud-alibaba-starters/spring-cloud-alibaba-commons/src/main/java/com/alibaba/cloud/commons/governance/routing/rule/Rule.java index 6c35dc243..80d64ca49 100644 --- a/spring-cloud-alibaba-starters/spring-cloud-alibaba-commons/src/main/java/com/alibaba/cloud/commons/governance/routing/rule/Rule.java +++ b/spring-cloud-alibaba-starters/spring-cloud-alibaba-commons/src/main/java/com/alibaba/cloud/commons/governance/routing/rule/Rule.java @@ -26,20 +26,53 @@ public interface Rule { * get type of rule. * @return String */ - String getType(); + default String getType() { + return null; + } - void setType(String type); - - String getCondition(); + /** + * get condition. + * @return String + */ + default String getCondition() { + return null; + } - void setCondition(String condition); + /** + * set condition. + * @param condition {@link String} + */ + default void setCondition(String condition) { + } - String getKey(); + /** + * get key of rule. + * @return String + */ + default String getKey() { + return null; + } - void setKey(String key); + /** + * set key of rule. + * @param key {@link String} + */ + default void setKey(String key) { + } - String getValue(); + /** + * get value of rule. + * @return String + */ + default String getValue() { + return null; + } - void setValue(String value); + /** + * set value of rule. + * @param value {@link String} + */ + default void setValue(String value) { + } } diff --git a/spring-cloud-alibaba-starters/spring-cloud-alibaba-commons/src/main/java/com/alibaba/cloud/commons/governance/routing/rule/UrlRoutingRule.java b/spring-cloud-alibaba-starters/spring-cloud-alibaba-commons/src/main/java/com/alibaba/cloud/commons/governance/routing/rule/UrlRoutingRule.java index fc3fb76b0..595cf878c 100644 --- a/spring-cloud-alibaba-starters/spring-cloud-alibaba-commons/src/main/java/com/alibaba/cloud/commons/governance/routing/rule/UrlRoutingRule.java +++ b/spring-cloud-alibaba-starters/spring-cloud-alibaba-commons/src/main/java/com/alibaba/cloud/commons/governance/routing/rule/UrlRoutingRule.java @@ -26,7 +26,7 @@ public class UrlRoutingRule { public static class PathRoutingRule implements Rule { - private String type; + private final String type = "path"; private String condition; @@ -67,11 +67,6 @@ public class UrlRoutingRule { return this.type; } - @Override - public void setType(String type) { - this.type = type; - } - @Override public boolean equals(Object o) { if (this == o) { @@ -99,9 +94,9 @@ public class UrlRoutingRule { } - public static class Parameter implements Rule { + public static class ParameterRoutingRule implements Rule { - private String type; + private final String type = "parameter"; private String condition; @@ -144,11 +139,6 @@ public class UrlRoutingRule { return this.type; } - @Override - public void setType(String type) { - this.type = type; - } - @Override public boolean equals(Object o) { if (this == o) { @@ -157,11 +147,11 @@ public class UrlRoutingRule { if (o == null || getClass() != o.getClass()) { return false; } - Parameter parameter = (Parameter) o; - return Objects.equals(getType(), parameter.getType()) - && Objects.equals(getCondition(), parameter.getCondition()) - && Objects.equals(getKey(), parameter.getKey()) - && Objects.equals(getValue(), parameter.getValue()); + ParameterRoutingRule parameterRoutingRule = (ParameterRoutingRule) o; + return Objects.equals(getType(), parameterRoutingRule.getType()) + && Objects.equals(getCondition(), parameterRoutingRule.getCondition()) + && Objects.equals(getKey(), parameterRoutingRule.getKey()) + && Objects.equals(getValue(), parameterRoutingRule.getValue()); } @Override diff --git a/spring-cloud-alibaba-starters/spring-cloud-starter-alibaba-controlplane-istio/pom.xml b/spring-cloud-alibaba-starters/spring-cloud-starter-alibaba-controlplane-istio/pom.xml index e49dc565b..649e4a34a 100644 --- a/spring-cloud-alibaba-starters/spring-cloud-starter-alibaba-controlplane-istio/pom.xml +++ b/spring-cloud-alibaba-starters/spring-cloud-starter-alibaba-controlplane-istio/pom.xml @@ -85,6 +85,11 @@ spring-cloud-starter-alibaba-governance-routing test + + org.springframework.cloud + spring-cloud-starter-openfeign + test + diff --git a/spring-cloud-alibaba-starters/spring-cloud-starter-alibaba-controlplane-istio/src/main/java/com/alibaba/cloud/governance/istio/filter/AbstractXdsResolveFilter.java b/spring-cloud-alibaba-starters/spring-cloud-starter-alibaba-controlplane-istio/src/main/java/com/alibaba/cloud/governance/istio/filter/AbstractXdsResolveFilter.java index f965f0605..e58cdefb8 100644 --- a/spring-cloud-alibaba-starters/spring-cloud-starter-alibaba-controlplane-istio/src/main/java/com/alibaba/cloud/governance/istio/filter/AbstractXdsResolveFilter.java +++ b/spring-cloud-alibaba-starters/spring-cloud-starter-alibaba-controlplane-istio/src/main/java/com/alibaba/cloud/governance/istio/filter/AbstractXdsResolveFilter.java @@ -37,8 +37,6 @@ public abstract class AbstractXdsResolveFilter protected static final String ALLOW_ANY = "allow_any"; - protected static final String PATH = "path"; - protected static final String VIRTUAL_INBOUND = "virtualInbound"; protected static final String CONNECTION_MANAGER = "envoy.filters.network.http_connection_manager"; diff --git a/spring-cloud-alibaba-starters/spring-cloud-starter-alibaba-controlplane-istio/src/main/java/com/alibaba/cloud/governance/istio/filter/impl/RoutingXdsResolveFilter.java b/spring-cloud-alibaba-starters/spring-cloud-starter-alibaba-controlplane-istio/src/main/java/com/alibaba/cloud/governance/istio/filter/impl/RoutingXdsResolveFilter.java index ede0521ef..bc76c0a51 100644 --- a/spring-cloud-alibaba-starters/spring-cloud-starter-alibaba-controlplane-istio/src/main/java/com/alibaba/cloud/governance/istio/filter/impl/RoutingXdsResolveFilter.java +++ b/spring-cloud-alibaba-starters/spring-cloud-starter-alibaba-controlplane-istio/src/main/java/com/alibaba/cloud/governance/istio/filter/impl/RoutingXdsResolveFilter.java @@ -135,15 +135,14 @@ public class RoutingXdsResolveFilter for (QueryParameterMatcher parameterMatcher : routeMatch .getQueryParametersList()) { - UrlRoutingRule.Parameter parameter = ConvUtil + UrlRoutingRule.ParameterRoutingRule parameterRoutingRule = ConvUtil .parameterMatcher2ParameterRule(parameterMatcher); - if (parameter != null) { - routeRules.add(parameter); + if (parameterRoutingRule != null) { + routeRules.add(parameterRoutingRule); } } UrlRoutingRule.PathRoutingRule path = new UrlRoutingRule.PathRoutingRule(); - path.setType(PATH); switch (routeMatch.getPathSpecifierCase()) { case PREFIX: path.setCondition(StringMatcherType.PREFIX.toString()); diff --git a/spring-cloud-alibaba-starters/spring-cloud-starter-alibaba-controlplane-istio/src/main/java/com/alibaba/cloud/governance/istio/util/ConvUtil.java b/spring-cloud-alibaba-starters/spring-cloud-starter-alibaba-controlplane-istio/src/main/java/com/alibaba/cloud/governance/istio/util/ConvUtil.java index 23147fd27..9e081013f 100644 --- a/spring-cloud-alibaba-starters/spring-cloud-starter-alibaba-controlplane-istio/src/main/java/com/alibaba/cloud/governance/istio/util/ConvUtil.java +++ b/spring-cloud-alibaba-starters/spring-cloud-starter-alibaba-controlplane-istio/src/main/java/com/alibaba/cloud/governance/istio/util/ConvUtil.java @@ -34,10 +34,6 @@ import io.envoyproxy.envoy.type.matcher.v3.RegexMatcher; */ public final class ConvUtil { - private static final String HEADER = "header"; - - private static final String PARAMETER = "parameter"; - private ConvUtil() { } @@ -125,17 +121,16 @@ public final class ConvUtil { return headerMatcher.getStringMatch(); } - public static UrlRoutingRule.Parameter parameterMatcher2ParameterRule( + public static UrlRoutingRule.ParameterRoutingRule parameterMatcher2ParameterRule( QueryParameterMatcher queryParameterMatcher) { - UrlRoutingRule.Parameter parameter = new UrlRoutingRule.Parameter(); + UrlRoutingRule.ParameterRoutingRule parameterRoutingRule = new UrlRoutingRule.ParameterRoutingRule(); StringMatcher stringMatcher = ConvUtil .convStringMatcher(queryParameterMatcher.getStringMatch()); if (stringMatcher != null) { - parameter.setCondition(stringMatcher.getType().toString()); - parameter.setKey(queryParameterMatcher.getName()); - parameter.setValue(stringMatcher.getMatcher()); - parameter.setType(PARAMETER); - return parameter; + parameterRoutingRule.setCondition(stringMatcher.getType().toString()); + parameterRoutingRule.setKey(queryParameterMatcher.getName()); + parameterRoutingRule.setValue(stringMatcher.getMatcher()); + return parameterRoutingRule; } return null; } @@ -149,7 +144,6 @@ public final class ConvUtil { headerRule.setCondition(stringMatcher.getType().toString()); headerRule.setKey(headerMatcher.getName()); headerRule.setValue(stringMatcher.getMatcher()); - headerRule.setType(HEADER); return headerRule; } return null; diff --git a/spring-cloud-alibaba-starters/spring-cloud-starter-alibaba-controlplane-opensergo/src/main/java/com/alibaba/cloud/governance/opensergo/OpenSergoTrafficRouterParser.java b/spring-cloud-alibaba-starters/spring-cloud-starter-alibaba-controlplane-opensergo/src/main/java/com/alibaba/cloud/governance/opensergo/OpenSergoTrafficRouterParser.java index 9cf4d42fa..b432bf019 100644 --- a/spring-cloud-alibaba-starters/spring-cloud-starter-alibaba-controlplane-opensergo/src/main/java/com/alibaba/cloud/governance/opensergo/OpenSergoTrafficRouterParser.java +++ b/spring-cloud-alibaba-starters/spring-cloud-starter-alibaba-controlplane-opensergo/src/main/java/com/alibaba/cloud/governance/opensergo/OpenSergoTrafficRouterParser.java @@ -55,12 +55,6 @@ public class OpenSergoTrafficRouterParser { protected static final Logger log = LoggerFactory .getLogger(OpenSergoTrafficRouterParser.class); - private static final String HEADER = "header"; - - private static final String PARAMETER = "parameter"; - - private static final String PATH = "path"; - public OpenSergoTrafficRouterParser() { } @@ -186,15 +180,14 @@ public class OpenSergoTrafficRouterParser { for (QueryParameterMatcher parameterMatcher : routeMatch .getQueryParametersList()) { - UrlRoutingRule.Parameter parameter = parameterMatcher2ParameterRule( + UrlRoutingRule.ParameterRoutingRule parameterRoutingRule = parameterMatcher2ParameterRule( parameterMatcher); - if (parameter != null) { - routeRules.add(parameter); + if (parameterRoutingRule != null) { + routeRules.add(parameterRoutingRule); } } UrlRoutingRule.PathRoutingRule path = new UrlRoutingRule.PathRoutingRule(); - path.setType(PATH); switch (routeMatch.getPathSpecifierCase()) { case PREFIX: path.setCondition(StringMatcherType.PREFIX.toString()); @@ -222,17 +215,16 @@ public class OpenSergoTrafficRouterParser { return routeRules; } - private UrlRoutingRule.Parameter parameterMatcher2ParameterRule( + private UrlRoutingRule.ParameterRoutingRule parameterMatcher2ParameterRule( QueryParameterMatcher queryParameterMatcher) { - UrlRoutingRule.Parameter parameter = new UrlRoutingRule.Parameter(); + UrlRoutingRule.ParameterRoutingRule parameterRoutingRule = new UrlRoutingRule.ParameterRoutingRule(); StringMatcher stringMatcher = ConvUtils .convStringMatcher(queryParameterMatcher.getStringMatch()); if (stringMatcher != null) { - parameter.setCondition(stringMatcher.getType().toString()); - parameter.setKey(queryParameterMatcher.getName()); - parameter.setValue(stringMatcher.getMatcher()); - parameter.setType(PARAMETER); - return parameter; + parameterRoutingRule.setCondition(stringMatcher.getType().toString()); + parameterRoutingRule.setKey(queryParameterMatcher.getName()); + parameterRoutingRule.setValue(stringMatcher.getMatcher()); + return parameterRoutingRule; } return null; } @@ -245,7 +237,6 @@ public class OpenSergoTrafficRouterParser { headerRule.setCondition(stringMatcher.getType().toString()); headerRule.setKey(headerMatcher.getName()); headerRule.setValue(stringMatcher.getMatcher()); - headerRule.setType(HEADER); return headerRule; } return null; diff --git a/spring-cloud-alibaba-starters/spring-cloud-starter-alibaba-governance-routing/pom.xml b/spring-cloud-alibaba-starters/spring-cloud-starter-alibaba-governance-routing/pom.xml index defddc721..6b73af280 100644 --- a/spring-cloud-alibaba-starters/spring-cloud-starter-alibaba-governance-routing/pom.xml +++ b/spring-cloud-alibaba-starters/spring-cloud-starter-alibaba-governance-routing/pom.xml @@ -20,11 +20,13 @@ org.springframework.cloud spring-cloud-starter-openfeign + true org.springframework spring-webmvc + true diff --git a/spring-cloud-alibaba-starters/spring-cloud-starter-alibaba-governance-routing/src/main/java/com/alibaba/cloud/routing/feign/RoutingFeignInterceptor.java b/spring-cloud-alibaba-starters/spring-cloud-starter-alibaba-governance-routing/src/main/java/com/alibaba/cloud/routing/feign/RoutingFeignInterceptor.java index ccd888919..54c51699f 100644 --- a/spring-cloud-alibaba-starters/spring-cloud-starter-alibaba-governance-routing/src/main/java/com/alibaba/cloud/routing/feign/RoutingFeignInterceptor.java +++ b/spring-cloud-alibaba-starters/spring-cloud-starter-alibaba-governance-routing/src/main/java/com/alibaba/cloud/routing/feign/RoutingFeignInterceptor.java @@ -20,12 +20,14 @@ import java.util.Enumeration; import javax.servlet.http.HttpServletRequest; -import com.alibaba.cloud.routing.util.RequestContext; import feign.RequestInterceptor; import feign.RequestTemplate; import org.slf4j.Logger; import org.slf4j.LoggerFactory; +import org.springframework.web.context.request.RequestContextHolder; +import org.springframework.web.context.request.ServletRequestAttributes; + /** * @author HH * @since 2.2.10-RC1 @@ -37,7 +39,8 @@ public class RoutingFeignInterceptor implements RequestInterceptor { @Override public void apply(RequestTemplate requestTemplate) { - final HttpServletRequest request = RequestContext.getRequest(); + final HttpServletRequest request = ((ServletRequestAttributes) RequestContextHolder + .getRequestAttributes()).getRequest(); final Enumeration headerNames = request.getHeaderNames(); if (headerNames == null) { return; diff --git a/spring-cloud-alibaba-starters/spring-cloud-starter-alibaba-governance-routing/src/main/java/com/alibaba/cloud/routing/ribbon/RoutingLoadBalanceRule.java b/spring-cloud-alibaba-starters/spring-cloud-starter-alibaba-governance-routing/src/main/java/com/alibaba/cloud/routing/ribbon/RoutingLoadBalanceRule.java index 0e8205681..a75bbeeaf 100644 --- a/spring-cloud-alibaba-starters/spring-cloud-starter-alibaba-governance-routing/src/main/java/com/alibaba/cloud/routing/ribbon/RoutingLoadBalanceRule.java +++ b/spring-cloud-alibaba-starters/spring-cloud-starter-alibaba-governance-routing/src/main/java/com/alibaba/cloud/routing/ribbon/RoutingLoadBalanceRule.java @@ -37,7 +37,6 @@ import com.alibaba.cloud.routing.publish.TargetServiceChangedPublisher; import com.alibaba.cloud.routing.repository.RoutingDataRepository; import com.alibaba.cloud.routing.util.ConditionMatchUtil; import com.alibaba.cloud.routing.util.LoadBalanceUtil; -import com.alibaba.cloud.routing.util.RequestContext; import com.alibaba.nacos.api.naming.NamingService; import com.alibaba.nacos.api.naming.pojo.Instance; import com.netflix.loadbalancer.AbstractServerPredicate; @@ -49,6 +48,8 @@ import org.slf4j.LoggerFactory; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.util.CollectionUtils; +import org.springframework.web.context.request.RequestContextHolder; +import org.springframework.web.context.request.ServletRequestAttributes; /** * @author HH @@ -94,16 +95,6 @@ public class RoutingLoadBalanceRule extends PredicateBasedRule { */ private AbstractServerPredicate predicate; - /** - * Match condition util. - */ - private final ConditionMatchUtil conditionMatchUtil = new ConditionMatchUtil(); - - /** - * Load Balance Util. - */ - private final LoadBalanceUtil loadBalanceUtil = new LoadBalanceUtil(); - @Autowired private NacosDiscoveryProperties nacosDiscoveryProperties; @@ -130,7 +121,7 @@ public class RoutingLoadBalanceRule extends PredicateBasedRule { final HashMap> routeData = routingDataRepository .getRouteRule(targetServiceName); if (routeData == null) { - return loadBalanceUtil.loadBalanceByOrdinaryRule(loadBalancer, key, + return LoadBalanceUtil.loadBalanceByOrdinaryRule(loadBalancer, key, routingProperties.getRule()); } @@ -204,7 +195,8 @@ public class RoutingLoadBalanceRule extends PredicateBasedRule { HashMap weightMap, HashSet fallbackVersionSet, HashMap fallbackWeightMap) { // Get request metadata. - final HttpServletRequest request = RequestContext.getRequest(); + final HttpServletRequest request = ((ServletRequestAttributes) RequestContextHolder + .getRequestAttributes()).getRequest(); final Enumeration headerNames = request.getHeaderNames(); HashMap requestHeaders = new HashMap<>(); if (headerNames != null) { @@ -354,19 +346,19 @@ public class RoutingLoadBalanceRule extends PredicateBasedRule { switch (condition) { case ConditionMatchUtil.EXACT: case ConditionMatchUtil.EQUAL: - return conditionMatchUtil.exactMatch(str, comparator); + return ConditionMatchUtil.exactMatch(str, comparator); case ConditionMatchUtil.REGEX: - return conditionMatchUtil.regexMatch(str, comparator); + return ConditionMatchUtil.regexMatch(str, comparator); case ConditionMatchUtil.PREFIX: - return conditionMatchUtil.prefixMatch(str, comparator); + return ConditionMatchUtil.prefixMatch(str, comparator); case ConditionMatchUtil.CONTAIN: - return conditionMatchUtil.containMatch(str, comparator); + return ConditionMatchUtil.containMatch(str, comparator); case ConditionMatchUtil.GREATER: - return conditionMatchUtil.greaterMatch(str, comparator); + return ConditionMatchUtil.greaterMatch(str, comparator); case ConditionMatchUtil.LESS: - return conditionMatchUtil.lessMatch(str, comparator); + return ConditionMatchUtil.lessMatch(str, comparator); case ConditionMatchUtil.NOT_EQUAL: - return conditionMatchUtil.noEqualMatch(str, comparator); + return ConditionMatchUtil.noEqualMatch(str, comparator); default: throw new UnsupportedOperationException( "unsupported string compare operation"); diff --git a/spring-cloud-alibaba-starters/spring-cloud-starter-alibaba-governance-routing/src/main/java/com/alibaba/cloud/routing/util/ConditionMatchUtil.java b/spring-cloud-alibaba-starters/spring-cloud-starter-alibaba-governance-routing/src/main/java/com/alibaba/cloud/routing/util/ConditionMatchUtil.java index 918461f57..695e09c46 100644 --- a/spring-cloud-alibaba-starters/spring-cloud-starter-alibaba-governance-routing/src/main/java/com/alibaba/cloud/routing/util/ConditionMatchUtil.java +++ b/spring-cloud-alibaba-starters/spring-cloud-starter-alibaba-governance-routing/src/main/java/com/alibaba/cloud/routing/util/ConditionMatchUtil.java @@ -22,7 +22,7 @@ import java.util.regex.Pattern; * @author HH * @since 2.2.10-RC1 */ -public class ConditionMatchUtil { +public final class ConditionMatchUtil { /** * Sign of exact. @@ -64,31 +64,34 @@ public class ConditionMatchUtil { */ public static final String NOT_EQUAL = "not_equal"; - public boolean exactMatch(String one, String another) { + private ConditionMatchUtil() { + } + + public static boolean exactMatch(String one, String another) { return one.equals(another); } - public boolean regexMatch(String regex, String path) { + public static boolean regexMatch(String regex, String path) { return Pattern.matches(regex, path); } - public boolean containMatch(String sub, String base) { + public static boolean containMatch(String sub, String base) { return base.contains(sub); } - public boolean prefixMatch(String prefix, String str) { + public static boolean prefixMatch(String prefix, String str) { return str.startsWith(prefix); } - public boolean greaterMatch(String str, String comparor) { + public static boolean greaterMatch(String str, String comparor) { return Integer.parseInt(comparor) > Integer.parseInt(str); } - public boolean lessMatch(String str, String comparor) { + public static boolean lessMatch(String str, String comparor) { return Integer.parseInt(comparor) < Integer.parseInt(str); } - public boolean noEqualMatch(String str, String comparor) { + public static boolean noEqualMatch(String str, String comparor) { return Integer.parseInt(str) == Integer.parseInt(comparor); } diff --git a/spring-cloud-alibaba-starters/spring-cloud-starter-alibaba-governance-routing/src/main/java/com/alibaba/cloud/routing/util/LoadBalanceUtil.java b/spring-cloud-alibaba-starters/spring-cloud-starter-alibaba-governance-routing/src/main/java/com/alibaba/cloud/routing/util/LoadBalanceUtil.java index 5f89c0495..dd04a092a 100644 --- a/spring-cloud-alibaba-starters/spring-cloud-starter-alibaba-governance-routing/src/main/java/com/alibaba/cloud/routing/util/LoadBalanceUtil.java +++ b/spring-cloud-alibaba-starters/spring-cloud-starter-alibaba-governance-routing/src/main/java/com/alibaba/cloud/routing/util/LoadBalanceUtil.java @@ -30,7 +30,7 @@ import com.netflix.loadbalancer.ZoneAvoidanceRule; * @author HH * @since 2.2.10-RC1 */ -public class LoadBalanceUtil { +public final class LoadBalanceUtil { /** * Default polling. @@ -67,35 +67,43 @@ public class LoadBalanceUtil { */ public static final String AVAILABILITY_FILTERING_RULE = "AvailabilityFilteringRule"; - public Server loadBalanceByOrdinaryRule(ILoadBalancer iLoadBalancer, Object key, - String rule) { - if (rule == null) { - rule = ZONE_AVOIDANCE_RULE; - } + private static final RoundRobinRule roundRobinRule = new RoundRobinRule(); + + private static final RandomRule randomRule = new RandomRule(); + + private static final WeightedResponseTimeRule weightedResponseTimeRule = new WeightedResponseTimeRule(); + + private static final BestAvailableRule bestAvailableRule = new BestAvailableRule(); + + private static final RetryRule retryRule = new RetryRule(); + + private static final ZoneAvoidanceRule zoneAvoidanceRule = new ZoneAvoidanceRule(); + + private static final AvailabilityFilteringRule availabilityFilteringRule = new AvailabilityFilteringRule(); + + private LoadBalanceUtil() { + } + + public static Server loadBalanceByOrdinaryRule(ILoadBalancer iLoadBalancer, + Object key, String rule) { switch (rule) { case ROUND_ROBIN_RULE: - return new RoundRobinRule().choose(iLoadBalancer, key); + return roundRobinRule.choose(iLoadBalancer, key); case RANDOM_RULE: - return new RandomRule().choose(iLoadBalancer, key); + return randomRule.choose(iLoadBalancer, key); case WEIGHTED_RESPONSE_TIME_RULE: - return new WeightedResponseTimeRule().choose(iLoadBalancer, key); + return weightedResponseTimeRule.choose(iLoadBalancer, key); case BEST_AVAILABLE_RULE: - BestAvailableRule bestAvailableRule = new BestAvailableRule(); bestAvailableRule.setLoadBalancer(iLoadBalancer); return bestAvailableRule.choose(key); case RETRY_RULE: - return new RetryRule().choose(iLoadBalancer, key); - case ZONE_AVOIDANCE_RULE: - ZoneAvoidanceRule zoneAvoidanceRule = new ZoneAvoidanceRule(); - zoneAvoidanceRule.setLoadBalancer(iLoadBalancer); - return zoneAvoidanceRule.choose(key); + return retryRule.choose(iLoadBalancer, key); case AVAILABILITY_FILTERING_RULE: - AvailabilityFilteringRule availabilityFilteringRule = new AvailabilityFilteringRule(); availabilityFilteringRule.setLoadBalancer(iLoadBalancer); return availabilityFilteringRule.choose(key); default: - throw new UnsupportedOperationException( - "unsupported string compare operation"); + zoneAvoidanceRule.setLoadBalancer(iLoadBalancer); + return zoneAvoidanceRule.choose(key); } } diff --git a/spring-cloud-alibaba-starters/spring-cloud-starter-alibaba-governance-routing/src/main/java/com/alibaba/cloud/routing/util/RequestContext.java b/spring-cloud-alibaba-starters/spring-cloud-starter-alibaba-governance-routing/src/main/java/com/alibaba/cloud/routing/util/RequestContext.java deleted file mode 100644 index 7bdcbc720..000000000 --- a/spring-cloud-alibaba-starters/spring-cloud-starter-alibaba-governance-routing/src/main/java/com/alibaba/cloud/routing/util/RequestContext.java +++ /dev/null @@ -1,49 +0,0 @@ -/* - * Copyright 2013-2018 the original author or authors. - * - * 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 - * - * https://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.alibaba.cloud.routing.util; - -import javax.servlet.http.HttpServletRequest; - -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; - -/** - * @author HH - * @since 2.2.10-RC1 - */ -public final class RequestContext { - - private static final Logger LOG = LoggerFactory.getLogger(RequestContext.class); - - private static final ThreadLocal REQUEST_THREAD_HOLDER = new ThreadLocal<>(); - - private RequestContext() { - } - - public static HttpServletRequest getRequest() { - return REQUEST_THREAD_HOLDER.get(); - } - - public static void setRequest(HttpServletRequest request) { - REQUEST_THREAD_HOLDER.set(request); - } - - public static void removeRequest() { - REQUEST_THREAD_HOLDER.remove(); - } - -} diff --git a/spring-cloud-alibaba-starters/spring-cloud-starter-alibaba-governance-routing/src/main/java/com/alibaba/cloud/routing/web/WebMvcAutoConfiguration.java b/spring-cloud-alibaba-starters/spring-cloud-starter-alibaba-governance-routing/src/main/java/com/alibaba/cloud/routing/web/WebMvcAutoConfiguration.java deleted file mode 100644 index a89590f72..000000000 --- a/spring-cloud-alibaba-starters/spring-cloud-starter-alibaba-governance-routing/src/main/java/com/alibaba/cloud/routing/web/WebMvcAutoConfiguration.java +++ /dev/null @@ -1,42 +0,0 @@ -/* - * Copyright 2013-2018 the original author or authors. - * - * 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 - * - * https://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.alibaba.cloud.routing.web; - -import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean; -import org.springframework.context.annotation.Bean; -import org.springframework.context.annotation.Configuration; - -/** - * @author HH - * @since 2.2.10-RC1 - */ -@Configuration(proxyBeanMethods = false) -public class WebMvcAutoConfiguration { - - @Bean - @ConditionalOnMissingBean - public WebMvcInterceptor webMvcInterceptor() { - return new WebMvcInterceptor(); - } - - @Bean - @ConditionalOnMissingBean - public WebMvcConfig webMvcConfig() { - return new WebMvcConfig(); - } - -} diff --git a/spring-cloud-alibaba-starters/spring-cloud-starter-alibaba-governance-routing/src/main/java/com/alibaba/cloud/routing/web/WebMvcConfig.java b/spring-cloud-alibaba-starters/spring-cloud-starter-alibaba-governance-routing/src/main/java/com/alibaba/cloud/routing/web/WebMvcConfig.java deleted file mode 100644 index a2ddf867a..000000000 --- a/spring-cloud-alibaba-starters/spring-cloud-starter-alibaba-governance-routing/src/main/java/com/alibaba/cloud/routing/web/WebMvcConfig.java +++ /dev/null @@ -1,37 +0,0 @@ -/* - * Copyright 2013-2018 the original author or authors. - * - * 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 - * - * https://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.alibaba.cloud.routing.web; - -import org.springframework.beans.factory.annotation.Autowired; -import org.springframework.web.servlet.config.annotation.InterceptorRegistry; -import org.springframework.web.servlet.config.annotation.WebMvcConfigurer; - -/** - * @author HH - * @since 2.2.10-RC1 - */ -public class WebMvcConfig implements WebMvcConfigurer { - - @Autowired - private WebMvcInterceptor webMvcInterceptor; - - @Override - public void addInterceptors(InterceptorRegistry registry) { - registry.addInterceptor(webMvcInterceptor); - } - -} diff --git a/spring-cloud-alibaba-starters/spring-cloud-starter-alibaba-governance-routing/src/main/java/com/alibaba/cloud/routing/web/WebMvcInterceptor.java b/spring-cloud-alibaba-starters/spring-cloud-starter-alibaba-governance-routing/src/main/java/com/alibaba/cloud/routing/web/WebMvcInterceptor.java deleted file mode 100644 index e501bb3f9..000000000 --- a/spring-cloud-alibaba-starters/spring-cloud-starter-alibaba-governance-routing/src/main/java/com/alibaba/cloud/routing/web/WebMvcInterceptor.java +++ /dev/null @@ -1,46 +0,0 @@ -/* - * Copyright 2013-2018 the original author or authors. - * - * 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 - * - * https://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.alibaba.cloud.routing.web; - -import javax.servlet.http.HttpServletRequest; -import javax.servlet.http.HttpServletResponse; - -import com.alibaba.cloud.routing.util.RequestContext; - -import org.springframework.lang.Nullable; -import org.springframework.web.servlet.HandlerInterceptor; - -/** - * @author HH - * @since 2.2.10-RC1 - */ -public class WebMvcInterceptor implements HandlerInterceptor { - - @Override - public boolean preHandle(HttpServletRequest request, HttpServletResponse response, - Object handler) { - RequestContext.setRequest(request); - return true; - } - - @Override - public void afterCompletion(HttpServletRequest request, HttpServletResponse response, - Object handler, @Nullable Exception ex) { - RequestContext.removeRequest(); - } - -} diff --git a/spring-cloud-alibaba-starters/spring-cloud-starter-alibaba-governance-routing/src/main/resources/META-INF/spring.factories b/spring-cloud-alibaba-starters/spring-cloud-starter-alibaba-governance-routing/src/main/resources/META-INF/spring.factories index 42bcdb73a..9c0a97812 100644 --- a/spring-cloud-alibaba-starters/spring-cloud-starter-alibaba-governance-routing/src/main/resources/META-INF/spring.factories +++ b/spring-cloud-alibaba-starters/spring-cloud-starter-alibaba-governance-routing/src/main/resources/META-INF/spring.factories @@ -2,5 +2,4 @@ org.springframework.boot.autoconfigure.EnableAutoConfiguration=\ com.alibaba.cloud.routing.RoutingAutoConfiguration,\ com.alibaba.cloud.routing.feign.RoutingFeignInterceptorAutoConfiguration,\ com.alibaba.cloud.routing.ribbon.RoutingLoadBalanceRuleAutoConfiguration,\ - com.alibaba.cloud.routing.web.WebMvcAutoConfiguration,\ com.alibaba.cloud.routing.RoutingPropertiesAutoConfiguration