diff --git a/dubbo-cluster/src/main/java/org/apache/dubbo/rpc/cluster/support/registry/ZoneAwareClusterInvoker.java b/dubbo-cluster/src/main/java/org/apache/dubbo/rpc/cluster/support/registry/ZoneAwareClusterInvoker.java index b0be28b7b09..7fe4b43830b 100644 --- a/dubbo-cluster/src/main/java/org/apache/dubbo/rpc/cluster/support/registry/ZoneAwareClusterInvoker.java +++ b/dubbo-cluster/src/main/java/org/apache/dubbo/rpc/cluster/support/registry/ZoneAwareClusterInvoker.java @@ -39,12 +39,12 @@ import java.util.Set; import java.util.stream.Collectors; -import static org.apache.dubbo.common.constants.CommonConstants.PREFERRED_KEY; import static org.apache.dubbo.common.constants.RegistryConstants.LOADBALANCE_AMONG_REGISTRIES; import static org.apache.dubbo.common.constants.RegistryConstants.REGISTRY_KEY; import static org.apache.dubbo.common.constants.RegistryConstants.REGISTRY_ZONE; import static org.apache.dubbo.common.constants.RegistryConstants.REGISTRY_ZONE_FORCE; import static org.apache.dubbo.common.constants.RegistryConstants.ZONE_KEY; +import static org.apache.dubbo.config.RegistryConfig.PREFER_REGISTRY_KEY; /** * When there're more than one registry for subscription. @@ -58,9 +58,7 @@ public class ZoneAwareClusterInvoker extends AbstractClusterInvoker { private static final Logger logger = LoggerFactory.getLogger(ZoneAwareClusterInvoker.class); - - private static final String PREFER_REGISTRY_KEY = REGISTRY_KEY + "." + PREFERRED_KEY; - + private static final String PREFER_REGISTRY_WITH_ZONE_KEY = REGISTRY_KEY + "." + ZONE_KEY; private final LoadBalance loadBalanceAmongRegistries = ExtensionLoader.getExtensionLoader(LoadBalance.class).getExtension(LOADBALANCE_AMONG_REGISTRIES); diff --git a/dubbo-common/src/main/java/org/apache/dubbo/config/RegistryConfig.java b/dubbo-common/src/main/java/org/apache/dubbo/config/RegistryConfig.java index 076759e2ec6..d822b057226 100644 --- a/dubbo-common/src/main/java/org/apache/dubbo/config/RegistryConfig.java +++ b/dubbo-common/src/main/java/org/apache/dubbo/config/RegistryConfig.java @@ -25,6 +25,8 @@ import static org.apache.dubbo.common.constants.CommonConstants.EXTRA_KEYS_KEY; import static org.apache.dubbo.common.constants.CommonConstants.SHUTDOWN_WAIT_KEY; +import static org.apache.dubbo.common.constants.CommonConstants.PREFERRED_KEY; +import static org.apache.dubbo.common.constants.RegistryConstants.REGISTRY_KEY; import static org.apache.dubbo.common.constants.RemotingConstants.BACKUP_KEY; import static org.apache.dubbo.common.utils.PojoUtils.updatePropertyIfAbsent; import static org.apache.dubbo.config.Constants.REGISTRIES_SUFFIX; @@ -37,6 +39,7 @@ public class RegistryConfig extends AbstractConfig { public static final String NO_AVAILABLE = "N/A"; + public static final String PREFER_REGISTRY_KEY = REGISTRY_KEY + "." + PREFERRED_KEY; private static final long serialVersionUID = 5508512956753757169L; /** @@ -486,6 +489,7 @@ public void setAccepts(String accepts) { this.accepts = accepts; } + @Parameter(key = PREFER_REGISTRY_KEY) public Boolean getPreferred() { return preferred; } diff --git a/dubbo-config/dubbo-config-api/src/test/java/org/apache/dubbo/config/RegistryConfigTest.java b/dubbo-config/dubbo-config-api/src/test/java/org/apache/dubbo/config/RegistryConfigTest.java index fb74b1ea48b..7e8fe8d8b2e 100644 --- a/dubbo-config/dubbo-config-api/src/test/java/org/apache/dubbo/config/RegistryConfigTest.java +++ b/dubbo-config/dubbo-config-api/src/test/java/org/apache/dubbo/config/RegistryConfigTest.java @@ -17,6 +17,9 @@ package org.apache.dubbo.config; +import org.apache.dubbo.common.URL; +import org.apache.dubbo.common.utils.UrlUtils; + import org.junit.jupiter.api.Assertions; import org.junit.jupiter.api.Test; @@ -26,6 +29,7 @@ import static org.apache.dubbo.common.constants.CommonConstants.SHUTDOWN_WAIT_KEY; import static org.apache.dubbo.config.Constants.SHUTDOWN_TIMEOUT_KEY; +import static org.apache.dubbo.config.RegistryConfig.PREFER_REGISTRY_KEY; import static org.hamcrest.CoreMatchers.is; import static org.hamcrest.MatcherAssert.assertThat; import static org.hamcrest.Matchers.equalTo; @@ -186,5 +190,28 @@ public void testEquals() throws Exception { registry2.setAddress("zookeeper://127.0.0.1:2183"); Assertions.assertNotEquals(registry1, registry2); } + + @Test + public void testPreferredWithTrueValue() { + RegistryConfig registry = new RegistryConfig(); + registry.setPreferred(true); + Map map = new HashMap<>(); + // process Parameter annotation + AbstractConfig.appendParameters(map, registry); + // Simulate the check that ZoneAwareClusterInvoker#doInvoke do + URL url = UrlUtils.parseURL("zookeeper://127.0.0.1:2181", map); + Assertions.assertTrue(url.getParameter(PREFER_REGISTRY_KEY, false)); + } + @Test + public void testPreferredWithFalseValue() { + RegistryConfig registry = new RegistryConfig(); + registry.setPreferred(false); + Map map = new HashMap<>(); + // Process Parameter annotation + AbstractConfig.appendParameters(map, registry); + // Simulate the check that ZoneAwareClusterInvoker#doInvoke do + URL url = UrlUtils.parseURL("zookeeper://127.0.0.1:2181", map); + Assertions.assertFalse(url.getParameter(PREFER_REGISTRY_KEY, false)); + } }