当前位置: 首页 > 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;}
}
http://www.xdnf.cn/news/484417.html

相关文章:

  • volatile关键字详解
  • 淘宝商家层级存在流量上限怎么办,如何突破流量上限?
  • 梁文锋署名,DeepSeek-V3新论文揭秘:低成本大模型训练如何突破算力瓶颈?
  • 养生:健康生活的极简密码
  • P21-RNN-心脏病预测
  • blender中旋转模型,导入到threejs中带了旋转信息
  • Java与C/C++跨平台互操作深度解析:Project Panama技术实战
  • 一种应用非常广泛的开源RTOS(实时操作系统):nuttx
  • Spring Security vs Shiro vs Sa-Token
  • 2024年美团春招技术岗第一批笔试
  • 23、电网数据管理与智能分析 - 负载预测模拟 - /能源管理组件/grid-data-smart-analysis
  • nfs网络文件系统
  • 网站推荐(第四期)
  • 【C++ 基础数论】质数判断
  • Pageassist安装(ollama+deepseek-r1)
  • AI 赋能 Copula 建模:大语言模型驱动的相关性分析革新
  • 每周资讯 | 腾讯Q1财报:国内游戏业务收入同比增长24%;Tripledot 8亿美元收购AppLovin游戏业务
  • 十一、Hive JOIN 连接查询
  • IDEA中git对于指定文件进行版本控制
  • 架构与UML4+1视图
  • 基于PXIE 总线架构的Kintex UltraScale 系列FPGA 高性能数据预处理板卡
  • leetcode2749. 得到整数零需要执行的最少操作数-medium
  • ai agent(智能体)开发 python高级应用5:crawl4ai 如何建立一个全面的知识库 第一步找分类
  • Redis 五种类型基础操作(redis-cli + Spring Data Redis)
  • STM32F407VET6的HAL库使用CRC校验的思路
  • React文件上传组件封装全攻略
  • WEB安全--Java安全--shiro550反序列化漏洞
  • Linux——UDP/TCP协议理论
  • 利用Python高效整理猫狗数据集训练集与验证集(附源码讲解)
  • 技术书籍推荐(001)