1
0
mirror of https://gitee.com/mirrors/Spring-Cloud-Alibaba.git synced 2021-06-26 13:25:11 +08:00

nacos 1.1.3 config data not exist

This commit is contained in:
zkzlx 2019-09-11 18:14:00 +08:00
parent 7a63f9fb4e
commit 5585da415a
4 changed files with 59 additions and 31 deletions

View File

@ -16,7 +16,12 @@
*/
package com.alibaba.cloud.nacos;
import com.alibaba.nacos.api.NacosFactory;
import com.alibaba.nacos.api.config.ConfigService;
import com.alibaba.nacos.api.exception.NacosException;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.BeansException;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
@ -25,7 +30,8 @@ import org.springframework.context.ApplicationContextAware;
* @author <a href="mailto:liaochunyhm@live.com">liaochuntao</a>
*/
public class NacosConfigManager implements ApplicationContextAware {
private static final Logger log = LoggerFactory
.getLogger(NacosConfigManager.class);
private ConfigService configService;
public ConfigService getConfigService() {
@ -37,6 +43,11 @@ public class NacosConfigManager implements ApplicationContextAware {
throws BeansException {
NacosConfigProperties properties = applicationContext
.getBean(NacosConfigProperties.class);
configService = properties.configServiceInstance();
try {
configService = NacosFactory.createConfigService(properties.getConfigServiceProperties());
properties.initConfigService(configService);
} catch (NacosException e) {
log.error("create config service error!properties={},e=,", properties, e);
}
}
}

View File

@ -403,13 +403,19 @@ public class NacosConfigProperties {
+ refreshableDataids + '\'' + ", extConfig=" + extConfig + '}';
}
/**
* @see NacosConfigManager#getConfigService()
*/
@Deprecated
public ConfigService configServiceInstance() {
return configService;
}
if (null != configService) {
return configService;
}
public void initConfigService(ConfigService configService){
this.configService = configService;
}
public Properties getConfigServiceProperties(){
Properties properties = new Properties();
properties.put(SERVER_ADDR, Objects.toString(this.serverAddr, ""));
properties.put(ENCODE, Objects.toString(this.encode, ""));
@ -424,7 +430,6 @@ public class NacosConfigProperties {
properties.put(CONFIG_RETRY_TIME, Objects.toString(this.configRetryTime, ""));
properties.put(ENABLE_REMOTE_SYNC_CONFIG,
Objects.toString(this.enableRemoteSyncConfig, ""));
String endpoint = Objects.toString(this.endpoint, "");
if (endpoint.contains(":")) {
int index = endpoint.indexOf(":");
@ -434,14 +439,7 @@ public class NacosConfigProperties {
else {
properties.put(ENDPOINT, endpoint);
}
try {
configService = NacosFactory.createConfigService(properties);
return configService;
}
catch (Exception e) {
log.error("create config service error!properties={},e=,", this, e);
return null;
}
return properties;
}
}

View File

@ -156,8 +156,13 @@ public class NacosPropertySourceLocator implements PropertySourceLocator {
String fileExtension = properties.getFileExtension();
String nacosGroup = properties.getGroup();
// load directly once by default
loadNacosDataIfPresent(compositePropertySource, dataIdPrefix, nacosGroup,
fileExtension, true);
// load with suffix, which have a higher priority than the default
loadNacosDataIfPresent(compositePropertySource,
dataIdPrefix + DOT + fileExtension, nacosGroup, fileExtension, true);
// Loaded with profile, which have a higher priority than the suffix
for (String profile : environment.getActiveProfiles()) {
String dataId = dataIdPrefix + SEP1 + profile + DOT + fileExtension;
loadNacosDataIfPresent(compositePropertySource, dataId, nacosGroup,
@ -168,22 +173,35 @@ public class NacosPropertySourceLocator implements PropertySourceLocator {
private void loadNacosDataIfPresent(final CompositePropertySource composite,
final String dataId, final String group, String fileExtension,
boolean isRefreshable) {
if (NacosContextRefresher.getRefreshCount() != 0) {
NacosPropertySource ps;
if (!isRefreshable) {
ps = NacosPropertySourceRepository.getNacosPropertySource(dataId);
}
else {
ps = nacosPropertySourceBuilder.build(dataId, group, fileExtension, true);
}
NacosPropertySource propertySource = this.loadNacosPropertySource(dataId, group,
fileExtension, isRefreshable);
this.addFirstPropertySource(composite, propertySource, false);
}
composite.addFirstPropertySource(ps);
private NacosPropertySource loadNacosPropertySource(final String dataId,
final String group, String fileExtension, boolean isRefreshable) {
if (NacosContextRefresher.getRefreshCount() != 0) {
if (!isRefreshable) {
return NacosPropertySourceRepository.getNacosPropertySource(dataId);
}
}
else {
NacosPropertySource ps = nacosPropertySourceBuilder.build(dataId, group,
fileExtension, isRefreshable);
composite.addFirstPropertySource(ps);
return nacosPropertySourceBuilder.build(dataId, group, fileExtension,
isRefreshable);
}
/**
* Add the nacos configuration to the first place and maybe ignore the empty
* configuration
*/
private void addFirstPropertySource(final CompositePropertySource composite,
NacosPropertySource nacosPropertySource, boolean ignoreEmpty) {
if (null == nacosPropertySource || null == composite) {
return;
}
if (ignoreEmpty && nacosPropertySource.getSource().isEmpty()) {
return;
}
composite.addFirstPropertySource(nacosPropertySource);
}
private static void checkDataIdFileExtension(String[] dataIdArray) {

View File

@ -24,8 +24,6 @@ import java.util.Properties;
*/
public class NacosDataParserHandler {
private static final NacosDataParserHandler HANDLER = new NacosDataParserHandler();
private AbstractNacosDataParser parser;
private NacosDataParserHandler() {
@ -68,7 +66,10 @@ public class NacosDataParserHandler {
}
public static NacosDataParserHandler getInstance() {
return HANDLER;
return ParserHandler.HANDLER;
}
private static class ParserHandler {
private static final NacosDataParserHandler HANDLER = new NacosDataParserHandler();
}
}