Spring Cloud Config 自定义环境存储库

5

我想知道是否有一个创建Spring Cloud Config自定义环境存储库的例子,因为已经有git、svn、vault存储库了,但我不想使用它们,我需要我的自定义存储库。例如,如果我只想将所有属性存储在Map中。

1个回答

12

在您的应用程序上下文中提供EnvironmentRepository的实现作为bean。然后,Spring Cloud Config Server将自动获取它。 以下是一个最简示例:

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;
    }
}

请注意,如果您有多个EnvironmentRepository(如Git、Vault、Native等),您还需要实现Ordered接口来指定顺序。
一个好的方法是查找现有的EnvironmentRepository实现,例如Spring Cloud Config Server包中的VaultEnvironmentRepository

1
我还创建了一个小的Maven库,其中包含一个ZooKeeper EnvironmentRepository,可用作Spring Cloud Config Server的即插即用依赖项。请参见github.com/felixoldenburg/JonesEnvironmentRepository。 - Felix Oldenburg
1
我在这里尝试类似的东西 - 但是没有起作用。请参见https://stackoverflow.com/questions/48762915/custom-spring-config-environmentrepository-not-being-picked-up - Vinay B
1
我使用了一个自定义的环境仓库,但是配置服务器不再加载本地或Git环境属性,只有自定义环境属性被加载。我想要加载自定义+本地/Git。请问有什么建议吗? - Komoo
2
确保配置文件处于活动状态:SPRING_PROFILES-ACTIVE=git,keyvault - Daniel Hilgarth

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接