应用程序在尝试获取 Sleuth 的踪迹时失败,并显示“需要一个‘brave.Tracer’类型的 bean,但无法找到”

问题描述 投票:0回答:2

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

我得到的错误是:

描述:

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

行动:

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

下面是我简单的 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
java spring spring-boot trace spring-cloud-sleuth
2个回答
1
投票

您已经提供了 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>

0
投票

使用正确的http 导入 org.apache.htrace.impl.MilliSpan; 导入 org.apache.htrace.Trace;

String traceId = Long.toString(new MilliSpan.Builder().build().getTraceId());

© www.soinside.com 2019 - 2024. All rights reserved.