Spring Profiles 与 ConfigServer 结合使用

问题描述

我有一个非常基本的 Spring Boot Config Server(刚刚添加了依赖项并使用 @Enableconfigserver 注释了主类)。

总的来说,我希望为我的每个应用程序支持具有不同属性源的多个环境,以下是 configserver 本身的示例:

配置文件认(类路径上的 application.yml):

enter image description here

配置文件:docker(类路径上的application-docker.yml):

enter image description here

配置文件认(configserver 存储库中的 application.yml):

enter image description here

因此,在我的情况下,所有三个屏幕截图中的所有属性都应该处于活动状态,我希望顺序/优先级如下:

  1. 来自类路径的application.yml
  2. application-ANY_PROFILE.yml 来自类路径
  3. 来自配置仓库的application.yml
  4. APP-NAME.yml 来自配置存储库(在这种情况下不存在)

到目前为止,这完美无缺,除了我遇到的问题是,当我使用命令启动应用程序时(当然在容器内),我在类路径上的 application-docker.yml 被忽略了:

java -jar -Dspring-boot.run.profiles=docker *.jar

正如你在这里看到的:

enter image description here

我的问题是,即使我将配置文件作为命令行参数提供,它也不会被接收。 这是为什么?

更新,这里是Dockerfile和entrpoint.sh:

enter image description here

enter image description here

解决方法

要激活一个或多个配置文件,请执行以下操作之一:

  1. 使用 VM 参数激活 -Dspring.profiles.active=<profiles>
  2. 使用程序参数激活 --spring.profiles.active=<profiles>

按照您的示例,以下应该有效:

java -jar -Dspring.profiles.active=docker *.jar