Skip to content

Commit 949f5f3

Browse files
committed
改为异步,避免插入接口日志导致接口响应时间长
1 parent e9cb7e8 commit 949f5f3

File tree

5 files changed

+80
-38
lines changed

5 files changed

+80
-38
lines changed

tang-commons/pom.xml

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,11 @@
4747
<artifactId>spring-boot-starter-websocket</artifactId>
4848
</dependency>
4949

50+
<dependency>
51+
<groupId>org.springframework.boot</groupId>
52+
<artifactId>spring-boot-starter-aop</artifactId>
53+
</dependency>
54+
5055
<dependency>
5156
<groupId>org.springframework</groupId>
5257
<artifactId>spring-context-indexer</artifactId>

tang-framework/pom.xml

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -23,11 +23,6 @@
2323
<artifactId>spring-boot-starter-web</artifactId>
2424
</dependency>
2525

26-
<dependency>
27-
<groupId>org.springframework.boot</groupId>
28-
<artifactId>spring-boot-starter-aop</artifactId>
29-
</dependency>
30-
3126
<dependency>
3227
<groupId>org.mybatis.spring.boot</groupId>
3328
<artifactId>mybatis-spring-boot-starter</artifactId>

tang-framework/src/main/kotlin/com/tang/framework/aop/ControllerAspect.kt

Lines changed: 6 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
package com.tang.framework.aop
22

33
import java.time.LocalDateTime
4-
import java.time.ZoneOffset
54

65
import org.aspectj.lang.ProceedingJoinPoint
76
import org.aspectj.lang.annotation.Around
@@ -10,13 +9,10 @@ import org.springframework.stereotype.Component
109

1110
import com.tang.commons.utils.AjaxResult
1211
import com.tang.commons.utils.LogUtils
13-
import com.tang.commons.utils.SecurityUtils
1412
import com.tang.commons.utils.ServletUtils
1513
import com.tang.commons.utils.SpringUtils
1614
import com.tang.commons.utils.page.TableDataResult
17-
import com.tang.commons.utils.time.TimeUtils
18-
import com.tang.system.entity.SysLogApi
19-
import com.tang.system.mapper.SysLogApiMapper
15+
import com.tang.system.service.SysLogApiService
2016

2117
/**
2218
* 控制器切面类,记录接口调用日志
@@ -28,17 +24,13 @@ import com.tang.system.mapper.SysLogApiMapper
2824
class ControllerAspect {
2925

3026
companion object {
31-
private val sysLogApiMapper = SpringUtils.getBean(SysLogApiMapper::class.java)
27+
private val sysLogApiService = SpringUtils.getBean(SysLogApiService::class.java)
3228
}
3329

3430
@Around("execution(* com.tang..controller..*.*(..))")
3531
fun around(proceedingJoinPoint: ProceedingJoinPoint): Any {
36-
val signature = proceedingJoinPoint.signature
37-
val className = proceedingJoinPoint.target.javaClass.name
38-
val methodName = signature.name
39-
val requestParams = proceedingJoinPoint.args.toList().toString()
4032
val startTimestamp = LocalDateTime.now()
41-
var message = ""
33+
val message: String
4234
var throwable: Throwable? = null
4335
var response: Any? = null
4436
message = try {
@@ -54,25 +46,10 @@ class ControllerAspect {
5446
e.message.toString()
5547
}
5648
val endTimestamp = LocalDateTime.now()
57-
val between = endTimestamp.toInstant(ZoneOffset.UTC).toEpochMilli() - startTimestamp.toInstant(ZoneOffset.UTC).toEpochMilli()
5849

59-
val sysLogApi = SysLogApi()
60-
sysLogApi.userId = if (SecurityUtils.isAuthenticated()) SecurityUtils.getUser().userId else null
61-
sysLogApi.className = className
62-
sysLogApi.methodName = methodName
63-
sysLogApi.requestUri = ServletUtils.getRequest().requestURI
64-
sysLogApi.requestType = ServletUtils.getRequest().method
65-
sysLogApi.requestParam = requestParams
66-
sysLogApi.responseBody = response?.toString()
67-
sysLogApi.loginType = if (SecurityUtils.isAuthenticated()) SecurityUtils.getUserModel().loginType else null
68-
sysLogApi.ip = if (SecurityUtils.isAuthenticated()) SecurityUtils.getUserModel().ip else null
69-
sysLogApi.location = if (SecurityUtils.isAuthenticated()) SecurityUtils.getUserModel().location else null
70-
sysLogApi.startTime = startTimestamp
71-
sysLogApi.endTime = endTimestamp
72-
sysLogApi.costTime = between
73-
sysLogApi.statusCode = if (throwable == null) "200" else "500"
74-
sysLogApi.message = message
75-
sysLogApiMapper.insertSysLogApi(sysLogApi)
50+
val requestURI = ServletUtils.getRequest().requestURI
51+
val method = ServletUtils.getRequest().method
52+
sysLogApiService.insertSysLogApi(proceedingJoinPoint, requestURI, method, response, startTimestamp, endTimestamp, throwable, message)
7653

7754
if (throwable != null) {
7855
throw throwable

tang-system/src/main/kotlin/com/tang/system/service/SysLogApiService.kt

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,9 @@
11
package com.tang.system.service;
22

3+
import java.time.LocalDateTime
4+
5+
import org.aspectj.lang.ProceedingJoinPoint
6+
37
import com.tang.system.entity.SysLogApi;
48

59
/**
@@ -33,6 +37,21 @@ interface SysLogApiService {
3337
*/
3438
fun insertSysLogApi(sysLogApi: SysLogApi): Int
3539

