简体   繁体   English

通过Rest API读取YAML配置

[英]Read YAML config through Rest API

I have a really complicated system which use multiple languages and frameworks (Java Python Scala Bash). 我有一个非常复杂的系统,它使用多种语言和框架(Java Python Scala Bash)。 In each module I need to retrieve configuration values which are similar and change frequently. 在每个模块中,我需要检索相似且经常更改的配置值。 Currently I'm maintaining multiple conf files which holds lots of duplicates. 目前,我正在维护多个conf文件,其中包含许多重复项。 I wonder if there is out of the box RestAPI which can retrieve variables by demand from remote location. 我想知道是否有开箱即用的RestAPI可以按需从远程位置检索变量。

All I manage to find by now are ways to load the entire file from remote source which is half a solution from me: 现在,我设法找到的是从远程源加载整个文件的方法,这是我的一半解决方案:

YAML.parse(open('https://link_to_file/file.yaml'))

My goal, which I fail to find a lead to it, is to make a direct call. 我的目标(无法找到线索)是直接致电。

MyRemoteAPI.get("level1.level2.x")

PS YAML is not mandatory solution for me, I'm Open for suggestions. PS YAML对我来说不是强制性的解决方案,我愿意征求建议。

I don't know about an out-of-the-box API, but it's fairly trivial to build. 我不了解现成的API,但是构建起来相当琐碎。 Make a service that will read the YAML file and traverse to the appropriate key. 提供一个将读取YAML文件并遍历到适当密钥的服务。 eg using a dynamic language like Ruby (+Rails), you could do something like 例如,使用像Ruby(+ Rails)这样的动态语言,您可以执行类似

def value
  config = YAML.load_file '/local/path/to/config.yaml'
  render plain: config.dig(params[:key].split('.'))
end

dig essentially traverses a structure and safely returns nil if a key isn't found, so this returns the value at the "leaf" of the requested path. dig本质上遍历结构,如果未找到键,则安全地返回nil,因此这将返回所请求路径的“叶”处的值。

You might also want to cache the structure in memory to prevent constantly reading from the file, eg could do something like @@config ||= YAML.parse(open('https://link_to_file/file.yaml')) or config = Rails.cache.fetch('config', expire_in: 1.hour) { ... } . 您可能还希望将结构缓存在内存中,以防止不断读取文件,例如可以执行类似@@config ||= YAML.parse(open('https://link_to_file/file.yaml'))config = Rails.cache.fetch('config', expire_in: 1.hour) { ... } And/or cache the API's HTTP response. 和/或缓存API的HTTP响应。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM