Jaeger,logback和Sleuth一起工作时出现问题

时间:2018-08-30 19:53:08

标签: spring-boot zipkin spring-cloud-sleuth opentracing jaeger

我正在开发一个Spring Boot应用程序,该应用程序中的日志记录是通过logback完成的,并且集成了Jaeger以进行检测。

Myservice.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,但是发生了同样的事情。我的申请有什么问题?依赖关系有问题吗?

1 个答案:

答案 0 :(得分:0)

您正在使用Camden和发行版2.0和Sleuth 2.0的发行版。那是完全不兼容的。请从头开始从start.spring.io生成一个项目,请不要为Spring Cloud项目手动放置任何版本,请重试。尝试使用Finchley发行火车代替Camden