Spring Cloud Config custom environment repository

感情迁移 提交于 2020-01-11 06:33:30

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!