RefreshScope 刷新 @ConfigurationProperties 配置 bean 后,访问配置 bean 有可能读取到旧值
作者: JunkCoder17创建于 2026年6月25日更新于 2026年7月4日
标签bugneed investigationarea/client
@Component @RequiredArgsConstructor @Slf4j public class ApolloAutoRefreshConfig implements BeanPostProcessor {
public static final String APOLLO_NAMESPACES = "${apollo.bootstrap.namespaces}";
private final RefreshScope refreshScope;
private final Environment environment;
private final List<PostRefreshConfigChangeListener> listeners;
private static final String NAMESPACE_DELIMITER = ",";
private static final Splitter NAMESPACE_SPLITTER = Splitter.on(NAMESPACE_DELIMITER)
.omitEmptyStrings().trimResults();
@ApolloConfigChangeListener(value = APOLLO_NAMESPACES)
@Trace
public void onChange(ConfigChangeEvent changeEvent) {
refreshScope.refreshAll();
log.info("Apollo config refreshed, namespace: {}, keys: {}", changeEvent.getNamespace(), changeEvent.changedKeys());
for (PostRefreshConfigChangeListener listener : listeners) {
if (listenerInterestedInEvent(listener, changeEvent)) {
listener.onChange(changeEvent);
}
}
}
private boolean listenerInterestedInEvent(PostRefreshConfigChangeListener listener, ConfigChangeEvent changeEvent) {
String namespace = changeEvent.getNamespace();
Set<String> changedKeys = changeEvent.changedKeys();
Set<String> interestedNamespaces = listener.getNamespaces();
Set<String> interestedKeys = listener.getInterestedKeys();
Set<String> interestedKeyPrefixes = listener.getInterestedKeyPrefixes();
if (!interestedNamespaces.contains(namespace)) {
return false;
}
if (CollectionUtils.isEmpty(interestedKeys) && CollectionUtils.isEmpty(interestedKeyPrefixes)) {
return true;
}
if (interestedKeys != null) {
for (String interestedKey : interestedKeys) {
if (changedKeys.contains(interestedKey)) {
return true;
}
}
}
if (interestedKeyPrefixes != null) {
for (String prefix : interestedKeyPrefixes) {
for (String changedKey : changedKeys) {
if
…
内容来源: apolloconfig/apollo