From c0da2ee8c5c98d9c5e23c5288c6a6a3287f3537e Mon Sep 17 00:00:00 2001 From: cvictory Date: Wed, 17 Apr 2019 19:08:38 +0800 Subject: [PATCH] compatible with Regigistry. #3882 --- .../dubbo/common/utils/StringUtils.java | 2 +- .../alibaba/dubbo/common/utils/UrlUtils.java | 93 ++++++++++ .../dubbo/registry/NotifyListener.java | 16 ++ .../registry/support/AbstractRegistry.java | 129 +++++++++++++ .../support/AbstractRegistryFactory.java | 18 ++ .../registry/support/FailbackRegistry.java | 171 ++++++++++++++++++ .../registry/support/FailbackRegistry.java | 40 +++- 7 files changed, 463 insertions(+), 6 deletions(-) create mode 100644 dubbo-compatible/src/main/java/com/alibaba/dubbo/common/utils/UrlUtils.java create mode 100644 dubbo-compatible/src/main/java/com/alibaba/dubbo/registry/support/AbstractRegistry.java create mode 100644 dubbo-compatible/src/main/java/com/alibaba/dubbo/registry/support/AbstractRegistryFactory.java create mode 100644 dubbo-compatible/src/main/java/com/alibaba/dubbo/registry/support/FailbackRegistry.java diff --git a/dubbo-common/src/main/java/org/apache/dubbo/common/utils/StringUtils.java b/dubbo-common/src/main/java/org/apache/dubbo/common/utils/StringUtils.java index a351346ef1a..a3d59c3960b 100644 --- a/dubbo-common/src/main/java/org/apache/dubbo/common/utils/StringUtils.java +++ b/dubbo-common/src/main/java/org/apache/dubbo/common/utils/StringUtils.java @@ -48,7 +48,7 @@ public final class StringUtils { private static final Pattern INT_PATTERN = Pattern.compile("^\\d+$"); private static final int PAD_LIMIT = 8192; - private StringUtils() { + protected StringUtils() { } /** diff --git a/dubbo-compatible/src/main/java/com/alibaba/dubbo/common/utils/UrlUtils.java b/dubbo-compatible/src/main/java/com/alibaba/dubbo/common/utils/UrlUtils.java new file mode 100644 index 00000000000..33502614f79 --- /dev/null +++ b/dubbo-compatible/src/main/java/com/alibaba/dubbo/common/utils/UrlUtils.java @@ -0,0 +1,93 @@ +package com.alibaba.dubbo.common.utils; + +import com.alibaba.dubbo.common.URL; + +import java.util.List; +import java.util.Map; +import java.util.Set; +import java.util.stream.Collectors; + +/** + * @author cvictory ON 2019-04-17 + */ +public class UrlUtils { + + public static URL parseURL(String address, Map defaults) { + return new URL(UrlUtils.parseURL(address, defaults)); + } + + public static List parseURLs(String address, Map defaults) { + return UrlUtils.parseURLs(address, defaults).stream().map(e -> new URL(e)).collect(Collectors.toList()); + } + + public static Map> convertRegister(Map> register) { + return org.apache.dubbo.common.utils.UrlUtils.convertRegister(register); + } + + public static Map convertSubscribe(Map subscribe) { + return org.apache.dubbo.common.utils.UrlUtils.convertSubscribe(subscribe); + } + + public static Map> revertRegister(Map> register) { + return org.apache.dubbo.common.utils.UrlUtils.revertRegister(register); + } + + public static Map revertSubscribe(Map subscribe) { + return org.apache.dubbo.common.utils.UrlUtils.revertSubscribe(subscribe); + } + + public static Map> revertNotify(Map> notify) { + return org.apache.dubbo.common.utils.UrlUtils.revertNotify(notify); + } + + //compatible for dubbo-2.0.0 + public static List revertForbid(List forbid, Set subscribed) { + Set urls = subscribed.stream().map(e -> e.getOriginalURL()).collect(Collectors.toSet()); + return org.apache.dubbo.common.utils.UrlUtils.revertForbid(forbid, urls); + } + + public static URL getEmptyUrl(String service, String category) { + return new URL(org.apache.dubbo.common.utils.UrlUtils.getEmptyUrl(service, category)); + } + + public static boolean isMatchCategory(String category, String categories) { + return org.apache.dubbo.common.utils.UrlUtils.isMatchCategory(category, categories); + } + + public static boolean isMatch(URL consumerUrl, URL providerUrl) { + return org.apache.dubbo.common.utils.UrlUtils.isMatch(consumerUrl.getOriginalURL(), providerUrl.getOriginalURL()); + } + + public static boolean isMatchGlobPattern(String pattern, String value, URL param) { + return org.apache.dubbo.common.utils.UrlUtils.isMatchGlobPattern(pattern, value, param.getOriginalURL()); + } + + public static boolean isMatchGlobPattern(String pattern, String value) { + return org.apache.dubbo.common.utils.UrlUtils.isMatchGlobPattern(pattern, value); + } + + public static boolean isServiceKeyMatch(URL pattern, URL value) { + return org.apache.dubbo.common.utils.UrlUtils.isServiceKeyMatch(pattern.getOriginalURL(), value.getOriginalURL()); + } + + + public static boolean isConfigurator(URL url) { + return org.apache.dubbo.common.utils.UrlUtils.isConfigurator(url.getOriginalURL()); + } + + public static boolean isRoute(URL url) { + return org.apache.dubbo.common.utils.UrlUtils.isRoute(url.getOriginalURL()); + } + + public static boolean isProvider(URL url) { + return org.apache.dubbo.common.utils.UrlUtils.isProvider(url.getOriginalURL()); + } + + public static int getHeartbeat(URL url) { + return org.apache.dubbo.common.utils.UrlUtils.getHeartbeat(url.getOriginalURL()); + } + + public static int getIdleTimeout(URL url) { + return org.apache.dubbo.common.utils.UrlUtils.getIdleTimeout(url.getOriginalURL()); + } +} diff --git a/dubbo-compatible/src/main/java/com/alibaba/dubbo/registry/NotifyListener.java b/dubbo-compatible/src/main/java/com/alibaba/dubbo/registry/NotifyListener.java index d7e82b5e144..633dc1b656e 100644 --- a/dubbo-compatible/src/main/java/com/alibaba/dubbo/registry/NotifyListener.java +++ b/dubbo-compatible/src/main/java/com/alibaba/dubbo/registry/NotifyListener.java @@ -42,4 +42,20 @@ public void notify(List urls) { } } } + + class ReverseCompatibleNotifyListener implements org.apache.dubbo.registry.NotifyListener { + + private NotifyListener listener; + + public ReverseCompatibleNotifyListener(NotifyListener listener) { + this.listener = listener; + } + + @Override + public void notify(List urls) { + if (listener != null) { + listener.notify(urls.stream().map(url -> new URL(url)).collect(Collectors.toList())); + } + } + } } diff --git a/dubbo-compatible/src/main/java/com/alibaba/dubbo/registry/support/AbstractRegistry.java b/dubbo-compatible/src/main/java/com/alibaba/dubbo/registry/support/AbstractRegistry.java new file mode 100644 index 00000000000..094d4e45072 --- /dev/null +++ b/dubbo-compatible/src/main/java/com/alibaba/dubbo/registry/support/AbstractRegistry.java @@ -0,0 +1,129 @@ +package com.alibaba.dubbo.registry.support; + +import org.apache.dubbo.common.URL; +import org.apache.dubbo.registry.NotifyListener; +import org.apache.dubbo.registry.Registry; + +import java.util.List; +import java.util.Map; +import java.util.Set; +import java.util.stream.Collectors; + +/** + * 2019-04-16 + */ +@Deprecated +public abstract class AbstractRegistry implements Registry { + + private CompatibleAbstractRegistry abstractRegistry; + + public AbstractRegistry(com.alibaba.dubbo.common.URL url) { + abstractRegistry = new CompatibleAbstractRegistry(url.getOriginalURL()); + } + + @Override + public com.alibaba.dubbo.common.URL getUrl() { + return new com.alibaba.dubbo.common.URL(abstractRegistry.getUrl()); + } + + protected void setUrl(com.alibaba.dubbo.common.URL url) { + abstractRegistry.setUrl(url.getOriginalURL()); + } + + public Set getRegistered() { + return abstractRegistry.getRegistered().stream().map(url -> new com.alibaba.dubbo.common.URL(url)).collect(Collectors.toSet()); + } + + public Map> getSubscribed() { + return abstractRegistry.getSubscribed().entrySet() + .stream() + .collect(Collectors.toMap(entry -> new com.alibaba.dubbo.common.URL(entry.getKey()), + entry -> convertToNotifyListeners(entry.getValue()))); + } + + public Map>> getNotified() { + return abstractRegistry.getNotified().entrySet().stream() + .collect(Collectors.toMap(entry -> new com.alibaba.dubbo.common.URL(entry.getKey()), + entry -> { + return entry.getValue().entrySet() + .stream() + .collect(Collectors.toMap(e -> e.getKey(), e -> { + return e.getValue().stream().map(url -> new com.alibaba.dubbo.common.URL(url)).collect(Collectors.toList()); + })); + })); + } + + + public List getCacheUrls(com.alibaba.dubbo.common.URL url) { + return abstractRegistry.lookup(url.getOriginalURL()).stream().map(tmpUrl -> new com.alibaba.dubbo.common.URL(tmpUrl)).collect(Collectors.toList()); + } + + public List lookup(com.alibaba.dubbo.common.URL url) { + return abstractRegistry.lookup(url.getOriginalURL()).stream().map(tmpUrl -> new com.alibaba.dubbo.common.URL(tmpUrl)).collect(Collectors.toList()); + } + + protected void notify(com.alibaba.dubbo.common.URL url, com.alibaba.dubbo.registry.NotifyListener listener, List urls) { + abstractRegistry.notify(url.getOriginalURL(), new com.alibaba.dubbo.registry.NotifyListener.ReverseCompatibleNotifyListener(listener), urls.stream().map(tmpUrl -> tmpUrl.getOriginalURL()).collect(Collectors.toList())); + } + + public void register(com.alibaba.dubbo.common.URL url) { + abstractRegistry.register(url.getOriginalURL()); + } + + public void unregister(com.alibaba.dubbo.common.URL url) { + abstractRegistry.unregister(url.getOriginalURL()); + } + + public void subscribe(com.alibaba.dubbo.common.URL url, com.alibaba.dubbo.registry.NotifyListener listener) { + abstractRegistry.subscribe(url.getOriginalURL(), new com.alibaba.dubbo.registry.NotifyListener.ReverseCompatibleNotifyListener(listener)); + } + + public void unsubscribe(com.alibaba.dubbo.common.URL url, com.alibaba.dubbo.registry.NotifyListener listener) { + abstractRegistry.unsubscribe(url.getOriginalURL(), new com.alibaba.dubbo.registry.NotifyListener.ReverseCompatibleNotifyListener(listener)); + } + + + @Override + public void register(URL url) { + this.register(new com.alibaba.dubbo.common.URL(url)); + } + + @Override + public void unregister(URL url) { + this.unregister(new com.alibaba.dubbo.common.URL(url)); + } + + @Override + public void subscribe(URL url, NotifyListener listener) { + this.subscribe(new com.alibaba.dubbo.common.URL(url), new com.alibaba.dubbo.registry.NotifyListener.CompatibleNotifyListener(listener)); + } + + @Override + public void unsubscribe(URL url, NotifyListener listener) { + this.unsubscribe(new com.alibaba.dubbo.common.URL(url), new com.alibaba.dubbo.registry.NotifyListener.CompatibleNotifyListener(listener)); + } + + final Set convertToNotifyListeners(Set notifyListeners) { + return notifyListeners.stream().map(listener -> new com.alibaba.dubbo.registry.NotifyListener.CompatibleNotifyListener(listener)).collect(Collectors.toSet()); + } + + + static class CompatibleAbstractRegistry extends org.apache.dubbo.registry.support.AbstractRegistry { + public CompatibleAbstractRegistry(URL url) { + super(url); + } + + @Override + public boolean isAvailable() { + return false; + } + + public void notify(URL url, NotifyListener listener, List urls) { + super.notify(url, listener, urls); + } + + public void setUrl(URL url) { + super.setUrl(url); + } + } +} diff --git a/dubbo-compatible/src/main/java/com/alibaba/dubbo/registry/support/AbstractRegistryFactory.java b/dubbo-compatible/src/main/java/com/alibaba/dubbo/registry/support/AbstractRegistryFactory.java new file mode 100644 index 00000000000..bcf3e80ced7 --- /dev/null +++ b/dubbo-compatible/src/main/java/com/alibaba/dubbo/registry/support/AbstractRegistryFactory.java @@ -0,0 +1,18 @@ +package com.alibaba.dubbo.registry.support; + +import org.apache.dubbo.common.URL; +import org.apache.dubbo.registry.Registry; + +/** + * 2019-04-16 + */ +@Deprecated +public abstract class AbstractRegistryFactory extends org.apache.dubbo.registry.support.AbstractRegistryFactory { + + + protected abstract com.alibaba.dubbo.registry.Registry createRegistry(com.alibaba.dubbo.common.URL url); + + protected Registry createRegistry(URL url) { + return createRegistry(new com.alibaba.dubbo.common.URL(url)); + } +} diff --git a/dubbo-compatible/src/main/java/com/alibaba/dubbo/registry/support/FailbackRegistry.java b/dubbo-compatible/src/main/java/com/alibaba/dubbo/registry/support/FailbackRegistry.java new file mode 100644 index 00000000000..ee6f599d2d6 --- /dev/null +++ b/dubbo-compatible/src/main/java/com/alibaba/dubbo/registry/support/FailbackRegistry.java @@ -0,0 +1,171 @@ +package com.alibaba.dubbo.registry.support; + +import com.alibaba.dubbo.common.URL; +import com.alibaba.dubbo.registry.NotifyListener; +import com.alibaba.dubbo.registry.Registry; + +import java.util.List; +import java.util.stream.Collectors; + +/** + * @author cvictory ON 2019-04-17 + */ +@Deprecated +public abstract class FailbackRegistry implements org.apache.dubbo.registry.Registry, Registry { + + private CompatibleFailbackRegistry failbackRegistry; + + public FailbackRegistry(URL url) { + failbackRegistry = new CompatibleFailbackRegistry(url.getOriginalURL(), this); + } + + public void removeFailedRegisteredTask(URL url) { + failbackRegistry.removeFailedRegisteredTask(url.getOriginalURL()); + } + + public void removeFailedUnregisteredTask(URL url) { + failbackRegistry.removeFailedUnregisteredTask(url.getOriginalURL()); + } + + public void removeFailedSubscribedTask(URL url, NotifyListener listener) { + failbackRegistry.removeFailedSubscribedTask(url.getOriginalURL(), new NotifyListener.ReverseCompatibleNotifyListener(listener)); + } + + public void removeFailedUnsubscribedTask(URL url, NotifyListener listener) { + failbackRegistry.removeFailedUnsubscribedTask(url.getOriginalURL(), new NotifyListener.ReverseCompatibleNotifyListener(listener)); + } + + public void removeFailedNotifiedTask(URL url, NotifyListener listener) { + failbackRegistry.removeFailedNotifiedTask(url.getOriginalURL(), new NotifyListener.ReverseCompatibleNotifyListener(listener)); + } + + public void register(URL url) { + failbackRegistry.register(url.getOriginalURL()); + } + + public void unregister(URL url) { + failbackRegistry.unregister(url.getOriginalURL()); + } + + public void subscribe(URL url, NotifyListener listener) { + failbackRegistry.subscribe(url.getOriginalURL(), new com.alibaba.dubbo.registry.NotifyListener.ReverseCompatibleNotifyListener(listener)); + } + + public void unsubscribe(URL url, NotifyListener listener) { + failbackRegistry.unsubscribe(url.getOriginalURL(), new com.alibaba.dubbo.registry.NotifyListener.ReverseCompatibleNotifyListener(listener)); + } + + protected void notify(URL url, NotifyListener listener, List urls) { + List urlResult = urls.stream().map(e -> e.getOriginalURL()).collect(Collectors.toList()); + failbackRegistry.notify(url.getOriginalURL(), new com.alibaba.dubbo.registry.NotifyListener.ReverseCompatibleNotifyListener(listener), urlResult); + } + + protected void doNotify(URL url, NotifyListener listener, List urls) { + List urlResult = urls.stream().map(e -> e.getOriginalURL()).collect(Collectors.toList()); + failbackRegistry.doNotify(url.getOriginalURL(), new com.alibaba.dubbo.registry.NotifyListener.ReverseCompatibleNotifyListener(listener), urlResult); + } + + protected void recover() throws Exception { + failbackRegistry.recover(); + } + + public List lookup(URL url) { + return failbackRegistry.lookup(url.getOriginalURL()).stream().map(e -> new URL(e)).collect(Collectors.toList()); + } + + public URL getUrl() { + return new URL(failbackRegistry.getUrl()); + } + + @Override + public void destroy() { + failbackRegistry.destroy(); + } + + // ==== Template method ==== + + public abstract void doRegister(URL url); + + public abstract void doUnregister(URL url); + + public abstract void doSubscribe(URL url, NotifyListener listener); + + public abstract void doUnsubscribe(URL url, NotifyListener listener); + + @Override + public void register(org.apache.dubbo.common.URL url) { + this.register(new URL(url)); + } + + @Override + public void unregister(org.apache.dubbo.common.URL url) { + this.unregister(new URL(url)); + } + + @Override + public void subscribe(org.apache.dubbo.common.URL url, org.apache.dubbo.registry.NotifyListener listener) { + this.subscribe(new URL(url), new NotifyListener.CompatibleNotifyListener(listener)); + } + + @Override + public void unsubscribe(org.apache.dubbo.common.URL url, org.apache.dubbo.registry.NotifyListener listener) { + this.unsubscribe(new URL(url), new NotifyListener.CompatibleNotifyListener(listener)); + } + + @Override + public List lookup(org.apache.dubbo.common.URL url) { + return failbackRegistry.lookup(url); + } + + + static class CompatibleFailbackRegistry extends org.apache.dubbo.registry.support.FailbackRegistry { + + private FailbackRegistry compatibleFailbackRegistry; + + public CompatibleFailbackRegistry(org.apache.dubbo.common.URL url, FailbackRegistry compatibleFailbackRegistry) { + super(url); + this.compatibleFailbackRegistry = compatibleFailbackRegistry; + } + + @Override + public void doRegister(org.apache.dubbo.common.URL url) { + this.compatibleFailbackRegistry.doRegister(new URL(url)); + } + + @Override + public void doUnregister(org.apache.dubbo.common.URL url) { + this.compatibleFailbackRegistry.doUnregister(new URL(url)); + } + + @Override + public void doSubscribe(org.apache.dubbo.common.URL url, org.apache.dubbo.registry.NotifyListener listener) { + this.compatibleFailbackRegistry.doSubscribe(new URL(url), new NotifyListener.CompatibleNotifyListener(listener)); + } + + @Override + public void doUnsubscribe(org.apache.dubbo.common.URL url, org.apache.dubbo.registry.NotifyListener listener) { + this.compatibleFailbackRegistry.doUnsubscribe(new URL(url), new NotifyListener.CompatibleNotifyListener(listener)); + } + + @Override + public void notify(org.apache.dubbo.common.URL url, org.apache.dubbo.registry.NotifyListener listener, List urls) { + super.notify(url, listener, urls); + } + + @Override + public void doNotify(org.apache.dubbo.common.URL url, org.apache.dubbo.registry.NotifyListener listener, List urls) { + super.doNotify(url, listener, urls); + } + + @Override + public boolean isAvailable() { + return false; + } + + @Override + public void recover() throws Exception { + super.recover(); + } + } + +} diff --git a/dubbo-registry/dubbo-registry-api/src/main/java/org/apache/dubbo/registry/support/FailbackRegistry.java b/dubbo-registry/dubbo-registry-api/src/main/java/org/apache/dubbo/registry/support/FailbackRegistry.java index cbf1527231f..0c638364a40 100644 --- a/dubbo-registry/dubbo-registry-api/src/main/java/org/apache/dubbo/registry/support/FailbackRegistry.java +++ b/dubbo-registry/dubbo-registry-api/src/main/java/org/apache/dubbo/registry/support/FailbackRegistry.java @@ -201,23 +201,53 @@ private void removeFailedNotified(URL url, NotifyListener listener) { } } - public ConcurrentMap getFailedRegistered() { + /** + * remove public modifier. + * + * @return + * @since 2.7.2 + */ + ConcurrentMap getFailedRegistered() { return failedRegistered; } - public ConcurrentMap getFailedUnregistered() { + /** + * remove public modifier. + * + * @return + * @since 2.7.2 + */ + ConcurrentMap getFailedUnregistered() { return failedUnregistered; } - public ConcurrentMap getFailedSubscribed() { + /** + * remove public modifier. + * + * @return + * @since 2.7.2 + */ + ConcurrentMap getFailedSubscribed() { return failedSubscribed; } - public ConcurrentMap getFailedUnsubscribed() { + /** + * remove public modifier. + * + * @return + * @since 2.7.2 + */ + ConcurrentMap getFailedUnsubscribed() { return failedUnsubscribed; } - public ConcurrentMap getFailedNotified() { + /** + * remove public modifier. + * + * @return + * @since 2.7.2 + */ + ConcurrentMap getFailedNotified() { return failedNotified; }