有如下springbean LaborFeeCalculator,其中的 basePercentage 是一个静态field。通过显式@Value的setter方法来进行配置参数赋值。
- @Component
- public class LaborFeeCalculator {
- private static FeeRate basePercentage;
- @Value("${bossKg.order-base-percentage:80}")
- public void setBasePercentage(BigDecimal value) {
- basePercentage = FeeRate.PERCENTAGE.of(value);
- }
- ...
- }
复制代码 |
问题:当apollo里的配置项”bossKg.order-base-percentage“的值发生变更时,static变量basePercentage的值会跟着变化吗? 也就是说,静态变量支持配置热更新吗?
经亲测,是可以的。
下面是测试代码。由后面的执行日志可以看出来,这3种使用@Value的方式,都可以实现配置热更。
- package com.emaxcard.boss.modules.usertaxmonthlytotal;
- import com.emax.trans.FeeRate;
- import com.emaxcard.boss.ServerApplication;
- import lombok.extern.slf4j.Slf4j;
- import org.junit.jupiter.api.Test;
- import org.junit.runner.RunWith;
- import org.springframework.beans.factory.annotation.Value;
- import org.springframework.boot.test.context.SpringBootTest;
- import org.springframework.test.context.junit4.SpringRunner;
- import java.math.BigDecimal;
- @RunWith(SpringRunner.class)
- @SpringBootTest(classes = ServerApplication.class)
- @Slf4j
- class LaborFeeCalculatorTest {
- private FeeRate basePercentage;
- @Value("${bossKg.order-base-percentage:10}")
- public void setBasePercentage(BigDecimal value) {
- basePercentage = FeeRate.PERCENTAGE.of(value);
- log.info("LaborFeeCalculatorTest.order-base-percentage:{}", basePercentage);
- }
- private static FeeRate basePercentage2;
- @Value("${bossKg.order-base-percentage:10}")
- public void setBasePercentage2(BigDecimal value) {
- basePercentage2 = FeeRate.PERCENTAGE.of(value);
- log.info("LaborFeeCalculatorTest.order-base-percentage:{}", basePercentage2);
- }
- @Value("${bossKg.order-base-percentage:10}")
- private String orderBasePercentage;
- @Test
- public void testCalculate() throws InterruptedException {
- for (int i = 0; i < 500; i++) {
- System.out.println("loop"+i + ":" + basePercentage + "++++++++++++++++" + basePercentage2 + "++++++++++++++++" + orderBasePercentage);
- Thread.sleep(1000);
- }
- }
-
- }
复制代码 |
下面是执行test的日志:
- loop0:0.77++++++++++++++++0.77++++++++++++++++77
- loop1:0.77++++++++++++++++0.77++++++++++++++++77
- loop2:0.77++++++++++++++++0.77++++++++++++++++77
- loop3:0.77++++++++++++++++0.77++++++++++++++++77
- loop4:0.77++++++++++++++++0.77++++++++++++++++77
- // 【【【这时,在apollo控制台修改&发布了property的值 77→93】】】
- // 项目中的 LaborFeeCalculator.setBasePercentage 感知到变化
- 12:53:56.891 [server-provider-unknown] [Apollo-Config-1] INFO c.c.f.a.s.property.AutoUpdateConfigChangeListener:? - Auto update apollo changed value successfully, new value: 93, key: bossKg.order-base-percentage, beanName: laborFeeCalculator, method: com.emaxcard.boss.modules.usertaxmonthlytotal.LaborFeeCalculator.setBasePercentage
- // 当前test类 LaborFeeCalculatorTest.orderBasePercentage 感知到变化
- 12:53:56.891 [server-provider-unknown] [Apollo-Config-1] INFO c.c.f.a.s.property.AutoUpdateConfigChangeListener:? - Auto update apollo changed value successfully, new value: 93, key: bossKg.order-base-percentage, beanName: com.emaxcard.boss.modules.usertaxmonthlytotal.LaborFeeCalculatorTest.ORIGINAL, field: com.emaxcard.boss.modules.usertaxmonthlytotal.LaborFeeCalculatorTest.orderBasePercentage
- // 当前test类 LaborFeeCalculatorTest.setBasePercentage 感知到变化
- 12:53:56.891 [server-provider-unknown] [Apollo-Config-1] INFO c.c.f.a.s.property.AutoUpdateConfigChangeListener:? - Auto update apollo changed value successfully, new value: 93, key: bossKg.order-base-percentage, beanName: com.emaxcard.boss.modules.usertaxmonthlytotal.LaborFeeCalculatorTest.ORIGINAL, method: com.emaxcard.boss.modules.usertaxmonthlytotal.LaborFeeCalculatorTest.setBasePercentage
- // 当前test类 LaborFeeCalculatorTest.setBasePercentage2 感知到变化
- 12:53:56.891 [server-provider-unknown] [Apollo-Config-1] INFO c.c.f.a.s.property.AutoUpdateConfigChangeListener:71 - Auto update apollo changed value successfully, new value: 93, key: bossKg.order-base-percentage, beanName: com.emaxcard.boss.modules.usertaxmonthlytotal.LaborFeeCalculatorTest.ORIGINAL, method: com.emaxcard.boss.modules.usertaxmonthlytotal.LaborFeeCalculatorTest.setBasePercentage2
- loop5:0.93++++++++++++++++0.93++++++++++++++++93
- loop6:0.93++++++++++++++++0.93++++++++++++++++93
- ...
复制代码 |
在日志中可以看到,Apollo分别更新了:
- LaborFeeCalculator.setBasePercentage(静态字段的setter)
- LaborFeeCalculatorTest.orderBasePercentage(实例字段)
- LaborFeeCalculatorTest.setBasePercentage(实例字段的setter)
- LaborFeeCalculatorTest.setBasePercentage2(静态字段的setter)
结论
- 静态字段支持热更新:在Apollo的扩展支持下,即使是静态字段,只要通过@Value注解并提供了setter方法,Apollo在配置变更时会主动调用该setter方法,从而更新静态字段的值。
来源:程序园用户自行投稿发布,如果侵权,请联系站长删除
免责声明:如果侵犯了您的权益,请联系站长,我们会及时删除侵权内容,谢谢合作! |