Micrometer Tracing traceId not passing into downstream.

huangapple go评论49阅读模式
英文:

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(&quot;/cart&quot;)
public String addItem() {
    logger.info(&quot;add item - process started&quot;);
    final Product itemProduct = 
restTemplate.getForObject(&quot;http://localhost:8081/product&quot;,
            Product.class);
    System.out.println(&quot;itemProduct&quot; + itemProduct);
    return &quot;ok&quot;;
    }
}






     &lt;dependency&gt;
        &lt;groupId&gt;org.springframework.boot&lt;/groupId&gt;
        &lt;artifactId&gt;spring-boot-starter-web&lt;/artifactId&gt;
    &lt;/dependency&gt;
    &lt;dependency&gt;
        &lt;groupId&gt;org.springframework.boot&lt;/groupId&gt;
        &lt;artifactId&gt;spring-boot-starter-actuator&lt;/artifactId&gt;
    &lt;/dependency&gt;
    &lt;dependency&gt;
        &lt;groupId&gt;org.springframework.boot&lt;/groupId&gt;
        &lt;artifactId&gt;spring-boot-starter-aop&lt;/artifactId&gt;
    &lt;/dependency&gt;
    &lt;dependency&gt;
        &lt;groupId&gt;io.micrometer&lt;/groupId&gt;
        &lt;artifactId&gt;micrometer-tracing-bridge-brave&lt;/artifactId&gt;
    &lt;/dependency&gt;
    &lt;dependency&gt;
        &lt;groupId&gt;org.springframework.boot&lt;/groupId&gt;
        &lt;artifactId&gt;spring-boot-starter-actuator&lt;/artifactId&gt;
    &lt;/dependency&gt;

答案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

&lt;dependency&gt;
    &lt;groupId&gt;io.zipkin.brave&lt;/groupId&gt;
    &lt;artifactId&gt;brave-instrumentation-spring-web&lt;/artifactId&gt;
&lt;/dependency&gt;

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

&lt;dependency&gt;
    &lt;groupId&gt;io.zipkin.brave&lt;/groupId&gt;
    &lt;artifactId&gt;brave-instrumentation-spring-web&lt;/artifactId&gt;
&lt;/dependency&gt;

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

&lt;dependencies&gt;
&lt;dependency&gt;
    &lt;groupId&gt;org.springframework.boot&lt;/groupId&gt;
    &lt;artifactId&gt;spring-boot-starter-aop&lt;/artifactId&gt;
&lt;/dependency&gt;
&lt;dependency&gt;
	&lt;groupId&gt;io.micrometer&lt;/groupId&gt;
	&lt;artifactId&gt;micrometer-tracing&lt;/artifactId&gt;
	&lt;version&gt;1.1.5&lt;/version&gt;
&lt;/dependency&gt;
&lt;dependency&gt;
	&lt;groupId&gt;io.micrometer&lt;/groupId&gt;
	&lt;artifactId&gt;micrometer-tracing-bridge-brave&lt;/artifactId&gt;
&lt;/dependency&gt;	
&lt;dependency&gt;
	&lt;groupId&gt;io.zipkin.brave&lt;/groupId&gt;
	&lt;artifactId&gt;brave-instrumentation-spring-web&lt;/artifactId&gt;
&lt;/dependency&gt;
&lt;dependency&gt;
	&lt;groupId&gt;io.zipkin.reporter2&lt;/groupId&gt;
	&lt;artifactId&gt;zipkin-reporter-brave&lt;/artifactId&gt;
	&lt;version&gt;2.15.1&lt;/version&gt;
&lt;/dependency&gt;

Please help to resolve 

huangapple
  • 本文由 发表于 2023年5月25日 22:31:02
  • 转载请务必保留本文链接:https://go.coder-hub.com/76333429.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定