Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

What is the full-link log tracing based on Spring Boot + Dubbo

2025-04-01 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

Shulou(Shulou.com)06/02 Report--

What is the full-link log tracking based on Spring Boot + Dubbo? in order to solve this problem, this article introduces the corresponding analysis and solution in detail, hoping to help more partners who want to solve this problem to find a more simple and feasible method.

I. Summary

After the analysis is completed, it will be realized concretely.

Service-a: implement dubbo services.

Service-b: implement the web service and invoke the service implemented by service-a.

2. Realize 2.1 log collection and storage

In this example, [Ali Cloud Log Service] is directly used for data storage and retrieval, and Aliyun Log Logback Appender is used for log collection and upload.

In fact, Ali realized a Logback Appender by himself. Of course, we can also implement it ourselves, such as uploading it to a self-built ELK.

2.2 trigger (external) for traceId generation, delivery, destruction of 2.2.1 traceId generation, destruction of 2.2.1.1 client requests, etc.

The trigger of an external class request is handled by an interceptor.

After the request comes, a traceId is generated and written to org.slf4j.MDC.

After the request is completed, remove the traceId from the org.slf4j.MDC.

Package com.example.dubboserviceb.interceptor;import com.example.dubboserviceb.constants.Constants;import org.slf4j.MDC;import org.springframework.web.servlet.HandlerInterceptor;import org.springframework.web.servlet.ModelAndView;import javax.servlet.http.HttpServletRequest;import javax.servlet.http.HttpServletResponse;import java.util.UUID / * * @ author lpe234 * @ since, 2019-5-25 14:43 * / public class TraceIdInterceptor implements HandlerInterceptor {@ Override public boolean preHandle (HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {/ / generate traceId String traceId = UUID.randomUUID () .toString () .replace ("-", "); / / put traceId MDC.put (Constants.TRACE_ID, 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 {/ / clear traceId MDC.remove (Constants.TRACE_ID);} 2.2.1.2 scheduled tasks and other triggers (internal)

(slightly.)

2.2.1 traceId delivery 2.2.1.1 WEB class delivery

The simple interface returns the class, adding the traceId field.

Package com.example.dubboserviceb.utils;import lombok.Data;/** * @ author lpe234 * @ since 14:55 on 2019-5-25 * / @ Datapublic class RestResponse {private Integer code; private String msg; private T data; private String traceId; public RestResponse () {} public RestResponse (Integer code, String msg, T data) {this.code = code; this.msg = msg; this.data = data } public static RestResponse ok (T data) {return new RestResponse (200, "ok", data);} public static RestResponse error (T data) {return new RestResponse (400, "error", data);}}

Before the request response result is generated, get the traceId in the current org.slf4j.MDC and set it to RestResponse.

Package com.example.dubboserviceb.advice;import com.example.dubboserviceb.constants.Constants;import com.example.dubboserviceb.utils.RestResponse;import org.slf4j.MDC;import org.springframework.core.MethodParameter;import org.springframework.http.MediaType;import org.springframework.http.converter.HttpMessageConverter;import org.springframework.http.server.ServerHttpRequest;import org.springframework.http.server.ServerHttpResponse;import org.springframework.web.bind.annotation.ControllerAdvice;import org.springframework.web.servlet.mvc.method.annotation.ResponseBodyAdvice / * @ author lpe234 * @ since, 2019-5-25 15:03 * / @ ControllerAdvicepublic class ResponseModifyAdvice implements ResponseBodyAdvice {@ Override public boolean supports (MethodParameter methodParameter, Class > aClass) {return true;} @ Override public Object beforeBodyWrite (Object o, MethodParameter methodParameter, MediaType mediaType, Class > aClass, ServerHttpRequest serverHttpRequest, ServerHttpResponse serverHttpResponse) {/ / put traceId to response ((RestResponse) o) .setTraceId (MDC.get (Constants.TRACE_ID)); return o;}}

Finally, the interface response data is as follows:

{"code": 200, "msg": "ok", "data": "Hello apple", "traceId": "6c25de3422374d51be58555ae9c380e8"} 2.2.1.2 Dubbo class delivery

The storage of traceId uses org.apache.dubbo.rpc.RpcContext (internally implemented using InternalThreadLocal).

With the help of Dubbo filters, traceId reads, writes, and clears between Dubbo services.

Package com.example.dubboservicea.filter;import com.example.dubboservicea.constants.Constants;import org.apache.dubbo.common.extension.Activate;import org.apache.dubbo.rpc.*;import org.slf4j.MDC / * @ author lpe234 * @ since, 2019-5-25 15:24 * / @ Activate (group = {org.apache.dubbo.common.Constants.PROVIDER, org.apache.dubbo.common.Constants.CONSUMER}) public class DubboTraceIdFilter implements Filter {@ Override public Result invoke (Invoker invoker, Invocation invocation) throws RpcException {RpcContext rpcContext = RpcContext.getContext () / / before if (rpcContext.isProviderSide ()) {/ / get traceId from dubbo consumer,and set traceId to MDC String traceId = rpcContext.getAttachment (Constants.TRACE_ID); MDC.put (Constants.TRACE_ID, traceId) } if (rpcContext.isConsumerSide ()) {/ / get traceId from MDC, and set traceId to rpcContext String traceId = MDC.get (Constants.TRACE_ID); rpcContext.setAttachment (Constants.TRACE_ID, traceId);} Result result = invoker.invoke (invocation) / / after if (rpcContext.isProviderSide ()) {/ / clear traceId from MDC MDC.remove (Constants.TRACE_ID);} return result;} @ Override public Result onResponse (Result result, Invoker invoker, Invocation invocation) {return result;}}

In addition, you need to create a com.alibaba.dubbo.rpc.Filter text file under the resources/META-INF/dubbo/ folder. The content is dubboTraceIdFilter=com.example.dubboservicea.filter.DubboTraceIdFilter.

The filter for dubbo is then configured in the spring-boot configuration file

# dubbodubbo: scan: base-packages: com.example.dubboservicea.provider, com.example.dubboservicea.reference protocol: name: dubbo port: 12101 registry: address: zookeeper://192.168.0.150:20001 provider: filter: dubboTraceIdFilter consumer: filter: dubboTraceIdFilter III. Result 3.1 HTTP requests the result to be returned

3.2 Log Service query

This is the answer to the question about full-link log tracking based on Spring Boot + Dubbo. I hope the above content can be of some help to you. If you still have a lot of doubts to be solved, you can follow the industry information channel for more related knowledge.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Internet Technology

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report