diff --git a/nacos-config-spring-boot-autoconfigure/src/main/java/com/alibaba/boot/nacos/config/properties/NacosConfigProperties.java b/nacos-config-spring-boot-autoconfigure/src/main/java/com/alibaba/boot/nacos/config/properties/NacosConfigProperties.java index 86a02390f..0d673ef77 100644 --- a/nacos-config-spring-boot-autoconfigure/src/main/java/com/alibaba/boot/nacos/config/properties/NacosConfigProperties.java +++ b/nacos-config-spring-boot-autoconfigure/src/main/java/com/alibaba/boot/nacos/config/properties/NacosConfigProperties.java @@ -280,9 +280,9 @@ public static class Config { private String configRetryTime; - private boolean autoRefresh = false; + private Boolean autoRefresh; - private boolean enableRemoteSyncConfig = false; + private Boolean enableRemoteSyncConfig; public String getServerAddr() { return serverAddr; @@ -388,19 +388,19 @@ public void setConfigRetryTime(String configRetryTime) { this.configRetryTime = configRetryTime; } - public boolean isAutoRefresh() { + public Boolean getAutoRefresh() { return autoRefresh; } - public void setAutoRefresh(boolean autoRefresh) { + public void setAutoRefresh(Boolean autoRefresh) { this.autoRefresh = autoRefresh; } - public boolean isEnableRemoteSyncConfig() { + public Boolean getEnableRemoteSyncConfig() { return enableRemoteSyncConfig; } - public void setEnableRemoteSyncConfig(boolean enableRemoteSyncConfig) { + public void setEnableRemoteSyncConfig(Boolean enableRemoteSyncConfig) { this.enableRemoteSyncConfig = enableRemoteSyncConfig; } } diff --git a/nacos-config-spring-boot-autoconfigure/src/main/java/com/alibaba/boot/nacos/config/util/NacosConfigPropertiesUtils.java b/nacos-config-spring-boot-autoconfigure/src/main/java/com/alibaba/boot/nacos/config/util/NacosConfigPropertiesUtils.java index c536e9ff8..04b2cd468 100644 --- a/nacos-config-spring-boot-autoconfigure/src/main/java/com/alibaba/boot/nacos/config/util/NacosConfigPropertiesUtils.java +++ b/nacos-config-spring-boot-autoconfigure/src/main/java/com/alibaba/boot/nacos/config/util/NacosConfigPropertiesUtils.java @@ -25,6 +25,10 @@ import org.springframework.boot.context.properties.bind.Binder; import org.springframework.core.ResolvableType; import org.springframework.core.env.ConfigurableEnvironment; +import org.springframework.util.CollectionUtils; +import org.springframework.util.StringUtils; + +import java.util.Objects; /** * Springboot used to own property binding configured binding @@ -37,15 +41,59 @@ public class NacosConfigPropertiesUtils { private static final Logger logger = LoggerFactory .getLogger(NacosConfigPropertiesUtils.class); - public static NacosConfigProperties buildNacosConfigProperties( - ConfigurableEnvironment environment) { - NacosConfigProperties nacosConfigProperties = new NacosConfigProperties(); - Binder binder = Binder.get(environment); - ResolvableType type = ResolvableType.forClass(NacosConfigProperties.class); - Bindable target = Bindable.of(type).withExistingValue(nacosConfigProperties); - binder.bind(NacosConfigConstants.PREFIX, target); - logger.info("nacosConfigProperties : {}", nacosConfigProperties); - return nacosConfigProperties; - } + public static NacosConfigProperties buildNacosConfigProperties( + ConfigurableEnvironment environment) { + NacosConfigProperties nacosConfigProperties = new NacosConfigProperties(); + Binder binder = Binder.get(environment); + ResolvableType type = ResolvableType.forClass(NacosConfigProperties.class); + Bindable target = Bindable.of(type).withExistingValue(nacosConfigProperties); + binder.bind(NacosConfigConstants.PREFIX, target); + //extConfig继承mainConfig的参数设置(除dataId和dataIds) + if (!CollectionUtils.isEmpty(nacosConfigProperties.getExtConfig())) { + nacosConfigProperties.getExtConfig().forEach(ext -> { + if (StringUtils.isEmpty(ext.getServerAddr())) { + ext.setServerAddr(nacosConfigProperties.getServerAddr()); + } + if (StringUtils.isEmpty(ext.getEndpoint())) { + ext.setEndpoint(nacosConfigProperties.getEndpoint()); + } + if (StringUtils.isEmpty(ext.getNamespace())) { + ext.setNamespace(nacosConfigProperties.getNamespace()); + } + if (StringUtils.isEmpty(ext.getAccessKey())) { + ext.setAccessKey(nacosConfigProperties.getAccessKey()); + } + if (StringUtils.isEmpty(ext.getSecretKey())) { + ext.setSecretKey(nacosConfigProperties.getSecretKey()); + } + if (StringUtils.isEmpty(ext.getRamRoleName())) { + ext.setRamRoleName(nacosConfigProperties.getRamRoleName()); + } + if (StringUtils.isEmpty(ext.getGroup())) { + ext.setGroup(nacosConfigProperties.getGroup()); + } + if (Objects.isNull(ext.getType())) { + ext.setType(nacosConfigProperties.getType()); + } + if (StringUtils.isEmpty(ext.getMaxRetry())) { + ext.setMaxRetry(nacosConfigProperties.getMaxRetry()); + } + if (StringUtils.isEmpty(ext.getConfigLongPollTimeout())) { + ext.setConfigLongPollTimeout(nacosConfigProperties.getConfigLongPollTimeout()); + } + if (StringUtils.isEmpty(ext.getConfigRetryTime())) { + ext.setConfigRetryTime(nacosConfigProperties.getConfigRetryTime()); + } + if (Objects.isNull(ext.getAutoRefresh())) { + ext.setAutoRefresh(nacosConfigProperties.isAutoRefresh()); + } + if (Objects.isNull(ext.getEnableRemoteSyncConfig())) { + ext.setEnableRemoteSyncConfig(nacosConfigProperties.isEnableRemoteSyncConfig()); + } + }); + } + logger.info("nacosConfigProperties : {}", nacosConfigProperties); + return nacosConfigProperties; + } } diff --git a/nacos-config-spring-boot-autoconfigure/src/main/java/com/alibaba/boot/nacos/config/util/NacosConfigUtils.java b/nacos-config-spring-boot-autoconfigure/src/main/java/com/alibaba/boot/nacos/config/util/NacosConfigUtils.java index 2bf7bc335..da392d7a4 100644 --- a/nacos-config-spring-boot-autoconfigure/src/main/java/com/alibaba/boot/nacos/config/util/NacosConfigUtils.java +++ b/nacos-config-spring-boot-autoconfigure/src/main/java/com/alibaba/boot/nacos/config/util/NacosConfigUtils.java @@ -92,7 +92,7 @@ private Properties buildSubNacosProperties(Properties globalProperties, config.getServerAddr(), config.getNamespace(), config.getEndpoint(), config.getSecretKey(), config.getAccessKey(), config.getRamRoleName(), config.getConfigLongPollTimeout(), config.getConfigRetryTime(), - config.getMaxRetry(), config.isEnableRemoteSyncConfig()); + config.getMaxRetry(), config.getEnableRemoteSyncConfig()); NacosPropertiesBuilder.merge(sub, globalProperties); return sub; } @@ -128,7 +128,7 @@ private List reqSubNacosConfig( dataIds.add(config.getDataId()); } final String groupName = environment.resolvePlaceholders(config.getGroup()); - final boolean isAutoRefresh = config.isAutoRefresh(); + final boolean isAutoRefresh = config.getAutoRefresh(); return new ArrayList<>(Arrays.asList(reqNacosConfig(subConfigProperties, dataIds.toArray(new String[0]), groupName, type, isAutoRefresh))); }