我们知道,springbean的静态field可以通过显式的setter方法(实例方法)实现注入。
下面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
的值会跟着变化吗? 也就是说,静态属性(static field)支持配置热更新吗?
我认为是可以的。因为这与field是否静态无关,而与 @Value修饰的setter方法有关。当配置值变更时,这个setter方法就会触发执行。
好奇的我,想看看apollo的相关日志。所以,亲测一下。
下面是测试代码。由后面的执行日志可以看出来,这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方法,从而更新静态字段的值。
来源链接:https://www.cnblogs.com/buguge/p/18979785
暂无评论内容