1

I have a hierarchy structure of applications files in my git repository as follows:

uri: https://bitbucket.org/repositorios-company/configuration-files

Directory:

-authorization-service
----application.yml
----application-development.yml
----application-uat.yml
----application-production.yml
-cpo-executor
----application.yml
----application-development.yml
----application-uat.yml
----application-production.yml

In config project yml file:

server:
    port: 8888
spring:
    application:
        name: config-server
    cloud:
        config:
            server:
                git:
                    username: ###### 
                    ....
                    uri: https://bitbucket.org/repositorios-company/cup-configuration-files
                    searchPaths: '{application}'

Problems:

  1. When I try to access the file of development by url http://localhost:8888/authorization-service/development spring load two files and not only one as I expected:
2021-01-13 10:34:40.549  INFO 141562 --- [nio-8888-exec-1] o.s.c.c.s.e.NativeEnvironmentRepository  : Adding property source: file:/tmp/config-repo-3531515016986363333/authorization-service/application.yml
2021-01-13 10:34:48.950  INFO 141562 --- [nio-8888-exec-2] o.s.c.c.s.e.NativeEnvironmentRepository  : Adding property source: file:/tmp/config-repo-3531515016986363333/authorization-service/application-development.yml

  1. When a client application, using the following configuration, tries to access the corresponding config file, spring only brings the application.yml file and not the file corresponding to the profile:

Client yml:

spring.application.name=authorization-service
spring.profiles.active=development
spring.cloud.config.uri=http://localhost:8888

After application starts, spring cloud config log shows the default application.yml:

2021-01-13 11:09:11.346  INFO 144899 --- [nio-8888-exec-2] o.s.c.c.s.e.NativeEnvironmentRepository  : Adding property source: file:/tmp/config-repo-1131390371944673193/authorization-service/application.yml

Edited: I've checked if the value changed in runtime and if it has taken the values from application-development.yml, but not.

Does anybody know how can I bring only one config file to the two situations?

Aldo Inácio da Silva
  • 824
  • 2
  • 14
  • 38

3 Answers3

1

When you access directly to your config server, it will show these 2 files, that is normal, the base config file and the second that override the base config file, that is a normal behaviour.

And when your client connects to your config server, it will fetch only one file which will have the mix of two property files configurations allocated in your config server.

The following post describes the use of a bootstrap.yml file that is located into the resources folder, this file allows to activate the profile when fetching the data from the config server.

What is the difference between putting a property on application.yml or bootstrap.yml in spring boot?

danyPasillas
  • 156
  • 5
  • I changed the server.port value of the two files and checked the values when my application starts and is getting the default values from application.yml not from application-development.yml. Have you ever tried this configuration? – Aldo Inácio da Silva Jan 13 '21 at 16:52
  • My project is already using bootstrap.property to load before, as mentioned, but it still isn't loading the desired values from application-development.yml :(. – Aldo Inácio da Silva Jan 14 '21 at 13:29
0

Three things to consider:

  1. Even though I utilized profile as "spring.profiles.active=development" Spring looks for application-dev.properties and not application-development.properties file. I utilized Spring actuator to see which profile Spring was looking for.

How to use Spring actuator

http://localhost:8080/actuator/env

{"activeProfiles":["dev"],....}
  1. Even thought two files were loaded in Spring Cloud Config, only the corresponding profile file was utilized by application client:
INFO 373818 ...NativeEnvironmentRepository  : Adding property source: file:/tmp/config-repo/authorization-service/application-dev.properties
INFO 373818 ...NativeEnvironmentRepository  : Adding property source: file:/tmp/config-repo/authorization-service/application.properties

But if you need an application only to consume the file corresponding to it profile, when access http://localhost:8888/authorization-service/dev, just remove the default application.yml from git repository.

  1. When using Spring Cloud Config utilize bootstrap.{yml|properties} and not application.{yml|properties} in your application client.

What is the difference between putting a property on application.yml or bootstrap.yml in spring boot?

Aldo Inácio da Silva
  • 824
  • 2
  • 14
  • 38
-2

you must tell spring what the specific file yml you need to use :

for example :

in the main class you must be added:

@PropertySource("classpath:foo.yml")

this annotation says at run main class use foo.yml

for more details Visit : https://www.baeldung.com/properties-with-spring?

I hope the solution helpful.