Skip to content

Commit

Permalink
Polish apache#6336 : [Refactor] org.apache.dubbo.metadata.ServiceName…
Browse files Browse the repository at this point in the history
…Mapping
  • Loading branch information
mercyblitz committed Jun 17, 2020
1 parent c546f3c commit 05e5c4f
Show file tree
Hide file tree
Showing 4 changed files with 67 additions and 15 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,6 @@

import java.util.List;

import static org.apache.dubbo.common.constants.CommonConstants.GROUP_KEY;
import static org.apache.dubbo.common.constants.CommonConstants.VERSION_KEY;
import static org.apache.dubbo.metadata.ServiceNameMapping.getDefaultExtension;

/**
Expand All @@ -45,11 +43,7 @@ public void onEvent(ServiceConfigExportedEvent event) {
ServiceConfig serviceConfig = event.getServiceConfig();
List<URL> exportedURLs = serviceConfig.getExportedUrls();
exportedURLs.forEach(url -> {
String serviceInterface = url.getServiceInterface();
String group = url.getParameter(GROUP_KEY);
String version = url.getParameter(VERSION_KEY);
String protocol = url.getProtocol();
serviceNameMapping.map(serviceInterface, group, version, protocol);
serviceNameMapping.map(url);
});
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
*/
package org.apache.dubbo.metadata;

import org.apache.dubbo.common.URL;
import org.apache.dubbo.common.config.configcenter.DynamicConfiguration;
import org.apache.dubbo.common.logger.Logger;
import org.apache.dubbo.common.logger.LoggerFactory;
Expand All @@ -27,6 +28,8 @@

import static java.lang.String.valueOf;
import static java.util.Arrays.asList;
import static org.apache.dubbo.common.constants.CommonConstants.GROUP_KEY;
import static org.apache.dubbo.common.constants.CommonConstants.VERSION_KEY;
import static org.apache.dubbo.common.utils.CollectionUtils.isNotEmpty;
import static org.apache.dubbo.common.utils.StringUtils.SLASH;
import static org.apache.dubbo.rpc.model.ApplicationModel.getName;
Expand All @@ -43,12 +46,18 @@ public class DynamicConfigurationServiceNameMapping implements ServiceNameMappin
private final Logger logger = LoggerFactory.getLogger(getClass());

