Skip to content

Commit bfb6077

Browse files
cvictorychickenlj
authored andcommitted
[compatible] Registry compatibility #3882 (#4015)
Fixes #3882
1 parent 8646277 commit bfb6077

File tree

6 files changed

+497
-5
lines changed

6 files changed

+497
-5
lines changed
Lines changed: 110 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,110 @@
1+
/*
2+
* Licensed to the Apache Software Foundation (ASF) under one or more
3+
* contributor license agreements. See the NOTICE file distributed with
4+
* this work for additional information regarding copyright ownership.
5+
* The ASF licenses this file to You under the Apache License, Version 2.0
6+
* (the "License"); you may not use this file except in compliance with
7+
* the License. You may obtain a copy of the License at
8+
*
9+
* http://www.apache.org/licenses/LICENSE-2.0
10+
*
11+
* Unless required by applicable law or agreed to in writing, software
12+
* distributed under the License is distributed on an "AS IS" BASIS,
13+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
* See the License for the specific language governing permissions and
15+
* limitations under the License.
16+
*/
17+
package com.alibaba.dubbo.common.utils;
18+
19+
import com.alibaba.dubbo.common.URL;
20+
21+
import java.util.List;
22+
import java.util.Map;
23+
import java.util.Set;
24+
import java.util.stream.Collectors;
25+
26+
/**
27+
* 2019-04-17
28+
*/
29+
@Deprecated
30+
public class UrlUtils {
31+
32+
public static URL parseURL(String address, Map<String, String> defaults) {
33+
return new URL(org.apache.dubbo.common.utils.UrlUtils.parseURL(address, defaults));
34+
}
35+
36+
public static List<URL> parseURLs(String address, Map<String, String> defaults) {
37+
return org.apache.dubbo.common.utils.UrlUtils.parseURLs(address, defaults).stream().map(e -> new URL(e)).collect(Collectors.toList());
38+
}
39+
40+
public static Map<String, Map<String, String>> convertRegister(Map<String, Map<String, String>> register) {
41+
return org.apache.dubbo.common.utils.UrlUtils.convertRegister(register);
42+
}
43+
44+
public static Map<String, String> convertSubscribe(Map<String, String> subscribe) {
45+
return org.apache.dubbo.common.utils.UrlUtils.convertSubscribe(subscribe);
46+
}
47+
48+
public static Map<String, Map<String, String>> revertRegister(Map<String, Map<String, String>> register) {
49+
return org.apache.dubbo.common.utils.UrlUtils.revertRegister(register);
50+
}
51+
52+
public static Map<String, String> revertSubscribe(Map<String, String> subscribe) {
53+
return org.apache.dubbo.common.utils.UrlUtils.revertSubscribe(subscribe);
54+
}
55+
56+
public static Map<String, Map<String, String>> revertNotify(Map<String, Map<String, String>> notify) {
57+
return org.apache.dubbo.common.utils.UrlUtils.revertNotify(notify);
58+
}
59+
60+
//compatible for dubbo-2.0.0
61+
public static List<String> revertForbid(List<String> forbid, Set<URL> subscribed) {
62+
Set<org.apache.dubbo.common.URL> urls = subscribed.stream().map(e -> e.getOriginalURL()).collect(Collectors.toSet());
63+
return org.apache.dubbo.common.utils.UrlUtils.revertForbid(forbid, urls);
64+
}
65+
66+
public static URL getEmptyUrl(String service, String category) {
67+
return new URL(org.apache.dubbo.common.utils.UrlUtils.getEmptyUrl(service, category));
68+
}
69+
70+
public static boolean isMatchCategory(String category, String categories) {
71+
return org.apache.dubbo.common.utils.UrlUtils.isMatchCategory(category, categories);
72+
}
73+
74+
public static boolean isMatch(URL consumerUrl, URL providerUrl) {
75+
return org.apache.dubbo.common.utils.UrlUtils.isMatch(consumerUrl.getOriginalURL(), providerUrl.getOriginalURL());
76+
}
77+
78+
public static boolean isMatchGlobPattern(String pattern, String value, URL param) {
79+
return org.apache.dubbo.common.utils.UrlUtils.isMatchGlobPattern(pattern, value, param.getOriginalURL());
80+
}
81+
82+
public static boolean isMatchGlobPattern(String pattern, String value) {
83+
return org.apache.dubbo.common.utils.UrlUtils.isMatchGlobPattern(pattern, value);
84+
}
85+
86+
public static boolean isServiceKeyMatch(URL pattern, URL value) {
87+
return org.apache.dubbo.common.utils.UrlUtils.isServiceKeyMatch(pattern.getOriginalURL(), value.getOriginalURL());
88+
}
89+
90+
91+
public static boolean isConfigurator(URL url) {
92+
return org.apache.dubbo.common.utils.UrlUtils.isConfigurator(url.getOriginalURL());
93+
}
94+
95+
public static boolean isRoute(URL url) {
96+
return org.apache.dubbo.common.utils.UrlUtils.isRoute(url.getOriginalURL());
97+
}
98+
99+
public static boolean isProvider(URL url) {
100+
return org.apache.dubbo.common.utils.UrlUtils.isProvider(url.getOriginalURL());
101+
}
102+
103+
public static int getHeartbeat(URL url) {
104+
return org.apache.dubbo.common.utils.UrlUtils.getHeartbeat(url.getOriginalURL());
105+
}
106+
107+
public static int getIdleTimeout(URL url) {
108+
return org.apache.dubbo.common.utils.UrlUtils.getIdleTimeout(url.getOriginalURL());
109+
}
110+
}

