2

Vault からプロパティとパスワードを取得する Spring Boot プロジェクトがあります。Spring Boot 2.4 以降では、チームは構成ファイルのロード方法を変更することを決定しました。これをロードするには、Spring Config Data を使用する必要があります。

ドキュメントと例を読んだ後、サンプルプロジェクトをセットアップしました。ここでは、公式の例application.ymlに従ってボールトの詳細を定義しています。また、環境固有のプロパティとファイルを含む環境固有のファイルをおよびで定義します。dev.ymlprod.yml

Github の例

アプリケーション.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 はプレースホルダーをボールトの実際の値に置き換えるはずでした。ただし、プレースホルダーは次のように表示されます



  .   ____          _            __ _ _
 /\\ / ___'_ __ _ _(_)_ __  __ _ \ \ \ \
( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \
 \\/  ___)| |_)| | | | | || (_| |  ) ) ) )
  '  |____| .__|_| |_|_| |_\__, | / / / /
 =========|_|==============|___/=/_/_/_/
 :: 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}
4

0 に答える 0