RestTemplate sendet dieselbe Trace-ID zwischen zwei Diensten.
RestClient sendet unterschiedliche Trace-ID zwischen zwei Diensten.
Klasse RestClientConfig:
Code: Select all
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.client.RestClient;
import org.springframework.web.client.support.RestClientAdapter;
import org.springframework.web.service.invoker.HttpServiceProxyFactory;
import com.example.clients.BeClient;
@Configuration
public class RestClientConfig {
@Value("${api.url}")
private String apiUrl;
@Bean
public BeClient beClient() {
RestClient restClient = RestClient.builder()
.baseUrl(apiUrl)
.build();
var restClientAdapter = RestClientAdapter.create(restClient);
var httpServiceProxyFactory = HttpServiceProxyFactory.builderFor(restClientAdapter).build();
return httpServiceProxyFactory.createClient(BeClient.class);
}
}
Code: Select all
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.service.annotation.GetExchange;
import org.springframework.web.service.annotation.HttpExchange;
import com.example.dtos.HelloWorldDto;
@HttpExchange
public interface BeClient {
@GetExchange("/message/{id}")
HelloWorldDto findById(@PathVariable("id") Long id);
}
Code: Select all
logging.pattern.correlation=[${spring.application.name:},%X{traceId:-},%X{spanId:-}]
Für den FE-Service lautet die Track-ID 678d0ddf7a804137440ac3007174d797.
Code: Select all
2025-01-19 15:36:15.397
2025-01-19T14:36:15.397Z INFO 1 --- [fe] [http-nio-8080-exec-1] [fe,678d0ddf7a804137440ac3007174d797,440ac3007174d797]c.e.controllers.HelloWorldController : Called FE method HelloWorldController.findById()
2025-01-19 15:35:47.974
2025-01-19T14:35:47.974Z INFO 1 --- [fe] [http-nio-8080-exec-1] [fe,,]o.s.web.servlet.DispatcherServlet : Completed initialization in 2 ms
2025-01-19 15:35:47.972
2025-01-19T14:35:47.972Z INFO 1 --- [fe] [http-nio-8080-exec-1] [fe,,]o.s.web.servlet.DispatcherServlet : Initializing Servlet 'dispatcherServlet'
Für BE-Service ist die Track-ID 678d0ddf07b239da05ec3e7febcb5842.
Code: Select all
2025-01-19 15:36:15.431
2025-01-19T14:36:15.431Z INFO 1 --- [be] [http-nio-8081-exec-4] [be,678d0ddf07b239da05ec3e7febcb5842,05ec3e7febcb5842]c.e.controllers.HelloWorldController : Called BE method HelloWorldController.helloWorld() for id 1
2025-01-19 15:35:57.771
2025-01-19T14:35:57.771Z INFO 1 --- [be] [http-nio-8081-exec-1] [be,,]o.s.web.servlet.DispatcherServlet : Completed initialization in 1 ms
2025-01-19 15:35:57.770
2025-01-19T14:35:57.770Z INFO 1 --- [be] [http-nio-8081-exec-1] [be,,]o.s.web.servlet.DispatcherServlet : Initializing Servlet 'dispatcherServlet'
Link zum GIT-Repository mit Quellcode: https://github.com/wisniewskikr/chrisblog -it-cloud/tree/main/spring-cloud/observability/springcloud-springboot3-observability-grafana-stack-restclient
ZusammenfassungIch verwende Java in der Version 23 und Spring Boot in Version 3.4.1. Irgendeine Idee, wie ich dieses Problem lösen kann?