英文:
Micrometer Tracing traceId not passing into downstream
问题
以下是您提供的内容的翻译:
"Let's say I have two Microservices. When trying to use Micrometer, traceId was not traversing to the next microservice."
假设我有两个微服务。尝试使用 Micrometer 时,traceId 未传递到下一个微服务。
"I am providing code of cart service which will call product service."
我提供购物车服务的代码,该服务将调用产品服务。
"The traceId which gets printed in cart and product service are different."
在购物车和产品服务中打印的 traceId 不同。
"Not able to understand the issue."
无法理解问题。
server.port=8081
spring.application.name=product_catalog
logging.level.org.springframework.web.servlet.DispatcherServlet=TRACE
management.tracing.sampling.probability=1.0
management.endpoints.web.exposure.include=*
logging.pattern.level=%5p [${spring.application.name:},%X{traceId:-},%X{spanId:-}]
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;
@RestController
public class ShoppingCartService {
@Autowired
RestTemplate restTemplate;
Logger logger = LoggerFactory.getLogger(ShoppingCartService.class);
@GetMapping("/cart")
public String addItem() {
logger.info("add item - process started");
final Product itemProduct =
restTemplate.getForObject("http://localhost:8081/product",
Product.class);
System.out.println("itemProduct" + itemProduct);
return "ok";
}
}
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-aop</artifactId>
</dependency>
<dependency>
<groupId>io.micrometer</groupId>
<artifactId>micrometer-tracing-bridge-brave</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
英文:
Let's say I have two Microservice. When trying to use Micrometer, traceId was not traversing to the next microservice.
I am providing code of cart service which will call product service.
The traceId which gets printed in cart and product service are different.
Not able to understand the issue.
server.port=8081
spring.application.name=product_catalog
logging.level.org.springframework.web.servlet.DispatcherServlet=TRACE
management.tracing.sampling.probability=1.0
management.endpoints.web.exposure.include=*
logging.pattern.level=%5p [${spring.application.name:},%X{traceId:-},%X{spanId:-}]
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;
@RestController
public class ShoppingCartService {
@Autowired
RestTemplate restTemplate;
Logger logger = LoggerFactory.getLogger(ShoppingCartService.class);
@GetMapping("/cart")
public String addItem() {
logger.info("add item - process started");
final Product itemProduct =
restTemplate.getForObject("http://localhost:8081/product",
Product.class);
System.out.println("itemProduct" + itemProduct);
return "ok";
}
}
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-aop</artifactId>
</dependency>
<dependency>
<groupId>io.micrometer</groupId>
<artifactId>micrometer-tracing-bridge-brave</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
答案1
得分: 2
After migrating to Spring Boot 3, you need to add an interceptor to RestTemplate.
This worked for me.
Add the library to pom.xml
<dependency>
<groupId>io.zipkin.brave</groupId>
<artifactId>brave-instrumentation-spring-web</artifactId>
</dependency>
Add TracingClientHttpRequestInterceptor to RestTemplate
import brave.Tracing;
import brave.http.HttpTracing;
import brave.spring.web.TracingClientHttpRequestInterceptor;
@Bean
public HttpTracing create(Tracing tracing) {
return HttpTracing
.newBuilder(tracing)
.build();
}
@Bean
public RestTemplate restTemplate(HttpTracing httpTracing) {
return new RestTemplateBuilder()
.interceptors(TracingClientHttpRequestInterceptor.create(httpTracing))
.build();
}
I hope this helps!
英文:
After migrating to Spring Boot 3, you need to add an interceptor to RestTemplate.
This worked for me.
Add the library to pom.xml
<dependency>
<groupId>io.zipkin.brave</groupId>
<artifactId>brave-instrumentation-spring-web</artifactId>
</dependency>
Add TracingClientHttpRequestInterceptor to RestTemplate
import brave.Tracing;
import brave.http.HttpTracing;
import brave.spring.web.TracingClientHttpRequestInterceptor;
@Bean
public HttpTracing create(Tracing tracing) {
return HttpTracing
.newBuilder(tracing)
.build();
}
@Bean
public RestTemplate restTemplate(HttpTracing httpTracing) {
return new RestTemplateBuilder()
.interceptors(TracingClientHttpRequestInterceptor.create(httpTracing))
.build();
}
I hope this helps!
答案2
得分: 0
I am getting same issue of traceid not being traversing between services. I have used following dependencies. I have injected RestTemplate with above approach
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-aop</artifactId>
</dependency>
<dependency>
<groupId>io.micrometer</groupId>
<artifactId>micrometer-tracing</artifactId>
<version>1.1.5</version>
</dependency>
<dependency>
<groupId>io.micrometer</groupId>
<artifactId>micrometer-tracing-bridge-brave</artifactId>
</dependency>
<dependency>
<groupId>io.zipkin.brave</groupId>
<artifactId>brave-instrumentation-spring-web</artifactId>
</dependency>
<dependency>
<groupId>io.zipkin.reporter2</groupId>
<artifactId>zipkin-reporter-brave</artifactId>
<version>2.15.1</version>
</dependency>
Please help to resolve
英文:
I am getting same issue of traceid not being traversing between services. I have used following dependencies. I have injected RestTemplate with above approach
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-aop</artifactId>
</dependency>
<dependency>
<groupId>io.micrometer</groupId>
<artifactId>micrometer-tracing</artifactId>
<version>1.1.5</version>
</dependency>
<dependency>
<groupId>io.micrometer</groupId>
<artifactId>micrometer-tracing-bridge-brave</artifactId>
</dependency>
<dependency>
<groupId>io.zipkin.brave</groupId>
<artifactId>brave-instrumentation-spring-web</artifactId>
</dependency>
<dependency>
<groupId>io.zipkin.reporter2</groupId>
<artifactId>zipkin-reporter-brave</artifactId>
<version>2.15.1</version>
</dependency>
Please help to resolve
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论