spring-cloud-starter-config POST/env 不工作 [英] spring-cloud-starter-config POST /env not working

查看:15
本文介绍了spring-cloud-starter-config POST/env 不工作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个小的 Spring Boot Web 应用程序(可执行 jar),它在 application.properties 文件中有一些自定义属性.我的目标是能够在运行时动态更改这些属性,而无需运行构建/部署或重新启动 java 进程.spring-cloud-starter-config 项目似乎很适合这个(即使我没有使用配置服务器),但我遇到了以下问题:

I have a little spring boot web application (executable jar) which has some custom properties inside the application.properties file. My goal is to be able to dynamically alter these properties at runtime without having to run a build/deploy or restarting the java process. The spring-cloud-starter-config project seems to be well suited for this (even though I am not using a config server) but I ran into the following problem:

在我的 pom.xml 中,我包含了对 spring-boot-starter-actuator 和 spring-cloud-starter-config 的依赖.application.properties 配置以下属性

In my pom.xml I include dependencies to spring-boot-starter-actuator and spring-cloud-starter-config. The application.properties configures below properties

management.port=8081
management.context-path=/management
management.security.enabled=false
foo=bar

我可以使用

curl http://localhost:8081/management/env/foo
{"foo":"bar"}

下面的更新似乎也成功了

Below update seems to succeed as well

curl -X POST -d foo=foo http://localhost:8081/management/env
{"foo":"foo"}

当我查询整个 env 时,我看到 manager 部分中的更改值和 applicationConfig 中的原始值

When I query the whole env I see the changed value in the manager section and the original value inside applicationConfig

curl http://localhost:8081/management/env
{
  ...
  "manager": {
    "foo": "foo"
  },
  ...
  "configServerClient": {
    "spring.cloud.config.enabled": "false"
  },
  ...
  "applicationConfig: [classpath:/application.properties]": {
  ...
  "foo": "bar",
  ...
  }
}  

现在当我再次查询变量时,我仍然得到旧值

Now when I query the variable again I still get the old value

curl http://localhost:8081/management/env/foo
{"foo":"bar"}

这与我在一些网络博客上读到的相反.根据我的理解,更改后的值应该显示出来.我做错了什么?有没有更好的方法可以在不重新启动服务器的情况下动态更改 application.properties 中的值?

This is contrary to what I read on some web blogs. In my understanding the changed value should show up. What am I doing wrong? Is there a better way to dynamically change values in the application.properties without restarting the server?

非常感谢您的帮助.

推荐答案

您需要在将更改发布到 env 后运行刷新

You need to run refresh after posting changes to env

curl -X POST http://localhost:8081/management/refresh

这篇关于spring-cloud-starter-config POST/env 不工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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