diff --git a/dubbo-common/src/main/java/org/apache/dubbo/config/context/ConfigManager.java b/dubbo-common/src/main/java/org/apache/dubbo/config/context/ConfigManager.java index e6d47f89ee0..e4e8748bb95 100644 --- a/dubbo-common/src/main/java/org/apache/dubbo/config/context/ConfigManager.java +++ b/dubbo-common/src/main/java/org/apache/dubbo/config/context/ConfigManager.java @@ -646,7 +646,7 @@ private C addIfAbsent(C config, Map config case IGNORE: { // ignore later config if (logger.isWarnEnabled() && duplicatedConfigs.add(config)) { - logger.warn(msgPrefix + "keep previous config and ignore later config: " + config); + logger.warn(msgPrefix + "keep previous config and ignore later config"); } return oldOne; } @@ -654,7 +654,7 @@ private C addIfAbsent(C config, Map config // clear previous config, add new config configsMap.clear(); if (logger.isWarnEnabled() && duplicatedConfigs.add(config)) { - logger.warn(msgPrefix + "override previous config with later config: " + config); + logger.warn(msgPrefix + "override previous config with later config"); } break; } @@ -663,16 +663,22 @@ private C addIfAbsent(C config, Map config String key = getId(config); if (key == null) { - // generate key for non-default config compatible with API usages - key = generateConfigId(config); + do { + // generate key if id is not set + key = generateConfigId(config); + } while (configsMap.containsKey(key)); } - C existedConfig = configsMap.putIfAbsent(key, config); - if (isEquals(existedConfig, config)) { + C existedConfig = configsMap.get(key); + if (existedConfig != null && !isEquals(existedConfig, config)) { String type = config.getClass().getSimpleName(); - throw new IllegalStateException(String.format("Duplicate %s found, there already has one default %s or more than two %ss have the same id, " + - "you can try to give each %s a different id, key: %s, prev: %s, new: %s", type, type, type, type, key, existedConfig, config)); + logger.warn(String.format("Duplicate %s found, there already has one default %s or more than two %ss have the same id, " + + "you can try to give each %s a different id, override previous config with later config. id: %s, prev: %s, later: %s", + type, type, type, type, key, existedConfig, config)); } + + // override existed config if any + configsMap.put(key, config); return config; } @@ -717,6 +723,9 @@ private AbstractInterfaceConfig checkDuplicatedInterfaceConfig(AbstractInterface if (prevConfig.equals(config)) { // TODO Is there any problem with ignoring duplicate and equivalent but different ReferenceConfig instances? + if (logger.isWarnEnabled() && duplicatedConfigs.add(config)) { + logger.warn("Ignore duplicated and equal config: "+config); + } return prevConfig; } diff --git a/dubbo-config/dubbo-config-api/src/main/java/org/apache/dubbo/config/bootstrap/DubboBootstrap.java b/dubbo-config/dubbo-config-api/src/main/java/org/apache/dubbo/config/bootstrap/DubboBootstrap.java index 7f371fab513..98eb32e92a2 100644 --- a/dubbo-config/dubbo-config-api/src/main/java/org/apache/dubbo/config/bootstrap/DubboBootstrap.java +++ b/dubbo-config/dubbo-config-api/src/main/java/org/apache/dubbo/config/bootstrap/DubboBootstrap.java @@ -84,6 +84,7 @@ import java.util.LinkedHashMap; import java.util.List; import java.util.Map; +import java.util.Optional; import java.util.Set; import java.util.concurrent.CompletableFuture; import java.util.concurrent.ExecutorService; @@ -767,6 +768,7 @@ private void startMetadataCenter() { for (MetadataReportConfig metadataReportConfig : metadataReportConfigs) { ConfigValidationUtils.validateMetadataConfig(metadataReportConfig); if (!metadataReportConfig.isValid()) { + logger.info("Ignore invalid metadata-report config: " + metadataReportConfig); continue; } MetadataReportInstance.init(metadataReportConfig); @@ -797,11 +799,14 @@ private void useRegistryAsConfigCenterIfNecessary() { .stream() .filter(this::isUsedRegistryAsConfigCenter) .map(this::registryAsConfigCenter) - .forEach(configManager::addConfigCenter); + .forEach(configCenter -> { + if (configManager.getConfigCenter(configCenter.getId()).isPresent()) { + return; + } + configManager.addConfigCenter(configCenter); + logger.info("use registry as config-center: " + configCenter); - if (configManager.getConfigCenters().size() > 0) { - logger.info("use registry as config-center: " + configManager.getConfigCenters()); - } + }); } } @@ -813,7 +818,8 @@ private boolean isUsedRegistryAsConfigCenter(RegistryConfig registryConfig) { private ConfigCenterConfig registryAsConfigCenter(RegistryConfig registryConfig) { String protocol = registryConfig.getProtocol(); Integer port = registryConfig.getPort(); - String id = "config-center-" + protocol + "-" + port; + URL url = URL.valueOf(registryConfig.getAddress()); + String id = "config-center-" + protocol + "-" + url.getHost() + "-" + port; ConfigCenterConfig cc = new ConfigCenterConfig(); cc.setId(id); if (cc.getParameters() == null) { @@ -853,11 +859,14 @@ private void useRegistryAsMetadataCenterIfNecessary() { .stream() .filter(this::isUsedRegistryAsMetadataCenter) .map(this::registryAsMetadataCenter) - .forEach(configManager::addMetadataReport); - - if (configManager.getMetadataConfigs().size() > 0) { - logger.info("use registry as metadata-center: " + configManager.getMetadataConfigs()); - } + .forEach(metadataReportConfig -> { + Optional configOptional = configManager.getConfig(MetadataReportConfig.class, metadataReportConfig.getId()); + if (configOptional.isPresent()) { + return; + } + configManager.addMetadataReport(metadataReportConfig); + logger.info("use registry as metadata-center: " + metadataReportConfig); + }); } } @@ -918,8 +927,8 @@ private boolean supportsExtension(Class extensionClass, String name) { private MetadataReportConfig registryAsMetadataCenter(RegistryConfig registryConfig) { String protocol = registryConfig.getProtocol(); - Integer port = registryConfig.getPort(); - String id = "metadata-center-" + protocol + "-" + port; + URL url = URL.valueOf(registryConfig.getAddress()); + String id = "metadata-center-" + protocol + "-" + url.getHost() + "-" + url.getPort(); MetadataReportConfig metadataReportConfig = new MetadataReportConfig(); metadataReportConfig.setId(id); if (metadataReportConfig.getParameters() == null) {