Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

New metadata service #14210

Merged
merged 30 commits into from
Jun 25, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
30 commits
Select commit Hold shift + click to select a range
23913df
new Triple stub
Feb 25, 2024
4008d64
Support MetadataServiceV2 config
namelessssssssssss Feb 25, 2024
cb26a7d
new Triple stub
Feb 26, 2024
6378efd
new Triple stub
Feb 26, 2024
5518f29
new Triple stub
Feb 26, 2024
537d702
new Triple stub
Mar 2, 2024
dd2e24d
Merge remote-tracking branch 'origin/3.3' into 3.3-new-metadata-servi…
namelessssssssssss Mar 3, 2024
17e3a00
Merge remote-tracking branch 'origin/3.3' into 3.3-new-metadata-servi…
May 14, 2024
d374458
Fix metadataV2 server npe
May 14, 2024
d44c600
Bug fix
May 19, 2024
17dce0e
Bug fix
May 19, 2024
e4bbcaf
Merge branch '3.3' into 3.3-new-metadata-service-stub
namelessssssssssss May 20, 2024
8997cae
Format
May 20, 2024
549fa7c
Merge remote-tracking branch 'origin/3.3-new-metadata-service-stub' i…
May 20, 2024
dfdcd02
Merge branch '3.3' into 3.3-new-metadata-service-stub
namelessssssssssss May 21, 2024
7f73df9
Merge branch '3.3' into 3.3-new-metadata-service-stub
namelessssssssssss May 24, 2024
d3d69f4
UT fix
May 26, 2024
552b882
Merge remote-tracking branch 'origin/3.3-new-metadata-service-stub' i…
May 26, 2024
0aae29d
Merge branch '3.3' into 3.3-new-metadata-service-stub
namelessssssssssss May 30, 2024
d1d98c6
Merge branch '3.3' into 3.3-new-metadata-service-stub
namelessssssssssss May 31, 2024
f2cd5e8
Merge branch '3.3' into 3.3-new-metadata-service-stub
namelessssssssssss Jun 1, 2024
5b5d692
fix ServiceInfo name & port
Jun 2, 2024
04c47de
Merge remote-tracking branch 'origin/3.3-new-metadata-service-stub' i…
Jun 2, 2024
0cdae24
Format
Jun 3, 2024
1f31a42
Merge branch '3.3' into 3.3-new-metadata-service-stub
namelessssssssssss Jun 11, 2024
ecca67b
Merge branch '3.3' into 3.3-new-metadata-service-stub
namelessssssssssss Jun 20, 2024
1be4682
A sorter name for METADATA_SERVICE_VERSION_NAME
Jun 20, 2024
f78d82f
Merge remote-tracking branch 'origin/3.3-new-metadata-service-stub' i…
Jun 20, 2024
9d855c6
Add license
Jun 23, 2024
c99eb72
Merge branch '3.3' into 3.3-new-metadata-service-stub
namelessssssssssss Jun 23, 2024
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -295,6 +295,27 @@ public class ApplicationConfig extends AbstractConfig {
*/
private String executorManagementMode;

/**
* Only use the new version of metadataService (MetadataServiceV2).
* <br> MetadataServiceV2 have better compatibility with other language's dubbo implement (dubbo-go).
* <br> If set to false (default):
* <br> 1. If your services are using triple protocol and {@link #metadataServiceProtocol} is not set
* <br> - Dubbo will export both MetadataService and MetadataServiceV2 with triple
* <br> 2. Set {@link #metadataServiceProtocol} = tri
* <br> - Dubbo will export both MetadataService and MetadataServiceV2 with triple
* <br> 3. Set {@link #metadataServiceProtocol} != tri
* <br> - Dubbo will only export MetadataService
* <br> 4. Your services are not using triple protocol, and {@link #metadataServiceProtocol} is not set
* <br> - Dubbo will only export MetadataService
* <br>
* <br> If set to true, dubbo will try to only use MetadataServiceV2.
* <br> It only activates when meet at least one of the following cases:
* <br> 1. Manually set {@link #metadataServiceProtocol} = tri
* <br> 2. Your services are using triple protocol
* <br>
*/
private Boolean onlyUseMetadataV2;

public ApplicationConfig() {}

public ApplicationConfig(ApplicationModel applicationModel) {
Expand Down Expand Up @@ -794,6 +815,15 @@ public String getExecutorManagementMode() {
return executorManagementMode;
}

@Parameter(excluded = true)
public Boolean getOnlyUseMetadataV2() {
return onlyUseMetadataV2;
}

public void setOnlyUseMetadataV2(Boolean onlyUseMetadataV2) {
this.onlyUseMetadataV2 = onlyUseMetadataV2;
}

@Override
public void refresh() {
super.refresh();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -188,7 +188,8 @@ public void setModuleEnvironment(ModuleEnvironment moduleEnvironment) {
this.moduleEnvironment = moduleEnvironment;
}

public ConsumerModel registerInternalConsumer(Class<?> internalService, URL url) {
public ConsumerModel registerInternalConsumer(
Class<?> internalService, URL url, ServiceDescriptor serviceDescriptor, Object proxyObject) {
ServiceMetadata serviceMetadata = new ServiceMetadata();
serviceMetadata.setVersion(url.getVersion());
serviceMetadata.setGroup(url.getGroup());
Expand All @@ -197,11 +198,12 @@ public ConsumerModel registerInternalConsumer(Class<?> internalService, URL url)
serviceMetadata.setServiceType(internalService);
String serviceKey = URL.buildKey(internalService.getName(), url.getGroup(), url.getVersion());
serviceMetadata.setServiceKey(serviceKey);

ConsumerModel consumerModel = new ConsumerModel(
serviceMetadata.getServiceKey(),
"jdk",
serviceRepository.lookupService(serviceMetadata.getServiceInterfaceName()),
proxyObject,
serviceDescriptor == null
? serviceRepository.lookupService(serviceMetadata.getServiceInterfaceName())
: serviceDescriptor,
this,
serviceMetadata,
new HashMap<>(0),
Expand All @@ -212,6 +214,15 @@ public ConsumerModel registerInternalConsumer(Class<?> internalService, URL url)
return consumerModel;
}

public ConsumerModel registerInternalConsumer(
Class<?> internalService, URL url, ServiceDescriptor serviceDescriptor) {
return registerInternalConsumer(internalService, url, serviceDescriptor, null);
}

public ConsumerModel registerInternalConsumer(Class<?> internalService, URL url) {
return registerInternalConsumer(internalService, url, null, null);
}

public boolean isLifeCycleManagedExternally() {
return lifeCycleManagedExternally;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,11 @@ private void initialize() {
.getSupportedExtensionInstances();
if (CollectionUtils.isNotEmpty(builtinServices)) {
for (BuiltinServiceDetector service : builtinServices) {
applicationModel.getInternalModule().getServiceRepository().registerService(service.getService());
Class<?> serviceClass = service.getService();
if (serviceClass == null) {
continue;
}
applicationModel.getInternalModule().getServiceRepository().registerService(serviceClass);
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,7 @@ public class InternalServiceConfigBuilder<T> {
private Class<T> interfaceClass;
private Executor executor;
private T ref;
private String version;

private InternalServiceConfigBuilder(ApplicationModel applicationModel) {
this.applicationModel = applicationModel;
Expand Down Expand Up @@ -103,6 +104,11 @@ public InternalServiceConfigBuilder<T> protocol(String protocol, String key) {
return getThis();
}

public InternalServiceConfigBuilder<T> version(String version) {
this.version = version;
return getThis();
}

/**
* Get other configured protocol from environment in priority order. If get nothing, use default dubbo.
*
Expand Down Expand Up @@ -296,7 +302,12 @@ public ServiceConfig<T> build(Consumer<ServiceConfig<T>> configConsumer) {
serviceConfig.setInterface(interfaceClass);
serviceConfig.setRef(this.ref);
serviceConfig.setGroup(applicationConfig.getName());
serviceConfig.setVersion("1.0.0");

if (StringUtils.isNotEmpty(version)) {
serviceConfig.setVersion(version);
} else {
serviceConfig.setVersion("1.0.0");
}
serviceConfig.setFilter("-default");

serviceConfig.setExecutor(executor);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@
*/
package org.apache.dubbo.config.metadata;

import org.apache.dubbo.common.URL;
import org.apache.dubbo.common.logger.ErrorTypeAwareLogger;
import org.apache.dubbo.common.logger.LoggerFactory;
import org.apache.dubbo.common.threadpool.manager.FrameworkExecutorRepository;
Expand All @@ -26,17 +25,22 @@
import org.apache.dubbo.config.ServiceConfig;
import org.apache.dubbo.config.bootstrap.builders.InternalServiceConfigBuilder;
import org.apache.dubbo.metadata.MetadataService;
import org.apache.dubbo.metadata.MetadataServiceV2;
import org.apache.dubbo.metadata.util.MetadataServiceVersionUtils;
import org.apache.dubbo.registry.client.metadata.MetadataServiceDelegation;
import org.apache.dubbo.registry.client.metadata.MetadataServiceDelegationV2;
import org.apache.dubbo.rpc.model.ApplicationModel;

import java.util.Collections;
import java.util.List;
import java.util.concurrent.ExecutorService;

import static java.util.Collections.emptyList;
import static org.apache.dubbo.common.constants.CommonConstants.METADATA_SERVICE_PORT_KEY;
import static org.apache.dubbo.common.constants.CommonConstants.METADATA_SERVICE_PROTOCOL_KEY;
import static org.apache.dubbo.common.constants.CommonConstants.TRIPLE;
import static org.apache.dubbo.common.constants.LoggerCodeConstants.CONFIG_METADATA_SERVICE_EXPORTED;
import static org.apache.dubbo.metadata.util.MetadataServiceVersionUtils.V1;
import static org.apache.dubbo.metadata.util.MetadataServiceVersionUtils.V2;

/**
* Export metadata service
Expand All @@ -45,39 +49,34 @@ public class ConfigurableMetadataServiceExporter {

private final ErrorTypeAwareLogger logger = LoggerFactory.getErrorTypeAwareLogger(getClass());

private MetadataServiceDelegation metadataService;
@Deprecated
private final MetadataServiceDelegation metadataService;

private final MetadataServiceDelegationV2 metadataServiceV2;

@Deprecated
private volatile ServiceConfig<MetadataService> serviceConfig;

private volatile ServiceConfig<MetadataServiceV2> serviceConfigV2;

private final ApplicationModel applicationModel;

public ConfigurableMetadataServiceExporter(
ApplicationModel applicationModel, MetadataServiceDelegation metadataService) {
ApplicationModel applicationModel,
MetadataServiceDelegation metadataService,
MetadataServiceDelegationV2 metadataServiceV2) {
this.applicationModel = applicationModel;
this.metadataService = metadataService;
this.metadataServiceV2 = metadataServiceV2;
}

public synchronized ConfigurableMetadataServiceExporter export() {
if (serviceConfig == null || !isExported()) {
ExecutorService internalServiceExecutor = applicationModel
.getFrameworkModel()
.getBeanFactory()
.getBean(FrameworkExecutorRepository.class)
.getInternalServiceExecutor();
this.serviceConfig = InternalServiceConfigBuilder.<MetadataService>newBuilder(applicationModel)
.interfaceClass(MetadataService.class)
.protocol(getApplicationConfig().getMetadataServiceProtocol(), METADATA_SERVICE_PROTOCOL_KEY)
.port(getApplicationConfig().getMetadataServicePort(), METADATA_SERVICE_PORT_KEY)
.registryId("internal-metadata-registry")
.executor(internalServiceExecutor)
.ref(metadataService)
.build(configConsumer -> configConsumer.setMethods(generateMethodConfig()));

// export
serviceConfig.export();

metadataService.setMetadataURL(serviceConfig.getExportedUrls().get(0));
if (logger.isInfoEnabled()) {
logger.info("The MetadataService exports urls : " + serviceConfig.getExportedUrls());
if (MetadataServiceVersionUtils.needExportV1(applicationModel)) {
exportV1();
}
if (MetadataServiceVersionUtils.needExportV2(applicationModel)) {
exportV2();
}
} else {
if (logger.isWarnEnabled()) {
Expand All @@ -92,18 +91,77 @@ public synchronized ConfigurableMetadataServiceExporter export() {
return this;
}

private static final String INTERNAL_METADATA_REGISTRY_ID = "internal-metadata-registry";

private void exportV1() {
ExecutorService internalServiceExecutor = applicationModel
.getFrameworkModel()
.getBeanFactory()
.getBean(FrameworkExecutorRepository.class)
.getInternalServiceExecutor();
this.serviceConfig = InternalServiceConfigBuilder.<MetadataService>newBuilder(applicationModel)
.interfaceClass(MetadataService.class)
.protocol(getApplicationConfig().getMetadataServiceProtocol(), METADATA_SERVICE_PROTOCOL_KEY)
.port(getApplicationConfig().getMetadataServicePort(), METADATA_SERVICE_PORT_KEY)
.registryId(INTERNAL_METADATA_REGISTRY_ID)
.executor(internalServiceExecutor)
.ref(metadataService)
.version(V1)
.build(configConsumer -> configConsumer.setMethods(generateMethodConfig()));

serviceConfig.export();
metadataService.setMetadataURL(serviceConfig.getExportedUrls().get(0));

if (logger.isInfoEnabled()) {
logger.info("The MetadataService exports urls : " + serviceConfig.getExportedUrls());
}
}

private void exportV2() {
ExecutorService internalServiceExecutor = applicationModel
.getFrameworkModel()
.getBeanFactory()
.getBean(FrameworkExecutorRepository.class)
.getInternalServiceExecutor();
this.serviceConfigV2 = InternalServiceConfigBuilder.<MetadataServiceV2>newBuilder(applicationModel)
.interfaceClass(MetadataServiceV2.class)
.protocol(TRIPLE, METADATA_SERVICE_PROTOCOL_KEY)
.port(getApplicationConfig().getMetadataServicePort(), METADATA_SERVICE_PORT_KEY)
.registryId(INTERNAL_METADATA_REGISTRY_ID)
.executor(internalServiceExecutor)
.ref(metadataServiceV2)
.version(V2)
.build();

serviceConfigV2.export();
metadataServiceV2.setMetadataUrl(serviceConfigV2.getExportedUrls().get(0));

if (logger.isInfoEnabled()) {
logger.info("The MetadataServiceV2 exports urls : " + serviceConfigV2.getExportedUrls());
}
}

public ConfigurableMetadataServiceExporter unexport() {
if (isExported()) {
serviceConfig.unexport();
serviceConfigV2.unexport();
metadataService.setMetadataURL(null);
}
return this;
}

public boolean isExported() {
private boolean v1Exported() {
return serviceConfig != null && serviceConfig.isExported() && !serviceConfig.isUnexported();
}

private boolean v2Exported() {
return serviceConfigV2 != null && serviceConfigV2.isExported() && !serviceConfigV2.isUnexported();
}

public boolean isExported() {
return v1Exported() || v2Exported();
}

private ApplicationConfig getApplicationConfig() {
return applicationModel.getApplicationConfigManager().getApplication().get();
}
Expand All @@ -129,14 +187,4 @@ private List<MethodConfig> generateMethodConfig() {

return Collections.singletonList(methodConfig);
}

// for unit test
public void setMetadataService(MetadataServiceDelegation metadataService) {
this.metadataService = metadataService;
}

// for unit test
public List<URL> getExportedURLs() {
return serviceConfig != null ? serviceConfig.getExportedUrls() : emptyList();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
import org.apache.dubbo.common.lang.Prioritized;
import org.apache.dubbo.common.utils.StringUtils;
import org.apache.dubbo.registry.client.metadata.MetadataServiceDelegation;
import org.apache.dubbo.registry.client.metadata.MetadataServiceDelegationV2;
import org.apache.dubbo.rpc.model.ApplicationModel;

import static org.apache.dubbo.common.constants.CommonConstants.DEFAULT_METADATA_STORAGE_TYPE;
Expand Down Expand Up @@ -78,10 +79,16 @@ public void setMetadataServiceExporter(ConfigurableMetadataServiceExporter metad
@Override
public synchronized void onModuleStarted(ApplicationModel applicationModel) {
// start metadata service exporter
@Deprecated
MetadataServiceDelegation metadataService =
applicationModel.getBeanFactory().getOrRegisterBean(MetadataServiceDelegation.class);

MetadataServiceDelegationV2 metadataServiceV2 =
applicationModel.getBeanFactory().getOrRegisterBean(MetadataServiceDelegationV2.class);

if (metadataServiceExporter == null) {
metadataServiceExporter = new ConfigurableMetadataServiceExporter(applicationModel, metadataService);
metadataServiceExporter =
new ConfigurableMetadataServiceExporter(applicationModel, metadataService, metadataServiceV2);
// fixme, let's disable local metadata service export at this moment
if (!REMOTE_METADATA_STORAGE_TYPE.equals(getMetadataType(applicationModel))
&& !INTERFACE_REGISTER_MODE.equals(getRegisterMode(applicationModel))) {
Expand Down
Loading
Loading