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.
and here is may code: 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
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