24

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.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 (i.e. prod config server would be loaded in the above settings, and then if prod is removed, stage would be loaded).

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 (i.e. app-name-dev.properties will load if the dev profile is active), but aren't being pulled from the proper config server.

Community
  • 1
  • 1
dev_feed
  • 689
  • 2
  • 7
  • 25
  • 2
    That will only work for yaml files not property files (afaik). Just add `bootstrap-[profile].properties` i.e. `bootstrap-dev.properties` which would contain the needed (overridden) configuration. – M. Deinum Dec 05 '16 at 20:11

3 Answers3

29

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.

M. Deinum
  • 115,695
  • 22
  • 220
  • 224
  • With Boot 2.4 you can use magic comment `#---` with helpers as `spring.config.activate.on-cloud-platform` & `spring.config.activate.on-profile` in `.properties` files – gavenkoa Aug 10 '22 at 14:17
9
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
jeton
  • 841
  • 12
  • 15
  • 3
    Why not just set `SPRING_PROFILES_ACTIVE` (in the environment) in Docker, and skip the `spring.profiles.active` in bootstrap.yml? – LarryW Jun 06 '19 at 00:42
0

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

Flashk
  • 41
  • 1
  • 6