Skip to content

Commit

Permalink
Fix the configuration file loading issue alibaba#4556 alibaba#4541
Browse files Browse the repository at this point in the history
  • Loading branch information
paderlol committed Dec 28, 2020
1 parent 374e7e3 commit 06b2cbc
Show file tree
Hide file tree
Showing 7 changed files with 46 additions and 115 deletions.
Expand Up @@ -23,9 +23,12 @@
import com.alibaba.nacos.common.executor.ThreadPoolManager;
import com.alibaba.nacos.common.notify.NotifyCenter;
import com.alibaba.nacos.sys.env.EnvUtil;
import com.alibaba.nacos.sys.file.FileChangeEvent;
import com.alibaba.nacos.sys.file.FileWatcher;
import com.alibaba.nacos.sys.file.WatchFileCenter;
import com.alibaba.nacos.sys.utils.DiskUtils;
import com.alibaba.nacos.sys.utils.InetUtils;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.boot.env.OriginTrackedMapPropertySource;
Expand All @@ -36,6 +39,8 @@
import java.io.IOException;
import java.nio.file.Paths;
import java.util.List;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.TimeUnit;

Expand All @@ -55,7 +60,9 @@ public class StartingApplicationListener implements NacosApplicationListener {

private static final String LOCAL_IP_PROPERTY_KEY = "nacos.local.ip";

private static final String FIRST_PRE_PROPERTIES = "first_pre";
private static final String NACOS_APPLICATION_CONF = "nacos_application_conf";

private static final Map<String, Object> SOURCES = new ConcurrentHashMap<>();

private ScheduledExecutorService scheduledExecutorService;

Expand All @@ -68,6 +75,8 @@ public void starting() {

@Override
public void environmentPrepared(ConfigurableEnvironment environment) {
makeWorkDir();

injectEnvironment(environment);

loadPreProperties(environment);
Expand All @@ -93,21 +102,18 @@ public void started(ConfigurableApplicationContext context) {

closeExecutor();

logFilePath();

judgeStorageMode(context.getEnvironment());
}

@Override
public void running(ConfigurableApplicationContext context) {
removePreProperties(context.getEnvironment());
}

@Override
public void failed(ConfigurableApplicationContext context, Throwable exception) {
starting = false;

logFilePath();
makeWorkDir();

LOGGER.error("Startup errors : {}", exception);
ThreadPoolManager.shutdown();
Expand All @@ -128,13 +134,36 @@ private void injectEnvironment(ConfigurableEnvironment environment) {

private void loadPreProperties(ConfigurableEnvironment environment) {
try {
environment.getPropertySources().addLast(new OriginTrackedMapPropertySource(FIRST_PRE_PROPERTIES,
EnvUtil.loadProperties(EnvUtil.getApplicationConfFileResource())));
} catch (IOException e) {
SOURCES.putAll(EnvUtil.loadProperties(EnvUtil.getApplicationConfFileResource()));
environment.getPropertySources()
.addLast(new OriginTrackedMapPropertySource(NACOS_APPLICATION_CONF, SOURCES));
registerWatcher();
} catch (Exception e) {
throw new NacosRuntimeException(NacosException.SERVER_ERROR, e);
}
}

private void registerWatcher() throws NacosException {

WatchFileCenter.registerWatcher(EnvUtil.getConfPath(), new FileWatcher() {
@Override
public void onChange(FileChangeEvent event) {
try {
Map<String, ?> tmp = EnvUtil.loadProperties(EnvUtil.getApplicationConfFileResource());
SOURCES.putAll(tmp);
} catch (IOException ignore) {
LOGGER.warn("Failed to monitor file {}", ignore);
}
}

@Override
public boolean interest(String context) {
return StringUtils.contains(context, "application.properties");
}
});

}

private void initSystemProperty() {
if (EnvUtil.getStandaloneMode()) {
System.setProperty(MODE_PROPERTY_KEY_STAND_MODE, "stand alone");
Expand All @@ -152,10 +181,6 @@ private void initSystemProperty() {
System.setProperty(LOCAL_IP_PROPERTY_KEY, InetUtils.getSelfIP());
}

private void removePreProperties(ConfigurableEnvironment environment) {
environment.getPropertySources().remove(FIRST_PRE_PROPERTIES);
}

private void logClusterConf() {
if (!EnvUtil.getStandaloneMode()) {
try {
Expand All @@ -173,7 +198,7 @@ private void closeExecutor() {
}
}

private void logFilePath() {
private void makeWorkDir() {
String[] dirNames = new String[] {"logs", "conf", "data"};
for (String dirName : dirNames) {
LOGGER.info("Nacos Log files: {}", Paths.get(EnvUtil.getNacosHome(), dirName).toString());
Expand Down
5 changes: 2 additions & 3 deletions distribution/bin/startup.cmd
Expand Up @@ -21,8 +21,7 @@ rem added double quotation marks to avoid the issue caused by the folder names c
rem removed the last 5 chars(which means \bin\) to get the base DIR.
set BASE_DIR="%BASE_DIR:~0,-5%"

set DEFAULT_SEARCH_LOCATIONS="classpath:/,classpath:/config/,file:./,file:./config/"
set CUSTOM_SEARCH_LOCATIONS=file:%BASE_DIR%/conf/,%DEFAULT_SEARCH_LOCATIONS%
set CUSTOM_SEARCH_LOCATIONS=file:%BASE_DIR%/conf/

set MODE="cluster"
set FUNCTION_MODE="all"
Expand Down Expand Up @@ -84,7 +83,7 @@ set "NACOS_OPTS=%NACOS_OPTS% -Dnacos.home=%BASE_DIR%"
set "NACOS_OPTS=%NACOS_OPTS% -jar %BASE_DIR%\target\%SERVER%.jar"

rem set nacos spring config location
set "NACOS_CONFIG_OPTS=--spring.config.location=%CUSTOM_SEARCH_LOCATIONS%"
set "NACOS_CONFIG_OPTS=--spring.config.additional-location=%CUSTOM_SEARCH_LOCATIONS%"

rem set nacos log4j file location
set "NACOS_LOG4J_OPTS=--logging.config=%BASE_DIR%/conf/nacos-logback.xml"
Expand Down
5 changes: 2 additions & 3 deletions distribution/bin/startup.sh
Expand Up @@ -78,8 +78,7 @@ done
export JAVA_HOME
export JAVA="$JAVA_HOME/bin/java"
export BASE_DIR=`cd $(dirname $0)/..; pwd`
export DEFAULT_SEARCH_LOCATIONS="classpath:/,classpath:/config/,file:./,file:./config/"
export CUSTOM_SEARCH_LOCATIONS=file:${BASE_DIR}/conf/,${DEFAULT_SEARCH_LOCATIONS}
export CUSTOM_SEARCH_LOCATIONS=file:${BASE_DIR}/conf/

#===========================================================================================
# JVM Configuration
Expand Down Expand Up @@ -117,7 +116,7 @@ JAVA_OPT="${JAVA_OPT} -Dloader.path=${BASE_DIR}/plugins/health,${BASE_DIR}/plugi
JAVA_OPT="${JAVA_OPT} -Dnacos.home=${BASE_DIR}"
JAVA_OPT="${JAVA_OPT} -jar ${BASE_DIR}/target/${SERVER}.jar"
JAVA_OPT="${JAVA_OPT} ${JAVA_OPT_EXT}"
JAVA_OPT="${JAVA_OPT} --spring.config.location=${CUSTOM_SEARCH_LOCATIONS}"
JAVA_OPT="${JAVA_OPT} --spring.config.additional-location=${CUSTOM_SEARCH_LOCATIONS}"
JAVA_OPT="${JAVA_OPT} --logging.config=${BASE_DIR}/conf/nacos-logback.xml"
JAVA_OPT="${JAVA_OPT} --server.max-http-header-size=524288"

Expand Down
5 changes: 3 additions & 2 deletions sys/src/main/java/com/alibaba/nacos/sys/env/EnvUtil.java
Expand Up @@ -366,9 +366,10 @@ public static Resource getApplicationConfFileResource() {
}

private static Resource getCustomFileResource() {
String path = getProperty("spring.config.location");
String path = getProperty("spring.config.additional-location");
InputStream inputStream = null;
if (StringUtils.isNotBlank(path) && path.contains(FILE_PREFIX)) {
String[] paths = path.split(",");
String[] paths = path.split(",", -1);
path = paths[paths.length - 1].substring(FILE_PREFIX.length());
return getRelativePathResource(path, "application.properties");
}
Expand Down
Expand Up @@ -38,6 +38,7 @@
*
* @author <a href="mailto:liaochuntao@live.com">liaochuntao</a>
*/
@Deprecated
public class NacosAutoRefreshPropertySourceLoader implements PropertySourceLoader {

private final Map<String, Object> properties = new ConcurrentHashMap<>(16);
Expand Down
3 changes: 0 additions & 3 deletions sys/src/main/resources/META-INF/spring.factories
@@ -1,6 +1,3 @@
# PropertySource Loaders
org.springframework.boot.env.PropertySourceLoader=\
com.alibaba.nacos.sys.env.NacosAutoRefreshPropertySourceLoader
# EnvironmentPostProcessor
org.springframework.boot.env.EnvironmentPostProcessor=\
com.alibaba.nacos.sys.env.NacosDefaultPropertySourceEnvironmentPostProcessor
Expand Down

This file was deleted.

0 comments on commit 06b2cbc

Please sign in to comment.