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

extConfig未配置的属性,继承主配置 #112

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
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 @@ -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;
Expand Down Expand Up @@ -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;
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand Down Expand Up @@ -128,7 +128,7 @@ private List<NacosPropertySource> 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)));
}
Expand Down