Commit e3cf6a66 authored by yanzg's avatar yanzg

将源码打包进jar包

parent 3b744dbc
......@@ -51,7 +51,7 @@ public class AspectFeign {
@Around(value = "feignAspect()")
public Object requestFeignAround(ProceedingJoinPoint joinPoint) throws Throwable {
Class<?> declaringType = joinPoint.getSignature().getDeclaringType();
String url = aspectLogUrl.getMethodUrl(joinPoint);
String url = aspectLogUrl.getWebMethodUrl(joinPoint);
Object requestBody = aspectLogBody.getRequestBody(joinPoint);
boolean clear = aspectLogStart.requestLogInit();
......@@ -66,7 +66,7 @@ public class AspectFeign {
result = joinPoint.proceed();
// 假如是标准格式,则验证接口是否成功,不成功则抛出异常
if (result instanceof ResponseResult) {
ResponseResult responseResult = (ResponseResult) result;
ResponseResult<?> responseResult = (ResponseResult<?>) result;
if (!ResultConstants.SUCCESS.equals(responseResult.getCode())) {
throw new CodeException(responseResult.getCode(), responseResult.getMessage(), responseResult.getTarget());
}
......
......@@ -12,6 +12,7 @@ import org.springframework.web.multipart.MultipartFile;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.nio.charset.StandardCharsets;
import java.util.ArrayList;
import java.util.List;
......@@ -109,4 +110,13 @@ public class AspectLogBody {
return para;
}
/**
* 获取消息内容
*
* @param message 消息
* @return 消息内容
*/
public String getRequestBody(Message message) {
return new String(message.getBody(), StandardCharsets.UTF_8);
}
}
......@@ -32,7 +32,7 @@ public class AspectLogUrl {
*
* @return 方法路径
*/
public String getMethodUrl(ProceedingJoinPoint joinPoint) {
public String getWebMethodUrl(ProceedingJoinPoint joinPoint) {
String url = StringHelper.EMPTY;
Signature signature = joinPoint.getSignature();
Class<?> declaringType = signature.getDeclaringType();
......@@ -57,11 +57,6 @@ public class AspectLogUrl {
GetMapping[] gets = targetMethod.getAnnotationsByType(GetMapping.class);
url = getFeignOrRequestUrl(feignClient, classRequests, classPosts, classGets, requests, posts, gets);
} else {
Message message = getMessage(joinPoint.getArgs());
if (message != null) {
url = message.getMessageProperties().getConsumerQueue();
}
}
if (StringHelper.isEmpty(url)) {
return baseUrl;
......@@ -81,9 +76,7 @@ public class AspectLogUrl {
* @param gets
* @return
*/
private String getFeignOrRequestUrl(FeignClient feignClient,
RequestMapping[] classRequests, PostMapping[] classPosts, GetMapping[] classGets,
RequestMapping[] requests, PostMapping[] posts, GetMapping[] gets) {
private String getFeignOrRequestUrl(FeignClient feignClient, RequestMapping[] classRequests, PostMapping[] classPosts, GetMapping[] classGets, RequestMapping[] requests, PostMapping[] posts, GetMapping[] gets) {
StringBuilder sb = new StringBuilder();
if (feignClient != null) {
sb.append(feignClient.value());
......@@ -121,11 +114,11 @@ public class AspectLogUrl {
/**
* 根据类型获取对象
*
* @param args 参数
* @param joinPoint 参数
* @return 消息实体
*/
public Message getMessage(Object[] args) {
for (Object item : args) {
public Message getMessage(ProceedingJoinPoint joinPoint) {
for (Object item : joinPoint.getArgs()) {
if (item instanceof Message) {
return (Message) item;
}
......@@ -133,4 +126,29 @@ public class AspectLogUrl {
return null;
}
/**
* 获取消息路径
*
* @param joinPoint 函数
* @param message 消息内容
* @return 消息路径
*/
public String getMessageUrl(ProceedingJoinPoint joinPoint, Message message) {
Signature signature = joinPoint.getSignature();
Class<?> declaringType = signature.getDeclaringType();
String name = signature.getName();
String baseUrl = String.format("%s:%s", declaringType.getSimpleName(), name);
String url;
if (message != null) {
url = message.getMessageProperties().getConsumerQueue();
} else {
url = StringHelper.EMPTY;
}
if (StringHelper.isEmpty(url)) {
return baseUrl;
}
return String.format("%s:%s", baseUrl, url);
}
}
package com.yanzuoguang.cloud.aop;
import com.yanzuoguang.mq.plan.YzgMqConsumer;
import com.yanzuoguang.mq.service.MessageLogService;
import com.yanzuoguang.util.exception.ExceptionHelper;
import com.yanzuoguang.util.vo.LogVo;
......@@ -8,13 +7,9 @@ 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.aspectj.lang.reflect.MethodSignature;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.stereotype.Component;
import java.lang.reflect.Method;
/**
* LogsAspect(接口请求日志切面)
*
......@@ -33,9 +28,7 @@ public class AspectMq {
private final AspectLogStart aspectLogStart;
private final AspectLogResult aspectLogResult;
public AspectMq(MessageLogService logService,
AspectLogUrl aspectLogUrl, AspectLogBody aspectLogBody,
AspectLogStart aspectLogStart, AspectLogResult aspectLogResult) {
public AspectMq(MessageLogService logService, AspectLogUrl aspectLogUrl, AspectLogBody aspectLogBody, AspectLogStart aspectLogStart, AspectLogResult aspectLogResult) {
this.logService = logService;
this.aspectLogUrl = aspectLogUrl;
this.aspectLogBody = aspectLogBody;
......@@ -46,27 +39,30 @@ public class AspectMq {
/**
* exec aop point aspect
*/
@Pointcut("execution(* *..mq..*Consumer.*(..))")
// @Pointcut("execution(* *..mq..*Consumer.*(..))")
@Pointcut("execution(* org.springframework.amqp.rabbit.listener.adapter.MessageListenerAdapter.onMessage(..))")
public void mqAspect() {
}
/**
* 执行环形切面
*
* @param joinPoint
* @return
* @param joinPoint 方法提
* @return 执行回掉
*/
@Around(value = "mqAspect()")
public Object requestMqAround(ProceedingJoinPoint joinPoint) throws Throwable {
Class declaringType = joinPoint.getSignature().getDeclaringType();
String url = aspectLogUrl.getMethodUrl(joinPoint);
Object requestBody = aspectLogBody.getRequestBody(joinPoint);
boolean clear = isRabbit(joinPoint);
if (clear) {
clear = aspectLogStart.requestLogInit();
Class<?> declaringType = joinPoint.getSignature().getDeclaringType();
Message message = aspectLogUrl.getMessage(joinPoint);
if (message == null) {
return executeMethod(joinPoint);
}
logService.logCurrent(message);
String url = aspectLogUrl.getMessageUrl(joinPoint, message);
String requestBody = aspectLogBody.getRequestBody(message);
LogVo log = new LogVo();
boolean clear = aspectLogStart.requestLogInit();
aspectLogStart.requestLog(declaringType, TAG, url, requestBody, log, clear);
long start = System.currentTimeMillis();
......@@ -86,34 +82,6 @@ public class AspectMq {
}
}
/**
* 判断是否MQ函数
*
* @param joinPoint 函数
* @return 是否MQ函数
*/
private boolean isRabbit(ProceedingJoinPoint joinPoint) {
boolean ret = false;
if (!(joinPoint.getSignature() instanceof MethodSignature)) {
return false;
}
MethodSignature methodSignature = (MethodSignature) joinPoint.getSignature();
Method targetMethod = methodSignature.getMethod();
Message message = aspectLogUrl.getMessage(joinPoint.getArgs());
if (message != null) {
logService.logCurrent(message);
}
if (YzgMqConsumer.class.equals(targetMethod.getDeclaringClass())) {
ret = false;
} else {
RabbitListener annotation = targetMethod.getAnnotation(RabbitListener.class);
if (annotation != null || message != null) {
ret = true;
}
}
return ret;
}
/**
* 执行函数
*
......
......@@ -82,7 +82,7 @@ public class AspectWeb {
}
Class declaringType = joinPoint.getSignature().getDeclaringType();
String url = aspectLogUrl.getMethodUrl(joinPoint);
String url = aspectLogUrl.getWebMethodUrl(joinPoint);
Object requestBody = aspectLogBody.getRequestBody(joinPoint);
boolean clear = aspectLogStart.requestLogInit();
LogVo log = new LogVo();
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment