java 您如何在引导文件中正确设置不同的 Spring 配置文件(用于 Spring Boot 以针对不同的云配置服务器)?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/40981861/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-11-03 05:35:13  来源:igfitidea点击:

How do you properly set different Spring profiles in bootstrap file (for Spring Boot to target different Cloud Config Servers)?

javaspring-bootspring-cloud-configspring-profiles

提问by dev_feed

We have different config servers per environment. Each spring boot application should target its corresponding config server. I have tried to achieve this by setting profiles in the bootstrap.properties file, e.g.:

我们每个环境都有不同的配置服务器。每个 spring boot 应用程序都应该针对其相应的配置服务器。我试图通过在 bootstrap.properties 文件中设置配置文件来实现这一点,例如:

spring.application.name=app-name
spring.cloud.config.uri=http://default-config-server.com

---
spring.profiles=dev
spring.cloud.config.uri=http://dev-config-server.com

---
spring.profiles=stage
spring.cloud.config.uri=http://stage-config-server.com

---
spring.profiles=prod
spring.cloud.config.uri=http://prod-config-server.com

And then I set the cla -Dspring.profiles.active=devbut the loaded config server is always the last one set in the file (i.e. prod config server would be loaded in the above settings, and then if prod is removed, stage would be loaded).

然后我设置了 cla-Dspring.profiles.active=dev但加载的配置服务器始终是文件中的最后一个设置(即 prod 配置服务器将在上述设置中加载,然后如果 prod 被删除,舞台将被加载)。

Is it possible to set bootstrap profiles for the cloud config server? I followed this examplebut can't seem to get it working. For what it's worth, these profiles work great to load the correct config (i.e. app-name-dev.properties will load if the dev profile is active), but aren't being pulled from the proper config server.

是否可以为云配置服务器设置引导程序配置文件?我遵循了这个例子,但似乎无法让它工作。就其价值而言,这些配置文件非常适合加载正确的配置(即,如果开发配置文件处于活动状态,则 app-name-dev.properties 将加载),但不会从正确的配置服务器中提取。

回答by M. Deinum

Specifying different profiles in a single file is only support for YAML files and doesn't apply to property files. For property files specify an environment specific bootstrap-[profile].propertiesto override properties from the default bootstrap.properties.

在单个文件中指定不同的配置文件仅支持 YAML 文件,不适用于属性文件。对于属性文件,指定特定bootstrap-[profile].properties于覆盖默认属性的环境bootstrap.properties

So in your case you would get 4 files bootstrap.properties, bootstrap-prod.properties, bootstrap-stage.propertiesand bootstrap-dev.properties.

所以你的情况,你会得到4个文件bootstrap.propertiesbootstrap-prod.propertiesbootstrap-stage.propertiesbootstrap-dev.properties

However instead of that you could also only provide the default bootstrap.propertiesand when starting the application override the property by passing a -Dspring.cloud.config.uri=<desired-uri>to your application.

但是,您也可以仅提供默认值,bootstrap.properties并在启动应用程序时通过将 a 传递-Dspring.cloud.config.uri=<desired-uri>给您的应用程序来覆盖该属性。

java -jar <your-app>.jar -Dspring.cloud.config.uri=<desired-url>

This will take precedence over the default configured values.

这将优先于默认配置值。

回答by jeton

I solved a similar problem with an environment variable in Docker. 

bootstrap.yml

引导程序.yml

spring:
  application:
    name: dummy_service
  cloud:
    config:
      uri: ${CONFIG_SERVER_URL:http://localhost:8888/}
      enabled: true
  profiles:
    active: ${SPR_PROFILE:dev}

Dockerfile

文件

ENV CONFIG_SERVER_URL=""
ENV SPR_PROFILE=""

Docker-compose.yml

Docker-compose.yml

version: '3'

services:

  dummy:
    image: xxx/xxx:latest
    restart: always
    environment:  
      - SPR_PROFILE=docker
      - CONFIG_SERVER_URL=http://configserver:8888/
    ports:
      - 8080:8080
    depends_on:
      - postgres
      - configserver
      - discovery