Skip to content

Commit

Permalink
2.7.8 service introspection (#6300)
Browse files Browse the repository at this point in the history
* Polish #6296 : Adding the new methods into MetadataReport to manipulate the exported URLs for service introspection

* Polish #6296 : Adding the new methods into MetadataReport to manipulate the exported URLs for service introspection

* Polish #6171 : [Feature] Introducing the composite implementation of MetadataService
  • Loading branch information
mercyblitz committed Jun 10, 2020
1 parent 951955a commit b086a95
Show file tree
Hide file tree
Showing 31 changed files with 518 additions and 736 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ public interface EventListener<E extends Event> extends java.util.EventListener,
* The comparison rule , refer to {@link #compareTo}.
*/
default int getPriority() {
return MIN_PRIORITY;
return NORMAL_PRIORITY;
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -101,9 +101,9 @@

/**
* See {@link ApplicationModel} and {@link ExtensionLoader} for why this class is designed to be singleton.
*
* <p>
* The bootstrap class of Dubbo
*
* <p>
* Get singleton instance by calling static method {@link #getInstance()}.
* Designed as singleton because some classes inside Dubbo, such as ExtensionLoader, are designed only for one instance per process.
*
Expand Down Expand Up @@ -509,12 +509,13 @@ private void initialize() {

startConfigCenter();

useRegistryAsConfigCenterIfNecessary();

loadRemoteConfigs();

checkGlobalConfigs();

// @since 2.7.8
startMetadataCenter();

initMetadataService();

initEventListener();
Expand Down Expand Up @@ -583,6 +584,9 @@ private void checkGlobalConfigs() {
}

private void startConfigCenter() {

useRegistryAsConfigCenterIfNecessary();

Collection<ConfigCenterConfig> configCenters = configManager.getConfigCenters();

// check Config Center
Expand Down Expand Up @@ -610,7 +614,10 @@ private void startConfigCenter() {
configManager.refreshAll();
}

private void startMetadataReport() {
private void startMetadataCenter() {

useRegistryAsMetadataCenterIfNecessary();

ApplicationConfig applicationConfig = getApplication();

String metadataType = applicationConfig.getMetadataType();
Expand Down Expand Up @@ -646,33 +653,87 @@ private void useRegistryAsConfigCenterIfNecessary() {
return;
}

configManager.getDefaultRegistries().stream()
.filter(registryConfig -> registryConfig.getUseAsConfigCenter() == null || registryConfig.getUseAsConfigCenter())
.forEach(registryConfig -> {
String protocol = registryConfig.getProtocol();
String id = "config-center-" + protocol + "-" + registryConfig.getPort();
ConfigCenterConfig cc = new ConfigCenterConfig();
cc.setId(id);
if (cc.getParameters() == null) {
cc.setParameters(new HashMap<>());
}
if (registryConfig.getParameters() != null) {
cc.getParameters().putAll(registryConfig.getParameters());
}
cc.getParameters().put(CLIENT_KEY, registryConfig.getClient());
cc.setProtocol(registryConfig.getProtocol());
cc.setPort(registryConfig.getPort());
cc.setAddress(getRegistryCompatibleAddress(registryConfig.getAddress()));
cc.setNamespace(registryConfig.getGroup());
cc.setUsername(registryConfig.getUsername());
cc.setPassword(registryConfig.getPassword());
if (registryConfig.getTimeout() != null) {
cc.setTimeout(registryConfig.getTimeout().longValue());
}
cc.setHighestPriority(false);
configManager.addConfigCenter(cc);
});
startConfigCenter();
configManager
.getDefaultRegistries()
.stream()
.filter(this::isUsedRegistryAsConfigCenter)
.map(this::registryAsConfigCenter)
.forEach(configManager::addConfigCenter);
}

private boolean isUsedRegistryAsConfigCenter(RegistryConfig registryConfig) {
// TODO: confirm ? registryConfig.getUseAsConfigCenter() == null || registryConfig.getUseAsConfigCenter()
return Boolean.TRUE.equals(registryConfig.getUseAsConfigCenter());
}

private ConfigCenterConfig registryAsConfigCenter(RegistryConfig registryConfig) {
String protocol = registryConfig.getProtocol();
Integer port = registryConfig.getPort();
String id = "config-center-" + protocol + "-" + port;
ConfigCenterConfig cc = new ConfigCenterConfig();
cc.setId(id);
if (cc.getParameters() == null) {
cc.setParameters(new HashMap<>());
}
if (registryConfig.getParameters() != null) {
cc.getParameters().putAll(registryConfig.getParameters()); // copy the parameters
}
cc.getParameters().put(CLIENT_KEY, registryConfig.getClient());
cc.setProtocol(protocol);
cc.setPort(port);
cc.setGroup(registryConfig.getGroup());
cc.setAddress(getRegistryCompatibleAddress(registryConfig.getAddress()));
cc.setNamespace(registryConfig.getGroup());
cc.setUsername(registryConfig.getUsername());
cc.setPassword(registryConfig.getPassword());
if (registryConfig.getTimeout() != null) {
cc.setTimeout(registryConfig.getTimeout().longValue());
}
cc.setHighestPriority(false);
return cc;
}

private void useRegistryAsMetadataCenterIfNecessary() {

Collection<MetadataReportConfig> metadataConfigs = configManager.getMetadataConfigs();

if (CollectionUtils.isNotEmpty(metadataConfigs)) {
return;
}

configManager
.getDefaultRegistries()
.stream()
.filter(this::isUsedRegistryAsMetadataCenter)
.map(this::registryAsMetadataCenter)
.forEach(configManager::addMetadataReport);

}

private boolean isUsedRegistryAsMetadataCenter(RegistryConfig registryConfig) {
// TODO: confirm ? registryConfig.getUseAsMetadataCenter() == null || registryConfig.getUseAsMetadataCenter()
return Boolean.TRUE.equals(registryConfig.getUseAsMetadataCenter());
}

private MetadataReportConfig registryAsMetadataCenter(RegistryConfig registryConfig) {
String protocol = registryConfig.getProtocol();
Integer port = registryConfig.getPort();
String id = "metadata-center-" + protocol + "-" + port;
MetadataReportConfig metadataReportConfig = new MetadataReportConfig();
metadataReportConfig.setId(id);
if (metadataReportConfig.getParameters() == null) {
metadataReportConfig.setParameters(new HashMap<>());
}
if (registryConfig.getParameters() != null) {
metadataReportConfig.getParameters().putAll(registryConfig.getParameters()); // copy the parameters
}
metadataReportConfig.getParameters().put(CLIENT_KEY, registryConfig.getClient());
metadataReportConfig.setGroup(registryConfig.getGroup());
metadataReportConfig.setAddress(getRegistryCompatibleAddress(registryConfig.getAddress()));
metadataReportConfig.setUsername(registryConfig.getUsername());
metadataReportConfig.setPassword(registryConfig.getPassword());
metadataReportConfig.setTimeout(registryConfig.getTimeout());
return metadataReportConfig;
}

private String getRegistryCompatibleAddress(String registryAddress) {
Expand Down Expand Up @@ -722,7 +783,6 @@ private void loadRemoteConfigs() {
* Initialize {@link MetadataService} from {@link WritableMetadataService}'s extension
*/
private void initMetadataService() {
startMetadataReport();
this.metadataService = getExtension(getMetadataType());
this.metadataServiceExporter = new ConfigurableMetadataServiceExporter(metadataService);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@

import org.apache.dubbo.config.bootstrap.rest.UserService;

import static org.apache.dubbo.common.constants.CommonConstants.REMOTE_METADATA_STORAGE_TYPE;

/**
* Dubbo Provider Bootstrap
*
Expand All @@ -28,8 +30,8 @@ public class ZookeeperDubboServiceConsumerBootstrap {
public static void main(String[] args) throws Exception {

DubboBootstrap bootstrap = DubboBootstrap.getInstance()
.application("zookeeper-dubbo-consumer")
.registry("zookeeper", builder -> builder.address("zookeeper://127.0.0.1:2181?registry-type=service&subscribed-services=zookeeper-dubbo-provider"))
.application("zookeeper-dubbo-consumer", app -> app.metadata(REMOTE_METADATA_STORAGE_TYPE))
.registry("zookeeper", builder -> builder.address("zookeeper://127.0.0.1:2181?registry-type=service&subscribed-services=zookeeper-dubbo-provider").useAsMetadataCenter(true))
.reference("echo", builder -> builder.interfaceClass(EchoService.class).protocol("dubbo"))
.reference("user", builder -> builder.interfaceClass(UserService.class).protocol("rest"))
.start();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,15 +19,17 @@
import org.apache.dubbo.config.bootstrap.rest.UserService;
import org.apache.dubbo.config.bootstrap.rest.UserServiceImpl;

import static org.apache.dubbo.common.constants.CommonConstants.REMOTE_METADATA_STORAGE_TYPE;

/**
* TODO
*/
public class ZookeeperDubboServiceProviderBootstrap {

public static void main(String[] args) {
DubboBootstrap.getInstance()
.application("zookeeper-dubbo-provider")
.registry(builder -> builder.address("zookeeper://127.0.0.1:2181?registry-type=service"))
.application("zookeeper-dubbo-provider", app -> app.metadata(REMOTE_METADATA_STORAGE_TYPE))
.registry(builder -> builder.address("zookeeper://127.0.0.1:2181?registry-type=service").useAsMetadataCenter(true))
.protocol("dubbo", builder -> builder.port(-1).name("dubbo"))
.protocol("rest", builder -> builder.port(8081).name("rest"))
.service("echo", builder -> builder.interfaceClass(EchoService.class).ref(new EchoServiceImpl()).protocolIds("dubbo"))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,22 @@
*/
package org.apache.dubbo.metadata;

public class MetadataConstants {
public static final String KEY_SEPARATOR = ":";
public static final String DEFAULT_PATH_TAG = "metadata";
public static final String KEY_REVISON_PREFIX = "revision";
public static final String META_DATA_STORE_TAG = ".metaData";
public static final String SERVICE_META_DATA_STORE_TAG = ".smd";
public static final String CONSUMER_META_DATA_STORE_TAG = ".cmd";
public interface MetadataConstants {
String KEY_SEPARATOR = ":";
String DEFAULT_PATH_TAG = "metadata";
String KEY_REVISON_PREFIX = "revision";
String META_DATA_STORE_TAG = ".metaData";
String SERVICE_META_DATA_STORE_TAG = ".smd";
String CONSUMER_META_DATA_STORE_TAG = ".cmd";

/**
* @since 2.7.8
*/
String EXPORTED_URLS_TAG = "exported-urls";

/**
* @since 2.7.8
*/
String SUBSCRIBED_URLS_TAG = "subscribed-urls";

}
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@

import static java.util.Collections.unmodifiableSortedSet;
import static java.util.stream.StreamSupport.stream;
import static org.apache.dubbo.common.URL.buildKey;

/**
* A framework interface of Dubbo Metadata Service defines the contract of Dubbo Services registartion and subscription
Expand Down Expand Up @@ -90,7 +91,7 @@ default String version() {
* @see #toSortedStrings(Stream)
* @see URL#toFullString()
*/
default SortedSet<String> getSubscribedURLs(){
default SortedSet<String> getSubscribedURLs() {
throw new UnsupportedOperationException("This operation is not supported for consumer.");
}

Expand Down Expand Up @@ -165,7 +166,9 @@ default SortedSet<String> getExportedURLs(String serviceInterface, String group,
*
* @return
*/
String getServiceDefinition(String interfaceName, String version, String group);
default String getServiceDefinition(String interfaceName, String version, String group) {
return getServiceDefinition(buildKey(interfaceName, group, version));
}

/**
* Interface definition.
Expand Down

This file was deleted.

Loading

0 comments on commit b086a95

Please sign in to comment.