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

前端 未结 3 1913
天命终不由人
天命终不由人 2020-12-30 01:51

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 profil

相关标签:
3条回答
  • 2020-12-30 02:10

    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].properties to override properties from the default bootstrap.properties.

    So in your case you would get 4 files bootstrap.properties, bootstrap-prod.properties, bootstrap-stage.properties and bootstrap-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.

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

    This will take precedence over the default configured values.

    0 讨论(0)
  • 2020-12-30 02:12

    @LarryW (I cannot answer on the same comment):

    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.

    0 讨论(0)
  • 2020-12-30 02:18
    I solved a similar problem with an environment variable in Docker. 
    

    bootstrap.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

    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
    
    0 讨论(0)
提交回复
热议问题