40+
/**
41+
* 新增接口日志信息
42+
*
43+
* @param proceedingJoinPoint 切点
44+
* @param requestURI 请求地址
45+
* @param method 请求方式
46+
* @param response 响应结果
47+
* @param startTimestamp 开始时间
48+
* @param endTimestamp 结束时间
49+
* @param throwable 异常信息
50+
* @param message 消息
51+
*/
52+
fun insertSysLogApi(proceedingJoinPoint: ProceedingJoinPoint, requestURI: String?, method: String?, response: Any?,
53+
startTimestamp: LocalDateTime, endTimestamp: LocalDateTime, throwable: Throwable?, message: String)
54+
3655
/**
3756
* 通过接口日志主键修改接口日志信息
3857
*

tang-system/src/main/kotlin/com/tang/system/service/impl/SysLogApiServiceImpl.kt

Lines changed: 50 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,16 @@
11
package com.tang.system.service.impl;
22

3-
import org.springframework.stereotype.Service;
3+
import java.time.LocalDateTime
4+
import java.time.ZoneOffset
45

5-
import com.tang.system.entity.SysLogApi;
6-
import com.tang.system.mapper.SysLogApiMapper;
7-
import com.tang.system.service.SysLogApiService;
6+
import org.aspectj.lang.ProceedingJoinPoint
7+
import org.springframework.scheduling.annotation.Async
8+
import org.springframework.stereotype.Service
9+
10+
import com.tang.commons.utils.SecurityUtils
11+
import com.tang.system.entity.SysLogApi
12+
import com.tang.system.mapper.SysLogApiMapper
13+
import com.tang.system.service.SysLogApiService
814

915
/**
1016
* 接口日志业务逻辑层接口实现
@@ -44,6 +50,46 @@ open class SysLogApiServiceImpl(private val sysLogApiMapper: SysLogApiMapper): S
4450
return sysLogApiMapper.insertSysLogApi(sysLogApi)
4551
}
4652

53+
/**
54+
* 新增接口日志信息
55+
*
56+
* @param proceedingJoinPoint 切点
57+
* @param requestURI 请求地址
58+
* @param method 请求方式
59+
* @param response 响应结果
60+
* @param startTimestamp 开始时间
61+
* @param endTimestamp 结束时间
62+
* @param throwable 异常信息
63+
* @param message 消息
64+
*/
65+
@Async
66+
override fun insertSysLogApi(proceedingJoinPoint: ProceedingJoinPoint, requestURI: String?, method: String?, response: Any?,
67+
startTimestamp: LocalDateTime, endTimestamp: LocalDateTime, throwable: Throwable?, message: String) {
68+
val signature = proceedingJoinPoint.signature
69+
val className = proceedingJoinPoint.target.javaClass.name
70+
val methodName = signature.name
71+
val requestParams = proceedingJoinPoint.args.toList().toString()
72+
val between = endTimestamp.toInstant(ZoneOffset.UTC).toEpochMilli() - startTimestamp.toInstant(ZoneOffset.UTC).toEpochMilli()
73+
74+
val sysLogApi = SysLogApi()
75+
sysLogApi.userId = if (SecurityUtils.isAuthenticated()) SecurityUtils.getUser().userId else null
76+
sysLogApi.className = className
77+
sysLogApi.methodName = methodName
78+
sysLogApi.requestUri = requestURI
79+
sysLogApi.requestType = method
80+
sysLogApi.requestParam = requestParams
81+
sysLogApi.responseBody = response?.toString()
82+
sysLogApi.loginType = if (SecurityUtils.isAuthenticated()) SecurityUtils.getUserModel().loginType else null
83+
sysLogApi.ip = if (SecurityUtils.isAuthenticated()) SecurityUtils.getUserModel().ip else null
84+
sysLogApi.location = if (SecurityUtils.isAuthenticated()) SecurityUtils.getUserModel().location else null
85+
sysLogApi.startTime = startTimestamp
86+
sysLogApi.endTime = endTimestamp
87+
sysLogApi.costTime = between
88+
sysLogApi.statusCode = if (throwable == null) "200" else "500"
89+
sysLogApi.message = message
90+
sysLogApiMapper.insertSysLogApi(sysLogApi)
91+
}
92+
4793
/**
4894
* 通过接口日志主键修改接口日志信息
4995
*

0 commit comments

Comments
 (0)