spring-cloud-starter-config POST /env not working

与世无争的帅哥 提交于 2019-12-12 10:07:04

问题


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:

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

I can read the variables using

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"}

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"}

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?

Your help is much appreciated.


回答1:


You need to run refresh after posting changes to env

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



回答2:


for me it only works with

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

otherwise I get

"Missing parameters: value,name"



回答3:


Maybe you need to set management.security.enabled:false to allow sending POST requests to /env endpoint.



来源:https://stackoverflow.com/questions/42962897/spring-cloud-starter-config-post-env-not-working

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!