0%

解决WARN:No URLs will be polled as dynamic configuration sources.

在spring boot中整合Eureka时,出现如下警告:

1
WARN 78340 --- [           main] c.n.c.sources.URLConfigurationSource     : No URLs will be polled as dynamic configuration sources.

解决方法

src/main/resources/添加一个空的文件config.properties,如图
目录结构

原因

这个警告在类com.netflix.config.sources.URLConfigurationSource中,如下代码:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
public URLConfigurationSource() {
List<URL> urlList = new ArrayList();
URL configFromClasspath = this.getConfigFileFromClasspath();
if (configFromClasspath != null) {
urlList.add(configFromClasspath);
}

String[] fileNames = getDefaultFileSources();
if (fileNames.length != 0) {
urlList.addAll(Arrays.asList(createUrls(fileNames)));
}

if (urlList.size() == 0) {
this.configUrls = new URL[0];
logger.warn("No URLs will be polled as dynamic configuration sources.");
logger.info("To enable URLs as dynamic configuration sources, define System property archaius.configurationSource.additionalUrls or make " + DEFAULT_CONFIG_FILE_FROM_CLASSPATH + " available on classpath.");
} else {
this.configUrls = (URL[])urlList.toArray(new URL[urlList.size()]);
logger.info("URLs to be used as dynamic configuration source: " + urlList);
}
}

可以看到当urlList的size为0时,就会出现这个警告,然后urlList的来源有两个地方:
getConfigFileFromClasspath()

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
private URL getConfigFileFromClasspath() {
URL url = null;
ClassLoader loader = Thread.currentThread().getContextClassLoader();
if (loader != null) {
url = loader.getResource(DEFAULT_CONFIG_FILE_FROM_CLASSPATH);
}

if (url == null) {
url = ClassLoader.getSystemResource(DEFAULT_CONFIG_FILE_FROM_CLASSPATH);
}

if (url == null) {
url = URLConfigurationSource.class.getResource(DEFAULT_CONFIG_FILE_FROM_CLASSPATH);
}

return url;
}

DEFAULT_CONFIG_FILE_FROM_CLASSPATH的定义如下

1
2
3
4
public static final String DEFAULT_CONFIG_FILE_FROM_CLASSPATH =
System.getProperty("archaius.configurationSource.defaultFileName") == null
? "config.properties"
: System.getProperty("archaius.configurationSource.defaultFileName");

从如上代码我们可知 当archaius.configurationSource.defaultFileName属性存在时getConfigFileFromClasspath() 就通过指定的文件名获取文件,否则就获取config.properties文件。
getDefaultFileSources()

1
2
3
4
5
6
7
8
9
10
11
private static final String[] getDefaultFileSources() {
String name = System.getProperty("archaius.configurationSource.additionalUrls");
String[] fileNames;
if (name != null) {
fileNames = name.split(",");
} else {
fileNames = new String[0];
}

return fileNames;
}

这段代码更简单,如果archaius.configurationSource.additionalUrls属性存在,就将它的值分割成String数组。

综上,如果同时满足如下条件将会产生警告:

  1. archaius.configurationSource.defaultFileName没有设置
  2. config.properties 文件不存在
  3. archaius.configurationSource.additionalUrls没有设置

所以我们的解决方案是创建config.properties文件破坏第2条,就不会有警告了,还有就是这个东西是在archaius-core这个依赖中,如果感兴趣可以自行去研究一下Netflix Archaius