带有 Spring Boot 2.4+ 配置的 Spring Cloud Vault 无法按预期工作

问题描述

我有一个从保险库获取属性和密码的 Spring Boot 项目。在 Spring Boot 2.4 及更高版本中,团队决定改变加载配置文件的方式。现在我们需要使用 Spring Config Data 来加载它。

阅读文档和示例后,我设置了 sample project。在这里,我按照 official example 定义 application.yml 上的保险库详细信息。并在 dev.ymlprod.yml 中定义了特定于环境的文件,其中包含特定于环境的属性和文件。

Github Example

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}

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)