Spring Cloud Config自定义环境存储库 [英] Spring Cloud Config custom environment repository

查看:256
本文介绍了Spring Cloud Config自定义环境存储库的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想知道是否有一个示例,该示例如何为Spring Cloud Config创建自定义EnvironmentRepository,原因是存在git,svn,保管库,但是我不想使用它们,我需要我的自定义库.例如,如果我只想将所有属性存储在地图中.

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.

推荐答案

在您的应用程序上下文中以bean的形式提供EnvironmentRepository的实现.然后,Spring cloud config服务器将自动将其拾取. 这是一个简单的示例:

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

请注意,如果您有多个EnvironmentRepository(Git,Vault,Native ...),则还需要实现Ordered接口来指定订单.

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

一种好方法是查找现有的EnvironmentRepository实现,例如

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

这篇关于Spring Cloud Config自定义环境存储库的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