This is an automated email from the ASF dual-hosted git repository. liujun pushed a commit to branch cloud-native in repository https://gitbox.apache.org/repos/asf/dubbo.git
commit 591e45a36e597d12a7fa09ca5687f00af035c058 Author: ken.lj <ken.lj...@gmail.com> AuthorDate: Mon Aug 12 19:44:38 2019 +0800 support old registry model and new registry model --- .../registry/support/AbstractRegistryFactory.java | 47 +++++++++++++++------- 1 file changed, 32 insertions(+), 15 deletions(-) diff --git a/dubbo-registry/dubbo-registry-api/src/main/java/org/apache/dubbo/registry/support/AbstractRegistryFactory.java b/dubbo-registry/dubbo-registry-api/src/main/java/org/apache/dubbo/registry/support/AbstractRegistryFactory.java index 0458ada..70e7597 100644 --- a/dubbo-registry/dubbo-registry-api/src/main/java/org/apache/dubbo/registry/support/AbstractRegistryFactory.java +++ b/dubbo-registry/dubbo-registry-api/src/main/java/org/apache/dubbo/registry/support/AbstractRegistryFactory.java @@ -47,9 +47,12 @@ public abstract class AbstractRegistryFactory implements RegistryFactory { // The lock for the acquisition process of the registry private static final ReentrantLock LOCK = new ReentrantLock(); - // Registry Collection Map<RegistryAddress, Registry> + // Registry Collection Map<RegistryAddress, ServiceOrientedRegistry> private static final Map<String, Registry> REGISTRIES = new HashMap<>(); + // Registry Collection Map<RegistryAddress, Registry> + private static final Map<String, Registry> COMPATIBLE_REGISTRIES = new HashMap<>(); + /** * Get all registries * @@ -94,28 +97,42 @@ public abstract class AbstractRegistryFactory implements RegistryFactory { // Lock the registry access process to ensure a single instance of the registry LOCK.lock(); try { - Registry registry = REGISTRIES.get(key); - if (registry != null) { - return registry; - } - // creates an instance of ServiceOrientedRegistry if supported - // since 2.7.4 - registry = ServiceOrientedRegistry.create(url); - if (registry == null) { // If not supported, create the default Registry + boolean isServiceOriented = ServiceOrientedRegistry.supports(url); + if (isServiceOriented) { + Registry registry = REGISTRIES.get(key); + if (registry != null) { + return registry; + } //create registry by spi/ioc - registry = createRegistry(url); - } - if (registry == null) { - throw new IllegalStateException("Can not create registry " + url); + registry = ServiceOrientedRegistry.create(url); + if (registry == null) { + throw new IllegalStateException("Can not service-oriented registry " + url); + } + REGISTRIES.put(key, registry); + return registry; + } else { + return getCompatibleRegistry(key, url); } - REGISTRIES.put(key, registry); - return registry; } finally { // Release the lock LOCK.unlock(); } } + private Registry getCompatibleRegistry(String key, URL url) { + Registry registry = COMPATIBLE_REGISTRIES.get(key); + if (registry != null) { + return registry; + } + //create registry by spi/ioc + registry = createRegistry(url); + if (registry == null) { + throw new IllegalStateException("Can not create registry " + url); + } + COMPATIBLE_REGISTRIES.put(key, registry); + return registry; + } + protected abstract Registry createRegistry(URL url); }