You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Spring Cloud Sleuth调用/actuator/refresh后logback springProperty丢失问题

问题根因

该问题属于Spring Cloud Sleuth的已知缺陷:调用/actuator/refresh端点触发配置刷新时,Sleuth会主动触发日志上下文重新初始化,但其初始化逻辑未兼容logback-spring.xml中自定义的springProperty配置,导致构建时注入的静态属性(如示例中的version)在日志上下文重建后丢失。

解决方案

方案一:关闭Sleuth日志自动刷新(最简便,无功能影响)

直接在application.yml中添加如下配置,禁用Sleuth在配置刷新时重建日志上下文的行为,不会影响链路traceId、spanId的正常注入:

spring:
  sleuth:
    log:
      slf4j:
        refresh:
          enabled: false

方案二:静态属性通过MDC全局注入

如果需要保留Sleuth的日志刷新能力,可放弃logback的springProperty配置,改为启动时将静态构建属性注入全局MDC:
添加配置类如下:

import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Configuration;
import org.slf4j.MDC;
import javax.annotation.PostConstruct;

@Configuration
public class MdcConfig {
    @Value("${spring.build.version}")
    private String version;

    @PostConstruct
    public void setMdcProperties() {
        MDC.put("version", version);
    }
}

之后直接在logback的日志模板中引用%mdc{version}即可,配置刷新后属性不会丢失。

方案三:升级Spring Cloud版本

该bug已在Spring Cloud 2021.0.5(Jubilee SR5)及更高版本中修复,升级对应依赖版本即可从根源解决问题,gradle依赖配置示例如下(需匹配对应Spring Boot版本):

implementation platform('org.springframework.cloud:spring-cloud-dependencies:2021.0.5')
implementation 'org.springframework.cloud:spring-cloud-starter-sleuth'

内容的提问来源于stack exchange,提问作者Jonathan R

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.09.29 09:27:06