简体   繁体   English

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

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

We have different config servers per environment.我们每个环境都有不同的配置服务器。 Each spring boot application should target its corresponding config server.每个 spring boot 应用程序都应该针对其相应的配置服务器。 I have tried to achieve this by setting profiles in the bootstrap.properties file, eg:我试图通过在 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=dev but the loaded config server is always the last one set in the file (ie 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 被删除,stage 将加载)。

Is it possible to set bootstrap profiles for the cloud config server?是否可以为云配置服务器设置引导程序配置文件? I followed this example but can't seem to get it working.我遵循了这个例子,但似乎无法让它工作。 For what it's worth, these profiles work great to load the correct config (ie 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 将加载),但不会从正确的配置服务器中提取。

Specifying different profiles in a single file is only support for YAML files and doesn't apply to property files.在单个文件中指定不同的配置文件仅支持 YAML 文件,不适用于属性文件。 For property files specify an environment specific bootstrap-[profile].properties to override properties from the default bootstrap.properties .对于属性文件,指定特定于环境的bootstrap-[profile].properties以覆盖默认bootstrap.properties属性。

So in your case you would get 4 files bootstrap.properties , bootstrap-prod.properties , bootstrap-stage.properties and bootstrap-dev.properties .因此,在您的情况下,您将获得 4 个文件bootstrap.propertiesbootstrap-prod.propertiesbootstrap-stage.propertiesbootstrap-dev.properties

However instead of that you could also only provide the default bootstrap.properties and when starting the application override the property by passing a -Dspring.cloud.config.uri=<desired-uri> to your application.但是,您也可以只提供默认的bootstrap.properties并在启动应用程序时通过将-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.这将优先于默认配置值。

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

@LarryW (I cannot answer on the same comment): @LarryW(我无法回答相同的评论):

I guess the advantage of explicitly adding the property is that it allows you to add a default value (in this case "dev") in case of not setting up the environment variable.我想显式添加属性的优点是它允许您在不设置环境变量的情况下添加默认值(在本例中为“dev”)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM