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..c8417610b52 --- /dev/null +++ b/dubbo-compatible/src/main/java/com/alibaba/dubbo/common/utils/UrlUtils.java @@ -0,0 +1,110 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You 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 + * + * http://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.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; + +/** + * 2019-04-17 + */ +@Deprecated +public class UrlUtils { + + public static URL parseURL(String address, Map defaults) { + return new URL(org.apache.dubbo.common.utils.UrlUtils.parseURL(address, defaults)); + } + + public static List parseURLs(String address, Map defaults) { + return org.apache.dubbo.common.utils.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..ef1bb3d9e75 --- /dev/null +++ b/dubbo-compatible/src/main/java/com/alibaba/dubbo/registry/support/AbstractRegistry.java @@ -0,0 +1,145 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You 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 + * + * http://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.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..9b0b698e63d --- /dev/null +++ b/dubbo-compatible/src/main/java/com/alibaba/dubbo/registry/support/AbstractRegistryFactory.java @@ -0,0 +1,34 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You 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 + * + * http://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.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..0156ddb4624 --- /dev/null +++ b/dubbo-compatible/src/main/java/com/alibaba/dubbo/registry/support/FailbackRegistry.java @@ -0,0 +1,187 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You 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 + * + * http://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.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; + +/** + * 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 293bf461d28..936b707a8ba 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 @@ -206,23 +206,23 @@ private void removeFailedNotified(URL url, NotifyListener listener) { } } - public ConcurrentMap getFailedRegistered() { + ConcurrentMap getFailedRegistered() { return failedRegistered; } - public ConcurrentMap getFailedUnregistered() { + ConcurrentMap getFailedUnregistered() { return failedUnregistered; } - public ConcurrentMap getFailedSubscribed() { + ConcurrentMap getFailedSubscribed() { return failedSubscribed; } - public ConcurrentMap getFailedUnsubscribed() { + ConcurrentMap getFailedUnsubscribed() { return failedUnsubscribed; } - public ConcurrentMap getFailedNotified() { + ConcurrentMap getFailedNotified() { return failedNotified; }