1

Lets say I have applicationA that has 3 property files:

-> applicationA
          - datasource.properties
          - security.properties
          - jms.properties

How do I move all properties to a spring cloud config server and keep them separate?

As of today I have configured the config server that will only read ONE property file as this seems to be the standard way. This file the config server picks up seems to be resolved by using the spring.application.name. In my case it will only read ONE file with this name:

-> applicationA.properties

How can I add the other files to be resolved by the config server?

Paulo Merson
  • 13,270
  • 8
  • 79
  • 72
Ugur Teker
  • 169
  • 2
  • 14
  • can you clarify your question please? do you want config Server to send 3 files namely datasource,security and jms .properties back to client application? – Indraneel Bende Feb 10 '19 at 14:55

3 Answers3

0

Not possible in the way how you requested. Spring Cloud Config Server uses NativeEnvironmentRepository which is:

Simple implementation of {@link EnvironmentRepository} that uses a SpringApplication and configuration files located through the normal protocols. The resulting Environment is composed of property sources located using the application name as the config file stem (spring.config.name) and the environment name as a Spring profile.

See: https://github.com/spring-cloud/spring-cloud-config/blob/master/spring-cloud-config-server/src/main/java/org/springframework/cloud/config/server/environment/NativeEnvironmentRepository.java

So basically every time when client request properties from Config Server it creates ConfigurableApplicationContext using SpringApplicationBuilder. And it is launched with next configuration property:

String config = application;
if (!config.startsWith("application")) {
    config = "application," + config;
}
list.add("--spring.config.name=" + config);

So possible names for property files will be only application.properties(or .yml) and config client application name that is requesting configuration - in your case applicationA.properties.

But you can "cheat". In config server configuration you can add such property

spring:
  cloud:
    config:
      server:
        git:
          search-paths: '{application}, {application}/your-subdirectory'

In this case Config Server will search for same property file names but in few directories and you can use subdirectories to keep your properties separate. So with configuration above you will be able to load configuration from:

applicationA/application.properies
applicationA/your-subdirectory/application.properies
nmyk
  • 1,582
  • 1
  • 8
  • 20
0

This can be done. You need to create your own EnvironmentRepository, which loads your property files.

org.springframework.cloud.config.server.support.AbstractScmAccessor#getSearchLocations searches for the property files to load :

for (String prof : profiles) {
            for (String app : apps) {
                String value = location;
                if (app != null) {
                    value = value.replace("{application}", app);
                }
                if (prof != null) {
                    value = value.replace("{profile}", prof);
                }
                if (label != null) {
                    value = value.replace("{label}", label);
                }
                if (!value.endsWith("/")) {
                    value = value + "/";
                }
                output.addAll(matchingDirectories(dir, value));
            }
        }

There you could add custom code, that reads the required property files. The above code matches exactly the behaviour described in the spring docs. The NativeEnvironmentRepository does NOT access GIT/SCM in any way, so you should use JGitEnvironmentRepository as base for your own implementation.

0

As @nmyk pointed out, NativeEnvironmentRepository boots a mini app in order to collect the properties by providing it with - sort of speak - "hardcoded" {appname}.* and application.* supported property file names. (@Stefan Isele - prefabware.com JGitEnvironmentRepository ends up using NativeEnvironmentRepository as well, for that matter).

I have issued a pull request for spring-cloud-config-server 1.4.x, that supports defining additional file names, through a spring.cloud.config.server.searchNames environment property, in the same sense one can do for a single springboot app, as defined in the Externalized Configuration.Application Property Files section of the documentation, using the spring.config.name enviroment property. I hope they review it soon, since it seems many have asked about this feature in stack overflow, and surely many many more search for it and read the currently advised solutions.

It worths mentioning that many ppl advise "abusing" the profile feature to achieve this, which is a bad practice, in my humble opinion, as I describe in this answer

user2435660
  • 363
  • 5
  • 9