首页 > 基础资料 博客日记

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

2025-07-11 23:20:05基础资料围观72

Java资料网推荐使用Apollo配置中心,**静态字段通过`@Value`的setter方法可以实现热更新**这篇文章给大家,欢迎收藏Java资料网享受知识的乐趣

有如下springbean LaborFeeCalculator,其中的 basePercentage 是一个静态field。通过显式@Valuesetter方法来进行配置参数赋值。

@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方法,从而更新静态字段的值。

文章来源:https://www.cnblogs.com/buguge/p/18979785
本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若内容造成侵权/违法违规/事实不符,请联系邮箱:jacktools123@163.com进行投诉反馈,一经查实,立即删除!

标签:

相关文章

本站推荐

标签云