从领事读值,同时引导春天开机

问题描述 投票:0回答:1

我的问题是有没有办法来获取一定的价值,并注入他们,而应用程序来达到bootstrap.yml。

我有这样的配置文件:

spring:
  application:
    name: myApp

  cloud:
    consul:
      enabled: true
      host: localhost
      port: 8500
      config:
        enabled: true

  datasource:
    url: jdbc:oracle:thin:@localhost:1111:XXXX
    username: ${nameOfVariable1}
    password: ${nameOfVariable1}
    driver-class-name: oracle.jdbc.OracleDriver

例如,我需要配置嵌入式tomcat的端口,或数据库凭据,我不想把它硬编码使用.yml属性文件,而不是我要放一些变量名使用.yml所以Spring会去总领事带来的价值。可能吗?

spring spring-boot consul spring-cloud-consul
1个回答
1
投票

您可以使用Spring Cloud Consul Config项目,帮助在特殊的“引导”阶段配置加载到Spring环境。

3个步骤:

  1. POM添加依赖:spring-cloud-starter-consul-config
  2. 使领事配置:spring.cloud.consul.config.enabled=true
  3. 增加一些配置在领事KV在特定的文件夹,如关键:config/testConsulApp/server.port,值:8081

然后启动样本Web应用程序,它会听8081。

spring cloud consul doc更多的细节。

demo code here

© www.soinside.com 2019 - 2024. All rights reserved.