应用程序在尝试获取 Sleuth 的踪迹时失败并显示 "required a bean of type 'brave.Tracer' that could not be found"

App is failing with "required a bean of type 'brave.Tracer' that could not be found" while trying to get Sleuth's trace

我正在从另一个微服务调用我的 Springboot 应用程序以获取跟踪和跨度 ID,但在启动应用程序时出现错误。 我想要做的是在 app1 中创建 Trace & Span,然后调用 app2 并在 app2 中获取从 app1 传递的 Trace id。在 app1 中,我能够获取跟踪和跨度,但在 app2 中,我想获取跟踪失败的地方。我试过参考 this.

我得到的错误是:

Description:

Parameter 0 of constructor in com.test.demo1.filter.MyFilter required a bean of type 'brave.Tracer' that could not be found.

Action:

Consider defining a bean of type 'brave.Tracer' in your configuration." I also tried creating the bean but is giving error "The constructor Tracer() is undefined"

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

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";
    }

}

pom.xml

<?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.test</groupId>
    <artifactId>demo1</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>demo1</name>
    <description>Demo project for Spring Boot</description>

    <properties>
        <java.version>11</java.version>
    </properties>

    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</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>
        
        <!-- https://mvnrepository.com/artifact/org.springframework.cloud/spring-cloud-sleuth -->
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-sleuth</artifactId>
            <version>1.0.0.RELEASE</version>
            <type>pom</type>
        </dependency>
        
        <!-- https://mvnrepository.com/artifact/io.opentracing.brave/brave-opentracing -->
        <dependency>
            <groupId>io.opentracing.brave</groupId>
            <artifactId>brave-opentracing</artifactId>
            <version>0.15.0</version>
        </dependency>
                
                  <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-cache</artifactId>
            <version>${version.spring.boot}</version>
            <exclusions>
                <exclusion>
                    <artifactId>spring-core</artifactId>
                    <groupId>org.springframework</groupId>
                </exclusion>
            </exclusions>
        </dependency>

        <dependency>
            <groupId>org.springframework.security</groupId>
            <artifactId>spring-security-test</artifactId>
            <scope>test</scope>
            <exclusions>
                <exclusion>
                    <artifactId>spring-web</artifactId>
                    <groupId>org.springframework</groupId>
                </exclusion>
                <exclusion>
                    <artifactId>spring-web</artifactId>
                    <groupId>org.springframework</groupId>
                </exclusion>
                <exclusion>
                    <artifactId>spring-web</artifactId>
                    <groupId>org.springframework</groupId>
                </exclusion>
                <exclusion>
                    <artifactId>spring-core</artifactId>
                    <groupId>org.springframework</groupId>
                </exclusion>
            </exclusions>
        </dependency>
        
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-cache</artifactId>
            <exclusions>
                <exclusion>
                    <artifactId>spring-core</artifactId>
                    <groupId>org.springframework</groupId>
                </exclusion>
            </exclusions>
        </dependency>
       
        <!-- https://mvnrepository.com/artifact/io.zipkin.brave/brave -->
        <dependency>
            <groupId>io.zipkin.brave</groupId>
            <artifactId>brave</artifactId>
            <version>5.12.6</version>
        </dependency>
        
        
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-actuator</artifactId>
            <exclusions>
                <exclusion>
                    <artifactId>spring-core</artifactId>
                    <groupId>org.springframework</groupId>
                </exclusion>
            </exclusions>
        </dependency>
                
    </dependencies>

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

</project>

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

下面是我的 mvn clean dependency:tree :--

[INFO] +- org.springframework.cloud:spring-cloud-starter-sleuth:jar:2.1.0.RELEASE:compile
[INFO] |  +- org.springframework.cloud:spring-cloud-starter:jar:2.1.0.RELEASE:compile
[INFO] |  |  +- org.springframework.cloud:spring-cloud-context:jar:2.1.0.RELEASE:compile
[INFO] |  |  |  \- org.springframework.security:spring-security-crypto:jar:5.3.4.RELEASE:compile
[INFO] |  |  +- org.springframework.cloud:spring-cloud-commons:jar:2.1.0.RELEASE:compile
[INFO] |  |  \- org.springframework.security:spring-security-rsa:jar:1.0.7.RELEASE:compile
[INFO] |  |     \- org.bouncycastle:bcpkix-jdk15on:jar:1.60:compile
[INFO] |  |        \- org.bouncycastle:bcprov-jdk15on:jar:1.60:compile
[INFO] |  +- org.springframework.boot:spring-boot-starter-aop:jar:2.3.4.RELEASE:compile
[INFO] |  \- org.springframework.cloud:spring-cloud-sleuth-core:jar:2.1.0.RELEASE:compile
[INFO] |     +- org.aspectj:aspectjrt:jar:1.9.6:compile
[INFO] |     +- io.zipkin.brave:brave:jar:5.6.1:compile
[INFO] |     |  +- io.zipkin.zipkin2:zipkin:jar:2.12.0:compile
[INFO] |     |  \- io.zipkin.reporter2:zipkin-reporter:jar:2.7.14:compile
[INFO] |     +- io.zipkin.brave:brave-context-log4j2:jar:5.6.1:compile
[INFO] |     +- io.zipkin.brave:brave-instrumentation-spring-web:jar:5.6.1:compile
[INFO] |     |  \- io.zipkin.brave:brave-instrumentation-http:jar:5.6.1:compile
[INFO] |     +- io.zipkin.brave:brave-instrumentation-spring-rabbit:jar:5.6.1:compile
[INFO] |     +- io.zipkin.brave:brave-instrumentation-kafka-clients:jar:5.6.1:compile
[INFO] |     +- io.zipkin.brave:brave-instrumentation-httpclient:jar:5.6.1:compile
[INFO] |     +- io.zipkin.brave:brave-instrumentation-httpasyncclient:jar:5.6.1:compile
[INFO] |     +- io.zipkin.brave:brave-instrumentation-spring-webmvc:jar:5.6.1:compile
[INFO] |     |  \- io.zipkin.brave:brave-instrumentation-servlet:jar:5.6.1:compile
[INFO] |     \- io.zipkin.brave:brave-instrumentation-jms:jar:5.6.1:compile

您在 1.0.0 版本中提供了 Sleuth,该版本已有几年历史。请到 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>