说明:
1、实现aop切入api日志 2、取消热部署,大坑,热部署导致redis拿不到值,暂时不知道如何解决,先把热部署关了
This commit is contained in:
parent
379281f949
commit
bcbeeba42c
|
|
@ -111,11 +111,11 @@
|
|||
<version>${hutool.version}</version>
|
||||
</dependency>
|
||||
|
||||
<!--热部署-->
|
||||
<dependency>
|
||||
<!--热部署、有毒,一加上redis就出问题 -->
|
||||
<!--<dependency>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-devtools</artifactId>
|
||||
</dependency>
|
||||
</dependency>-->
|
||||
|
||||
|
||||
|
||||
|
|
|
|||
|
|
@ -1,14 +1,18 @@
|
|||
package com.xjs.log.aop;
|
||||
|
||||
import com.ruoyi.common.log.annotation.Log;
|
||||
import cn.hutool.core.date.DateUtil;
|
||||
import com.xjs.log.consts.ReqConst;
|
||||
import com.xjs.log.mapper.ApiLogMapper;
|
||||
import lombok.extern.log4j.Log4j2;
|
||||
import org.aspectj.lang.JoinPoint;
|
||||
import org.aspectj.lang.ProceedingJoinPoint;
|
||||
import org.aspectj.lang.Signature;
|
||||
import org.aspectj.lang.annotation.*;
|
||||
import org.aspectj.lang.reflect.SourceLocation;
|
||||
import org.springframework.stereotype.Component;
|
||||
|
||||
import java.lang.reflect.Method;
|
||||
import javax.annotation.Resource;
|
||||
import java.time.LocalDateTime;
|
||||
import java.time.temporal.ChronoUnit;
|
||||
import java.util.Optional;
|
||||
|
||||
/**
|
||||
* @author xiejs
|
||||
|
|
@ -17,45 +21,74 @@ import java.lang.reflect.Method;
|
|||
*/
|
||||
@Component
|
||||
@Aspect
|
||||
@Log4j2
|
||||
public class ApiLogAspect {
|
||||
|
||||
@Resource
|
||||
private ApiLogMapper apiLogMapper;
|
||||
|
||||
/**
|
||||
* 声明AOP签名
|
||||
*/
|
||||
@Pointcut("@annotation(com.xjs.log.aop.ApiLog)")
|
||||
public void pointcut() {
|
||||
}
|
||||
|
||||
/**
|
||||
* 环绕切入
|
||||
*/
|
||||
@Around("pointcut()")
|
||||
public Object doAround(ProceedingJoinPoint joinPoint) throws Throwable {
|
||||
try {
|
||||
LocalDateTime localDateTime1 = DateUtil.date().toLocalDateTime();
|
||||
Object obj = joinPoint.proceed();
|
||||
LocalDateTime localDateTime2 = DateUtil.date().toLocalDateTime();
|
||||
long between = ChronoUnit.MILLIS.between(localDateTime1, localDateTime2);
|
||||
log.info("调用接口耗费时间:{}ms", between);
|
||||
return obj;
|
||||
} catch (Throwable e) {
|
||||
e.printStackTrace();
|
||||
throw e;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 处理完请求后执行
|
||||
*
|
||||
* @param joinPoint 切点
|
||||
*/
|
||||
@AfterReturning(pointcut = "@annotation(apiLog)", returning = "jsonResult")
|
||||
public void doAfterReturning(JoinPoint joinPoint, ApiLog apiLog, Object jsonResult)
|
||||
{
|
||||
public void doAfterReturning(JoinPoint joinPoint, ApiLog apiLog, Object jsonResult) {
|
||||
this.handleApiLog(joinPoint, apiLog, null, jsonResult);
|
||||
}
|
||||
|
||||
|
||||
@AfterThrowing(value = "@annotation(apiLog)", throwing = "e")
|
||||
public void doAfterThrowing(JoinPoint joinPoint, ApiLog apiLog, Exception e)
|
||||
{
|
||||
public void doAfterThrowing(JoinPoint joinPoint, ApiLog apiLog, Exception e) {
|
||||
handleApiLog(joinPoint, apiLog, e, null);
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
private void handleApiLog(JoinPoint joinPoint, ApiLog apiLog, final Exception e, Object jsonResult) {
|
||||
com.xjs.log.domain.ApiLog entity = new com.xjs.log.domain.ApiLog();
|
||||
String name = apiLog.name();//请求名称
|
||||
entity.setApiName(name);
|
||||
String url = apiLog.url();//请求地址
|
||||
entity.setUrl(url);
|
||||
Object[] args = joinPoint.getArgs();//请求体
|
||||
StringBuilder builder = new StringBuilder();
|
||||
for (Object arg : args) {
|
||||
|
||||
builder.append(arg);
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
entity.setMethod(apiLog.method());
|
||||
entity.setRequest(builder.toString());
|
||||
entity.setResponse(Optional.ofNullable(jsonResult).toString());
|
||||
if (e != null) {
|
||||
entity.setIsSuccess(ReqConst.ERROR);
|
||||
}else {
|
||||
entity.setIsSuccess(ReqConst.SUCCESS);
|
||||
}
|
||||
apiLogMapper.insert(entity);
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
|||
|
|
@ -0,0 +1,57 @@
|
|||
package com.xjs.log.config;
|
||||
|
||||
import com.alibaba.fastjson.serializer.SerializerFeature;
|
||||
import com.alibaba.fastjson.serializer.ValueFilter;
|
||||
import com.alibaba.fastjson.support.config.FastJsonConfig;
|
||||
import com.alibaba.fastjson.support.spring.FastJsonHttpMessageConverter;
|
||||
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
|
||||
import com.fasterxml.jackson.databind.ObjectMapper;
|
||||
import com.fasterxml.jackson.databind.module.SimpleModule;
|
||||
import com.fasterxml.jackson.databind.ser.std.ToStringSerializer;
|
||||
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
|
||||
import org.springframework.boot.autoconfigure.http.HttpMessageConverters;
|
||||
import org.springframework.context.annotation.Bean;
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
import org.springframework.context.annotation.Primary;
|
||||
import org.springframework.http.converter.HttpMessageConverter;
|
||||
import org.springframework.http.converter.json.Jackson2ObjectMapperBuilder;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
/**
|
||||
* @author xiejs
|
||||
* @desc 全局序列化处理配置
|
||||
* @create 2021-12-26
|
||||
*/
|
||||
@Configuration
|
||||
public class JsonConfig {
|
||||
@Bean
|
||||
public HttpMessageConverters fastJsonHttpMessageConverters() {
|
||||
FastJsonHttpMessageConverter fastConverter = new FastJsonHttpMessageConverter();
|
||||
FastJsonConfig fastJsonConfig = new FastJsonConfig();
|
||||
List<SerializerFeature> list = new ArrayList<>();
|
||||
list.add(SerializerFeature.PrettyFormat);
|
||||
list.add(SerializerFeature.WriteMapNullValue);
|
||||
list.add(SerializerFeature.WriteNullStringAsEmpty);
|
||||
list.add(SerializerFeature.WriteNullListAsEmpty);
|
||||
list.add(SerializerFeature.QuoteFieldNames);
|
||||
list.add(SerializerFeature.WriteDateUseDateFormat);
|
||||
list.add(SerializerFeature.DisableCircularReferenceDetect);
|
||||
list.add(SerializerFeature.WriteBigDecimalAsPlain);
|
||||
fastJsonConfig.setSerializerFeatures(list.toArray(new SerializerFeature[list.size()]));
|
||||
fastConverter.setFastJsonConfig(fastJsonConfig);
|
||||
HttpMessageConverter<?> converter = fastConverter;
|
||||
fastJsonConfig.setSerializeFilters(new ValueFilter() {
|
||||
@Override
|
||||
public Object process(Object object, String name, Object value) {
|
||||
if ((StringUtils.endsWith(name, "Id") || StringUtils.equals(name,"id")) && value != null
|
||||
&& value.getClass() == Long.class) {
|
||||
return String.valueOf(value);
|
||||
}
|
||||
return value;
|
||||
}
|
||||
});
|
||||
return new HttpMessageConverters(converter);
|
||||
}
|
||||
}
|
||||
|
|
@ -0,0 +1,11 @@
|
|||
package com.xjs.log.consts;
|
||||
|
||||
/**
|
||||
* @author xiejs
|
||||
* @desc
|
||||
* @create 2021-12-26
|
||||
*/
|
||||
public class ReqConst {
|
||||
public static final Integer SUCCESS = 1;
|
||||
public static final Integer ERROR = 2;
|
||||
}
|
||||
|
|
@ -1,5 +1,11 @@
|
|||
package com.xjs.log.domain;
|
||||
|
||||
import com.baomidou.mybatisplus.annotation.IdType;
|
||||
import com.baomidou.mybatisplus.annotation.TableId;
|
||||
import com.baomidou.mybatisplus.annotation.TableName;
|
||||
import com.fasterxml.jackson.databind.annotation.JsonSerialize;
|
||||
import com.fasterxml.jackson.databind.ser.std.ToStringSerializer;
|
||||
import lombok.Data;
|
||||
import org.apache.commons.lang3.builder.ToStringBuilder;
|
||||
import org.apache.commons.lang3.builder.ToStringStyle;
|
||||
import com.ruoyi.common.core.annotation.Excel;
|
||||
|
|
@ -13,11 +19,14 @@ import java.io.Serializable;
|
|||
* @author xjs
|
||||
* @date 2021-12-26
|
||||
*/
|
||||
@Data
|
||||
@TableName("api_log")
|
||||
public class ApiLog implements Serializable
|
||||
{
|
||||
private static final long serialVersionUID = 1L;
|
||||
|
||||
/** $column.columnComment */
|
||||
@TableId
|
||||
@JsonSerialize(using= ToStringSerializer.class)
|
||||
private Long id;
|
||||
|
||||
/** 接口名称 */
|
||||
|
|
@ -43,81 +52,4 @@ public class ApiLog implements Serializable
|
|||
/** 是否请求成功 */
|
||||
@Excel(name = "是否请求成功")
|
||||
private Integer isSuccess;
|
||||
|
||||
public void setId(Long id)
|
||||
{
|
||||
this.id = id;
|
||||
}
|
||||
|
||||
public Long getId()
|
||||
{
|
||||
return id;
|
||||
}
|
||||
public void setApiName(String apiName)
|
||||
{
|
||||
this.apiName = apiName;
|
||||
}
|
||||
|
||||
public String getApiName()
|
||||
{
|
||||
return apiName;
|
||||
}
|
||||
public void setUrl(String url)
|
||||
{
|
||||
this.url = url;
|
||||
}
|
||||
|
||||
public String getUrl()
|
||||
{
|
||||
return url;
|
||||
}
|
||||
public void setMethod(String method)
|
||||
{
|
||||
this.method = method;
|
||||
}
|
||||
|
||||
public String getMethod()
|
||||
{
|
||||
return method;
|
||||
}
|
||||
public void setRequest(String request)
|
||||
{
|
||||
this.request = request;
|
||||
}
|
||||
|
||||
public String getRequest()
|
||||
{
|
||||
return request;
|
||||
}
|
||||
public void setResponse(String response)
|
||||
{
|
||||
this.response = response;
|
||||
}
|
||||
|
||||
public String getResponse()
|
||||
{
|
||||
return response;
|
||||
}
|
||||
public void setIsSuccess(Integer isSuccess)
|
||||
{
|
||||
this.isSuccess = isSuccess;
|
||||
}
|
||||
|
||||
public Integer getIsSuccess()
|
||||
{
|
||||
return isSuccess;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
return new ToStringBuilder(this,ToStringStyle.MULTI_LINE_STYLE)
|
||||
.append("id", getId())
|
||||
.append("apiName", getApiName())
|
||||
.append("url", getUrl())
|
||||
.append("method", getMethod())
|
||||
.append("request", getRequest())
|
||||
.append("response", getResponse())
|
||||
.append("isSuccess", getIsSuccess())
|
||||
.toString();
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -1,19 +1,20 @@
|
|||
package com.xjs.log.mapper;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
|
||||
import com.xjs.log.domain.ApiLog;
|
||||
|
||||
/**
|
||||
* 日志Mapper接口
|
||||
*
|
||||
*
|
||||
* @author xjs
|
||||
* @date 2021-12-26
|
||||
*/
|
||||
public interface ApiLogMapper
|
||||
{
|
||||
public interface ApiLogMapper extends BaseMapper<ApiLog> {
|
||||
/**
|
||||
* 查询日志
|
||||
*
|
||||
*
|
||||
* @param id 日志主键
|
||||
* @return 日志
|
||||
*/
|
||||
|
|
@ -21,7 +22,7 @@ public interface ApiLogMapper
|
|||
|
||||
/**
|
||||
* 查询日志列表
|
||||
*
|
||||
*
|
||||
* @param apiLog 日志
|
||||
* @return 日志集合
|
||||
*/
|
||||
|
|
@ -29,7 +30,7 @@ public interface ApiLogMapper
|
|||
|
||||
/**
|
||||
* 删除日志
|
||||
*
|
||||
*
|
||||
* @param id 日志主键
|
||||
* @return 结果
|
||||
*/
|
||||
|
|
@ -37,7 +38,7 @@ public interface ApiLogMapper
|
|||
|
||||
/**
|
||||
* 批量删除日志
|
||||
*
|
||||
*
|
||||
* @param ids 需要删除的数据主键集合
|
||||
* @return 结果
|
||||
*/
|
||||
|
|
|
|||
|
|
@ -1,5 +1,6 @@
|
|||
package com.xjs.translation.client;
|
||||
|
||||
import com.alibaba.fastjson.JSONObject;
|
||||
import com.xjs.log.aop.ApiLog;
|
||||
import com.xjs.translation.domain.qo.translation.BaiDuTranslationQo;
|
||||
import org.springframework.cloud.openfeign.FeignClient;
|
||||
|
|
@ -17,6 +18,6 @@ public interface BaiduFeignClient {
|
|||
@ApiLog(name = "baidu",
|
||||
url = "http://api.fanyi.baidu.com/api/trans/vip/translate",
|
||||
method = "Post")
|
||||
String translationApi(BaiDuTranslationQo qo);
|
||||
JSONObject translationApi(BaiDuTranslationQo qo);
|
||||
|
||||
}
|
||||
|
|
|
|||
|
|
@ -40,20 +40,19 @@ public class BaiDuTranslationServiceImpl implements TranslationService {
|
|||
String sign = SecureUtil.md5(append);
|
||||
baiDuTranslationQo.setSign(sign);
|
||||
baiDuTranslationQo.setQ(translationQo.getQ());
|
||||
String translationStr = baiduFeignClient.translationApi(baiDuTranslationQo);
|
||||
JSONObject jsonObject = JSONObject.parseObject(translationStr);
|
||||
JSONObject jsonObject = baiduFeignClient.translationApi(baiDuTranslationQo);
|
||||
if(Objects.nonNull(jsonObject.getString("error_code"))){
|
||||
throw new BusinessException("百度翻译接口调用异常");
|
||||
}
|
||||
TranslationVo translationVo = new TranslationVo();
|
||||
String from = jsonObject.getString("from");
|
||||
String to = jsonObject.getString("to");
|
||||
String transResultStr = jsonObject.getString("trans_result");
|
||||
JSONArray jsonArray = JSONObject.parseArray(transResultStr);
|
||||
|
||||
JSONArray transResult = jsonObject.getJSONArray("trans_result");
|
||||
HashMap<String, String> map = new HashMap<>();
|
||||
List<Map<String, String>> maps = new ArrayList<>();
|
||||
map.put("src", jsonArray.getJSONObject(0).getString("src"));
|
||||
map.put("dst", jsonArray.getJSONObject(0).getString("dst"));
|
||||
map.put("src", transResult.getJSONObject(0).getString("src"));
|
||||
map.put("dst", transResult.getJSONObject(0).getString("dst"));
|
||||
maps.add(map);
|
||||
translationVo.setFrom(from);
|
||||
translationVo.setTo(to);
|
||||
|
|
|
|||
|
|
@ -35,7 +35,7 @@ PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
|
|||
delete from api_log where id = #{id}
|
||||
</delete>
|
||||
|
||||
<delete id="deleteApiLogByIds" parameterType="String">
|
||||
<delete id="deleteApiLogByIds" parameterType="Long">
|
||||
delete from api_log where id in
|
||||
<foreach item="id" collection="array" open="(" separator="," close=")">
|
||||
#{id}
|
||||
|
|
|
|||
Loading…
Reference in New Issue