耐心和持久胜过激烈和狂热——拉封丹
分享一个springboot
动态配置框架:https://github.com/Code2Life/spring-boot-dynamic-config
在一些场景下可以用到热更新配置
轻量方便,简单好用
GAV
1 2 3 4 5
| <dependency> <groupId>top.code2life</groupId> <artifactId>spring-boot-dynamic-config</artifactId> <version>1.0.9</version> </dependency>
|
添加注解:@DynamicConfig
以及@Value
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35
| import lombok.Data; import org.springframework.beans.factory.annotation.Value; import org.springframework.stereotype.Component; import top.code2life.config.DynamicConfig;
import java.util.Set;
@Data @Component @DynamicConfig public class DynamicFeatures {
@Value("${dynamic-test-plain:default}") private String plainValue;
@Value("#{@featureGate.convert('${dynamic-feature-conf}')}") private Set<String> someBetaFeatureConfig;
@Value("#{@testComponent.transform(${dynamic.transform-a:20}, ${dynamic.transform-b:10})} ") private double transformBySpEL;
public double transform(double t1, double t2) { return t1 / t2; } }
|
在配置pojo
上加上注解@ConfigurationProperties
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27
| import lombok.Data; import org.springframework.boot.context.properties.ConfigurationProperties; import org.springframework.context.annotation.Configuration; import top.code2life.config.DynamicConfig;
import java.util.Map;
@Data @DynamicConfig @Configuration @ConfigurationProperties(prefix = "my-prop") public class TestConfigurationProperties {
private String str;
private Double doubleVal;
private Map<String, Object> mapVal; }
|
启动项目
1
| java -jar your-spring-boot-app.jar --spring.config.location=/path/to/config
|