找回密码
 立即注册
首页 业界区 安全 使用Apollo配置中心,**静态字段通过`@Value`的setter方 ...

使用Apollo配置中心,**静态字段通过`@Value`的setter方法可以实现热更新**

移国拱 2025-7-11 21:38:39
有如下springbean LaborFeeCalculator,其中的 basePercentage 是一个静态field。通过显式@Value的setter方法来进行配置参数赋值。
  1. @Component
  2. public class LaborFeeCalculator {
  3.     private static FeeRate basePercentage;
  4.     @Value("${bossKg.order-base-percentage:80}")
  5.     public void setBasePercentage(BigDecimal value) {
  6.         basePercentage = FeeRate.PERCENTAGE.of(value);
  7.     }
  8.         ...
  9. }
复制代码
 
问题:当apollo里的配置项”bossKg.order-base-percentage“的值发生变更时,static变量basePercentage的值会跟着变化吗? 也就是说,静态变量支持配置热更新吗?
 
经亲测,是可以的。
 
 
下面是测试代码。由后面的执行日志可以看出来,这3种使用@Value的方式,都可以实现配置热更。
  1. package com.emaxcard.boss.modules.usertaxmonthlytotal;
  2. import com.emax.trans.FeeRate;
  3. import com.emaxcard.boss.ServerApplication;
  4. import lombok.extern.slf4j.Slf4j;
  5. import org.junit.jupiter.api.Test;
  6. import org.junit.runner.RunWith;
  7. import org.springframework.beans.factory.annotation.Value;
  8. import org.springframework.boot.test.context.SpringBootTest;
  9. import org.springframework.test.context.junit4.SpringRunner;
  10. import java.math.BigDecimal;
  11. @RunWith(SpringRunner.class)
  12. @SpringBootTest(classes = ServerApplication.class)
  13. @Slf4j
  14. class LaborFeeCalculatorTest {
  15.     private FeeRate basePercentage;
  16.     @Value("${bossKg.order-base-percentage:10}")
  17.     public void setBasePercentage(BigDecimal value) {
  18.         basePercentage = FeeRate.PERCENTAGE.of(value);
  19.         log.info("LaborFeeCalculatorTest.order-base-percentage:{}", basePercentage);
  20.     }
  21.     private static FeeRate basePercentage2;
  22.     @Value("${bossKg.order-base-percentage:10}")
  23.     public void setBasePercentage2(BigDecimal value) {
  24.         basePercentage2 = FeeRate.PERCENTAGE.of(value);
  25.         log.info("LaborFeeCalculatorTest.order-base-percentage:{}", basePercentage2);
  26.     }
  27.     @Value("${bossKg.order-base-percentage:10}")
  28.     private String orderBasePercentage;
  29.     @Test
  30.     public void testCalculate() throws InterruptedException {
  31.         for (int i = 0; i < 500; i++) {
  32.             System.out.println("loop"+i + ":" + basePercentage + "++++++++++++++++" + basePercentage2 + "++++++++++++++++" + orderBasePercentage);
  33.             Thread.sleep(1000);
  34.         }
  35.     }
  36.    
  37. }
复制代码
 
下面是执行test的日志:
  1. loop0:0.77++++++++++++++++0.77++++++++++++++++77
  2. loop1:0.77++++++++++++++++0.77++++++++++++++++77
  3. loop2:0.77++++++++++++++++0.77++++++++++++++++77
  4. loop3:0.77++++++++++++++++0.77++++++++++++++++77
  5. loop4:0.77++++++++++++++++0.77++++++++++++++++77
  6. // 【【【这时,在apollo控制台修改&发布了property的值 77→93】】】
  7. // 项目中的 LaborFeeCalculator.setBasePercentage 感知到变化
  8. 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
  9. // 当前test类 LaborFeeCalculatorTest.orderBasePercentage 感知到变化
  10. 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
  11. // 当前test类 LaborFeeCalculatorTest.setBasePercentage 感知到变化
  12. 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
  13. // 当前test类 LaborFeeCalculatorTest.setBasePercentage2 感知到变化
  14. 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
  15. loop5:0.93++++++++++++++++0.93++++++++++++++++93
  16. loop6:0.93++++++++++++++++0.93++++++++++++++++93
  17. ...
复制代码
 
在日志中可以看到,Apollo分别更新了:

  • LaborFeeCalculator.setBasePercentage(静态字段的setter)
  • LaborFeeCalculatorTest.orderBasePercentage(实例字段)
  • LaborFeeCalculatorTest.setBasePercentage(实例字段的setter)
  • LaborFeeCalculatorTest.setBasePercentage2(静态字段的setter)
结论


  • 静态字段支持热更新:在Apollo的扩展支持下,即使是静态字段,只要通过@Value注解并提供了setter方法,Apollo在配置变更时会主动调用该setter方法,从而更新静态字段的值。

来源:程序园用户自行投稿发布,如果侵权,请联系站长删除
免责声明:如果侵犯了您的权益,请联系站长,我们会及时删除侵权内容,谢谢合作!
您需要登录后才可以回帖 登录 | 立即注册