【问题标题】:Spring boot on Tomcat with external configuration使用外部配置在 Tomcat 上进行 Spring Boot
【发布时间】:2017-07-14 18:11:59
【问题描述】:

我在stackoverflow上找不到这个问题的答案,所以我在这里问,所以我可以得到一些想法。

我有一个 Spring Boot 应用程序,已在 Tomcat 8 上部署为 war 包。我遵循了这个指南 Create a deployable war file,它似乎工作得很好。

但是我目前遇到的问题是能够将配置外部化,以便我可以将配置作为 puppet 模板进行管理。

在我所拥有的项目中,

src/main/resources
                  -- config/application.yml
                  -- config/application.dev.yml
                  -- config/application.prod.yml
                  -- logback-spring.yml

那么我怎么可能在外部加载 config/application.dev.ymlconfig/application.prod.yml 并仍然保留 config/application.yml ? (包含默认属性,包括spring.application.name

我已经读到配置是按这个顺序加载的,

  1. 当前目录的 /config 子目录。
  2. 当前目录
  3. 一个类路径 /config 包
  4. 类路径根

因此我尝试从/opt/apache-tomcat/lib 加载配置文件无济于事。

到目前为止的效果

通过export CATALINA_OPTS="-Dspring.config.location=/opt/apache-tomcat/lib/application.dev.yml"加载

然而我想知道的是

  1. 找出通过 /opt/apache-tomcat/lib 类路径加载不起作用的原因。
  2. 有没有更好的方法来实现这一点?

【问题讨论】:

    标签: spring-boot configuration war tomcat8


    【解决方案1】:

    您对加载顺序是正确的。根据Spring boot documentation

    SpringApplication 将从以下位置的 application.properties 文件中加载属性并将它们添加到 Spring 环境中:

    • 当前目录的 /config 子目录。
    • 当前目录
    • 类路径 /config 包
    • 类路径根

    列表按优先级排序(在列表中较高位置定义的属性会覆盖在较低位置定义的属性)。

    [注意]
    您还可以使用 YAML ('.yml') 文件来替代 '.properties'。

    这意味着如果您将 application.yml 文件放置到 /opt/apache-tomcat/lib/opt/apache-tomcat/lib/config,它将被加载。

    1. 找出无法通过 /opt/apache-tomcat/lib 类路径加载的原因。

    但是,如果您将 application.dev.yml 放置到该路径,它将不会被加载,因为 application.dev.yml 不是 Spring 正在寻找的文件名。如果您希望 Spring 也读取该文件,则需要将其作为选项提供
    --spring.config.name=application.dev-Dspring.config.name=application.dev
    但我不建议使用此方法

    1. 有没有更好的方法来实现这一点?

    是的。使用Spring profile-specific properties。您可以将文件从application.dev.yml 重命名为application-dev.yml,并提供-Dspring.profiles.active=dev 选项。 Spring 将读取application-dev.ymlapplication.yml 文件,并且配置文件特定的配置将覆盖默认配置。

    我建议在每个相应的服务器/tomcat 实例上添加-Dspring.profiles.active=dev(或prodto CATALINA_OPTS

    【讨论】:

      【解决方案2】:
          I have finally simplified solution for reading custom properties from external location i.e outside of the spring boot project. Please refer to below steps.
      Note: This Solution created and executed windows.Few commands and folders naming convention may vary if you are deploying application on other operating system like Linux..etc.
      
               1. Create a folder in suitable drive.
               eg: D:/boot-ext-config
      
               2. Create a .properties file in above created folder  with relevant property key/values and name it as you wish.I created dev.properties for testing purpose.
               eg :D:/boot-ext-config/dev.properties
      
                  sample values:
                  dev.hostname=www.example.com
      
      
              3. Create a java class in your application as below
              ------------------------------------------------------
              import org.springframework.boot.context.properties.ConfigurationProperties;
              import org.springframework.context.annotation.PropertySource;
      
              @PropertySource("classpath:dev.properties")
              @ConfigurationProperties("dev")
              public class ConfigProperties {
                  private String hostname;
      
                  //setters and getters
              }
              --------------------------------------------
               4. Add @EnableConfigurationProperties(ConfigProperties.class) to SpringBootApplication as below
              --------------------------------------------
              @SpringBootApplication
              @EnableConfigurationProperties(ConfigProperties.class)
              public class RestClientApplication {
                  public static void main(String[] args)  {
                       SpringApplication.run(RestClientApplication.class, args);
              }
              }
              ---------------------------------------------------------
              5. In Controller classes we can inject the instance using @Autowired and fetch properties
      
              @Autowired
                  private ConfigProperties configProperties;
      
              and access properties using getter method 
             System.out.println("**********hostName******+configProperties.getHostName());
      
      Build your  spring boot maven project and run the below command to start application.
      
      -> set SPRING_CONFIG_LOCATION=<path to your properties file>
      
      ->java -jar app-name.jar 
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2020-06-03
        • 1970-01-01
        • 1970-01-01
        • 2017-11-26
        • 2018-05-17
        • 1970-01-01
        • 2017-11-24
        • 2016-03-28
        相关资源
        最近更新 更多