英文:
Is it possible to non-programmatically merge config objects that are at different hierarchical levels?
问题
我正在使用TypeSafe Config进行翻译。假设我想在应用程序的各个配置级别上具有弹性模式的应用程序范围默认值,但又想在较低的配置级别上进行特定的覆盖。我希望有一个通用的解决方案,而不是为每个流手动编写配置创建代码。请参考以下示例配置:
application.conf
application {
streams {
retry {
attempts = 100
poll-interval = 5s
minBackoff = 200 ms
maxBackoff = 2s
randomFactor = 0.25
}
}
}
service1.conf
application {
streams {
stream1 {
retry {
attempts = 2
}
}
stream2 {
retry {
poll-interval = 5s
}
}
}
}
有没有一种非编程方式合并这些配置的方法?使用application.conf中的默认值,并在较低级别上存在值时进行覆盖?
这里有什么最佳实践?
我不能使用:
var defaultConf = ConfigFactory.load("application.conf");
var conf = ConfigFactory.load("service1.conf").withFallback(defaultConf);
因为这些配置不在同一层级上。
英文:
I'm using TypeSafe Config. Imagine I want to have application wide defaults for resilience patterns but override them specifically at lower config level. I would prefer to have a generic solution instead of manually coding the config creation for each and every stream. Please see example configs:
application.conf
application {
streams {
retry {
attempts = 100
poll-interval = 5s
minBackoff = 200 ms
maxBackoff = 2s
randomFactor = 0.25
}
}
}
service1.conf
application {
streams {
stream1 {
retry {
attempts = 2
}
}
stream2 {
retry {
poll-interval = 5s
}
}
}
Is there a way to non-programmatically merge these configs? Use the defaults from application.conf and override if the value exists in the lower level?
What's the best practice here?
I can't use:
var defaultConf = ConfigFactory.load("application.conf");
var conf = ConfigFactory.load("service1.conf").withFallback(defaultConf);
Because the configs are not at the same hierarchical level.
答案1
得分: 1
总的来说,我同意Ivan的观点,并且同意他在关于参考配置的其他答案中提供的链接。你可能忽略了一个"层次结构"的部分。你可以通过继承的方式来实现,而不需要编程。这里是文档链接:https://github.com/lightbend/config#inheritance。你可以在参考配置中定义一个通用配置,然后在层次结构中进行继承和覆盖。文档中有一个与你的情况非常相似的示例。
简而言之,你可以通过以下方式引用通用配置:
application.streams.stream1 = ${application.streams}
application.streams.stream1.retry.attempts = 2
application.streams.stream2 = ${application.streams}
application.streams.stream2.retry.poll-interval = 5s
英文:
In general I agree with Ivan and the link to his other answer regarding reference configs. (Or any type of base config.)
The part that I think you are missing is the "hierarchy". You can do that, in a non-programmatic way, using inheritance. Here is the docs: https://github.com/lightbend/config#inheritance . You can have a generic config in your reference and then inherit and override in your hierarchy. The docs have an example very similar to yours.
The gist would be to reference the generic config with something like:
application.streams.stream1 = ${application.streams}
application.streams.stream1.retry.attempts = 2
application.streams.stream2 = ${application.streams}
application.streams.stream2.retry.poll-interval = 5s
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论