Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Spring Cloud Config custom environment repository

I am wondering is there an example how to create a custom EnvironmentRepository for Spring Cloud Config, cause there are git, svn, vault repositories, but I don't wanna use them, I need my custom one. For instance if I just want to store all properties in a Map.

like image 213
idmitriev Avatar asked Dec 23 '22 19:12

idmitriev


1 Answers

Provide an implementation of the EnvironmentRepository as a bean in your application context. Spring cloud config server then will pick it up automatically. Here's a minimalistic example:

public class CustomEnvironmentRepository implements 
EnvironmentRepository
{
    @Override
    public Environment findOne(String application, String profile, String label)
    {
        Environment environment = new Environment(application, profile);

        final Map<String, String> properties = loadYouProperties();
        environment.add(new PropertySource("mapPropertySource", properties));
        return environment;
    }
}

Note if you have multiple EnvironmentRepository (Git, Vault, Native...) you'd also want to implement the Ordered interface to specify an order.

A good approach is to look up existing EnvironmentRepository implementation like the VaultEnvironmentRepository from the Spring cloud config server package.

like image 175
Felix Oldenburg Avatar answered Dec 28 '22 08:12

Felix Oldenburg