makcalif
makcalif

Reputation: 113

reading dynamic configurations split in multiple properties file from config server

I want to add several hundred configuration files in this pattern:

/application.yaml
/1000/application.yaml
/2000/application.yaml
/3000/application.yaml
/4000/application.yaml

Where 1000, 2000, 3000 are sender codes. When a REST call is made to my API, it will have a parameter 'senderCode' which have values like 1000, 2000, etc

Based on that I want to read the configs form the corresponding application.yaml from config server.

My config server's application yaml has:

  cloud:
    config:
      server:
        git:
          uri: http://example.com/my-configurations
          search-paths: 1000, 1001, 1002, 1003

With above settings, I can configure properties int the client application like this:

@ConfigurationProperties(prefix = "1000")
public class CodeBasedConfig {
    String senderName;
    String senderSource;
}

But that mean creating thousands of files like the one above. I want to be able to load configs from multiple files into a map like this:

key: 1000
value: configs for 1000
key: 2000
value: configs for 2000

On a side note, it will be bonus, if I could read configurations on demand for a given sender, instead of loading them all upfront.

Upvotes: 0

Views: 297

Answers (1)

makcalif
makcalif

Reputation: 113

I changed the file structure a bit but grouping them under a folder named clients

/application.yaml
clients/1000/application.yaml
clients/2000/application.yaml
clients/3000/application.yaml
clients/4000/application.yaml

After this I updated the spring cloud server's searchPaths like this:

spring:
  cloud:
    config:
      server:
        git:
          uri: <my git url>
          username: ${git_username}
          password: ${git_password}
          label: develop
          searchPaths: clients/*

With this the config sever is supposed to read all application.yaml's under 'clients' folder and merge them in a unified configuration.

As an FYI, my application.yaml's under clients folder have content like this:

1000/application.yaml
clients:
  '1000':
     attribute1: value1
     attribute2: value2

2000/application.yaml
clients:
  '2000':
     attribute1: value1
     attribute2: value2

When config server serves these configs to clients the above configs appear as:

clients:
  '1000':
     attribute1: value1
     attribute2: value2
  '2000':
     attribute1: value1
     attribute2: value2

This way I could break/refactor large configuration into more maintainable configuration files

Upvotes: 1

Related Questions