Apollo Client 1.6.0 + @RefreshScope + @Value 刷新问题解析

问题描述

在使用 Apollo Client 1.6.0 结合 Spring Cloud 的 @RefreshScope 和 @Value 注解时,遇到以下问题:

  • 项目启动时第一次属性注入成功
  • 后续配置变更时,@Value 属性会刷新,但总是刷新为第一次的旧值,而不是最新的配置值
<dependency>
	<groupId>com.ctrip.framework.apollo</groupId>
	<artifactId>apollo-client</artifactId>
	<version>1.6.0</version>
</dependency>

问题本质深度解构

解决方案

apollo客户端升级到2.1.0

<dependency>
	<groupId>com.ctrip.framework.apollo</groupId>
	<artifactId>apollo-client</artifactId>
	<version>2.1.0</version>
</dependency>

自定监听

import com.ctrip.framework.apollo.model.ConfigChange;
import com.ctrip.framework.apollo.model.ConfigChangeEvent;
import com.ctrip.framework.apollo.spring.annotation.ApolloConfigChangeListener;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeansException;
import org.springframework.cloud.context.environment.EnvironmentChangeEvent;
import org.springframework.cloud.context.scope.refresh.RefreshScope;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.stereotype.Component;

@Component
@Slf4j
public class ApolloChangeListener implements ApplicationContextAware {
    private final RefreshScope refreshScope;
    private ApplicationContext applicationContext;

    public ApolloChangeListener(RefreshScope refreshScope) {
        this.refreshScope = refreshScope;
    }

    /**
     * 实时监听配置修改,并对修改项进行处理
     *
     * @param changeEvent 配置修改事件(其中包含修改信息)
     */
    @ApolloConfigChangeListener("${apollo.bootstrap.namespaces}")
    private void configChangeHandler(ConfigChangeEvent changeEvent) {
        log.info("================Apollo auto refresh start===========================");
        for (String changedKey : changeEvent.changedKeys()) {
            ConfigChange configChange = changeEvent.getChange(changedKey);
            String oldValue = configChange.getOldValue();
            String newValue = configChange.getNewValue();
            log.info("changedKey:{},oldValue:{}, newValue:{}", changedKey, oldValue, newValue);
        }
        refreshContext(changeEvent);
        log.info("================Apollo auto refresh end===========================");
    }

    /**
     * 跟进配置修改刷新上下文内容
     *
     * @param changeEvent 配置修改事件(其中包含修改信息)
     */
    public void refreshContext(ConfigChangeEvent changeEvent) {
        this.applicationContext.publishEvent(new EnvironmentChangeEvent(changeEvent.changedKeys()));
        refreshScope.refreshAll();
    }

    @Override
    public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
        this.applicationContext = applicationContext;
    }
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值