在下面使用sample.xml
成功读取资源getResourceAsStream
中的文件。
@Value("${sample}")
private String sample;
InputStream is = this.getClass().getClassLoader().getResourceAsStream(sample);
resources / application.properties
sample=sample.xml
不过,在将application.properties
和sample.xml
外部化为 consul 之后,不再找到sample.xml。
还在日志中仅打印检测到application.properties,而不打印在myapp中也显示为myapp/application/sample.xml
的sample.xml
b.c.PropertySourceBootstrapConfiguration : Located property source: CompositePropertySource {name='consul', propertySources=[ConsulFilesPropertySource {name='myapp/application/application.properties'}]}
所以剩下的唯一方法是从领事那里下载文件,然后打开一个输入流。我们不能在第一个示例中继续使用getResourceAsStream
来使用外部文件进行咨询,就像从s resources
目录中进行读取一样吗?
http://${spring.cloud.consul.host}:${spring.cloud.consul.port}/v1/kv/${spring.cloud.consul.config.prefix}/sample.xml?raw=true&xml
bootstrap.yml
spring:
application:
name: myapp
cloud:
consul:
config:
enabled: true
format: files
fail-fast: true
prefix: myapp/application
host: localhost
port: 8500