如何在使用spring boot时配置动态属性? [英] How to configure dynamic properties while using spring boot?

查看:544
本文介绍了如何在使用spring boot时配置动态属性?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我打算将Spring Boot用于我的任务。它是一个连接数据库的典型服务器应用程序。我知道我可以使用 Spring配置将我的财产外部化,例如数据库连接细节。但我还有其他动态属性需要在运行时更新。例如脚蹼/功能标志。我的应用程序的某些功能需要动态控制,例如想象一下像 app.cool-feature.enable = true 这样的属性,过了一会儿就会被 app.cool-feature.enable = false <关闭相同的功能/ p>

有关建议在运行时采集此类动态行为的最佳做法是什么?我可以考虑以下选项来触发更改......




  • 将JMS消息发送到服务器实例,并进行上述属性更改

  • 在服务器实例上调用公开的API端点,例如POST http:// myapp / admin / config / update {
    config:{
    app.cool-feature.enable:true
    }
    }



我知道我可以写我自己的自定义代码实现这个(这将是第三次)但只是想知道是否已经有我不知道的动态属性配置的标准方式/常见做法。如果它可以与Apache ZooKeeper,coreos etcd,Netflix策展人等其他解决方案一起使用并与Spring紧密集成,那将会很棒。



思考?

解决方案

如果您使用的是Spring引导,请查看 @ConfigurationProperties 。您将需要提供Bean来访问您的房产。
因此,在执行期间可以更改属性的原始值,因为它们是bean的常规属性。



在您的情况下例如:

  @Component 
@ConfigurationProperties
public class JmsProperties {

private String url =vm: // localhost(假设您使用ActiveMQ);

public String getUrl()...
public void setUrl(String value)...

}

然后在你的JMS消息监听器中注入这个bean。



当然如果你使用JMS和Spring启动,使用自动配置你已经有了属性类......


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?

解决方案

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()...
    public void setUrl(String value)...

}

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

这篇关于如何在使用spring boot时配置动态属性?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