繁体   English   中英

我可以在Spring Cloud Config Server中托管JSON文件吗?

[英]Can I host JSON file in Spring Cloud Config Server?

我们使用Spring Cloud Config Server来托管Spring Boot应用程序的所有配置。 我们希望从Config Server中检索一个巨大的JSON文本。

我们当前的方法是将json文本定义为属性值

myproperty.jsontext="{'name':'value'}"

除了将JSON文本定义为属性值之外,有没有办法从配置服务器托管和获取它?

Spring Cloud Config Server是否支持.json文件?

更新(附加问题):

我可以按如下方式访问searchLocations属性吗?

@Value("${spring.cloud.config.server.native.searchLocations}

在进行访问时,我们不断收到以下错误

Caused by: java.lang.IllegalArgumentException: Could not resolve placeholder 'spring.cloud.config.server.native.searchLocations' in string value "${spring.cloud.config.server.native.searchLocations}"

我已经通过https://github.com/spring-projects/spring-boot/issues/4027#issuecomment-144649875向Spring Boot提交了功能支持。 如果这对您有用,请告诉我......

public class JsonPropertySourceLoader implements PropertySourceLoader {


    private static final String JSON = "json";

    @Override
    public final String[] getFileExtensions() {
        return new String[] { JSON };
    }

    @Override
    public PropertySource<?> load(final String name,
        final Resource resource, final String profile) {

         final Map<String, Object> source = process(resource);
         return new MapPropertySource(name, source);
    }

    @SuppressWarnings("unchecked")
    private Map<String, Object> process(final Resource resource) {
        Map<String, Object> map = null;
        try {
            map = new ObjectMapper()
                .readValue(resource.getFile(), LinkedHashMap.class);
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
        return map;
    }

}

解决方法

由于yamlJson的子集,因此您可以将Json内容存储在Yaml文件中。

  • 创建q文件config.yaml
  • 在其中添加Json内容
  • yamljsonproperties请求文件,一切正常!

我在将现有Node.js应用程序集成到Config Service时测试了这种方法,其中所有配置都在.json文件中。 所以,我只是重命名了所有这些并添加到Config Repo中。

有可能提供任意档案。 归结为使用端点/{name}/{profile}/{label}/{path}其中{path}是实际文件名...例如/a-bootiful-client/default/master/myjson.json会给你文件内容。 但是,默认情况下,响应的内容类型不是application/json而是text/html;charset=UTF-8

但它也适用于“Accept:application / json”:

curl -H "Accept: application/json" http://localhost:8888/a-bootiful-client/default/master/a-bootiful-client.json
{
        "propName":"propValue"
}

请参阅此处的文档: http//cloud.spring.io/spring-cloud-static/spring-cloud-config/1.4.3.RELEASE/single/spring-cloud-config.html#_serving_plain_text

暂无
暂无

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

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