From 7b9f76d73c481085563203ba826b5771cfd3f1ce Mon Sep 17 00:00:00 2001 From: mercyblitz Date: Fri, 15 Mar 2019 10:22:09 +0800 Subject: [PATCH] Polish apache/incubator-dubbo#3582 : Add Path --- .../support/cloud/CloudNativeRegistry.java | 22 +++++++++---------- .../nacos/NacosServiceInstanceFactory.java | 2 ++ .../DemoServiceConsumerBootstrap.java | 6 ++++- .../test/resources/provider-config.properties | 4 ++-- 4 files changed, 20 insertions(+), 14 deletions(-) diff --git a/dubbo-registry/dubbo-registry-api/src/main/java/org/apache/dubbo/registry/support/cloud/CloudNativeRegistry.java b/dubbo-registry/dubbo-registry-api/src/main/java/org/apache/dubbo/registry/support/cloud/CloudNativeRegistry.java index bd43f150c7c..7caefea8aab 100644 --- a/dubbo-registry/dubbo-registry-api/src/main/java/org/apache/dubbo/registry/support/cloud/CloudNativeRegistry.java +++ b/dubbo-registry/dubbo-registry-api/src/main/java/org/apache/dubbo/registry/support/cloud/CloudNativeRegistry.java @@ -37,6 +37,8 @@ import java.util.stream.Collectors; import static java.util.Collections.singletonList; +import static org.apache.dubbo.common.Constants.PATH_KEY; +import static org.apache.dubbo.common.Constants.PROTOCOL_KEY; import static org.apache.dubbo.common.Constants.PROVIDER_SIDE; import static org.apache.dubbo.common.Constants.SIDE_KEY; @@ -140,13 +142,6 @@ protected List getAllServiceNames() { return cloudServiceDiscovery.getServices(); } - private void doSubscribe(final URL url, final NotifyListener listener, final List serviceNames) { - for (String serviceName : serviceNames) { - List serviceInstances = cloudServiceDiscovery.getServiceInstances(serviceName); - notifySubscriber(url, listener, serviceInstances); - } - } - /** * Get the service names from the specified {@link URL url} * @@ -202,7 +197,7 @@ private void filterServiceNames(List serviceNames) { filter(serviceNames, cloudServiceDiscovery::supports); } - private void doSubscribe(final URL url, final NotifyListener listener, final Set serviceNames) { + private void doSubscribe(final URL url, final NotifyListener listener, final Collection serviceNames) { Collection serviceInstances = serviceNames.stream() .map(cloudServiceDiscovery::getServiceInstances) .flatMap(v -> v.stream()) @@ -237,15 +232,20 @@ private List buildURLs(URL consumerURL, Collection serviceInstances) { for (S serviceInstance : serviceInstances) { URL url = buildURL(serviceInstance); if (UrlUtils.isMatch(consumerURL, url)) { - urls.add(url); + urls.add(url.setPath(consumerURL.getPath())); } } return urls; } private URL buildURL(S serviceInstance) { - URL url = new URL(serviceInstance.getMetadata().get(Constants.PROTOCOL_KEY), - serviceInstance.getHost(), serviceInstance.getPort(), + Map metadata = serviceInstance.getMetadata(); + String path = metadata.get(PATH_KEY); + String protocol = metadata.get(PROTOCOL_KEY); + URL url = new URL(protocol, + serviceInstance.getHost(), + serviceInstance.getPort(), + path, serviceInstance.getMetadata()); return url; } diff --git a/dubbo-registry/dubbo-registry-nacos/src/main/java/org/apache/dubbo/registry/nacos/NacosServiceInstanceFactory.java b/dubbo-registry/dubbo-registry-nacos/src/main/java/org/apache/dubbo/registry/nacos/NacosServiceInstanceFactory.java index 73850c610a0..fba511b39d2 100644 --- a/dubbo-registry/dubbo-registry-nacos/src/main/java/org/apache/dubbo/registry/nacos/NacosServiceInstanceFactory.java +++ b/dubbo-registry/dubbo-registry-nacos/src/main/java/org/apache/dubbo/registry/nacos/NacosServiceInstanceFactory.java @@ -31,6 +31,7 @@ import static org.apache.dubbo.common.Constants.DEFAULT_CATEGORY; import static org.apache.dubbo.common.Constants.GROUP_KEY; import static org.apache.dubbo.common.Constants.INTERFACE_KEY; +import static org.apache.dubbo.common.Constants.PATH_KEY; import static org.apache.dubbo.common.Constants.PROTOCOL_KEY; import static org.apache.dubbo.common.Constants.PROVIDERS_CATEGORY; import static org.apache.dubbo.common.Constants.VERSION_KEY; @@ -58,6 +59,7 @@ private Instance createInstance(URL url) { String category = url.getParameter(CATEGORY_KEY, DEFAULT_CATEGORY); URL newURL = url.addParameter(CATEGORY_KEY, category); newURL = newURL.addParameter(PROTOCOL_KEY, url.getProtocol()); + newURL = newURL.addParameter(PATH_KEY, url.getPath()); String ip = url.getHost(); int port = url.getPort(); String serviceName = createServiceName(url); diff --git a/dubbo-registry/dubbo-registry-nacos/src/test/java/org/apache/dubbo/demo/consumer/DemoServiceConsumerBootstrap.java b/dubbo-registry/dubbo-registry-nacos/src/test/java/org/apache/dubbo/demo/consumer/DemoServiceConsumerBootstrap.java index cf2754a1ddf..3e0caf20275 100644 --- a/dubbo-registry/dubbo-registry-nacos/src/test/java/org/apache/dubbo/demo/consumer/DemoServiceConsumerBootstrap.java +++ b/dubbo-registry/dubbo-registry-nacos/src/test/java/org/apache/dubbo/demo/consumer/DemoServiceConsumerBootstrap.java @@ -33,13 +33,17 @@ @PropertySource(value = "classpath:/consumer-config.properties") public class DemoServiceConsumerBootstrap { - @Reference(version = "${demo.service.version}", protocol = "dubbo") + @Reference(version = "${demo.service.version}") private DemoService demoService; + @Reference(version = "${demo.service.version}", protocol = "rest") + private DemoService restDemoService; + @PostConstruct public void init() throws InterruptedException { for (int j = 0; j < 10; j++) { System.out.println(demoService.sayName("小马哥(mercyblitz)")); +// System.out.println(restDemoService.sayName("小马哥(mercyblitz)")); } Thread.sleep(TimeUnit.SECONDS.toMillis(5)); } diff --git a/dubbo-registry/dubbo-registry-nacos/src/test/resources/provider-config.properties b/dubbo-registry/dubbo-registry-nacos/src/test/resources/provider-config.properties index a52a00b2f86..e2dd335b0ed 100644 --- a/dubbo-registry/dubbo-registry-nacos/src/test/resources/provider-config.properties +++ b/dubbo-registry/dubbo-registry-nacos/src/test/resources/provider-config.properties @@ -7,8 +7,8 @@ dubbo.registry.address=127.0.0.1:8848 ### Dubbo Protocol using random port dubbo.protocols.dubbo.port=-1 ### REST protocol -#dubbo.protocols.rest.port=9090 -#dubbo.protocols.rest.server=netty +dubbo.protocols.rest.port=9090 +dubbo.protocols.rest.server=netty # Provider @Service info demo.service.version=1.0.0 demo.service.name=demoService \ No newline at end of file