2
votes

I have this configuration in a spring boot application:

spring:
  application:
    name: my-app
  profiles:
    active: ${ENVIRONMENT}
  cloud:
    config:
     uri: http://localhost:8888

My config server reads the following files:

my-app-dev.yaml

prop: dev property

my-app-pro.yaml

prop: pro property.

I launch the spring boot app setting -DENVIRONMENT=dev, loading correctly the dev external Git properties.

When I inject the Environment in let's say a controller and do env.getActiveProfiles() I get "dev" as expected.

I would like to add more profiles from the git configuration. For instance:

my-app-dev.yaml

prop: dev property
spring:
  active:
    profiles: dev,business1

my-app-pro.yaml

prop: dev property
spring:
  active:
    profiles: pro,business2

So that env.getActiveProfiles() returns ["dev","business1"]. However what it returns is the initial "dev".

How could this be done?

UPDATE:

As suggested by Dave Syer I tried using spring.profiles.include in the Git files but the new profiles aren't added to the Environment:

my-app-dev.yaml

prop: dev property
spring:
  profiles:
    include: business1

my-app-pro.yaml

prop: dev property
spring:
  profiles:
    include: business2

environment.getActiveProfiles() ---> "dev"

1
@DaveSyer, I just tried it but the injected Environment.getActiveProfiles() returns only the original ${ENVIRONMENT} (dev) profile. - codependent
I don't know how you are testing this but it works for me as expected. For the sake of clarity: you can't change the active profile in your client app by editing the remote config files, but you can change the content that is loaded in the config server (I thought that's what you wanted). So if you load /my-app/dev you should see the config from my-app-business1.yml in the result. - Dave Syer
@DaveSyer Thanks for answering. I don't want to change the active profile, I want to add profiles to the application with the ones defined in the Git repo so that env.getActiveProfiles() returns both dev coming from the app internal application.yml (spring: active: profiles: dev) and business1 from Git myapp-dev.yaml (spring: profiles: include: business1). Maybe there's something wrong with my proyect. I'll start from scratch with a basic Spring Boot app to check it again and make it available in GitHub in case it doesn't work. - codependent
The selection of properties files in config server is based on the active profiles provided by the incoming app. So when you set dev as active profile, configserver will send the my-app-dev.yml. From this point of view, we are done with profile once the file is identified. Therefore, it's confusing to define again profiles in selected files. Since we can include multiple profiles on the app side, I expect configserver to provide the same number of properties files. Can you try to create my-app-dev.yml and my-app-business1.yml in git and enable both profiles on app side ? - Hui Wang

1 Answers

2
votes

Update your spring-boot to 1.5.4.

I tested the same case on my Mac, I found that spring with different version behaves different.

When I'm using Spring Boot 1.3.8.RELEASE with Spring Cloud Brixton.SR7, I got the [dev] profile as active profile(also with Spring Boot 1.4.5.RELEASE)

When I'm using Spring Boot 1.5.4.RELEASE with Spring Cloud Dalston.SR1, I got the [business1, dev] profile as active profile

So I believe it is a bug in Spring Boot 1.3.x and 1.4.x