【问题标题】:spring-cloud-starter-config POST /env not workingspring-cloud-starter-config POST /env 不工作
【发布时间】:2017-03-22 21:22:54
【问题描述】:

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

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

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

我可以使用

读取变量
curl http://localhost:8081/management/env/foo
{"foo":"bar"}

下面的更新似乎也成功了

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

当我查询整个环境时,我看到管理器部分中的更改值和 applicationConfig 中的原始值

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

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

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

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

非常感谢您的帮助。

【问题讨论】:

  • 你得到的输出是什么?

标签: spring spring-boot spring-boot-actuator spring-cloud-config


【解决方案1】:

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

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

【讨论】:

  • 由于没有人发表评论,这对我也有用。
【解决方案2】:

对我来说它只适用于

curl -H "Content-Type: application/json" -X POST -d '{"name":"foo", "value":"bar"}' http://localhost:8081/management/env/

否则我会得到

"Missing parameters: value,name"

【讨论】:

    【解决方案3】:

    如果您使用的是高于 2.2.4 的 Spring Boot 版本,则必须使用下面的属性手动启用 POST API 调用。

    management.endpoint.env.post.enabled=true

    【讨论】:

      【解决方案4】:

      也许您需要设置management.security.enabled:false 以允许向/env 端点发送POST 请求。

      【讨论】:

      • 虽然此代码可以解决问题,including an explanation 说明如何以及为什么解决问题将真正有助于提高您的帖子质量,并可能导致更多的赞成票。请记住,您正在为将来的读者回答问题,而不仅仅是现在提问的人。请edit您的回答添加解释并说明适用的限制和假设。
      猜你喜欢
      • 2022-07-14
      • 2020-04-17
      • 2021-03-11
      • 2021-11-16
      • 2018-10-03
      • 2016-02-18
      • 2019-01-07
      • 2018-01-27
      • 2018-07-22
      相关资源
      最近更新 更多