问题
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.
回答1:
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.
来源:https://stackoverflow.com/questions/42872052/spring-cloud-config-custom-environment-repository