0

我正在开发一个 Spring Boot 应用程序,其中日志记录由 logback 完成,并且 Jaeger 被集成以进行检测。

我的服务.java

@SpringBootApplication
public class Myservice{


public static void main(String[] args) {
    SpringApplication.run(Myservice.class, args);
}

@Bean
public RestTemplate restTemplate(RestTemplateBuilder restTemplateBuilder) {
    return restTemplateBuilder.build();
}

 @Bean
  public io.opentracing.Tracer jaegerTracer() {
    Builder builder = new Builder("Myservice",
        new RemoteReporter(new HttpSender(http://192.168.1.20:14268/api/traces), 10,
        65000, new Metrics(new StatsFactoryImpl(new NullStatsReporter()))),
        new ConstSampler(true))
        .registerInjector(Builtin.HTTP_HEADERS, new B3TextMapCodec())
        .registerExtractor(Builtin.HTTP_HEADERS, new B3TextMapCodec());
    return builder.build();
  }
}

logback.xml

<?xml version="1.0" encoding="UTF-8"?>
<configuration >
 <include resource="org/springframework/boot/logging/logback/defaults.xml"/>
<springProperty scope="context" name="springAppName"
    source="spring.application.name" />
<appender name="consoleAppender" 
class="ch.qos.logback.core.ConsoleAppender">
        <filter class="ch.qos.logback.classic.filter.ThresholdFilter">
            Minimum logging level to be presented in the console logs
            <level>INFO</level>
        </filter>
    <encoder 
class="net.logstash.logback.encoder.LoggingEventCompositeJsonEncoder">
        <providers>
            <pattern>
                <pattern>
                    {
                    "service": "${springAppName:-}",
                    "trace": "%X{X-B3-TraceId:-}",
                    "span": "%X{X-B3-SpanId:-}"
                    }
                </pattern>
            </pattern>
         </providers>
    </encoder>
</appender>

<root level="INFO">
    <appender-ref ref="consoleAppender" />
</root>
</configuration>

pom.xml

<parent>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-parent</artifactId>
    <version>2.0.4.RELEASE</version>
    <relativePath /> <!-- lookup parent from repository -->
</parent>
<dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-actuator</artifactId>
    </dependency>
    <dependency>
        <groupId>io.projectreactor</groupId>
        <artifactId>reactor-core</artifactId>
        <version>3.1.5.RELEASE</version>
    </dependency>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-validation</artifactId>
    </dependency>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-web</artifactId>
    </dependency>

    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-devtools</artifactId>
        <scope>runtime</scope>
    </dependency>
    <dependency>
        <groupId>org.projectlombok</groupId>
        <artifactId>lombok</artifactId>
        <optional>true</optional>
    </dependency>

    <!-- JPA Data Dependency -->

    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-data-jpa</artifactId>
    </dependency>

    <!-- Oracle driver Dependency -->

    <dependency>
        <groupId>com.oracle</groupId>
        <artifactId>ojdbc6</artifactId>
        <version>11.2.0.3</version>
    </dependency>
<!-- Opentracing with Jaeger Dependency -->

    <dependency>
        <groupId>io.opentracing</groupId>
        <artifactId>opentracing-api</artifactId>
        <version>0.31.0</version>
    </dependency>
    <dependency>
        <groupId>io.opentracing.contrib</groupId>
        <artifactId>opentracing-spring-cloud-starter</artifactId>
        <version>0.1.4</version>
    </dependency>
    <dependency>
        <groupId>com.uber.jaeger</groupId>
        <artifactId>jaeger-core</artifactId>
        <version>0.24.0</version>
    </dependency>
    <dependency>
        <groupId>com.uber.jaeger</groupId>
        <artifactId>jaeger-b3</artifactId>
        <version>0.24.0</version>
    </dependency>

    <!-- Logback Dependency -->

    <dependency>
        <groupId>ch.qos.logback</groupId>
        <artifactId>logback-classic</artifactId>
        <version>1.2.3</version>
    </dependency>
    <dependency>
        <groupId>net.logstash.logback</groupId>
        <artifactId>logstash-logback-encoder</artifactId>
        <version>4.10</version>
    </dependency>

Jeager 已正确连接到服务器并获取跟踪。问题在于 logback 日志。traceId 和 spanId 没有打印在日志中。

{"service" : "Myservice", "trace": "", "span":""}

但我自己找到了解决方案。我在我的 Spring Boot 应用程序中添加了 Spring Cloud Sleuth。

    <!-- spring-cloud-sleuth dependency -->

    <dependency>
        <groupId>org.springframework.cloud</groupId>
        <artifactId>spring-cloud-dependencies</artifactId>
        <version>Camden.SR1</version>
        <type>pom</type>
        <scope>import</scope>
    </dependency>
    <dependency>
        <groupId>org.springframework.cloud</groupId>
        <artifactId>spring-cloud-starter-sleuth</artifactId>
        <version>2.0.1.RELEASE</version>
    </dependency>

现在所有的跟踪信息都在 logback 日志中可用。但问题是 Jaeger 停止向 Jaeger 服务器注册跟踪。

我尝试了 Zipkin 而不是 Jaeger,但同样的事情发生了。我的申请有什么问题?依赖项有问题吗?

4

1 回答 1

-1

您正在使用Camden带有 boot2.0和 Sleuth的发布火车2.0。那是完全不相容的。请从start.spring.io从头开始生成项目,spring cloud项目请不要手动放任何版本,请重试。尝试使用Finchley发布火车而不是Camden

于 2018-09-04T13:48:04.067 回答