java 使用spring boot时如何配置动态属性?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/28756014/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to configure dynamic properties while using spring boot?
提问by eton dolittle
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 Configurationto externalize my properties e.g. db connection details. But I also have other dynamicproperties 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=trueand then after a while the same feature would be turned off by app.cool-feature.enable=false
我打算使用 Spring Boot 来完成我的任务。它是一个典型的服务器应用程序,连接到数据库。我知道我可以使用Spring Configuration来外部化我的属性,例如 db 连接详细信息。但我还有其他需要在运行时更新的动态属性。例如鳍状肢/功能标志。我的应用程序的某些功能需要动态控制,例如想象一个类似app.cool-feature.enable=true的属性,然后一段时间后相同的功能将被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 } }
- 将 JMS 消息发送到具有上述属性更改的服务器实例
- 在服务器实例上调用公开的 API 端点,例如 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.
我知道我可以编写自己的自定义代码来实现这个(这将是第三次),但只是想知道是否已经有关于我不知道的动态属性配置的标准方法/通用实践。如果它可以与其他解决方案一起使用,例如 Apache ZooKeeper、coreos etcd、Netflix curator 等,并且与 Spring 紧密集成,那就太好了。
Thoughts?
想法?
回答by Vince108
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.
如果您使用的是 Spring Boot,请查看@ConfigurationProperties
. 您将需要提供一个 Bean 来访问您的属性。因此,属性的原始值可以在执行期间更改,因为它们是 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.
然后将此 bean 注入 JMS 消息侦听器中。
Of course if you use JMS and Spring boot, with autoconfiguration you already have Properties class...
当然,如果您使用 JMS 和 Spring boot,通过自动配置,您已经拥有 Properties 类...
回答by Avis
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 linkfor same.
您的需求是“Spring Cloud Config”的一个很好的用例,您不仅可以将所有配置集中放置,还可以动态刷新它们,然后您的引用应用程序可以从下一刻开始选择这些配置。请参阅此标准弹簧链接。
回答by Riggs
You might want to take a look at Togglz: www.togglz.org
您可能想看看 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 是 Java 功能切换模式的实现。Feature Toggles 是持续部署和交付上下文中非常常见的敏捷开发实践。基本思想是将切换与您正在处理的每个新功能相关联。这允许您在应用程序运行时启用或禁用这些功能,甚至对于个人用户也是如此。
Togglz is not bound to the spring framework but supports it.
Togglz 不受 spring 框架的约束,但支持它。