【问题标题】:SpringBoot yml how can I run two profiles at the same timeSpringBoot yml 如何同时运行两个配置文件
【发布时间】:2022-01-22 17:52:17
【问题描述】:

在我的应用程序 yml 中,我有两个需要同时运行的配置文件

spring:
  profiles:
    active: 
      awss3, @env@
  application: 
    name: CONFIG-SERVICE

无论环境如何,我都需要运行 awss3。这可能吗?

【问题讨论】:

  • 你试过spring.profiles.include吗?

标签: spring-boot yaml


【解决方案1】:

在我看来,您的要求适合个人资料组。这种方法更可取,因为它允许您直观地告知开发人员哪些环境需要哪些配置文件

https://docs.spring.io/spring-boot/docs/current/reference/html/features.html#features.profiles.groups

示例

spring:
  profiles:
    group:
      production:
      - "aws3"
      - "prodmq"

然后您可以使用以下命令启动应用程序:

--spring.profiles.active=production

要激活制作,aws3prodmq 配置文件一击即可。

澄清一下:group: 下的关键字production 是您在以spring.profiles.active=production 运行应用程序时将使用的关键字


或者你可以简单地从命令行添加任何你想要的配置文件。

java -jar -Dspring.profiles.active=awss3,prodmq demo-0.0.1-SNAPSHOT.jar

【讨论】:

  • 我正在使用 Spring 2.5.3 配置文件组似乎很混乱。 Spring如何知道使用哪个组导入是有道理的,但是在组中,它似乎并没有说明如何使用哪个@env@。
  • 我在编辑后的答案中澄清了@Tony,祝你好运
  • 感谢您的帮助,我似乎无法正常工作。我以 mvn spring-boot:run 运行。它从 pom 中获取环境。并使用 spring:profiles:active: @activatedProperties@ 将其添加到 yml,在这种情况下,activatedProperties 是本地的。但是这个 spring:profiles:group:local: -"aws3" 似乎不起作用。组后的参数,应该与激活的属性匹配。只是想让它在本地工作。
【解决方案2】:

是的,使用属性:

// Use your dynamic profile
spring.profiles.active=@env@
// Include your default profile
spring.profiles.include=awss3

yaml:

spring:
  profiles:
    include: awss3
    active: 
      @env@

【讨论】:

  • 请注意,profiles.include 指令自 2.4.0 起已弃用。如果可能,应首选配置文件组。
  • 谢谢,遗憾的是我使用的是 2.5.3,这不再受支持,很遗憾,因为设置非常简单
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2012-07-17
  • 2016-12-12
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-12-28
相关资源
最近更新 更多