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.
You need to run refresh after posting changes to env
curl -X POST http://localhost:8081/management/refresh
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"
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