在Spring Boot中外部化配置,在同一容器中运行多个应用程序 [英] Externalizing configuration in Spring Boot with multiple applications running in the same container

查看:260
本文介绍了在Spring Boot中外部化配置,在同一容器中运行多个应用程序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在构建多个Spring Boot应用程序,这些应用程序将部署在同一个servlet容器中。但是我很难让Spring Boot按照我想要的方式使用外部配置文件,而不像框架那样。

I'm building multiple Spring Boot applications which will be deployed on the same servlet container. But I'm having difficulties getting Spring Boot to work with externalized configuration files the way I want, and not like the framework wants.

情况:


  • 多个Spring Boot应用程序将部署在单个servlet容器(WAR文件)上

  • 配置文件的位置将通过JVM属性设置 spring.config.location

  • 嵌入式部署不是一个选项

问题:

由于应用程序部署在相同的JVM,属性 spring.config.location 对于所有应用程序具有相同的值。我希望我们的应用程序都使用相同的配置文件命名(application.properties),因此指定 spring.config.name 不是一个选项。

As the applications are deployed on the same JVM, the property spring.config.location has the same value for all applications. I want our applications to all use the same configuration file naming (application.properties), so specifying spring.config.name is not an option.

我想要的是什么:


  • 否需要设置 spring.config.name ,因为配置名称应该在所有应用程序中标准化(常量)

  • 外部化配置属性应该覆盖我已部署的WAR中打包的application.properties中的值

  • 配置文件特定的配置(应用程序 - {profile})应该是可能的

  • 没有硬编码的配置代码中的位置

  • 在每个应用程序目录布局中组织配置文件:

  • no need to set spring.config.name as the configuration name should be standardized accross all our applications (constant)
  • the externalized configuration properties should override values from application.properties packaged inside my deployed WAR
  • profile specific configurations (application-{profile}) should be possible
  • no hardcoded config locations in code
  • organizing configuration files in a per-application directory layout:

$ {spring.config.location } /app1/application.properties
$ {spring.config.location} /app2/application.properties
$ {spring.config.location} /app3/application.properties

${spring.config.location}/app1/application.properties ${spring.config.location}/app2/application.properties ${spring.config.location}/app3/application.properties

问题:

是否存在某种影响机制或覆盖外部配置文件的加载或解析?

Is there some mechanism to influence or override loading or resolving of external configuration files?

还有其他的认可吗? ches以获得所需的结果?

Are there other approaches to get the desired result?

推荐答案

@Iulian Rosca建议使用类似 $的模式{properties_home} / $ {application_id} /application.properties 让我想到了定义一个自定义的JVM属性,比如 app.config.root 和使用此属性在应用程序生命周期的早期覆盖 spring.config.location

The suggestions of @Iulian Rosca to use a pattern like ${properties_home}/${application_id}/application.properties brought me to the idea of defining a custom JVM property like app.config.root and using this property to override spring.config.location very early in the application lifecycle.

我的应用程序类现在看起来像这样,适用于嵌入式和容器部署:

My application class looks now like this and works for embedded and container deployments:

@SpringBootApplication
public class Application extends SpringBootServletInitializer {

    @Override
    protected SpringApplicationBuilder configure(SpringApplicationBuilder builder) {
        return configureApplication(builder);
    }

    public static void main(String[] args) {
        configureApplication(new SpringApplicationBuilder()).run(args);
    }

    private static SpringApplicationBuilder configureApplication(SpringApplicationBuilder builder) {
        return builder
            .sources(Application.class)
            .properties("spring.config.location:${${app.config.root}/myapp1/:#{null}}");
    }

}

此解决方案的重要说明:

Important notes to this solution:


  • app.config.root 必须由JVM或JNDI属性设置为外部

  • app.config.root 只能包含一个外部配置路径(对于我的要求,这已足够)与<$ c相比$ c> spring.config.location 其中可以指定多个以逗号分隔的路径

  • SpringApplicationBuilder.properties(...)设置应用程序的默认属性。因此,无法再在外部指定 spring.config.location ,因为JVM或JNDI属性优先于默认属性,因此会覆盖 spring。 config.location 再次。

  • app.config.root has to be set externally by JVM or JNDI property
  • app.config.root can only contain a single external configuration path (for my requirements this was sufficient) in contrast to spring.config.location where multiple comma-separated paths can be specified
  • SpringApplicationBuilder.properties(...) sets the application's default properties. Because of this, spring.config.location cannot be specified externally anymore, as JVM or JNDI Properties have priority over default properties and would therefore override spring.config.location again.

这篇关于在Spring Boot中外部化配置,在同一容器中运行多个应用程序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