How to configure dynamic properties while using spring boot?
Asked Answered
P

3

20

I'm planning to use Spring Boot for my assignment. Its a typical server application with connection to database. I know I can use Spring Configuration to externalize my properties e.g. db connection details. But I also have other dynamic properties which needs be updated at runtime. e.g. flippers/feature flags. Certain features of my application needs to be controlled dynamically e.g. imagine a property like app.cool-feature.enable=true and then after a while the same feature would be turned off by app.cool-feature.enable=false

Any suggestions what is the best practice around ingesting such dynamic behavior at runtime? I can think of following options to trigger the change...

  • Send a JMS message to server instance with above property change
  • Call an exposed API endpoint on the server instance e.g. POST http://myapp/admin/config/update { "config": { "app.cool-feature.enable": true } }

I know I can write the my own custom code implementing this (it would be for the 3rd time) but just wondering if there is already standard way/common practice around dynamic property configurations that I'm not aware of. Also it would be great if it can work with other solutions like Apache ZooKeeper, coreos etcd, Netflix curator etc and have close integration with Spring.

Thoughts?

Physicochemical answered 27/2, 2015 at 1:28 Comment(0)
V
8

If you are using Spring boot have a look on @ConfigurationProperties. You will be required to provide a Bean to access your properties. Therefore original values of the properties can be changed during execution since they are regular properties of a bean.

In your case for example:

@Component
@ConfigurationProperties
public class JmsProperties {

    private String url = "vm://localhost"; // (let's suppose you use ActiveMQ)
    
    public String getUrl() {
      // Do work here
    }
    public void setUrl(String value) {
      // Do work here
    }
}

And then inject this bean in you JMS message listener.

Of course if you use JMS and Spring boot, with autoconfiguration you already have Properties class...

Viradis answered 9/9, 2015 at 18:30 Comment(0)
B
8

Your requirement is a good use-case for "Spring Cloud Config" where not only you can have your all configurations centrally located but also can refresh them dynamically and which in turn can be picked by your referencing app from the very next moment. Refer this standard spring link for same.

Brinkema answered 24/9, 2015 at 5:51 Comment(0)
S
6

You might want to take a look at Togglz: www.togglz.org

From their homepage:

Togglz is an implementation of the Feature Toggles pattern for Java. Feature Toggles are a very common agile development practices in the context of continuous deployment and delivery. The basic idea is to associate a toggle with each new feature you are working on. This allows you to enable or disable these features at application runtime, even for individual users.

Togglz is not bound to the spring framework but supports it.

Smallminded answered 27/2, 2015 at 7:52 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.