From 6b0753cd6289b5de1569550c6076d9372e2ab594 Mon Sep 17 00:00:00 2001 From: "jh.ma" Date: Sat, 7 Mar 2020 22:16:42 +0800 Subject: [PATCH] =?UTF-8?q?extConfig=E6=9C=AA=E9=85=8D=E7=BD=AE=E7=9A=84?= =?UTF-8?q?=E5=B1=9E=E6=80=A7=EF=BC=8C=E7=BB=A7=E6=89=BF=E4=B8=BB=E9=85=8D?= =?UTF-8?q?=E7=BD=AE=EF=BC=8C=E5=87=8F=E5=B0=91=E9=85=8D=E7=BD=AE=E5=8F=82?= =?UTF-8?q?=E6=95=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../properties/NacosConfigProperties.java | 12 ++-- .../util/NacosConfigPropertiesUtils.java | 68 ++++++++++++++++--- .../nacos/config/util/NacosConfigUtils.java | 4 +- 3 files changed, 66 insertions(+), 18 deletions(-) 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))); }