@Override
public void map(String serviceInterface, String group, String version, String protocol) {
public void map(URL exportedURL) {

String serviceInterface = exportedURL.getServiceInterface();

if (IGNORED_SERVICE_INTERFACES.contains(serviceInterface)) {
return;
}

String group = exportedURL.getParameter(GROUP_KEY);
String version = exportedURL.getParameter(VERSION_KEY);
String protocol = exportedURL.getProtocol();

DynamicConfiguration dynamicConfiguration = DynamicConfiguration.getDynamicConfiguration();

// the Dubbo Service Key as group
Expand All @@ -66,10 +75,15 @@ public void map(String serviceInterface, String group, String version, String pr
}

@Override
public Set<String> get(String serviceInterface, String group, String version, String protocol) {
public Set<String> get(URL subscribedURL) {

DynamicConfiguration dynamicConfiguration = DynamicConfiguration.getDynamicConfiguration();

String serviceInterface = subscribedURL.getServiceInterface();
String group = subscribedURL.getParameter(GROUP_KEY);
String version = subscribedURL.getParameter(VERSION_KEY);
String protocol = subscribedURL.getProtocol();

Set<String> serviceNames = new LinkedHashSet<>();
execute(() -> {
Set<String> keys = dynamicConfiguration.getConfigKeys(buildGroup(serviceInterface, group, version, protocol));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
*/
package org.apache.dubbo.metadata;

import org.apache.dubbo.common.URL;
import org.apache.dubbo.common.extension.SPI;

import java.util.Set;
Expand All @@ -37,8 +38,20 @@ public interface ServiceNameMapping {
* @param group the group of Dubbo service interface (optional)
* @param version the version of Dubbo service interface version (optional)
* @param protocol the protocol of Dubbo service interface exported (optional)
* @deprecated 2.7.8 This method will be removed since 3.0
*/
void map(String serviceInterface, String group, String version, String protocol);
@Deprecated
default void map(String serviceInterface, String group, String version, String protocol) {
throw new UnsupportedOperationException("This method has been deprecated and should not be invoked!");
}

/**
* Map the specified Dubbo service {@link URL} to current Dubbo service name
*
* @param exportedURL the {@link URL} that the Dubbo Provider exported
* @since 2.7.8
*/
void map(URL exportedURL);

/**
* Get the service names from the specified Dubbo service interface, group, version and protocol
Expand All @@ -47,10 +60,22 @@ public interface ServiceNameMapping {
* @param group the group of Dubbo service interface (optional)
* @param version the version of Dubbo service interface version (optional)
* @param protocol the protocol of Dubbo service interface exported (optional)
* @return
* @return non-null {@link Set}
* @deprecated 2.7.8 This method will be removed since 3.0
*/
Set<String> get(String serviceInterface, String group, String version, String protocol);
@Deprecated
default Set<String> get(String serviceInterface, String group, String version, String protocol) {
throw new UnsupportedOperationException("This method has been deprecated and should not be invoked!");
}

/**
* Get the service names from the subscribed Dubbo service {@link URL}
*
* @param subscribedURL the {@link URL} that the Dubbo consumer subscribed
* @return non-null {@link Set}
* @since 2.7.8
*/
Set<String> get(URL subscribedURL);

/**
* Get the default extension of {@link ServiceNameMapping}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
*/
package org.apache.dubbo.metadata;

import org.apache.dubbo.common.URL;
import org.apache.dubbo.common.config.configcenter.DynamicConfiguration;
import org.apache.dubbo.common.config.configcenter.DynamicConfigurationFactory;
import org.apache.dubbo.config.ApplicationConfig;
Expand All @@ -28,10 +29,13 @@
import java.util.TreeSet;

import static java.util.Arrays.asList;
import static org.apache.dubbo.common.constants.CommonConstants.GROUP_KEY;
import static org.apache.dubbo.common.constants.CommonConstants.VERSION_KEY;
import static org.apache.dubbo.common.extension.ExtensionLoader.getExtensionLoader;
import static org.apache.dubbo.metadata.DynamicConfigurationServiceNameMapping.buildGroup;
import static org.apache.dubbo.metadata.ServiceNameMapping.getDefaultExtension;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertThrows;

/**
* {@link DynamicConfigurationServiceNameMapping} Test
Expand Down Expand Up @@ -61,6 +65,17 @@ public void testBuildGroup() {
assertEquals("mapping/test", buildGroup("test", "default", "1.0.0", "dubbo"));
}

@Test
public void testAndGetOnFailed() {
assertThrows(UnsupportedOperationException.class, () -> {
serviceNameMapping.map(null, null, null, null);
});

assertThrows(UnsupportedOperationException.class, () -> {
serviceNameMapping.get(null, null, null, null);
});
}

@Test
public void testMapAndGet() {

Expand All @@ -74,14 +89,18 @@ public void testMapAndGet() {
String version = null;
String protocol = null;

serviceNameMapping.map(serviceInterface, group, version, protocol);
URL url = URL.valueOf("dubbo://127.0.0.1:20880").setServiceInterface(serviceInterface)
.addParameter(GROUP_KEY, group)
.addParameter(VERSION_KEY, version);

serviceNameMapping.map(url);

ApplicationModel.getConfigManager().removeConfig(new ApplicationConfig(serviceName));
ApplicationModel.getConfigManager().setApplication(new ApplicationConfig(serviceName2));

serviceNameMapping.map(serviceInterface, group, version, protocol);
serviceNameMapping.map(url);

Set<String> serviceNames = serviceNameMapping.get(serviceInterface, group, version, protocol);
Set<String> serviceNames = serviceNameMapping.get(url);

assertEquals(new TreeSet(asList(serviceName, serviceName2)), serviceNames);

Expand Down

0 comments on commit 05e5c4f

Please sign in to comment.