Spring Cloud Sleuth是一個在應用中實現日誌跟蹤的強有力的工具。使用Sleuth庫能夠應用於計劃任務 、多線程服務或複雜的Web請求,尤爲是在一個由多個服務組成的系統中。當咱們在這些應用中來診斷問題時,即便有日誌記錄也很難判斷出一個請求須要將哪些操做關聯在一塊兒。html
若是想要診斷複雜操做,一般的解決方案是在請求中傳遞惟一的ID到每一個方法來識別日誌。而Sleuth能夠與日誌框架Logback、SLF4J輕鬆地集成,經過添加獨特的標識符來使用日誌跟蹤和診斷問題。前端
<dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-sleuth</artifactId> <version>2.1.1.RELEASE</version> </dependency>
SLF4J 配置 http://www.javashuo.com/article/p-rhncmqhv-hd.html java
在使用slf4j打印日誌時會打印traceId,經過這ID能夠把整個請求鏈給找出來.web
以下是打印出來的實例spring
2019-06-19 17:16:21.453 [00c62017e97b7823,00c62017e97b7823] [http-nio-8777-exec-1]
須要把traceId給前端,使用aop,在每一個返回結果中增長traceId字段json
import brave.Tracer; import com.alibaba.fastjson.JSON; import com.alibaba.fastjson.JSONObject; import lombok.extern.slf4j.Slf4j; import org.aspectj.lang.ProceedingJoinPoint; import org.aspectj.lang.annotation.Around; import org.aspectj.lang.annotation.Aspect; import org.aspectj.lang.annotation.Pointcut; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.context.annotation.Configuration; import org.springframework.web.context.request.RequestAttributes; import org.springframework.web.context.request.RequestContextHolder; import org.springframework.web.context.request.ServletRequestAttributes; import com.google.gson.Gson; import org.slf4j.Logger; import org.slf4j.LoggerFactory;;import javax.servlet.http.HttpServletRequest; @Aspect //定義一個切面 @Configuration @Slf4j public class TraceAspect { // 定義切點Pointcut @Pointcut("execution(* com.gf.controller..*.*(..))") public void excudeService() { } @Autowired Tracer tracer; @Around("excudeService()") public Object doAround(ProceedingJoinPoint pjp) throws Throwable { RequestAttributes ra = RequestContextHolder.getRequestAttributes(); ServletRequestAttributes sra = (ServletRequestAttributes) ra; HttpServletRequest request = sra.getRequest(); String url = request.getRequestURL().toString(); String method = request.getMethod(); String uri = request.getRequestURI(); String queryString = request.getQueryString(); log.info("請求開始, 各個參數, url: {}, method: {}, uri: {}, params: {}", url, method, uri, queryString); // result的值就是被攔截方法的返回值 Object result = pjp.proceed(); JSONObject dd = JSONObject.parseObject(JSON.toJSONString(result)); dd.put("traceId",tracer.currentSpan().context().traceIdString()); log.info("請求結束,controller的返回值是 {} - {}", tracer.currentSpan().toString(), dd.toJSONString()); return dd; } }
轉載自:https://blog.csdn.net/peterwanghao/article/details/79967634多線程