Fix issue of metadata only registred to one nacos namespace (3.2.x) (#13439)

* Fix nacos metadata namespace

* Fix code style

---------

Co-authored-by: nameless <x1544669126@gmail.com>
This commit is contained in:
namelessssssssssss 2023-11-30 19:19:29 +08:00 committed by GitHub
parent b38a2815a1
commit 6dfddbfb2b
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 35 additions and 5 deletions

View File

@ -28,4 +28,6 @@ public interface MetadataConstants {
String REPORT_CONSUMER_URL_KEY = "report-consumer-definition";
String PATH_SEPARATOR = "/";
String NAMESPACE_KEY = "namespace";
}

View File

@ -19,6 +19,7 @@ package org.apache.dubbo.metadata.report;
import org.apache.dubbo.common.URL;
import org.apache.dubbo.common.URLBuilder;
import org.apache.dubbo.common.resource.Disposable;
import org.apache.dubbo.common.utils.StringUtils;
import org.apache.dubbo.config.MetadataReportConfig;
import org.apache.dubbo.metadata.report.support.NopMetadataReport;
import org.apache.dubbo.rpc.model.ApplicationModel;
@ -35,6 +36,7 @@ import static org.apache.dubbo.common.constants.CommonConstants.DEFAULT_METADATA
import static org.apache.dubbo.common.constants.CommonConstants.PORT_KEY;
import static org.apache.dubbo.common.constants.CommonConstants.REGISTRY_LOCAL_FILE_CACHE_ENABLED;
import static org.apache.dubbo.common.utils.StringUtils.isEmpty;
import static org.apache.dubbo.metadata.MetadataConstants.NAMESPACE_KEY;
import static org.apache.dubbo.metadata.report.support.Constants.METADATA_REPORT_KEY;
/**
@ -102,18 +104,26 @@ public class MetadataReportInstance implements Disposable {
url = url.addParameterIfAbsent(
REGISTRY_LOCAL_FILE_CACHE_ENABLED,
String.valueOf(applicationModel.getCurrentConfig().getEnableFileCache()));
String relatedRegistryId = isEmpty(config.getRegistry())
? (isEmpty(config.getId()) ? DEFAULT_KEY : config.getId())
: config.getRegistry();
// RegistryConfig registryConfig = applicationModel.getConfigManager().getRegistry(relatedRegistryId)
// .orElseThrow(() -> new IllegalStateException("Registry id " + relatedRegistryId + " does not
// exist."));
MetadataReport metadataReport = metadataReportFactory.getMetadataReport(url);
if (metadataReport != null) {
metadataReports.put(relatedRegistryId, metadataReport);
metadataReports.put(getRelatedRegistryId(config, url), metadataReport);
}
}
private String getRelatedRegistryId(MetadataReportConfig config, URL url) {
String relatedRegistryId = isEmpty(config.getRegistry())
? (isEmpty(config.getId()) ? DEFAULT_KEY : config.getId())
: config.getRegistry();
String namespace = url.getParameter(NAMESPACE_KEY);
if (!StringUtils.isEmpty(namespace)) {
relatedRegistryId += ":" + namespace;
}
return relatedRegistryId;
}
public Map<String, MetadataReport> getMetadataReports(boolean checked) {
return metadataReports;
}

View File

@ -50,7 +50,7 @@ public abstract class AbstractMetadataReportFactory implements MetadataReportFac
@Override
public MetadataReport getMetadataReport(URL url) {
url = url.setPath(MetadataReport.class.getName()).removeParameters(EXPORT_KEY, REFER_KEY);
String key = url.toServiceString();
String key = toMetadataReportKey(url);
MetadataReport metadataReport = serviceStoreMap.get(key);
if (metadataReport != null) {
@ -88,6 +88,10 @@ public abstract class AbstractMetadataReportFactory implements MetadataReportFac
}
}
protected String toMetadataReportKey(URL url) {
return url.toServiceString();
}
@Override
public void destroy() {
lock.lock();

View File

@ -17,9 +17,12 @@
package org.apache.dubbo.metadata.store.nacos;
import org.apache.dubbo.common.URL;
import org.apache.dubbo.common.utils.StringUtils;
import org.apache.dubbo.metadata.report.MetadataReport;
import org.apache.dubbo.metadata.report.support.AbstractMetadataReportFactory;
import static org.apache.dubbo.metadata.MetadataConstants.NAMESPACE_KEY;
/**
* metadata report factory impl for nacos
*/
@ -28,4 +31,15 @@ public class NacosMetadataReportFactory extends AbstractMetadataReportFactory {
protected MetadataReport createMetadataReport(URL url) {
return new NacosMetadataReport(url);
}
@Override
protected String toMetadataReportKey(URL url) {
String namespace = url.getParameter(NAMESPACE_KEY);
if (!StringUtils.isEmpty(namespace)) {
return URL.valueOf(url.toServiceString())
.addParameter(NAMESPACE_KEY, namespace)
.toString();
}
return super.toMetadataReportKey(url);
}
}