Skip to content

Commit

Permalink
Polish apache#3582 : Add Path
Browse files Browse the repository at this point in the history
  • Loading branch information
mercyblitz committed Mar 15, 2019
1 parent 02689eb commit 7b9f76d
Show file tree
Hide file tree
Showing 4 changed files with 20 additions and 14 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -140,13 +142,6 @@ protected List<String> getAllServiceNames() {
return cloudServiceDiscovery.getServices();
}

private void doSubscribe(final URL url, final NotifyListener listener, final List<String> serviceNames) {
for (String serviceName : serviceNames) {
List<S> serviceInstances = cloudServiceDiscovery.getServiceInstances(serviceName);
notifySubscriber(url, listener, serviceInstances);
}
}

/**
* Get the service names from the specified {@link URL url}
*
Expand Down Expand Up @@ -202,7 +197,7 @@ private void filterServiceNames(List<String> serviceNames) {
filter(serviceNames, cloudServiceDiscovery::supports);
}

private void doSubscribe(final URL url, final NotifyListener listener, final Set<String> serviceNames) {
private void doSubscribe(final URL url, final NotifyListener listener, final Collection<String> serviceNames) {
Collection<S> serviceInstances = serviceNames.stream()
.map(cloudServiceDiscovery::getServiceInstances)
.flatMap(v -> v.stream())
Expand Down Expand Up @@ -237,15 +232,20 @@ private List<URL> buildURLs(URL consumerURL, Collection<S> 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<String, String> 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;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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));
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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

0 comments on commit 7b9f76d

Please sign in to comment.