我有一个Spring Boot项目,从vault
获取属性和密码。在Spring Boot 2.4及以后版本中,团队决定改变加载配置文件的方式。现在我们需要使用Spring配置数据来加载这些文件。
在阅读了文档和示例后,我建立了一个示例项目。在这里,我按照官方的例子在application.yml中定义了vault
的细节。并在dev.yml
和prod.yml
中定义了环境特定的文件,其中包含环境特定的属性和文件。
application.yml
server:
port: 8081
spring:
application:
name: pres
cloud:
vault:
authentication: TOKEN
uri: ${VAULT_URL}
connection-timeout: 5000
read-timeout: 15000
kv:
enabled: true
backend: secret
application-name: app/pres
token: ${TOKEN}
config:
import: vault://secret/app/pres
---
spring:
config:
activate:
on-profile: "dev"
import: dev.yml
---
spring:
config:
activate:
on-profile: "prod"
import: prod.yml
dev.yml
spring:
datasource:
url: "jdbc:mysql://localhost/dev"
username: "dev"
password: "dev"
#### ELK Logging
elk:
logging:
rabbitmq:
hostname: ${pres.elk.logging.rabbitmq.hostname}
port: 5672
username: ${pres.elk.logging.rabbitmq.username}
password: ${pres.elk.logging.rabbitmq.password}
projectVersion: '@project.version@'
prod.yml
spring:
datasource:
url: "jdbc:mysql://localhost:3306/prod"
username: "prod"
password: "prod"
#### ELK Logging
elk:
logging:
rabbitmq:
hostname: ${pres.elk.logging.rabbitmq.hostname}
port: 5672
username: ${pres.elk.logging.rabbitmq.username}
password: ${pres.elk.logging.rabbitmq.password}
projectVersion: '@project.version@'
因此,当我启动应用程序时,Spring Boot应该用vault
中的实际值替换占位符。但是,我只看到占位符,如下所示
. ____ _ __ _ _
/\\ / ___'_ __ _ _(_)_ __ __ _ \ \ \ \
( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \
\\/ ___)| |_)| | | | | || (_| | ) ) ) )
' |____| .__|_| |_|_| |_\__, | / / / /
=========|_|==============|___/=/_/_/_/
:: Spring Boot :: (v2.5.2)
2021-06-26 12:14:03.546 INFO 69016 --- [ restartedMain] c.e.profiletest.ProfileTestApplication : Starting ProfileTestApplication using Java 16.0.1 on pjmacbookpro with PID 69016 (/Users/pjadda/IdeaProjects/ProfileTest/target/classes started by pjadda in /Users/pjadda/IdeaProjects/ProfileTest)
2021-06-26 12:14:03.548 INFO 69016 --- [ restartedMain] c.e.profiletest.ProfileTestApplication : The following profiles are active: dev
2021-06-26 12:14:03.593 INFO 69016 --- [ restartedMain] o.s.v.c.e.LeaseAwareVaultPropertySource : Vault location [secret/app/pres] not resolvable: Not found
2021-06-26 12:14:03.594 INFO 69016 --- [ restartedMain] .e.DevToolsPropertyDefaultsPostProcessor : Devtools property defaults active! Set 'spring.devtools.add-properties' to 'false' to disable
2021-06-26 12:14:03.594 INFO 69016 --- [ restartedMain] .e.DevToolsPropertyDefaultsPostProcessor : For additional web related logging consider setting the 'logging.level.web' property to 'DEBUG'
2021-06-26 12:14:04.176 INFO 69016 --- [ restartedMain] o.s.cloud.context.scope.GenericScope : BeanFactory id=6bdb5f01-aa31-3158-8281-edfa1a02ac83
2021-06-26 12:14:04.603 INFO 69016 --- [ restartedMain] o.s.b.w.embedded.tomcat.TomcatWebServer : Tomcat initialized with port(s): 8081 (http)
2021-06-26 12:14:04.615 INFO 69016 --- [ restartedMain] o.apache.catalina.core.StandardService : Starting service [Tomcat]
2021-06-26 12:14:04.615 INFO 69016 --- [ restartedMain] org.apache.catalina.core.StandardEngine : Starting Servlet engine: [Apache Tomcat/9.0.48]
2021-06-26 12:14:04.680 INFO 69016 --- [ restartedMain] o.a.c.c.C.[Tomcat].[localhost].[/] : Initializing Spring embedded WebApplicationContext
2021-06-26 12:14:04.680 INFO 69016 --- [ restartedMain] w.s.c.ServletWebServerApplicationContext : Root WebApplicationContext: initialization completed in 1085 ms
2021-06-26 12:14:05.032 INFO 69016 --- [ restartedMain] o.s.b.d.a.OptionalLiveReloadServer : LiveReload server is running on port 35729
2021-06-26 12:14:05.121 INFO 69016 --- [ restartedMain] o.s.b.w.embedded.tomcat.TomcatWebServer : Tomcat started on port(s): 8081 (http) with context path ''
2021-06-26 12:14:05.135 INFO 69016 --- [ restartedMain] c.e.profiletest.ProfileTestApplication : Started ProfileTestApplication in 2.79 seconds (JVM running for 3.622)
2021-06-26 12:14:05.465 INFO 69016 --- [nio-8081-exec-1] o.a.c.c.C.[Tomcat].[localhost].[/] : Initializing Spring DispatcherServlet 'dispatcherServlet'
2021-06-26 12:14:05.465 INFO 69016 --- [nio-8081-exec-1] o.s.web.servlet.DispatcherServlet : Initializing Servlet 'dispatcherServlet'
2021-06-26 12:14:05.466 INFO 69016 --- [nio-8081-exec-1] o.s.web.servlet.DispatcherServlet : Completed initialization in 1 ms
username:${pres.elk.logging.rabbitmq.username}
password:${pres.elk.logging.rabbitmq.password}
StackOverflow:Spring Cloud Vault with Spring Boot 2.4+ config not working as expected - Stack Overflow