| 
					
				 | 
			
			
				@@ -2,7 +2,9 @@ package com.tzld.piaoquan.risk.control.aop; 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				  
			 | 
		
	
		
			
				 | 
				 | 
			
			
				 import com.alibaba.fastjson.JSON; 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				 import com.google.common.base.Stopwatch; 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+import com.tzld.piaoquan.risk.control.common.base.Constant; 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				 import com.tzld.piaoquan.risk.control.service.LoghubService; 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+import org.apache.rocketmq.shaded.org.slf4j.MDC; 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				 import org.aspectj.lang.ProceedingJoinPoint; 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				 import org.aspectj.lang.annotation.Around; 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				 import org.aspectj.lang.annotation.Aspect; 
			 | 
		
	
	
		
			
				| 
					
				 | 
			
			
				@@ -17,6 +19,7 @@ import javax.servlet.http.HttpServletRequest; 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				 import java.util.HashMap; 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				 import java.util.Map; 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				 import java.util.Objects; 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+import java.util.UUID; 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				 import java.util.concurrent.TimeUnit; 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				  
			 | 
		
	
		
			
				 | 
				 | 
			
			
				 @Aspect 
			 | 
		
	
	
		
			
				| 
					
				 | 
			
			
				@@ -38,25 +41,33 @@ public class RequestLogAspect { 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				     @Around("allController()") 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				     public Object around(ProceedingJoinPoint joinPoint) throws Throwable { 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				         // 获取请求信息 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-        HttpServletRequest request = Objects.requireNonNull((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getRequest(); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-        String uri = request.getRequestURI(); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-        String clientIp = request.getRemoteAddr(); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-        String queryString = request.getQueryString(); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-        String method = joinPoint.getSignature().getName(); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-        Stopwatch stopwatch = Stopwatch.createStarted(); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-        // 执行目标方法 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-        Object result = joinPoint.proceed(); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-        // 计算耗时 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-        long executionTime = stopwatch.stop().elapsed(TimeUnit.MILLISECONDS); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-        Map<String, Object> data = new HashMap<>(); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-        data.put("uri", uri); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-        data.put("clientIp", clientIp); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-        data.put("method", method); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-        data.put("executionTime", executionTime); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-        data.put("queryString", queryString); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-        data.put("requestBody", JSON.toJSONString(joinPoint.getArgs())); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-        data.put("responseBody", JSON.toJSONString(result)); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-        loghubService.asyncSubmitLog(aliyunLogProject, aliyunLogLogstoreRequest, "", data); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-        return result; 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+        String traceId = UUID.randomUUID().toString(); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+        MDC.put(Constant.LOG_TRACE_ID, traceId);  // 关键:让所有日志自动携带 traceId 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+        try { 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+            HttpServletRequest request = Objects.requireNonNull((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getRequest(); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+            String uri = request.getRequestURI(); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+            String clientIp = request.getRemoteAddr(); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+            String queryString = request.getQueryString(); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+            String method = joinPoint.getSignature().getName(); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+            Stopwatch stopwatch = Stopwatch.createStarted(); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+            // 执行目标方法 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+            Object result = joinPoint.proceed(); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+            // 计算耗时 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+            long executionTime = stopwatch.stop().elapsed(TimeUnit.MILLISECONDS); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+            Map<String, Object> data = new HashMap<>(); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+            data.put("uri", uri); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+            data.put("clientIp", clientIp); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+            data.put("method", method); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+            data.put("executionTime", executionTime); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+            data.put("queryString", queryString); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+            data.put("logTraceId", traceId); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+            data.put("requestBody", JSON.toJSONString(joinPoint.getArgs())); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+            data.put("responseBody", JSON.toJSONString(result)); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+            loghubService.asyncSubmitLog(aliyunLogProject, aliyunLogLogstoreRequest, "", data); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+            return result; 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+        } finally { 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+            // 确保在方法执行完毕后清除 traceId 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+            MDC.clear(); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+        } 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				     } 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				 } 
			 |