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

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

问题描述

我打算在我的作业中使用 Spring Boot.它是连接到数据库的典型服务器应用程序.我知道我可以使用 Spring 配置 外部化我的属性,例如数据库连接详细信息.但我还有其他动态属性需要在运行时更新.例如鳍状肢/功能标志.我的应用程序的某些功能需要动态控制,例如想象一个像 app.cool-feature.enable=true 这样的属性,然后一段时间后,app.cool-feature.enable=false

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

  • 向具有上述属性更改的服务器实例发送 JMS 消息
  • 调用服务器实例上公开的 API 端点,例如POST http://myapp/admin/config/update {配置":{app.cool-feature.enable":true}}
  • 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 } }

我知道我可以编写自己的自定义代码来实现这个(这将是第三次),但只是想知道是否已经有关于动态属性配置的标准方法/常见做法,但我不知道.此外,如果它可以与其他解决方案一起使用,例如 Apache ZooKeeper、coreos etcd、Netflix curator 等,并且与 Spring 紧密集成,那就太好了.

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.

想法?

推荐答案

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

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.

以您的情况为例:

@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
    }
}

然后将此 bean 注入您的 JMS 消息侦听器中.

And then inject this bean in you JMS message listener.

当然,如果您使用 JMS 和 Spring Boot,通过自动配置,您已经拥有 Properties 类...

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

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

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