SpringBean静态成员变量能否实现Apollo配置热更新?

1个月前发布 gsjqwyl
22 0 0

SpringBean静态成员变量能否实现Apollo配置的热更新?

我们知道,springbean的静态字段可以通过显式的setter方法(实例方法)来实现注入。

下面的LaborFeeCalculator中,basePercentage是一个静态字段。通过带有@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”的值发生变化时,静态变量basePercentage的值会随之改变吗?也就是说,静态属性(static 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方法,从而实现静态字段值的更新。
© 版权声明

相关文章

没有相关内容!

暂无评论

none
暂无评论...