Skip to content

Commit 2cfc2b3

Browse files
chickenljcvictory
authored andcommitted
Fixes #3478, #3477 and #3445
1 parent 08d5f15 commit 2cfc2b3

File tree

9 files changed

+95
-76
lines changed

9 files changed

+95
-76
lines changed

dubbo-common/src/main/java/org/apache/dubbo/common/URL.java

Lines changed: 22 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1297,7 +1297,7 @@ public InetSocketAddress toInetSocketAddress() {
12971297
}
12981298

12991299
/**
1300-
* The format is '{group}/{interfaceName/path}*{version}'
1300+
* The format is '{group}*{interfaceName}:{version}'
13011301
*
13021302
* @return
13031303
*/
@@ -1307,18 +1307,36 @@ public String getEncodedServiceKey() {
13071307
return serviceKey;
13081308
}
13091309

1310+
/**
1311+
* The format of return value is '{group}/{interfaceName}:{version}'
1312+
* @return
1313+
*/
13101314
public String getServiceKey() {
13111315
String inf = getServiceInterface();
13121316
if (inf == null) {
13131317
return null;
13141318
}
1319+
return buildKey(inf, getParameter(Constants.GROUP_KEY), getParameter(Constants.VERSION_KEY));
1320+
}
1321+
1322+
/**
1323+
* The format of return value is '{group}/{path/interfaceName}:{version}'
1324+
* @return
1325+
*/
1326+
public String getPathKey() {
1327+
String inf = StringUtils.isNotEmpty(path) ? path : getServiceInterface();
1328+
if (inf == null) {
1329+
return null;
1330+
}
1331+
return buildKey(inf, getParameter(Constants.GROUP_KEY), getParameter(Constants.VERSION_KEY));
1332+
}
1333+
1334+
public static String buildKey(String path, String group, String version) {
13151335
StringBuilder buf = new StringBuilder();
1316-
String group = getParameter(Constants.GROUP_KEY);
13171336
if (group != null && group.length() > 0) {
13181337
buf.append(group).append("/");
13191338
}
1320-
buf.append(inf);
1321-
String version = getParameter(Constants.VERSION_KEY);
1339+
buf.append(path);
13221340
if (version != null && version.length() > 0) {
13231341
buf.append(":").append(version);
13241342
}

dubbo-common/src/test/java/org/apache/dubbo/common/URLTest.java

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -687,4 +687,22 @@ public void testDefaultPort() {
687687
Assertions.assertEquals("10.20.153.10:2181", URL.appendDefaultPort("10.20.153.10:0", 2181));
688688
Assertions.assertEquals("10.20.153.10:2181", URL.appendDefaultPort("10.20.153.10", 2181));
689689
}
690+
691+
@Test
692+
public void testGetServiceKey () {
693+
URL url1 = URL.valueOf("10.20.130.230:20880/context/path?interface=org.apache.dubbo.test.interfaceName");
694+
Assertions.assertEquals("org.apache.dubbo.test.interfaceName", url1.getServiceKey());
695+
696+
URL url2 = URL.valueOf("10.20.130.230:20880/org.apache.dubbo.test.interfaceName?interface=org.apache.dubbo.test.interfaceName");
697+
Assertions.assertEquals("org.apache.dubbo.test.interfaceName", url2.getServiceKey());
698+
699+
URL url3 = URL.valueOf("10.20.130.230:20880/org.apache.dubbo.test.interfaceName?interface=org.apache.dubbo.test.interfaceName&group=group1&version=1.0.0");
700+
Assertions.assertEquals("group1/org.apache.dubbo.test.interfaceName:1.0.0", url3.getServiceKey());
701+
702+
URL url4 = URL.valueOf("10.20.130.230:20880/context/path?interface=org.apache.dubbo.test.interfaceName");
703+
Assertions.assertEquals("context/path", url4.getPathKey());
704+
705+
URL url5 = URL.valueOf("10.20.130.230:20880/context/path?interface=org.apache.dubbo.test.interfaceName&group=group1&version=1.0.0");
706+
Assertions.assertEquals("group1/context/path:1.0.0", url5.getPathKey());
707+
}
690708
}

dubbo-config/dubbo-config-api/src/main/java/org/apache/dubbo/config/ReferenceConfig.java

