【问题标题】:TracingFilter deprecated in spring cloud sleuth在 Spring Cloud sleuth 中不推荐使用 TracingFilter
【发布时间】:2021-11-15 09:29:30
【问题描述】:

似乎 TracingFilter 在 Spring Cloud Sleuth 中已被弃用,我找不到任何替代注入 mockMvc 的方法,例如:

    @Autowired
    private TracingFilter traceFilter;

    @BeforeAll
    void init() throws IOException, URISyntaxException {
        this.mockMvc = MockMvcBuilders.webAppContextSetup(this.context).apply(springSecurity())
                .addFilters(this.traceFilter)
                .build();
    }

有人找到替代品了吗?

乔纳森。

【问题讨论】:

  • TracingFilter 未被弃用。
  • 对不起,我以为是!升级后我开始收到此错误No qualifying bean of type 'org.springframework.cloud.sleuth.instrument.web.servlet.TracingFilter' available: expected at least 1 bean which qualifies as autowire candidate. Dependency annotations: {@org.springframework.beans.factory.annotation.Autowired(required=true)}

标签: spring-boot spring-cloud spring-cloud-sleuth


【解决方案1】:

TracingFilter 未被弃用。

你看到的问题:

No qualifying bean of type 'org.springframework.cloud.sleuth.instrument.web.servlet.TracingFilter'

因为 Sleuth 没有创建 TracingFilter bean(TracingFilterFilter 并且以正确的顺序注册它可能会很棘手)。

它是在TraceWebServletConfiguration 中创建的。你也可以在这里做类似的事情,虽然我不确定我是否理解你想要做的事情。

【讨论】:

  • 非常感谢您提供的信息!我实际上是在尝试在 MockMvc 中注册 TracingFilter 以测试响应中的 traceId。在旧版本中很容易做到,但是在新的spring cloud版本中我没有成功实现!
【解决方案2】:

解决方法:

protected static TracingFilter tracingFilter(final WebApplicationContext context) {
    return TracingFilter.create(context.getBean(CurrentTraceContext.class), context.getBean(HttpServerHandler.class));
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2021-09-06
    • 1970-01-01
    • 2021-07-28
    • 1970-01-01
    • 2020-05-10
    • 2018-06-30
    • 2020-05-28
    • 2016-11-23
    相关资源
    最近更新 更多