Java spring cloud sleuth中的每个新http请求都有不同的traceId
最近,我开始使用spring cloud sleuth跟踪应用程序中的请求,并编写了示例项目来测试此功能,但当应用程序运行时,日志显示每个新http请求都有不同的跟踪ID 这是梅的密码: Config.javaJava spring cloud sleuth中的每个新http请求都有不同的traceId,java,spring,spring-cloud-sleuth,Java,Spring,Spring Cloud Sleuth,最近,我开始使用spring cloud sleuth跟踪应用程序中的请求,并编写了示例项目来测试此功能,但当应用程序运行时,日志显示每个新http请求都有不同的跟踪ID 这是梅的密码: Config.java package com.cloud; import org.springframework.boot.autoconfigure.EnableAutoConfiguration; import org.springframework.context.annotation.Componen
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
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();
}
}
下面是日志:
you called hi: span id is 6716502497271349964 and trace id is 6716502497271349964
you called hi2: span id is -4542377791493777157 and trace id is -4542377791493777157
我复制/粘贴了您的代码,它可以正常工作,但我应该指出一些不同之处: 首先也是最重要的一点,我和一个主要的巫婆一起运行它,当然有和springboot Anotion: @SpringBootApplication(scanBasePackages=“com.cloud”) 第二,我删除了你的配置类,因为它什么都不做 第三,密歇根州的spring版本发布了1.5.1.0版,spring云版本发布了Dalston.BUILD-SNAPSHOT 以下是日志: 2017-03-07 10:48:29.716信息[traceId,2488a4c8d3f7238a,2488a4c8d3f7238a,false]4179---[nio-8080-exec-1]com.cloud.Controller:跨度id为2632535164654658442,跟踪id为26325351644658442
2017-03-07 10:48:45.162信息[traceId,2488a4c8d3f7238a,F5E4764485B30FD85,false]4179---[nio-8080-exec-2]com.cloud.Controller:span id是-728327186587517563,跟踪id是26325351644658442您使用的是哪个版本的侦探?你能在Github上的某个地方发布一个样本,让我们看看发生了什么吗?我使用的是1.0.12版本。请在某个地方发布你的样本,并提供一个到itOK的链接,我会尝试在Gitub上发布,谢谢。我真的很怀疑。理论上,你可以自己安排一切,但我不能保证它会起作用。所以我们假设答案是否定的,这只有在我使用@Springbootapplication注释的情况下才有效,我在应用程序运行的情况下使用了springboot,一切都很好