This is an automated email from the ASF dual-hosted git repository. liujun pushed a commit to branch master in repository https://gitbox.apache.org/repos/asf/dubbo.git
The following commit(s) were added to refs/heads/master by this push: new c29bb65 Fixes multi-registry subscription loadbalance strategy does not work properly. (#5686) c29bb65 is described below commit c29bb653e011e38c775bffeaae6cf525b067f274 Author: ken.lj <ken.lj...@gmail.com> AuthorDate: Sun Feb 9 13:33:27 2020 +0800 Fixes multi-registry subscription loadbalance strategy does not work properly. (#5686) * refactor directory to make cluster and invoker load balance work * add weight property in dubbo.xsd * distinguish usage of getUrl and getConsumerUrl * fix directory related UT * fix ut --- .../java/org/apache/dubbo/rpc/cluster/Directory.java | 3 +++ .../rpc/cluster/directory/AbstractDirectory.java | 16 +++------------- .../rpc/cluster/support/AbstractClusterInvoker.java | 6 +++++- .../rpc/cluster/support/FailoverClusterInvoker.java | 2 +- .../rpc/cluster/support/ForkingClusterInvoker.java | 4 ++-- .../rpc/cluster/support/MergeableClusterInvoker.java | 2 +- .../cluster/support/wrapper/MockClusterInvoker.java | 10 +++++----- .../org/apache/dubbo/rpc/cluster/StickyTest.java | 2 ++ .../cluster/support/AbstractClusterInvokerTest.java | 5 ++--- .../cluster/support/FailSafeClusterInvokerTest.java | 2 ++ .../cluster/support/FailbackClusterInvokerTest.java | 2 ++ .../cluster/support/FailfastClusterInvokerTest.java | 2 ++ .../cluster/support/FailoverClusterInvokerTest.java | 2 ++ .../cluster/support/ForkingClusterInvokerTest.java | 2 ++ .../cluster/support/MergeableClusterInvokerTest.java | 4 ++++ .../common/extension/AdaptiveClassCodeGenerator.java | 20 ++++++++++++++++---- .../src/main/resources/META-INF/compat/dubbo.xsd | 5 +++++ .../src/main/resources/META-INF/dubbo.xsd | 5 +++++ .../registry/integration/RegistryDirectory.java | 9 +-------- .../dubbo/registry/integration/RegistryProtocol.java | 2 +- .../dubbo/registry/dubbo/RegistryDirectoryTest.java | 10 +++++----- 21 files changed, 71 insertions(+), 44 deletions(-) diff --git a/dubbo-cluster/src/main/java/org/apache/dubbo/rpc/cluster/Directory.java b/dubbo-cluster/src/main/java/org/apache/dubbo/rpc/cluster/Directory.java index a03a781..2940bf6 100644 --- a/dubbo-cluster/src/main/java/org/apache/dubbo/rpc/cluster/Directory.java +++ b/dubbo-cluster/src/main/java/org/apache/dubbo/rpc/cluster/Directory.java @@ -17,6 +17,7 @@ package org.apache.dubbo.rpc.cluster; import org.apache.dubbo.common.Node; +import org.apache.dubbo.common.URL; import org.apache.dubbo.rpc.Invocation; import org.apache.dubbo.rpc.Invoker; import org.apache.dubbo.rpc.RpcException; @@ -48,4 +49,6 @@ public interface Directory<T> extends Node { List<Invoker<T>> getAllInvokers(); + URL getConsumerUrl(); + } \ No newline at end of file diff --git a/dubbo-cluster/src/main/java/org/apache/dubbo/rpc/cluster/directory/AbstractDirectory.java b/dubbo-cluster/src/main/java/org/apache/dubbo/rpc/cluster/directory/AbstractDirectory.java index 36110c9..814fb64 100644 --- a/dubbo-cluster/src/main/java/org/apache/dubbo/rpc/cluster/directory/AbstractDirectory.java +++ b/dubbo-cluster/src/main/java/org/apache/dubbo/rpc/cluster/directory/AbstractDirectory.java @@ -20,7 +20,6 @@ import org.apache.dubbo.common.URL; import org.apache.dubbo.common.logger.Logger; import org.apache.dubbo.common.logger.LoggerFactory; import org.apache.dubbo.common.utils.StringUtils; -import org.apache.dubbo.common.utils.UrlUtils; import org.apache.dubbo.rpc.Invocation; import org.apache.dubbo.rpc.Invoker; import org.apache.dubbo.rpc.RpcException; @@ -30,7 +29,6 @@ import org.apache.dubbo.rpc.cluster.RouterChain; import java.util.Collections; import java.util.List; -import java.util.Map; import static org.apache.dubbo.common.constants.CommonConstants.MONITOR_KEY; import static org.apache.dubbo.rpc.cluster.Constants.REFER_KEY; @@ -57,22 +55,14 @@ public abstract class AbstractDirectory<T> implements Directory<T> { } public AbstractDirectory(URL url, RouterChain<T> routerChain) { - this(url, url, routerChain); - } - - public AbstractDirectory(URL url, URL consumerUrl, RouterChain<T> routerChain) { if (url == null) { throw new IllegalArgumentException("url == null"); } - if (UrlUtils.isRegistry(url)) { - Map<String, String> queryMap = StringUtils.parseQueryString(url.getParameterAndDecoded(REFER_KEY)); - this.url = url.addParameters(queryMap).removeParameter(MONITOR_KEY); - } else { - this.url = url; - } + this.url = url.removeParameter(REFER_KEY).removeParameter(MONITOR_KEY); + this.consumerUrl = url.addParameters(StringUtils.parseQueryString(url.getParameterAndDecoded(REFER_KEY))) + .removeParameter(MONITOR_KEY); - this.consumerUrl = consumerUrl; setRouterChain(routerChain); } diff --git a/dubbo-cluster/src/main/java/org/apache/dubbo/rpc/cluster/support/AbstractClusterInvoker.java b/dubbo-cluster/src/main/java/org/apache/dubbo/rpc/cluster/support/AbstractClusterInvoker.java index 972e95c..5afb4c1 100644 --- a/dubbo-cluster/src/main/java/org/apache/dubbo/rpc/cluster/support/AbstractClusterInvoker.java +++ b/dubbo-cluster/src/main/java/org/apache/dubbo/rpc/cluster/support/AbstractClusterInvoker.java @@ -88,6 +88,10 @@ public abstract class AbstractClusterInvoker<T> implements Invoker<T> { return directory.getUrl(); } + protected URL getConsumerUrl() { + return directory.getConsumerUrl(); + } + @Override public boolean isAvailable() { Invoker<T> invoker = stickyInvoker; @@ -251,7 +255,7 @@ public abstract class AbstractClusterInvoker<T> implements Invoker<T> { List<Invoker<T>> invokers = list(invocation); LoadBalance loadbalance = initLoadBalance(invokers, invocation); - RpcUtils.attachInvocationIdIfAsync(getUrl(), invocation); + RpcUtils.attachInvocationIdIfAsync(getConsumerUrl(), invocation); return doInvoke(invocation, invokers, loadbalance); } diff --git a/dubbo-cluster/src/main/java/org/apache/dubbo/rpc/cluster/support/FailoverClusterInvoker.java b/dubbo-cluster/src/main/java/org/apache/dubbo/rpc/cluster/support/FailoverClusterInvoker.java index 5efe0ce..e49a998 100644 --- a/dubbo-cluster/src/main/java/org/apache/dubbo/rpc/cluster/support/FailoverClusterInvoker.java +++ b/dubbo-cluster/src/main/java/org/apache/dubbo/rpc/cluster/support/FailoverClusterInvoker.java @@ -58,7 +58,7 @@ public class FailoverClusterInvoker<T> extends AbstractClusterInvoker<T> { List<Invoker<T>> copyInvokers = invokers; checkInvokers(copyInvokers, invocation); String methodName = RpcUtils.getMethodName(invocation); - int len = getUrl().getMethodParameter(methodName, RETRIES_KEY, DEFAULT_RETRIES) + 1; + int len = getConsumerUrl().getMethodParameter(methodName, RETRIES_KEY, DEFAULT_RETRIES) + 1; if (len <= 0) { len = 1; } diff --git a/dubbo-cluster/src/main/java/org/apache/dubbo/rpc/cluster/support/ForkingClusterInvoker.java b/dubbo-cluster/src/main/java/org/apache/dubbo/rpc/cluster/support/ForkingClusterInvoker.java index 7ce7729..c0a1ddf 100644 --- a/dubbo-cluster/src/main/java/org/apache/dubbo/rpc/cluster/support/ForkingClusterInvoker.java +++ b/dubbo-cluster/src/main/java/org/apache/dubbo/rpc/cluster/support/ForkingClusterInvoker.java @@ -65,8 +65,8 @@ public class ForkingClusterInvoker<T> extends AbstractClusterInvoker<T> { try { checkInvokers(invokers, invocation); final List<Invoker<T>> selected; - final int forks = getUrl().getParameter(FORKS_KEY, DEFAULT_FORKS); - final int timeout = getUrl().getParameter(TIMEOUT_KEY, DEFAULT_TIMEOUT); + final int forks = getConsumerUrl().getParameter(FORKS_KEY, DEFAULT_FORKS); + final int timeout = getConsumerUrl().getParameter(TIMEOUT_KEY, DEFAULT_TIMEOUT); if (forks <= 0 || forks >= invokers.size()) { selected = invokers; } else { diff --git a/dubbo-cluster/src/main/java/org/apache/dubbo/rpc/cluster/support/MergeableClusterInvoker.java b/dubbo-cluster/src/main/java/org/apache/dubbo/rpc/cluster/support/MergeableClusterInvoker.java index babc1d5..0a9217b 100644 --- a/dubbo-cluster/src/main/java/org/apache/dubbo/rpc/cluster/support/MergeableClusterInvoker.java +++ b/dubbo-cluster/src/main/java/org/apache/dubbo/rpc/cluster/support/MergeableClusterInvoker.java @@ -63,7 +63,7 @@ public class MergeableClusterInvoker<T> extends AbstractClusterInvoker<T> { @Override protected Result doInvoke(Invocation invocation, List<Invoker<T>> invokers, LoadBalance loadbalance) throws RpcException { checkInvokers(invokers, invocation); - String merger = getUrl().getMethodParameter(invocation.getMethodName(), MERGER_KEY); + String merger = getConsumerUrl().getMethodParameter(invocation.getMethodName(), MERGER_KEY); if (ConfigUtils.isEmpty(merger)) { // If a method doesn't have a merger, only invoke one Group for (final Invoker<T> invoker : invokers) { if (invoker.isAvailable()) { diff --git a/dubbo-cluster/src/main/java/org/apache/dubbo/rpc/cluster/support/wrapper/MockClusterInvoker.java b/dubbo-cluster/src/main/java/org/apache/dubbo/rpc/cluster/support/wrapper/MockClusterInvoker.java index 2a59ac9..2898f4b 100644 --- a/dubbo-cluster/src/main/java/org/apache/dubbo/rpc/cluster/support/wrapper/MockClusterInvoker.java +++ b/dubbo-cluster/src/main/java/org/apache/dubbo/rpc/cluster/support/wrapper/MockClusterInvoker.java @@ -72,13 +72,13 @@ public class MockClusterInvoker<T> implements Invoker<T> { public Result invoke(Invocation invocation) throws RpcException { Result result = null; - String value = directory.getUrl().getMethodParameter(invocation.getMethodName(), MOCK_KEY, Boolean.FALSE.toString()).trim(); + String value = directory.getConsumerUrl().getMethodParameter(invocation.getMethodName(), MOCK_KEY, Boolean.FALSE.toString()).trim(); if (value.length() == 0 || "false".equalsIgnoreCase(value)) { //no mock result = this.invoker.invoke(invocation); } else if (value.startsWith("force")) { if (logger.isWarnEnabled()) { - logger.warn("force-mock: " + invocation.getMethodName() + " force-mock enabled , url : " + directory.getUrl()); + logger.warn("force-mock: " + invocation.getMethodName() + " force-mock enabled , url : " + directory.getConsumerUrl()); } //force:direct mock result = doMockInvoke(invocation, null); @@ -103,7 +103,7 @@ public class MockClusterInvoker<T> implements Invoker<T> { } if (logger.isWarnEnabled()) { - logger.warn("fail-mock: " + invocation.getMethodName() + " fail-mock enabled , url : " + directory.getUrl(), e); + logger.warn("fail-mock: " + invocation.getMethodName() + " fail-mock enabled , url : " + directory.getConsumerUrl(), e); } result = doMockInvoke(invocation, e); } @@ -118,7 +118,7 @@ public class MockClusterInvoker<T> implements Invoker<T> { List<Invoker<T>> mockInvokers = selectMockInvoker(invocation); if (CollectionUtils.isEmpty(mockInvokers)) { - minvoker = (Invoker<T>) new MockInvoker(directory.getUrl(), directory.getInterface()); + minvoker = (Invoker<T>) new MockInvoker(directory.getConsumerUrl(), directory.getInterface()); } else { minvoker = mockInvokers.get(0); } @@ -165,7 +165,7 @@ public class MockClusterInvoker<T> implements Invoker<T> { } catch (RpcException e) { if (logger.isInfoEnabled()) { logger.info("Exception when try to invoke mock. Get mock invokers error for service:" - + directory.getUrl().getServiceInterface() + ", method:" + invocation.getMethodName() + + directory.getConsumerUrl().getServiceInterface() + ", method:" + invocation.getMethodName() + ", will contruct a new mock with 'new MockInvoker()'.", e); } } diff --git a/dubbo-cluster/src/test/java/org/apache/dubbo/rpc/cluster/StickyTest.java b/dubbo-cluster/src/test/java/org/apache/dubbo/rpc/cluster/StickyTest.java index 2862f2c..f9f9311 100644 --- a/dubbo-cluster/src/test/java/org/apache/dubbo/rpc/cluster/StickyTest.java +++ b/dubbo-cluster/src/test/java/org/apache/dubbo/rpc/cluster/StickyTest.java @@ -59,9 +59,11 @@ public class StickyTest { @BeforeEach public void setUp() throws Exception { dic = mock(Directory.class); + invocation = new RpcInvocation(); given(dic.getUrl()).willReturn(url); + given(dic.getConsumerUrl()).willReturn(url); given(dic.list(invocation)).willReturn(invokers); given(dic.getInterface()).willReturn(StickyTest.class); diff --git a/dubbo-cluster/src/test/java/org/apache/dubbo/rpc/cluster/support/AbstractClusterInvokerTest.java b/dubbo-cluster/src/test/java/org/apache/dubbo/rpc/cluster/support/AbstractClusterInvokerTest.java index 955ec0e..e94ebab 100644 --- a/dubbo-cluster/src/test/java/org/apache/dubbo/rpc/cluster/support/AbstractClusterInvokerTest.java +++ b/dubbo-cluster/src/test/java/org/apache/dubbo/rpc/cluster/support/AbstractClusterInvokerTest.java @@ -19,7 +19,6 @@ package org.apache.dubbo.rpc.cluster.support; import org.apache.dubbo.common.URL; import org.apache.dubbo.common.extension.ExtensionLoader; import org.apache.dubbo.common.utils.NetUtils; -import org.apache.dubbo.common.utils.StringUtils; import org.apache.dubbo.rpc.Invocation; import org.apache.dubbo.rpc.Invoker; import org.apache.dubbo.rpc.Result; @@ -66,6 +65,7 @@ public class AbstractClusterInvokerTest { StaticDirectory<IHelloService> dic; RpcInvocation invocation = new RpcInvocation(); URL url = URL.valueOf("registry://localhost:9090/org.apache.dubbo.rpc.cluster.support.AbstractClusterInvokerTest.IHelloService?refer=" + URL.encode("application=abstractClusterInvokerTest")); + URL tmpUrl = url.removeParameter(REFER_KEY).removeParameter(MONITOR_KEY); Invoker<IHelloService> invoker1; Invoker<IHelloService> invoker2; @@ -124,6 +124,7 @@ public class AbstractClusterInvokerTest { invokers.add(invoker1); dic = new StaticDirectory<IHelloService>(url, invokers, null); + cluster = new AbstractClusterInvoker(dic) { @Override protected Result doInvoke(Invocation invocation, List invokers, LoadBalance loadbalance) @@ -225,8 +226,6 @@ public class AbstractClusterInvokerTest { @Test public void testCloseAvailablecheck() { LoadBalance lb = mock(LoadBalance.class); - Map<String, String> queryMap = StringUtils.parseQueryString(url.getParameterAndDecoded(REFER_KEY)); - URL tmpUrl = url.addParameters(queryMap).removeParameter(MONITOR_KEY); given(lb.select(invokers, tmpUrl, invocation)).willReturn(invoker1); initlistsize5(); diff --git a/dubbo-cluster/src/test/java/org/apache/dubbo/rpc/cluster/support/FailSafeClusterInvokerTest.java b/dubbo-cluster/src/test/java/org/apache/dubbo/rpc/cluster/support/FailSafeClusterInvokerTest.java index 8ab596b..f255ccb 100644 --- a/dubbo-cluster/src/test/java/org/apache/dubbo/rpc/cluster/support/FailSafeClusterInvokerTest.java +++ b/dubbo-cluster/src/test/java/org/apache/dubbo/rpc/cluster/support/FailSafeClusterInvokerTest.java @@ -60,6 +60,7 @@ public class FailSafeClusterInvokerTest { dic = mock(Directory.class); given(dic.getUrl()).willReturn(url); + given(dic.getConsumerUrl()).willReturn(url); given(dic.list(invocation)).willReturn(invokers); given(dic.getInterface()).willReturn(DemoService.class); invocation.setMethodName("method1"); @@ -103,6 +104,7 @@ public class FailSafeClusterInvokerTest { dic = mock(Directory.class); given(dic.getUrl()).willReturn(url); + given(dic.getConsumerUrl()).willReturn(url); given(dic.list(invocation)).willReturn(null); given(dic.getInterface()).willReturn(DemoService.class); diff --git a/dubbo-cluster/src/test/java/org/apache/dubbo/rpc/cluster/support/FailbackClusterInvokerTest.java b/dubbo-cluster/src/test/java/org/apache/dubbo/rpc/cluster/support/FailbackClusterInvokerTest.java index b745b0e..d648943 100644 --- a/dubbo-cluster/src/test/java/org/apache/dubbo/rpc/cluster/support/FailbackClusterInvokerTest.java +++ b/dubbo-cluster/src/test/java/org/apache/dubbo/rpc/cluster/support/FailbackClusterInvokerTest.java @@ -70,6 +70,7 @@ public class FailbackClusterInvokerTest { dic = mock(Directory.class); given(dic.getUrl()).willReturn(url); + given(dic.getConsumerUrl()).willReturn(url); given(dic.list(invocation)).willReturn(invokers); given(dic.getInterface()).willReturn(FailbackClusterInvokerTest.class); @@ -128,6 +129,7 @@ public class FailbackClusterInvokerTest { dic = mock(Directory.class); given(dic.getUrl()).willReturn(url); + given(dic.getConsumerUrl()).willReturn(url); given(dic.list(invocation)).willReturn(null); given(dic.getInterface()).willReturn(FailbackClusterInvokerTest.class); diff --git a/dubbo-cluster/src/test/java/org/apache/dubbo/rpc/cluster/support/FailfastClusterInvokerTest.java b/dubbo-cluster/src/test/java/org/apache/dubbo/rpc/cluster/support/FailfastClusterInvokerTest.java index 6a70686..0bdd9e6 100644 --- a/dubbo-cluster/src/test/java/org/apache/dubbo/rpc/cluster/support/FailfastClusterInvokerTest.java +++ b/dubbo-cluster/src/test/java/org/apache/dubbo/rpc/cluster/support/FailfastClusterInvokerTest.java @@ -59,6 +59,7 @@ public class FailfastClusterInvokerTest { dic = mock(Directory.class); given(dic.getUrl()).willReturn(url); + given(dic.getConsumerUrl()).willReturn(url); given(dic.list(invocation)).willReturn(invokers); given(dic.getInterface()).willReturn(FailfastClusterInvokerTest.class); @@ -104,6 +105,7 @@ public class FailfastClusterInvokerTest { dic = mock(Directory.class); given(dic.getUrl()).willReturn(url); + given(dic.getConsumerUrl()).willReturn(url); given(dic.list(invocation)).willReturn(null); given(dic.getInterface()).willReturn(FailfastClusterInvokerTest.class); diff --git a/dubbo-cluster/src/test/java/org/apache/dubbo/rpc/cluster/support/FailoverClusterInvokerTest.java b/dubbo-cluster/src/test/java/org/apache/dubbo/rpc/cluster/support/FailoverClusterInvokerTest.java index 790d7cd..613a527 100644 --- a/dubbo-cluster/src/test/java/org/apache/dubbo/rpc/cluster/support/FailoverClusterInvokerTest.java +++ b/dubbo-cluster/src/test/java/org/apache/dubbo/rpc/cluster/support/FailoverClusterInvokerTest.java @@ -68,6 +68,7 @@ public class FailoverClusterInvokerTest { dic = mock(Directory.class); given(dic.getUrl()).willReturn(url); + given(dic.getConsumerUrl()).willReturn(url); given(dic.list(invocation)).willReturn(invokers); given(dic.getInterface()).willReturn(FailoverClusterInvokerTest.class); invocation.setMethodName("method1"); @@ -146,6 +147,7 @@ public class FailoverClusterInvokerTest { dic = mock(Directory.class); given(dic.getUrl()).willReturn(url); + given(dic.getConsumerUrl()).willReturn(url); given(dic.list(invocation)).willReturn(null); given(dic.getInterface()).willReturn(FailoverClusterInvokerTest.class); invocation.setMethodName("method1"); diff --git a/dubbo-cluster/src/test/java/org/apache/dubbo/rpc/cluster/support/ForkingClusterInvokerTest.java b/dubbo-cluster/src/test/java/org/apache/dubbo/rpc/cluster/support/ForkingClusterInvokerTest.java index 8943b9a..40ba6e2 100644 --- a/dubbo-cluster/src/test/java/org/apache/dubbo/rpc/cluster/support/ForkingClusterInvokerTest.java +++ b/dubbo-cluster/src/test/java/org/apache/dubbo/rpc/cluster/support/ForkingClusterInvokerTest.java @@ -24,6 +24,7 @@ import org.apache.dubbo.rpc.RpcContext; import org.apache.dubbo.rpc.RpcException; import org.apache.dubbo.rpc.RpcInvocation; import org.apache.dubbo.rpc.cluster.Directory; + import org.junit.jupiter.api.Assertions; import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.Test; @@ -57,6 +58,7 @@ public class ForkingClusterInvokerTest { dic = mock(Directory.class); given(dic.getUrl()).willReturn(url); + given(dic.getConsumerUrl()).willReturn(url); given(dic.list(invocation)).willReturn(invokers); given(dic.getInterface()).willReturn(ForkingClusterInvokerTest.class); diff --git a/dubbo-cluster/src/test/java/org/apache/dubbo/rpc/cluster/support/MergeableClusterInvokerTest.java b/dubbo-cluster/src/test/java/org/apache/dubbo/rpc/cluster/support/MergeableClusterInvokerTest.java index 475b621..b0ff96f 100644 --- a/dubbo-cluster/src/test/java/org/apache/dubbo/rpc/cluster/support/MergeableClusterInvokerTest.java +++ b/dubbo-cluster/src/test/java/org/apache/dubbo/rpc/cluster/support/MergeableClusterInvokerTest.java @@ -143,6 +143,8 @@ public class MergeableClusterInvokerTest { } }); given(directory.getUrl()).willReturn(url); + given(directory.getConsumerUrl()).willReturn(url); + given(directory.getConsumerUrl()).willReturn(url); given(directory.getInterface()).willReturn(MenuService.class); mergeableClusterInvoker = new MergeableClusterInvoker<MenuService>(directory); @@ -208,6 +210,8 @@ public class MergeableClusterInvokerTest { } }); given(directory.getUrl()).willReturn(url); + given(directory.getConsumerUrl()).willReturn(url); + given(directory.getConsumerUrl()).willReturn(url); given(directory.getInterface()).willReturn(MenuService.class); mergeableClusterInvoker = new MergeableClusterInvoker<MenuService>(directory); diff --git a/dubbo-common/src/main/java/org/apache/dubbo/common/extension/AdaptiveClassCodeGenerator.java b/dubbo-common/src/main/java/org/apache/dubbo/common/extension/AdaptiveClassCodeGenerator.java index 73790a1..2bffa05 100644 --- a/dubbo-common/src/main/java/org/apache/dubbo/common/extension/AdaptiveClassCodeGenerator.java +++ b/dubbo-common/src/main/java/org/apache/dubbo/common/extension/AdaptiveClassCodeGenerator.java @@ -24,6 +24,8 @@ import org.apache.dubbo.common.utils.StringUtils; import java.lang.reflect.Method; import java.lang.reflect.Modifier; import java.util.Arrays; +import java.util.HashMap; +import java.util.Map; import java.util.stream.Collectors; import java.util.stream.IntStream; @@ -344,6 +346,7 @@ public class AdaptiveClassCodeGenerator { private String generateUrlAssignmentIndirectly(Method method) { Class<?>[] pts = method.getParameterTypes(); + Map<String, Integer> getterReturnUrl = new HashMap<>(); // find URL getter method for (int i = 0; i < pts.length; ++i) { for (Method m : pts[i].getMethods()) { @@ -353,15 +356,24 @@ public class AdaptiveClassCodeGenerator { && !Modifier.isStatic(m.getModifiers()) && m.getParameterTypes().length == 0 && m.getReturnType() == URL.class) { - return generateGetUrlNullCheck(i, pts[i], name); + getterReturnUrl.put(name, i); } } } - // getter method not found, throw - throw new IllegalStateException("Failed to create adaptive class for interface " + type.getName() - + ": not found url parameter or url attribute in parameters of method " + method.getName()); + if (getterReturnUrl.size() <= 0) { + // getter method not found, throw + throw new IllegalStateException("Failed to create adaptive class for interface " + type.getName() + + ": not found url parameter or url attribute in parameters of method " + method.getName()); + } + Integer index = getterReturnUrl.get("getUrl"); + if (index != null) { + return generateGetUrlNullCheck(index, pts[index], "getUrl"); + } else { + Map.Entry<String, Integer> entry = getterReturnUrl.entrySet().iterator().next(); + return generateGetUrlNullCheck(entry.getValue(), pts[entry.getValue()], entry.getKey()); + } } /** diff --git a/dubbo-config/dubbo-config-spring/src/main/resources/META-INF/compat/dubbo.xsd b/dubbo-config/dubbo-config-spring/src/main/resources/META-INF/compat/dubbo.xsd index 8e1b9f4..1805b2a 100644 --- a/dubbo-config/dubbo-config-spring/src/main/resources/META-INF/compat/dubbo.xsd +++ b/dubbo-config/dubbo-config-spring/src/main/resources/META-INF/compat/dubbo.xsd @@ -623,6 +623,11 @@ <xsd:documentation><![CDATA[ Is this registry the preferred one. ]]></xsd:documentation> </xsd:annotation> </xsd:attribute> + <xsd:attribute name="weight" type="xsd:integer"> + <xsd:annotation> + <xsd:documentation><![CDATA[ weight of registry. ]]></xsd:documentation> + </xsd:annotation> + </xsd:attribute> </xsd:complexType> <xsd:complexType name="metadataReportType"> diff --git a/dubbo-config/dubbo-config-spring/src/main/resources/META-INF/dubbo.xsd b/dubbo-config/dubbo-config-spring/src/main/resources/META-INF/dubbo.xsd index 4204629..bfaaf3f 100644 --- a/dubbo-config/dubbo-config-spring/src/main/resources/META-INF/dubbo.xsd +++ b/dubbo-config/dubbo-config-spring/src/main/resources/META-INF/dubbo.xsd @@ -617,6 +617,11 @@ <xsd:documentation><![CDATA[ Is this registry the preferred one. ]]></xsd:documentation> </xsd:annotation> </xsd:attribute> + <xsd:attribute name="weight" type="xsd:integer"> + <xsd:annotation> + <xsd:documentation><![CDATA[ weight of registry. ]]></xsd:documentation> + </xsd:annotation> + </xsd:attribute> </xsd:complexType> <xsd:complexType name="metadataReportType"> diff --git a/dubbo-registry/dubbo-registry-api/src/main/java/org/apache/dubbo/registry/integration/RegistryDirectory.java b/dubbo-registry/dubbo-registry-api/src/main/java/org/apache/dubbo/registry/integration/RegistryDirectory.java index 37122eb..5790690 100644 --- a/dubbo-registry/dubbo-registry-api/src/main/java/org/apache/dubbo/registry/integration/RegistryDirectory.java +++ b/dubbo-registry/dubbo-registry-api/src/main/java/org/apache/dubbo/registry/integration/RegistryDirectory.java @@ -67,7 +67,6 @@ import static org.apache.dubbo.common.constants.CommonConstants.ENABLED_KEY; import static org.apache.dubbo.common.constants.CommonConstants.GROUP_KEY; import static org.apache.dubbo.common.constants.CommonConstants.INTERFACE_KEY; import static org.apache.dubbo.common.constants.CommonConstants.MONITOR_KEY; -import static org.apache.dubbo.common.constants.CommonConstants.PREFERRED_KEY; import static org.apache.dubbo.common.constants.CommonConstants.PROTOCOL_KEY; import static org.apache.dubbo.common.constants.RegistryConstants.APP_DYNAMIC_CONFIGURATORS_CATEGORY; import static org.apache.dubbo.common.constants.RegistryConstants.CATEGORY_KEY; @@ -78,7 +77,6 @@ import static org.apache.dubbo.common.constants.RegistryConstants.DEFAULT_CATEGO import static org.apache.dubbo.common.constants.RegistryConstants.DYNAMIC_CONFIGURATORS_CATEGORY; import static org.apache.dubbo.common.constants.RegistryConstants.EMPTY_PROTOCOL; import static org.apache.dubbo.common.constants.RegistryConstants.PROVIDERS_CATEGORY; -import static org.apache.dubbo.common.constants.RegistryConstants.REGISTRY_KEY; import static org.apache.dubbo.common.constants.RegistryConstants.ROUTERS_CATEGORY; import static org.apache.dubbo.common.constants.RegistryConstants.ROUTE_PROTOCOL; import static org.apache.dubbo.registry.Constants.CONFIGURATORS_SUFFIX; @@ -156,11 +154,6 @@ public class RegistryDirectory<T> extends AbstractDirectory<T> implements Notify } private URL turnRegistryUrlToConsumerUrl(URL url) { - // save any parameter in registry that will be useful to the new url. - String isDefault = url.getParameter(PREFERRED_KEY); - if (StringUtils.isNotEmpty(isDefault)) { - queryMap.put(REGISTRY_KEY + "." + PREFERRED_KEY, isDefault); - } return URLBuilder.from(url) .setPath(url.getServiceInterface()) .clearParameters() @@ -633,7 +626,7 @@ public class RegistryDirectory<T> extends AbstractDirectory<T> implements Notify } @Override - public URL getUrl() { + public URL getConsumerUrl() { return this.overrideDirectoryUrl; } diff --git a/dubbo-registry/dubbo-registry-api/src/main/java/org/apache/dubbo/registry/integration/RegistryProtocol.java b/dubbo-registry/dubbo-registry-api/src/main/java/org/apache/dubbo/registry/integration/RegistryProtocol.java index 77ec75f..3e3eeec 100644 --- a/dubbo-registry/dubbo-registry-api/src/main/java/org/apache/dubbo/registry/integration/RegistryProtocol.java +++ b/dubbo-registry/dubbo-registry-api/src/main/java/org/apache/dubbo/registry/integration/RegistryProtocol.java @@ -419,7 +419,7 @@ public class RegistryProtocol implements Protocol { directory.setRegistry(registry); directory.setProtocol(protocol); // all attributes of REFER_KEY - Map<String, String> parameters = new HashMap<String, String>(directory.getUrl().getParameters()); + Map<String, String> parameters = new HashMap<String, String>(directory.getConsumerUrl().getParameters()); URL subscribeUrl = new URL(CONSUMER_PROTOCOL, parameters.remove(REGISTER_IP_KEY), 0, type.getName(), parameters); if (directory.isShouldRegister()) { directory.setRegisteredConsumerUrl(subscribeUrl); diff --git a/dubbo-registry/dubbo-registry-default/src/test/java/org/apache/dubbo/registry/dubbo/RegistryDirectoryTest.java b/dubbo-registry/dubbo-registry-default/src/test/java/org/apache/dubbo/registry/dubbo/RegistryDirectoryTest.java index 570647c..9ce163c 100644 --- a/dubbo-registry/dubbo-registry-default/src/test/java/org/apache/dubbo/registry/dubbo/RegistryDirectoryTest.java +++ b/dubbo-registry/dubbo-registry-default/src/test/java/org/apache/dubbo/registry/dubbo/RegistryDirectoryTest.java @@ -145,7 +145,7 @@ public class RegistryDirectoryTest { field.setAccessible(true); Map<String, String> queryMap = (Map<String, String>) field.get(reg); Assertions.assertEquals("bar", queryMap.get("foo")); - Assertions.assertEquals(url.clearParameters().addParameter("foo", "bar"), reg.getUrl()); + Assertions.assertEquals(url.clearParameters().addParameter("foo", "bar"), reg.getConsumerUrl()); } @Test @@ -413,7 +413,7 @@ public class RegistryDirectoryTest { serviceUrls.add(SERVICEURL.addParameter(MOCK_KEY, "true")); registryDirectory2.notify(serviceUrls); - Assertions.assertEquals("true", registryDirectory2.getUrl().getParameter("mock")); + Assertions.assertEquals("true", registryDirectory2.getConsumerUrl().getParameter("mock")); } } @@ -783,7 +783,7 @@ public class RegistryDirectoryTest { List<URL> durls = new ArrayList<URL>(); durls.add(SERVICEURL.setHost("10.20.30.140").addParameter("timeout", "1")); registryDirectory.notify(durls); - Assertions.assertNull(registryDirectory.getUrl().getParameter("mock")); + Assertions.assertNull(registryDirectory.getConsumerUrl().getParameter("mock")); //override durls = new ArrayList<URL>(); @@ -792,7 +792,7 @@ public class RegistryDirectoryTest { List<Invoker<?>> invokers = registryDirectory.list(invocation); Invoker<?> aInvoker = invokers.get(0); Assertions.assertEquals("1000", aInvoker.getUrl().getParameter("timeout")); - Assertions.assertEquals("fail", registryDirectory.getUrl().getParameter("mock")); + Assertions.assertEquals("fail", registryDirectory.getConsumerUrl().getParameter("mock")); //override clean durls = new ArrayList<URL>(); @@ -803,7 +803,7 @@ public class RegistryDirectoryTest { //Need to be restored to the original providerUrl Assertions.assertEquals("1", aInvoker.getUrl().getParameter("timeout")); - Assertions.assertNull(registryDirectory.getUrl().getParameter("mock")); + Assertions.assertNull(registryDirectory.getConsumerUrl().getParameter("mock")); } /**