如何使用 Spring Config Service 设置本地配置属性 [英] How to set local config properties with Spring Config Service

查看:32
本文介绍了如何使用 Spring Config Service 设置本地配置属性的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以我最近学习了 Spring Cloud 的这个很棒的配置服务,经过一番努力,我能够设置我们的分布式应用程序,多个节点从一个配置服务器读取配置属性.

So I recently learned this awesome config service of Spring Cloud, and after some struggling I'm able to get our distributed application set up, with multiple nodes reading config properties from one config server.

然而,我不知道如何解决的一个问题是,即使大多数 props 在多个节点上都是相同的,但有些 props 需要本地版本,我不知道如何将本地 prop 与配置服务一起设置.

However one problem I don't know how to resolve is even though most props are same across multiple nodes, some props need local version and I don't know how to set local prop together with config service.

例如这是我的bootstrap.properties

spring.cloud.config.uri=${config.server:http://localhost:8888}
spring.application.name=worker

如果我在 bootstrap.properties 下或单独的 application.properties 文件中添加额外的道具,我可以在执行器的 env 端点下看到它们,就像这样,

If I add additional props under bootstrap.properties or in a separate application.properties file, I can see them under actuator's env endpoint, like this,

configService:file:///home/me/work/config-test/worker.properties: 
{
    server.timeout: "100"
},

applicationConfig: [classpath:/application.properties]: {
    server.timeout: "50"
},

在我的应用程序中,server.timeout 现在将始终为 100,当我使用这样的集中配置服务时,我不知道如何引用本地值?

In my app the server.timeout will always be 100 now, I don't know how can I reference a local value when I use a centralized config service like this?

推荐答案

好的,经过更多研究后我找到了答案.来自 https://docs.pivotal.io/spring-云服务/config-server/configuration-clients.html

OK, after some more research I found the answer. From https://docs.pivotal.io/spring-cloud-services/config-server/configuration-clients.html

Spring 应用程序可以使用配置服务器作为属性源.来自配置服务器的属性将覆盖本地定义的属性(例如,通过类路径中的 application.yml).

A Spring application can use a Config Server as a property source. Properties from a Config Server will override those defined locally (e.g. via an application.yml in the classpath).

所以正确的做法是将任何客户端属性排除在配置服务之外.这对我来说也意味着对于这样的属性,没有办法有一个默认值,只有在客户端值出现时才会被覆盖,这会很好.

So the right thing to do is to leave any client side property out of the config service. This to me also means for such properties there is no way to have a default that will only be overwritten if a client side value presents, which would be good to have.

这篇关于如何使用 Spring Config Service 设置本地配置属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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