Lines changed: 5 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -22,10 +22,10 @@
2222
import org.apache.dubbo.common.bytecode.Wrapper;
2323
import org.apache.dubbo.common.extension.ExtensionLoader;
2424
import org.apache.dubbo.common.utils.ClassHelper;
25+
import org.apache.dubbo.common.utils.CollectionUtils;
2526
import org.apache.dubbo.common.utils.ConfigUtils;
2627
import org.apache.dubbo.common.utils.NetUtils;
2728
import org.apache.dubbo.common.utils.StringUtils;
28-
import org.apache.dubbo.common.utils.CollectionUtils;
2929
import org.apache.dubbo.config.annotation.Reference;
3030
import org.apache.dubbo.config.context.ConfigManager;
3131
import org.apache.dubbo.config.support.Parameter;
@@ -304,10 +304,11 @@ private void init() {
304304

305305
ref = createProxy(map);
306306

307-
ApplicationModel.initConsumerModel(getUniqueServiceName(), buildConsumerModel(attributes));
307+
String serviceKey = URL.buildKey(interfaceName, group, version);
308+
ApplicationModel.initConsumerModel(serviceKey, buildConsumerModel(serviceKey, attributes));
308309
}
309310

310-
private ConsumerModel buildConsumerModel(Map<String, Object> attributes) {
311+
private ConsumerModel buildConsumerModel(String serviceKey, Map<String, Object> attributes) {
311312
Method[] methods = interfaceClass.getMethods();
312313
Class serviceInterface = interfaceClass;
313314
if (interfaceClass == GenericService.class) {
@@ -318,9 +319,8 @@ private ConsumerModel buildConsumerModel(Map<String, Object> attributes) {
318319
methods = interfaceClass.getMethods();
319320
}
320321
}
321-
return new ConsumerModel(getUniqueServiceName(), serviceInterface, ref, methods, attributes);
322+
return new ConsumerModel(serviceKey, serviceInterface, ref, methods, attributes);
322323
}
323-
324324
@SuppressWarnings({"unchecked", "rawtypes", "deprecation"})
325325
private T createProxy(Map<String, String> map) {
326326
if (shouldJvmRefer(map)) {
@@ -602,19 +602,6 @@ Invoker<?> getInvoker() {
602602
return invoker;
603603
}
604604

605-
@Parameter(excluded = true)
606-
public String getUniqueServiceName() {
607-
StringBuilder buf = new StringBuilder();
608-
if (group != null && group.length() > 0) {
609-
buf.append(group).append("/");
610-
}
611-
buf.append(interfaceName);
612-
if (StringUtils.isNotEmpty(version)) {
613-
buf.append(":").append(version);
614-
}
615-
return buf.toString();
616-
}
617-
618605
@Override
619606
@Parameter(excluded = true)
620607
public String getPrefix() {

dubbo-config/dubbo-config-api/src/main/java/org/apache/dubbo/config/ServiceConfig.java

Lines changed: 13 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,7 @@
5353
import java.util.HashSet;
5454
import java.util.List;
5555
import java.util.Map;
56+
import java.util.Optional;
5657
import java.util.UUID;
5758
import java.util.concurrent.Executors;
5859
import java.util.concurrent.ScheduledExecutorService;
@@ -371,9 +372,6 @@ protected synchronized void doExport() {
371372
if (StringUtils.isEmpty(path)) {
372373
path = interfaceName;
373374
}
374-
String uniqueServiceName = getUniqueServiceName();
375-
ProviderModel providerModel = new ProviderModel(uniqueServiceName, ref, interfaceClass);
376-
ApplicationModel.initProviderModel(uniqueServiceName, providerModel);
377375
doExportUrls();
378376
}
379377

@@ -413,6 +411,9 @@ public synchronized void unexport() {
413411
private void doExportUrls() {
414412
List<URL> registryURLs = loadRegistries(true);
415413
for (ProtocolConfig protocolConfig : protocols) {
414+
String pathKey = URL.buildKey(getContextPath(protocolConfig).map(p -> p + "/" + path).orElse(path), group, version);
415+
ProviderModel providerModel = new ProviderModel(pathKey, ref, interfaceClass);
416+
ApplicationModel.initProviderModel(pathKey, providerModel);
416417
doExportUrlsFor1Protocol(protocolConfig, registryURLs);
417418
}
418419
}
@@ -512,14 +513,9 @@ private void doExportUrlsFor1Protocol(ProtocolConfig protocolConfig, List<URL> r
512513
}
513514
}
514515
// export service
515-
String contextPath = protocolConfig.getContextpath();
516-
if (StringUtils.isEmpty(contextPath) && provider != null) {
517-
contextPath = provider.getContextpath();
518-
}
519-
520516
String host = this.findConfigedHosts(protocolConfig, registryURLs, map);
521517
Integer port = this.findConfigedPorts(protocolConfig, name, map);
522-
URL url = new URL(name, host, port, (StringUtils.isEmpty(contextPath) ? "" : contextPath + "/") + path, map);
518+
URL url = new URL(name, host, port, getContextPath(protocolConfig).map(p -> p + "/" + path).orElse(path), map);
523519

524520
if (ExtensionLoader.getExtensionLoader(ConfiguratorFactory.class)
525521
.hasExtension(url.getProtocol())) {
@@ -598,6 +594,14 @@ private void exportLocal(URL url) {
598594
}
599595
}
600596

597+
private Optional<String> getContextPath(ProtocolConfig protocolConfig) {
598+
String contextPath = protocolConfig.getContextpath();
599+
if (StringUtils.isEmpty(contextPath) && provider != null) {
600+
contextPath = provider.getContextpath();
601+
}
602+
return Optional.ofNullable(contextPath);
603+
}
604+
601605
protected Class getServiceClass(T ref) {
602606
return ref.getClass();
603607
}
@@ -987,19 +991,6 @@ public void setProviders(List<ProviderConfig> providers) {
987991
this.protocols = convertProviderToProtocol(providers);
988992
}
989993

990-
@Parameter(excluded = true)
991-
public String getUniqueServiceName() {
992-
StringBuilder buf = new StringBuilder();
993-
if (group != null && group.length() > 0) {
994-
buf.append(group).append("/");
995-
}
996-
buf.append(StringUtils.isNotEmpty(path) ? path : interfaceName);
997-
if (version != null && version.length() > 0) {
998-
buf.append(":").append(version);
999-
}
1000-
return buf.toString();
1001-
}
1002-
1003994
@Override
1004995
@Parameter(excluded = true)
1005996
public String getPrefix() {

dubbo-config/dubbo-config-api/src/test/java/org/apache/dubbo/config/ServiceConfigTest.java

Lines changed: 1 addition & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@
3131
import org.apache.dubbo.rpc.Invoker;
3232
import org.apache.dubbo.rpc.Protocol;
3333
import org.apache.dubbo.rpc.service.GenericService;
34+
3435
import org.junit.jupiter.api.AfterEach;
3536
import org.junit.jupiter.api.Assertions;
3637
import org.junit.jupiter.api.BeforeEach;
@@ -243,13 +244,4 @@ public void testMock2() throws Exception {
243244
service.setMock(true);
244245
});
245246
}
246-
247-
@Test
248-
public void testUniqueServiceName() throws Exception {
249-
ServiceConfig<Greeting> service = new ServiceConfig<Greeting>();
250-
service.setGroup("dubbo");
251-
service.setInterface(Greeting.class);
252-
service.setVersion("1.0.0");
253-
assertThat(service.getUniqueServiceName(), equalTo("dubbo/" + Greeting.class.getName() + ":1.0.0"));
254-
}
255247
}

dubbo-config/dubbo-config-api/src/test/java/org/apache/dubbo/config/builders/ServiceBuilderTest.java

Lines changed: 0 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,6 @@
1919
import org.apache.dubbo.config.MethodConfig;
2020
import org.apache.dubbo.config.ProviderConfig;
2121
import org.apache.dubbo.config.ServiceConfig;
22-
import org.apache.dubbo.config.api.Greeting;
2322

2423
import org.junit.jupiter.api.Assertions;
2524
import org.junit.jupiter.api.Test;
@@ -34,15 +33,6 @@
3433

3534
class ServiceBuilderTest {
3635

37-
@Test
38-
public void testUniqueServiceName() throws Exception {
39-
ServiceBuilder<Greeting> builder = new ServiceBuilder<>();
40-
builder.group("dubbo").interfaceClass(Greeting.class).version("1.0.0");
41-
42-
ServiceConfig<Greeting> service = builder.build();
43-
assertThat(service.getUniqueServiceName(), equalTo("dubbo/" + Greeting.class.getName() + ":1.0.0"));
44-
}
45-
4636
@Test
4737
void path() {
4838
ServiceBuilder builder = new ServiceBuilder();

dubbo-registry/dubbo-registry-api/src/main/java/org/apache/dubbo/registry/integration/RegistryProtocol.java

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -296,8 +296,18 @@ private URL getRegisteredProviderUrl(final URL providerUrl, final URL registryUr
296296
MONITOR_KEY, BIND_IP_KEY, BIND_PORT_KEY, QOS_ENABLE, QOS_PORT, ACCEPT_FOREIGN_IP, VALIDATION_KEY,
297297
INTERFACES);
298298
} else {
299-
String[] paramsToRegistry = getParamsToRegistry(DEFAULT_REGISTER_PROVIDER_KEYS,
300-
registryUrl.getParameter(EXTRA_KEYS_KEY, new String[0]));
299+
String extra_keys = registryUrl.getParameter(EXTRA_KEYS_KEY, "");
300+
// if path is not the same as interface name then we should keep INTERFACE_KEY,
301+
// otherwise, the registry structure of zookeeper would be '/dubbo/path/providers',
302+
// but what we expect is '/dubbo/interface/providers'
303+
if (!providerUrl.getPath().equals(providerUrl.getParameter(Constants.INTERFACE_KEY))) {
304+
if (StringUtils.isNotEmpty(extra_keys)) {
305+
extra_keys += ",";
306+
}
307+
extra_keys += Constants.INTERFACE_KEY;
308+
}
309+
String[] paramsToRegistry = getParamsToRegistry(DEFAULT_REGISTER_PROVIDER_KEYS
310+
, Constants.COMMA_SPLIT_PATTERN.split(extra_keys));
301311
return URL.valueOf(providerUrl, paramsToRegistry, providerUrl.getParameter(METHODS_KEY, (String[]) null));
302312
}
303313

dubbo-rpc/dubbo-rpc-rest/src/main/java/org/apache/dubbo/rpc/protocol/rest/RestProtocol.java

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -87,7 +87,7 @@ public int getDefaultPort() {
8787
@Override
8888
protected <T> Runnable doExport(T impl, Class<T> type, URL url) throws RpcException {
8989
String addr = getAddr(url);
90-
Class implClass = ApplicationModel.getProviderModel(url.getServiceKey()).getServiceInstance().getClass();
90+
Class implClass = ApplicationModel.getProviderModel(url.getPathKey()).getServiceInstance().getClass();
9191
RestServer server = servers.computeIfAbsent(addr, restServer -> {
9292
RestServer s = serverFactory.createServer(url.getParameter(Constants.SERVER_KEY, DEFAULT_SERVER));
9393
s.start(url);
@@ -228,8 +228,21 @@ public void destroy() {
228228
clients.clear();
229229
}
230230

231+
/**
232+
* getPath() will return: [contextpath + "/" +] path
233+
* 1. contextpath is empty if user does not set through ProtocolConfig or ProviderConfig
234+
* 2. path will never be empty, it's default value is the interface name.
235+
*
236+
* @return return path only if user has explicitly gave then a value.
237+
*/
231238
protected String getContextPath(URL url) {
232239
String contextPath = url.getPath();
240+
if (contextPath.equalsIgnoreCase(url.getParameter(Constants.INTERFACE_KEY))) {
241+
return "";
242+
}
243+
if (contextPath.endsWith(url.getParameter(Constants.INTERFACE_KEY))) {
244+
contextPath = contextPath.substring(0, contextPath.lastIndexOf(url.getParameter(Constants.INTERFACE_KEY)));
245+
}
233246
return contextPath.endsWith("/") ? contextPath.substring(0, contextPath.length() - 1) : contextPath;
234247
}
235248

dubbo-rpc/dubbo-rpc-rest/src/test/java/org/apache/dubbo/rpc/protocol/rest/RestProtocolTest.java

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -21,11 +21,11 @@
2121
import org.apache.dubbo.common.extension.ExtensionLoader;
2222
import org.apache.dubbo.common.utils.NetUtils;
2323
import org.apache.dubbo.rpc.Exporter;
24+
import org.apache.dubbo.rpc.Invoker;
2425
import org.apache.dubbo.rpc.Protocol;
2526
import org.apache.dubbo.rpc.ProxyFactory;
2627
import org.apache.dubbo.rpc.Result;
2728
import org.apache.dubbo.rpc.RpcContext;
28-
import org.apache.dubbo.rpc.Invoker;
2929
import org.apache.dubbo.rpc.RpcException;
3030
import org.apache.dubbo.rpc.RpcInvocation;
3131
import org.apache.dubbo.rpc.model.ApplicationModel;
@@ -43,7 +43,7 @@ public class RestProtocolTest {
4343
private Protocol protocol = ExtensionLoader.getExtensionLoader(Protocol.class).getExtension("rest");
4444
private ProxyFactory proxy = ExtensionLoader.getExtensionLoader(ProxyFactory.class).getAdaptiveExtension();
4545
private final int availablePort = NetUtils.getAvailablePort();
46-
private final URL exportUrl = URL.valueOf("rest://127.0.0.1:" + availablePort + "/rest");
46+
private final URL exportUrl = URL.valueOf("rest://127.0.0.1:" + availablePort + "/rest?interface=org.apache.dubbo.rpc.protocol.rest.DemoService");
4747

4848
@AfterEach
4949
public void tearDown() {
@@ -52,10 +52,10 @@ public void tearDown() {
5252

5353
@Test
5454
public void testRestProtocol() {
55-
URL url = URL.valueOf("rest://127.0.0.1:5342/rest/say?version=1.0.0");
55+
URL url = URL.valueOf("rest://127.0.0.1:5342/rest/say?version=1.0.0&interface=org.apache.dubbo.rpc.protocol.rest.DemoService");
5656
DemoServiceImpl server = new DemoServiceImpl();
57-
ProviderModel providerModel = new ProviderModel(url.getServiceKey(), server, DemoService.class);
58-
ApplicationModel.initProviderModel(url.getServiceKey(), providerModel);
57+
ProviderModel providerModel = new ProviderModel(url.getPathKey(), server, DemoService.class);
58+
ApplicationModel.initProviderModel(url.getPathKey(), providerModel);
5959

6060
Exporter<DemoService> exporter = protocol.export(proxy.getInvoker(server, DemoService.class, url));
6161
Invoker<DemoService> invoker = protocol.refer(DemoService.class, url);
@@ -73,13 +73,13 @@ public void testRestProtocol() {
7373
public void testRestProtocolWithContextPath() {
7474
DemoServiceImpl server = new DemoServiceImpl();
7575
Assertions.assertFalse(server.isCalled());
76-
URL url = URL.valueOf("rest://127.0.0.1:5341/a/b/c?version=1.0.0");
77-
ProviderModel providerModel = new ProviderModel(url.getServiceKey(), server, DemoService.class);
78-
ApplicationModel.initProviderModel(url.getServiceKey(), providerModel);
76+
URL url = URL.valueOf("rest://127.0.0.1:5341/a/b/c?version=1.0.0&interface=org.apache.dubbo.rpc.protocol.rest.DemoService");
77+
ProviderModel providerModel = new ProviderModel(url.getPathKey(), server, DemoService.class);
78+
ApplicationModel.initProviderModel(url.getPathKey(), providerModel);
7979

8080
Exporter<DemoService> exporter = protocol.export(proxy.getInvoker(server, DemoService.class, url));
8181

82-
url = URL.valueOf("rest://127.0.0.1:5341/a/b/c/?version=1.0.0");
82+
url = URL.valueOf("rest://127.0.0.1:5341/a/b/c/?version=1.0.0&interface=org.apache.dubbo.rpc.protocol.rest.DemoService");
8383
Invoker<DemoService> invoker = protocol.refer(DemoService.class, url);
8484
DemoService client = proxy.getProxy(invoker);
8585
String result = client.sayHello("haha");
@@ -128,7 +128,7 @@ public void testServletWithoutWebConfig() {
128128
Assertions.assertThrows(RpcException.class, () -> {
129129
DemoService server = new DemoServiceImpl();
130130
ProviderModel providerModel = new ProviderModel(exportUrl.getServiceKey(), server, DemoService.class);
131-
ApplicationModel.initProviderModel(exportUrl.getServiceKey(), providerModel);
131+
ApplicationModel.initProviderModel(exportUrl.getPathKey(), providerModel);
132132

133133
URL servletUrl = exportUrl.addParameter(Constants.SERVER_KEY, "servlet");
134134

0 commit comments

Comments
 (0)