14

I'm trying to migrate our stable of app servers to get their configuration from a Spring Cloud Config server. Each app has a {my-app}.yml file on the config server and we can use profiles (either in files named {my-app}-{profile}.yml or using multi-profile YAML documents) to have different configuration per environment for each app, and we can even include one profile in another using spring.profiles.include to provide some sort of inheritance - so far, so good.

However, we can only include profiles from the same app in each other and we have several apps configured from the same config server which share a lot of config per environment - for instance they almost all use the same DataSource config to connect to the same database and likewise for messaging, cache and so on. That's a lot of duplicated config and a lot of places it needs to be changed - precisely what Spring Cloud Config is supposed to avoid!

Is there a way to "include" (via profiles or otherwise!) shared config properties across apps in Spring Cloud Config server?

Update

In addition to the correct answer by @vladsfl below, beware if you're using the native profile on the config server to serve config from the filesystem or classpath instead of a git repo, the config server will use application.yml and its profile variants for itself but refuse to serve them out to other apps. The solution is to use spring.cloud.config.server.native.searchLocations to pull the served config from a different location.

Sonafets
  • 497
  • 1
  • 4
  • 13

3 Answers3

15

Yes. You can have application.yml or application-<profile>.yml on your config server and now every application that is using this config server will inherit all settings in application.yml. Every application that runs in specific profile will inherit settings from application-<profile>.yml.

LarryW
  • 121
  • 1
  • 6
vladsfl
  • 617
  • 8
  • 18
  • `application-.yml` seems an odd way to display it, I'd say `application-.yml`. Also works with properties. – spencergibb Apr 20 '16 at 09:01
  • 1
    Thanks - the reason this wasn't working for me is because I was using the "native" profile on the config server and loading from the classpath instead of a git repository, which explicitly excludes application-*.{yml|properties} from the served config! Your answer helped in prodding me to figure that out though. – Sonafets Apr 20 '16 at 10:00
  • @spencergibb. I did type there but I guess it got stripped out – vladsfl Apr 20 '16 at 12:41
  • 1
    Is there a way to change the name of that file? I mean changing name of `application.properties` to `something.properties`. – The_Cute_Hedgehog Apr 27 '18 at 07:00
6

Probably it's too late already, but in case someone else is struggling with the same issue, the final solution is as follows:

You can create as many yml files under config-server classpath as you wish. Even if it is in native profile selected, there will be provided to client applications. The only thing not mentioned before is, you should tell the client application to read those settings files as well.

Here is a working example:

Config server file structure:

resources
|-config
   |-auth-service.yml - service specific configuration file
|-application.yml - config server settings
|-settings.yml - general settings file, planed to be loaded in every service

Client application bootstrap.yml file:

spring:
application:
  name: auth-service
cloud:
  config:
    username: "config-user"
    password: "config-password-1234"
    uri: "http://config-service:8888"
    name: ${spring.application.name}, settings

The key is name: ${spring.application.name}, settings which tells the config client to load the following settings from the config server:

  • ${spring.application.name} which will load config/auth-service.yml
  • settings which will load settings.yml
Zsolt Tolvaly
  • 3,528
  • 4
  • 26
  • 25
  • When i tried this I get an error, seems like it will not try to pull for both apps, it just considers it a single app name: Could not locate PropertySource and the fail fast property is set, failing: {"timestamp":1625004407773,"status":500,"error":"Internal Server Error","exception":"java.lang.IllegalStateException","message":"Cannot clone or checkout repository","path":"/appname1,%20appname2/dev"} – andrewps Jun 29 '21 at 22:07
  • I also couldn't make this work with Spring Cloud Config Server v3.0.4. But when add `search-paths: '{application}'` in the config server *application.yml* and remove `name` property from client service, everything works. [Here](https://gist.github.com/emrekgn/93fab341cbe29ea54e866e51001a5f5c#file-a-spring-cloud-config-server-application-yml-L14) is a full example. – emrekgn Aug 18 '21 at 12:20
0

If you want to keep configuration files simple across environments, try mergeconfigs, it is designed to support merging several configuration files and to facilitate local development. Disclaimer: I am the owner of mergeconfigs.

Alakazam
  • 461
  • 5
  • 14
  • It seems that this is your own GitHub account. Please always disclose your affiliation when recommending external resources. Thanks. – Eric Aya Apr 16 '23 at 10:59