dropwizard:从非文件源读取配置 [英] dropwizard: read configuration from a non-file source

查看:62
本文介绍了dropwizard:从非文件源读取配置的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

从诸如数据库或REST调用之类的dropwizard中读取配置的正确方法是什么?我有一个用例,其中我无法包含带有某些值的yml文件,并且应该在启动时从具有REST调用的预配置URL中检索设置/配置。

What's the right way to read configuration in dropwizard from something like a database, or a REST call? I have a use case where I cannot have a yml file with some values, and should retrieve settings/config at startup time from a preconfigured URL with REST calls.

是吗有权在 ApplicationConfiguration 类的get方法中调用这些REST调用?

Is it right to just invoke these REST calls in the get methods of the ApplicationConfiguration class?

推荐答案

类似于我在这里的答案,您实现了 ConfigurationSourceProvider 通过以下方式实现希望实现和配置dropwizard应用程序以在Application类上使用它的方式:

Similar to my answer here, you implement the ConfigurationSourceProvider interface the way you wish to implement and configure your dropwizard application to use it on your Application class by:

@Override
public void initialize(Bootstrap<MyConfiguration> bootstrap){
  bootstrap.setConfigurationSourceProvider(new MyDatabaseConfigurationSourceProvider());
}

默认情况下, InputStream 返回的结果将读取为YAML,并映射到 Configuration 对象。 默认实施

By default, the InputStream you return is read as YAML and mapped to the Configuration object. The default implementation

您可以通过

bootstrap.setConfigurationFactoryFactory(new MyDatabaseConfigurationFactoryFactory<>());

然后您有了 FactoryFactory :)返回 Factory ,它读取 InputStream 并返回您的配置

Then you have your FactoryFactory :) that returns a Factory which reads the InputStream and returns your Configuration.

public T build(ConfigurationSourceProvider provider, String path {
  Decode.onWhateverFormatYouWish(provider.open(path));
}

这篇关于dropwizard:从非文件源读取配置的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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