HTTP调用丢失traceId
在使用HTTP调用第三方服务接口时traceId将丢失,需要对HTTP调用工具进行改造,在发送时在request header中添加traceId,在下层被调用方添加拦截器获取header中的traceId添加到MDC中。
HTTP调用有多种方式,比较常见的有HttpClient、OKHttp、RestTemplate,所以只给出这几种HTTP调用的解决方式。
1、HttpClient:
实现HttpClient拦截器:
public class HttpClientTraceIdInterceptor implements HttpRequestInterceptor { @Override public void process(HttpRequest httpRequest, HttpContext httpContext) throws HttpException, IOException { String traceId = MDC.get(Constants.TRACE_ID); //当前线程调用中有traceId,则将该traceId进行透传 if (traceId != null) { //添加请求体 httpRequest.addHeader(Constants.TRACE_ID, traceId); } } }
实现HttpRequestInterceptor接口并重写process方法
如果调用线程中含有traceId,则需要将获取到的traceId通过request中的header向下透传下去。
为HttpClient添加拦截器:
private static CloseableHttpClient httpClient = HttpClientBuilder.create() .addInterceptorFirst(new HttpClientTraceIdInterceptor()) .build();
通过addInterceptorFirst方法为HttpClient添加拦截器。
2、OKHttp:
实现OKHttp拦截器:
public class OkHttpTraceIdInterceptor implements Interceptor { @Override public Response intercept(Chain chain) throws IOException { String traceId = MDC.get(Constants.TRACE_ID); Request request = null; if (traceId != null) { //添加请求体 request = chain.request().newBuilder().addHeader(Constants.TRACE_ID, traceId).build(); } Response originResponse = chain.proceed(request); return originResponse; } }
实现 Interceptor拦截器,重写interceptor方法,实现逻辑和HttpClient差不多,如果能够获取到当前线程的traceId则向下透传。
为OkHttp添加拦截器:
private static OkHttpClient client = new OkHttpClient.Builder() .addNetworkInterceptor(new OkHttpTraceIdInterceptor()) .build();
调用addNetworkInterceptor方法添加拦截器。
3、RestTemplate:
实现RestTemplate拦截器:
public class RestTemplateTraceIdInterceptor implements ClientHttpRequestInterceptor { @Override public ClientHttpResponse intercept(HttpRequest httpRequest, byte[] bytes, ClientHttpRequestExecution clientHttpRequestExecution) throws IOException { String traceId = MDC.get(Constants.TRACE_ID); if (traceId != null) { httpRequest.getHeaders().add(Constants.TRACE_ID, traceId); } return clientHttpRequestExecution.execute(httpRequest, bytes); } }
实现ClientHttpRequestInterceptor接口,并重写intercept方法,其余逻辑都是一样的不重复说明。
为RestTemplate添加拦截器:
restTemplate.setInterceptors(Arrays.asList(new RestTemplateTraceIdInterceptor()));
调用setInterceptors方法添加拦截器。
4、第三方服务拦截器:
HTTP调用第三方服务接口全流程traceId需要第三方服务配合,第三方服务需要添加拦截器拿到request header中的traceId并添加到MDC中。
public class LogInterceptor implements HandlerInterceptor { @Override public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception { //如果有上层调用就用上层的ID String traceId = request.getHeader(Constants.TRACE_ID); if (traceId == null) { traceId = TraceIdUtils.getTraceId(); } MDC.put("traceId", traceId); return true; } @Override public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler, ModelAndView modelAndView) throws Exception { } @Override public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex) throws Exception { MDC.remove(Constants.TRACE_ID); } }
说明:
- 先从request header中获取traceId
- 从request header中获取不到traceId则说明不是第三方调用,直接生成一个新的traceId
- 将生成的traceId存入MDC中
除了需要添加拦截器之外,还需要在日志格式中添加traceId的打印,如下:
<property name="pattern">[TRACEID:%X{traceId}] %d{HH:mm:ss.SSS} %-5level %class{-1}.%M()/%L - %msg%xEx%n</property>
注意:需要添加 %X{traceId}