dubbo-compatible/src/main/java/com/alibaba/dubbo/registry/NotifyListener.java

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,4 +42,20 @@ public void notify(List<URL> urls) {
4242
}
4343
}
4444
}
45+
46+
class ReverseCompatibleNotifyListener implements org.apache.dubbo.registry.NotifyListener {
47+
48+
private NotifyListener listener;
49+
50+
public ReverseCompatibleNotifyListener(NotifyListener listener) {
51+
this.listener = listener;
52+
}
53+
54+
@Override
55+
public void notify(List<org.apache.dubbo.common.URL> urls) {
56+
if (listener != null) {
57+
listener.notify(urls.stream().map(url -> new URL(url)).collect(Collectors.toList()));
58+
}
59+
}
60+
}
4561
}
Lines changed: 145 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,145 @@
1+
/*
2+
* Licensed to the Apache Software Foundation (ASF) under one or more
3+
* contributor license agreements. See the NOTICE file distributed with
4+
* this work for additional information regarding copyright ownership.
5+
* The ASF licenses this file to You under the Apache License, Version 2.0
6+
* (the "License"); you may not use this file except in compliance with
7+
* the License. You may obtain a copy of the License at
8+
*
9+
* http://www.apache.org/licenses/LICENSE-2.0
10+
*
11+
* Unless required by applicable law or agreed to in writing, software
12+
* distributed under the License is distributed on an "AS IS" BASIS,
13+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
* See the License for the specific language governing permissions and
15+
* limitations under the License.
16+
*/
17+
package com.alibaba.dubbo.registry.support;
18+
19+
import org.apache.dubbo.common.URL;
20+
import org.apache.dubbo.registry.NotifyListener;
21+
import org.apache.dubbo.registry.Registry;
22+
23+
import java.util.List;
24+
import java.util.Map;
25+
import java.util.Set;
26+
import java.util.stream.Collectors;
27+
28+
/**
29+
* 2019-04-16
30+
*/
31+
@Deprecated
32+
public abstract class AbstractRegistry implements Registry {
33+
34+
private CompatibleAbstractRegistry abstractRegistry;
35+
36+
public AbstractRegistry(com.alibaba.dubbo.common.URL url) {
37+
abstractRegistry = new CompatibleAbstractRegistry(url.getOriginalURL());
38+
}
39+
40+
@Override
41+
public com.alibaba.dubbo.common.URL getUrl() {
42+
return new com.alibaba.dubbo.common.URL(abstractRegistry.getUrl());
43+
}
44+
45+
protected void setUrl(com.alibaba.dubbo.common.URL url) {
46+
abstractRegistry.setUrl(url.getOriginalURL());
47+
}
48+
49+
public Set<com.alibaba.dubbo.common.URL> getRegistered() {
50+
return abstractRegistry.getRegistered().stream().map(url -> new com.alibaba.dubbo.common.URL(url)).collect(Collectors.toSet());
51+
}
52+
53+
public Map<com.alibaba.dubbo.common.URL, Set<com.alibaba.dubbo.registry.NotifyListener>> getSubscribed() {
54+
return abstractRegistry.getSubscribed().entrySet()
55+
.stream()
56+
.collect(Collectors.toMap(entry -> new com.alibaba.dubbo.common.URL(entry.getKey()),
57+
entry -> convertToNotifyListeners(entry.getValue())));
58+
}
59+
60+
public Map<com.alibaba.dubbo.common.URL, Map<String, List<com.alibaba.dubbo.common.URL>>> getNotified() {
61+
return abstractRegistry.getNotified().entrySet().stream()
62+
.collect(Collectors.toMap(entry -> new com.alibaba.dubbo.common.URL(entry.getKey()),
63+
entry -> {
64+
return entry.getValue().entrySet()
65+
.stream()
66+
.collect(Collectors.toMap(e -> e.getKey(), e -> {
67+
return e.getValue().stream().map(url -> new com.alibaba.dubbo.common.URL(url)).collect(Collectors.toList());
68+
}));
69+
}));
70+
}
71+
72+
73+
public List<com.alibaba.dubbo.common.URL> getCacheUrls(com.alibaba.dubbo.common.URL url) {
74+
return abstractRegistry.lookup(url.getOriginalURL()).stream().map(tmpUrl -> new com.alibaba.dubbo.common.URL(tmpUrl)).collect(Collectors.toList());
75+
}
76+
77+
public List<com.alibaba.dubbo.common.URL> lookup(com.alibaba.dubbo.common.URL url) {
78+
return abstractRegistry.lookup(url.getOriginalURL()).stream().map(tmpUrl -> new com.alibaba.dubbo.common.URL(tmpUrl)).collect(Collectors.toList());
79+
}
80+
81+
protected void notify(com.alibaba.dubbo.common.URL url, com.alibaba.dubbo.registry.NotifyListener listener, List<com.alibaba.dubbo.common.URL> urls) {
82+
abstractRegistry.notify(url.getOriginalURL(), new com.alibaba.dubbo.registry.NotifyListener.ReverseCompatibleNotifyListener(listener), urls.stream().map(tmpUrl -> tmpUrl.getOriginalURL()).collect(Collectors.toList()));
83+
}
84+
85+
public void register(com.alibaba.dubbo.common.URL url) {
86+
abstractRegistry.register(url.getOriginalURL());
87+
}
88+
89+
public void unregister(com.alibaba.dubbo.common.URL url) {
90+
abstractRegistry.unregister(url.getOriginalURL());
91+
}
92+
93+
public void subscribe(com.alibaba.dubbo.common.URL url, com.alibaba.dubbo.registry.NotifyListener listener) {
94+
abstractRegistry.subscribe(url.getOriginalURL(), new com.alibaba.dubbo.registry.NotifyListener.ReverseCompatibleNotifyListener(listener));
95+
}
96+
97+
public void unsubscribe(com.alibaba.dubbo.common.URL url, com.alibaba.dubbo.registry.NotifyListener listener) {
98+
abstractRegistry.unsubscribe(url.getOriginalURL(), new com.alibaba.dubbo.registry.NotifyListener.ReverseCompatibleNotifyListener(listener));
99+
}
100+
101+
102+
@Override
103+
public void register(URL url) {
104+
this.register(new com.alibaba.dubbo.common.URL(url));
105+
}
106+
107+
@Override
108+
public void unregister(URL url) {
109+
this.unregister(new com.alibaba.dubbo.common.URL(url));
110+
}
111+
112+
@Override
113+
public void subscribe(URL url, NotifyListener listener) {
114+
this.subscribe(new com.alibaba.dubbo.common.URL(url), new com.alibaba.dubbo.registry.NotifyListener.CompatibleNotifyListener(listener));
115+
}
116+
117+
@Override
118+
public void unsubscribe(URL url, NotifyListener listener) {
119+
this.unsubscribe(new com.alibaba.dubbo.common.URL(url), new com.alibaba.dubbo.registry.NotifyListener.CompatibleNotifyListener(listener));
120+
}
121+
122+
final Set<com.alibaba.dubbo.registry.NotifyListener> convertToNotifyListeners(Set<NotifyListener> notifyListeners) {
123+
return notifyListeners.stream().map(listener -> new com.alibaba.dubbo.registry.NotifyListener.CompatibleNotifyListener(listener)).collect(Collectors.toSet());
124+
}
125+
126+
127+
static class CompatibleAbstractRegistry extends org.apache.dubbo.registry.support.AbstractRegistry {
128+
public CompatibleAbstractRegistry(URL url) {
129+
super(url);
130+
}
131+
132+
@Override
133+
public boolean isAvailable() {
134+
return false;
135+
}
136+
137+
public void notify(URL url, NotifyListener listener, List<URL> urls) {
138+
super.notify(url, listener, urls);
139+
}
140+
141+
public void setUrl(URL url) {
142+
super.setUrl(url);
143+
}
144+
}
145+
}
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
/*
2+
* Licensed to the Apache Software Foundation (ASF) under one or more
3+
* contributor license agreements. See the NOTICE file distributed with
4+
* this work for additional information regarding copyright ownership.
5+
* The ASF licenses this file to You under the Apache License, Version 2.0
6+
* (the "License"); you may not use this file except in compliance with
7+
* the License. You may obtain a copy of the License at
8+
*
9+
* http://www.apache.org/licenses/LICENSE-2.0
10+
*
11+
* Unless required by applicable law or agreed to in writing, software
12+
* distributed under the License is distributed on an "AS IS" BASIS,
13+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
* See the License for the specific language governing permissions and
15+
* limitations under the License.
16+
*/
17+
package com.alibaba.dubbo.registry.support;
18+
19+
import org.apache.dubbo.common.URL;
20+
import org.apache.dubbo.registry.Registry;
21+
22+
/**
23+
* 2019-04-16
24+
*/
25+
@Deprecated
26+
public abstract class AbstractRegistryFactory extends org.apache.dubbo.registry.support.AbstractRegistryFactory {
27+
28+
29+
protected abstract com.alibaba.dubbo.registry.Registry createRegistry(com.alibaba.dubbo.common.URL url);
30+
31+
protected Registry createRegistry(URL url) {
32+
return createRegistry(new com.alibaba.dubbo.common.URL(url));
33+
}
34+
}

0 commit comments

Comments
 (0)