RestClient 无法在我的 Spring Boot 3 应用程序中处理跟踪 ID

问题描述 投票:0回答:1

我在 Spring Boot 3 应用程序中从 RestTemplate 迁移到 RestClient。但现在我遇到了跟踪 ID 的问题:发生此特定整体操作的端到端操作的标识符。

RestTemplate 在两个服务之间发送相同的跟踪 ID。

RestClient 在两个服务之间发送不同的跟踪 ID

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

    }

}

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

}

文件application.properties

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

日志FE

对于 FE 服务,轨道 ID 为 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'

日志BE

BE 服务轨道 ID 为 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'

源代码

链接到包含源代码的 GIT 存储库:https://github.com/wisniewskikr/chrisblog-it-cloud/tree/main/spring-cloud/observability/springcloud-springboot3-observability-grafana-stack-restclient

总结

我在版本 23 中使用 Java,在版本 3.4.1 中使用 Spring Boot。知道如何解决这个问题吗?

java spring spring-boot
1个回答
0
投票

我能够通过添加 RestClient.Builder 作为在类 RestClientConfig 中创建的 bean 的参数来解决我的问题。

因此,为了在 Spring Boot 3 应用程序中通过 RestClient 有效处理 Trace ID,类 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.Builder restClientBuilder) {

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

    }

}

工作源代码您可以在这里找到:https://github.com/wisniewskikr/chrisblog-it-cloud/tree/main/spring-cloud/observability/springcloud-springboot3-observability-grafana-stack-restclient

© www.soinside.com 2019 - 2024. All rights reserved.