I am trying to reload configuration of my application during runtime. The configuration is in a yaml file and the binding with @ConfigurationProperties works as expected. Next thing is. I want to reload the config when the yaml has changed. Or rather I am checking with @Scheduled whether the file has changed.
I would like to avoid running a second server for having my Environment update. The two questions I have:
ConfigurableEnvironment maybe?Spring cloud config documentation states:
The
EnvironmentChangeEventcovers a large class of refresh use cases, as long as you can actually make a change to theEnvironmentand publish the event (those APIs are public and part of core Spring)
So publishing the Event is working, but I do not get on how to actually update the properties.
There is quite a discussion on that: how to refresh properties without any configuration server. There is a Dave Syer post on that here which brings some light - but still isn't self-explanatory.
The most natural approach for spring-boot/-cloud would be following (as discussed on spring-cloud-config github):
@Component
@ConfigurationProperties("ignored")
@RefreshScope
public class Config {
private List<String> path;
public List<String> getPath() {
return path;
}
public void setPath(List<String> path) {
this.path = path;
}
}
This doesn't work due to some proxy issues between @RefreshScope and @ConfigurationProperties - both annotations result in bean proxies with are at odds with each other.
Therefore I started looking at it from a spring perspective. The propertySources are accessible through Environment so you can access and modify them by
final String propertySourceName = "externalConfiguration";
// name of propertySource as defined by
// @PropertySource(name = "externalConfiguration", value = "${application.config.location}")
ResourcePropertySource propertySource = new ResourcePropertySource(propertySourceName, new FileSystemResource(file));
MutablePropertySources sources = ctx.getEnvironment().getPropertySources();
sources.replace(propertySourceName, propertySource);
My use case was based on "user editing the file" so the properties refreshed was based on the FileSystemWatcher, which mutated the propertySources. For the sources to be correctly ingested by the configuration bean, the scope of the bean needed to be a prototype - to be correctly rebuilt on every invocation.
The complete example is available as a gist. No config server is included whatsoever. Hope that helps
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With