尝试获取Sleuth的跟踪信息时,应用程序失败并显示“需要找不到'brave.Tracer'类型的Bean”

问题描述

我正在从另一个微服务中调用我的Springboot应用程序以获取跟踪和跨度ID,但是在启动该应用程序时出现错误。 我想做的是在app1中创建跟踪和跨度,然后调用app2并获取从app1在app2中传递的跟踪ID。在app1中,我能够获取跟踪和跨度,但在app2中,我想获取其跟踪失败的信息。我尝试引用this

我得到的错误是:

说明:

com.test.demo1.filter.MyFilter中的构造函数的参数0需要一个类型为'brave.Tracer'的bean,无法找到。

动作:

请考虑在您的配置中定义类型为'brave.Tracer'的bean。” 我也尝试创建Bean,但给出错误“构造函数Tracer()未定义”

下面是我简单的RestController和pom.xml

EXT_MODULES = [Extension('example.src.ex',sources=['example/src/ex.pyx'])]

setup_info = dist(
    ...
    ext_modules=cythonize(EXT_MODULES,compiler_directives={'language_level': '3'}),...
)
setup(**setup_info)

pom.xml

running build_ext
building 'example.src.ex' extension
creating build/temp.linux-x86_64-3.8
creating build/temp.linux-x86_64-3.8/example
creating build/temp.linux-x86_64-3.8/example/src
clang -Wno-unused-result -Wsign-compare -DNDEBUG -g -fwrapv -O3 -Wall -DTHREAD_STACK_SIZE=0x100000 -fPIC -I/usr/local/include/python3.8 -c example/src/ex.c -o build/temp.linux-x86_64-3.8/example/src/ex.o
creating build/lib.linux-x86_64-3.8
creating build/lib.linux-x86_64-3.8/example
creating build/lib.linux-x86_64-3.8/example/src
gcc -shared -Wl,--strip-all build/temp.linux-x86_64-3.8/example/src/ex.o -L/usr/local/lib -o build/lib.linux-x86_64-3.8/example/src/ex.cpython-38-x86_64-linux-gnu.so

如何解决此问题或实现此目标?

以下是我的mvn clean依赖项:tree:-

package com.test.demo1.controller;

import java.util.logging.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import brave.Tracer;


@RestController
@Component
public class Greeting {

private static final Logger logger = Logger.getLogger(Greeting.class.getName());
    
    @Autowired
    Tracer tracer;

    @RequestMapping("/greeting")
    public String greeting() {
        logger.info("Hello info from from spring sleuth");
        logger.info("tracer is -----> "+tracer.currentSpan().context().traceIdString());
        System.out.println("tracer-id :"+ tracer.currentSpan().context().traceIdString());
        return "Hello";
    }

}

解决方法

您提供了Sleuth的1.0.0版本,该版本已有两年的历史了。请转到start.spring.io以Sleuth作为依赖项生成一个项目。然后,您将确保没有手动混淆依赖关系。

正确设置的示例

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.3.4.RELEASE</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>
    <groupId>com.example</groupId>
    <artifactId>demo</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>demo</name>
    <description>Demo project for Spring Boot</description>

    <properties>
        <java.version>11</java.version>
        <spring-cloud.version>Hoxton.SR8</spring-cloud.version>
    </properties>

    <dependencies>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-sleuth</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-zipkin</artifactId>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
            <exclusions>
                <exclusion>
                    <groupId>org.junit.vintage</groupId>
                    <artifactId>junit-vintage-engine</artifactId>
                </exclusion>
            </exclusions>
        </dependency>
    </dependencies>

    <dependencyManagement>
        <dependencies>
            <dependency>
                <groupId>org.springframework.cloud</groupId>
                <artifactId>spring-cloud-dependencies</artifactId>
                <version>${spring-cloud.version}</version>
                <type>pom</type>
                <scope>import</scope>
            </dependency>
        </dependencies>
    </dependencyManagement>

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>

</project>