简体   繁体   English

如何使用 springboot 微服务获得用于跟踪的 req id?

[英]How can i achieve req id for tracing using springboot microservices?

The ask is:问题是:

Whenever a client calls API's, i want to tag it with a unique identifier or use one supplied by the client (usually in a query param) and pass it across components until that request is fulfilled sucessfully or fails.每当客户端调用 API 时,我都想用唯一标识符标记它或使用客户端提供的标识符(通常在查询参数中)并将其传递到组件之间,直到成功完成请求或失败。 The goal is to get a holistic picture of how a request was handled by different components and what happened at each component and quickly identify issues.目标是全面了解不同组件如何处理请求以及每个组件发生了什么,并快速识别问题。

How can i achieve this using springboot microservices.我如何使用 springboot 微服务实现这一点。 please help me.请帮我。

Spring Cloud Sleuth is what you are looking for: https://cloud.spring.io/spring-cloud-sleuth/reference/html/ Spring Cloud Sleuth 就是你要找的: https://cloud.spring.io/spring-cloud-sleuth/reference/html/

Spring Cloud Sleuth's solution is to inject span and trace IDs into log entries. Spring Cloud Sleuth 的解决方案是将跨度和跟踪 ID 注入日志条目。 A trace ID is a unique identifier that an entire request flow will share.跟踪 ID 是整个请求流将共享的唯一标识符。 IA span is more local and is defined for each request received for each request sent event. IA 跨度更本地化,并且针对每个请求发送事件收到的每个请求进行定义。 They define particular interaction points.它们定义特定的交互点。

The initial span, or root span, is generated when a client request is received from outside the distributed system.当从分布式系统外部接收到客户端请求时,会生成初始跨度或根跨度。 This request lacks trace and span information.此请求缺少跟踪和跨度信息。 The root span becomes the trace ID for the rest of the request flow through the system / systems.根跨度成为通过系统的请求流的 rest 的跟踪 ID。

The diagram below shows how Sleuth span and trace generation would work through a hypothetical service.network.下图显示了 Sleuth 跨度和跟踪生成如何通过假设的 service.network 工作。

在此处输入图像描述

All you need to do in your code is to add the dependency spring-cloud-starter-sleuth and Spring will automatically instrument the following communication channels:您需要在代码中做的就是添加依赖项spring-cloud-starter-sleuth sleuth,Spring 将自动检测以下通信渠道:

  • requests over messaging technologies like Apache Kafka or RabbitMQ通过消息技术请求,如 Apache Kafka 或 RabbitMQ
  • HTTP headers received at Spring MVC controllers HTTP 标头在 Spring MVC 控制器处收到
  • requests made with the RestTemplate使用 RestTemplate 发出的请求

If you want to start simple you could define a filter (eg by extending OncePerRequestFilter ) that generates/extracts a request ID.如果您想从简单开始,您可以定义一个过滤器(例如,通过扩展OncePerRequestFilter )来生成/提取请求 ID。 You can also put it into Logback's MDC so that it is included in every logging statement that is issued from the thread executing the request (if configured):您还可以将它放入 Logback 的MDC中,以便它包含在从执行请求的线程发出的每个日志记录语句中(如果已配置):

@Component
public class RequestIdFilter extends OncePerRequestFilter {

    private final ThreadLocal<String> requestId = new ThreadLocal<>();

    public Optional<String> getCurrentRequestId() {
        return Optional.ofNullable(requestId.get());
    }
    
    @Override
    protected void doFilterInternal(HttpServletRequest request, HttpServletResponse response, FilterChain chain) throws ServletException, IOException {
        try {
            requestId.set(UUID.randomUUID().toString()); // or extract from request
            MDC.put("requestId", requestId.get());
            chain.doFilter(request, response);
        } finally {
            requestId.remove();
        }
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM