简体   繁体   English

Spring Cloud Sleuth中每个新的HTTP请求都具有不同的traceId

[英]different traceId every new http request in spring cloud sleuth

Recently, I started with spring cloud sleuth to trace the request in the application, and I wrote sample project to test this feature, but when the application is run, the log shows that there are different trace ids for every new http request. 最近,我开始使用Spring Cloud Sleuth来跟踪应用程序中的请求,并编写了示例项目来测试此功能,但是当应用程序运行时,日志显示每个新的HTTP请求都有不同的跟踪ID。

and here is may code: Config.java 这是可能的代码:Config.java

package com.cloud;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.EnableWebMvc;

@Configuration
@ComponentScan()
@EnableAutoConfiguration()
@EnableWebMvc
public class Config {
}

Controller.java Controller.java

package com.cloud;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.sleuth.Span;
import org.springframework.cloud.sleuth.SpanAccessor;
import org.springframework.context.annotation.Bean;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;

@RestController
public class Controller {

@Autowired
SpanAccessor spanAccessor;
@Autowired
RestTemplate restTemplate;
int port = 8080;
private static final Logger LOGGER = LoggerFactory.getLogger(Controller.class);

@RequestMapping("/")
public String hi() throws InterruptedException {
    LOGGER.debug("you called hi");
    debug();
    String s = this.restTemplate
            .getForObject("http://localhost:" + this.port + "/cloud/hi2", String.class);
    return "hi/" + s;
}

@RequestMapping("/hi2")
public String hi2() throws InterruptedException {
    LOGGER.debug("you called hi2");
    debug();
    return "welcome";
}

public void debug(){
    Span span = spanAccessor.getCurrentSpan();
    LOGGER.info("span id is "+span.getSpanId()+" and trace id is "+span.getTraceId());
}

@Bean
public RestTemplate restTemplate(){
    return new RestTemplate();
}
}

and here is log: 这是日志:

you called hi: span id is 6716502497271349964 and trace id is 6716502497271349964
you called hi2: span id is -4542377791493777157  and trace id is -4542377791493777157

i copy/paste your code and it works, but i should mention some diferences: 我复制/粘贴您的代码,它可以正常工作,但我应该提一些区别:

first and most important, i run it with a main witch of course have and springboot anotation: 首先也是最重要的一点,我当然要用一个主要的女巫has和springboot注释来运行它:

@SpringBootApplication(scanBasePackages="com.cloud") @SpringBootApplication(scanBasePackages =“ com.cloud”)

second, i delete your Config class, becaouse it doesnt do anything. 第二,我删除您的Config类,因为它没有任何作用。

thirt, mi spring version its 1.5.1.RELEASE and the spring cloud version its Dalston.BUILD-SNAPSHOT thirt,mi春季版本为1.5.1.RELEASE,春季云版本为Dalston.BUILD-SNAPSHOT

this are the logs: 这是日志:

2017-03-07 10:48:29.716 INFO [traceId,2488a4c8d3f7238a,2488a4c8d3f7238a,false] 4179 --- [nio-8080-exec-1] com.cloud.Controller : span id is 2632535164654658442 and trace id is 2632535164654658442 2017-03-07 10:48:45.162 INFO [traceId,2488a4c8d3f7238a,f5e476485b30fd85,false] 4179 --- [nio-8080-exec-2] com.cloud.Controller : span id is -728327186587517563 and trace id is 2632535164654658442 2017-03-07 10:48:29.716 INFO [traceId,2488a4c8d3f7238a,2488a4c8d3f7238a,false] 4179 --- [nio-8080-exec-1] com.cloud.Controller:范围ID为2632535164654658442和跟踪ID为2632535164654658442 2017- 03-07 10:48:45.162 INFO [traceId,2488a4c8d3f7238a,f5e476485b30fd85,false] 4179 --- [nio-8080-exec-2] com.cloud.Controller:范围ID是-728327186587517563563和跟踪ID是2632535164654658442

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 在使用 Spring Cloud Sleuth 的每个 HTTP 请求之后清除 MDC 上下文是否安全? - Is it safe to clear the MDC context after every HTTP request with Spring Cloud Sleuth? Spring Cloud Sleuth:将traceId传播到其他spring应用程序 - Spring Cloud Sleuth: Propagate traceId to other spring application Spring 云侦探 3.0.1 使用 logback 在日志中生成 traceid 和 spanid - Spring cloud sleuth 3.0.1 generate traceid & spanid in logs using logback Spring 侦探没有在 spring 调度程序内创建新的 traceid - Spring sleuth not creating new traceid inside a spring scheduler Spring Cloud Sleuth v2.2.3 如何将 TraceId 传播到另一个线程 - Spring Cloud Sleuth v2.2.3 how to propagate TraceId to another thread Spring-Cloud-Sleuth 在模式布局中启用 MDC 属性以记录 TraceId - Spring-Cloud-Sleuth enable MDC properties in pattern layout to log TraceId 从Kafka消息在春季侦探中注入TraceId - Inject TraceId in spring sleuth from Kafka message 如何使用 opentelemetry 在 Spring sleuth 中设置自定义 traceId - How to set custom traceId in Spring sleuth with opentelemetry Spring Interceptor 与 Spring sleuth 3.xx 的 afterCompletion 方法具有不同的 traceId - Spring Interceptor has different traceId for afterCompletion method with Spring sleuth 3.x.x Sleuth 不会在 Spring Boot 应用程序中打印 spanId、traceId - Sleuth doesnt print spanId,traceId in spring boot application
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM