当前位置: 首页 > news >正文

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();}@Overridepublic void setApplicationContext(ApplicationContext applicationContext) throws BeansException {this.applicationContext = applicationContext;}
}

相关文章:

  • 【AT32】 AT32 移植 Freemodbus 主站
  • ssti刷刷刷
  • threejs小案例——贴图翻转
  • 华为云Flexus+DeepSeek征文|SpringBoot开发实战:基于ModelArts Studio高效集成DeepSeek大模型服务
  • 归并排序:分治思想的优雅实现
  • 后端框架(1):Mybatis
  • 在 C# 中将 DataGridView 数据导出为 CSV
  • ROS资料推荐学习
  • xss-labs靶场第11-14关基础详解
  • Microsoft Azure 服务4月更新告示
  • handsome主题美化及优化:10.1.0最新版 - 1
  • 网站运维基础 | 2. cms介绍及wordpress的搭建
  • 大模型——Crawl4AI使用JsonCssExtractionStrategy进行结构化数据提取
  • 真题卷001——算法备赛
  • PCIeSwitch 学习
  • MySQL的触发器
  • 本土DevOps革命:Gitee如何撬动中国企业的数字化转型新动能
  • 哈希表的实现02
  • 【C++/Qt shared_ptr 与 线程池】合作使用案例
  • 非受控组件在React中的使用场景有哪些?
  • 特朗普公开“怼”库克:苹果不应在印度生产手机
  • 河南信阳:对违规吃喝问题不遮丑不护短,露头就打、反复敲打
  • 党建评:对违规宴饮等问题要坚决露头就打
  • 回望星河深处,唤醒文物记忆——读《发现武王墩》
  • 马上评丨岂能为流量拿自己的生命开玩笑
  • 中拉论坛部长级会议为何悬挂海地和圣卢西亚的国旗?外交部回应