最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

java - RestClient dosen't handle Trace ID in my Spring Boot 3 application - Stack Overflow

programmeradmin1浏览0评论

I migrated from RestTemplate to RestClient in my Spring Boot 3 application. But now I have a problem with Trace ID: the identifier of the end-to-end operation in which this particular overall operation took place.

RestTemplate sends the same Trace ID between two services.

RestClient sends different Trace ID between two services.

Class RestClientConfig:

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);

    }

}

Class BeClient:

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);

}

File application.properties:

logging.pattern.correlation=[${spring.application.name:},%X{traceId:-},%X{spanId:-}]

Logs FE:

For FE service Track Id is 678d0ddf7a804137440ac3007174d797.

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'

Logs BE:

For BE service Track ID is 678d0ddf07b239da05ec3e7febcb5842.

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'

Source Code

Link to GIT repository with source code:

Summary

I use Java in version 23 and Spring Boot in version 3.4.1. Any idea how can I resolve this issue?

I migrated from RestTemplate to RestClient in my Spring Boot 3 application. But now I have a problem with Trace ID: the identifier of the end-to-end operation in which this particular overall operation took place.

RestTemplate sends the same Trace ID between two services.

RestClient sends different Trace ID between two services.

Class RestClientConfig:

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);

    }

}

Class BeClient:

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);

}

File application.properties:

logging.pattern.correlation=[${spring.application.name:},%X{traceId:-},%X{spanId:-}]

Logs FE:

For FE service Track Id is 678d0ddf7a804137440ac3007174d797.

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'

Logs BE:

For BE service Track ID is 678d0ddf07b239da05ec3e7febcb5842.

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'

Source Code

Link to GIT repository with source code: https://github.com/wisniewskikr/chrisblog-it-cloud/tree/main/spring-cloud/observability/springcloud-springboot3-observability-grafana-stack-restclient

Summary

I use Java in version 23 and Spring Boot in version 3.4.1. Any idea how can I resolve this issue?

Share Improve this question asked Jan 19 at 14:58 ChrisChris 13 bronze badges 1
  • This section in docs might be relevant: docs.spring.io/spring-boot/reference/actuator/… – Roar S. Commented Jan 19 at 16:46
Add a comment  | 

1 Answer 1

Reset to default 0

I was able to solve my problem by adding RestClient.Builder as a parameter of the bean created in the class RestClientConfig.

So for valid handling Trace ID by RestClient in Spring Boot 3 application the class RestClientConfig should be configured in following way:

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.Builder restClientBuilder) {

        RestClient restClient = restClientBuilder
                .baseUrl(apiUrl)
                .build();
        var restClientAdapter = RestClientAdapter.create(restClient);
        var httpServiceProxyFactory = HttpServiceProxyFactory.builderFor(restClientAdapter).build();
        return httpServiceProxyFactory.createClient(BeClient.class);

    }

}

Working source code you can find here: https://github.com/wisniewskikr/chrisblog-it-cloud/tree/main/spring-cloud/observability/springcloud-springboot3-observability-grafana-stack-restclient

发布评论

评论列表(0)

  1. 暂无评论