Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to configure dynamic properties while using spring boot?

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?

like image 953
eton dolittle Avatar asked Feb 27 '15 01:02

eton dolittle


People also ask

How do I set application properties dynamically in spring boot?

run(SpringBootTestApplication. class, args); and used SpringApplicationBuilder class to add properties to the app. Now, these properties are defined in the program, You can apply condition and change property values based on your requirements. Save this answer.

How do I set environment properties in spring boot?

Environment-Specific Properties File. If we need to target different environments, there's a built-in mechanism for that in Boot. We can simply define an application-environment. properties file in the src/main/resources directory, and then set a Spring profile with the same environment name.

What is @configuration in spring boot?

Spring @Configuration annotation is part of the spring core framework. Spring Configuration annotation indicates that the class has @Bean definition methods. So Spring container can process the class and generate Spring Beans to be used in the application.


1 Answers

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...

like image 126
Vince108 Avatar answered Oct 11 '22 19:10

Vince108