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

Fix the configuration file loading issue #4556 #4541 #4582

Closed
wants to merge 11 commits into from
Original file line number Diff line number Diff line change
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 Down Expand Up @@ -100,7 +107,6 @@ public void started(ConfigurableApplicationContext context) {

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

@Override
Expand All @@ -109,7 +115,7 @@ public void failed(ConfigurableApplicationContext context, Throwable exception)

logFilePath();

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

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

private void registerWatcher() {
try {
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) {

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why just ignore the Exception ?

}
}

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

}
}

private void initSystemProperty() {
if (EnvUtil.getStandaloneMode()) {
System.setProperty(MODE_PROPERTY_KEY_STAND_MODE, "stand alone");
Expand All @@ -152,10 +183,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 Down
5 changes: 2 additions & 3 deletions distribution/bin/startup.cmd
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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: 1 addition & 2 deletions sys/src/main/resources/META-INF/spring.factories
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
# 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