Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Submit feedback
Sign in / Register
Toggle navigation
Y
yzg-util
Project
Project
Details
Activity
Releases
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
YZG
yzg-util
Commits
22522213
Commit
22522213
authored
Aug 08, 2019
by
yanzg
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
常规BUG的修改
parent
f451bbd9
Changes
4
Show whitespace changes
Inline
Side-by-side
Showing
4 changed files
with
128 additions
and
123 deletions
+128
-123
BaseRequestAspect.java
...ain/java/com/yanzuoguang/cloud/aop/BaseRequestAspect.java
+93
-0
FeignAspect.java
.../src/main/java/com/yanzuoguang/cloud/aop/FeignAspect.java
+14
-19
MqAspect.java
...oud/src/main/java/com/yanzuoguang/cloud/aop/MqAspect.java
+10
-37
WebAspect.java
...ud/src/main/java/com/yanzuoguang/cloud/aop/WebAspect.java
+11
-67
No files found.
yzg-util-cloud/src/main/java/com/yanzuoguang/cloud/aop/BaseRequestAspect.java
View file @
22522213
package
com
.
yanzuoguang
.
cloud
.
aop
;
import
com.alibaba.fastjson.JSON
;
import
com.yanzuoguang.cloud.CloudContans
;
import
com.yanzuoguang.util.cache.MemoryCache
;
import
com.yanzuoguang.util.contants.ResultConstants
;
import
com.yanzuoguang.util.helper.JsonHelper
;
import
com.yanzuoguang.util.helper.StringHelper
;
import
com.yanzuoguang.util.log.Log
;
import
com.yanzuoguang.util.thread.ThreadNext
;
import
com.yanzuoguang.util.vo.LogVo
;
import
com.yanzuoguang.util.vo.ResponseResult
;
...
...
@@ -21,6 +24,7 @@ import java.util.concurrent.LinkedBlockingQueue;
/**
* 基本处理拦截类
*
* @author 颜佐光
*/
public
class
BaseRequestAspect
implements
ThreadNext
.
Next
{
...
...
@@ -35,6 +39,9 @@ public class BaseRequestAspect implements ThreadNext.Next {
@Value
(
"${yzg.cacheTime:120}"
)
protected
int
cacheTime
=
120
;
@Value
(
"${yzg.reqSize:5000}"
)
private
int
reqSize
;
@Autowired
protected
ApplicationContext
context
;
...
...
@@ -96,6 +103,92 @@ public class BaseRequestAspect implements ThreadNext.Next {
return
logInterVo
;
}
/**
* 获取JSON,当Json过长时,截断
*
* @param paraJson
* @return
*/
private
String
getMaxString
(
String
paraJson
)
{
if
(
paraJson
!=
null
&&
paraJson
.
length
()
>
reqSize
)
{
paraJson
=
paraJson
.
substring
(
0
,
reqSize
);
}
return
paraJson
;
}
/**
* 记录请求日志
*
* @param joinPoint
* @return
*/
protected
long
requestLog
(
String
tag
,
ProceedingJoinPoint
joinPoint
)
{
long
start
=
System
.
currentTimeMillis
();
try
{
String
name
=
joinPoint
.
getSignature
().
getName
();
Log
.
threadBegin
();
Log
.
info
(
joinPoint
.
getSignature
().
getDeclaringType
(),
" %s [ %s ] request: %s"
,
tag
,
name
,
this
.
getMaxString
(
JsonHelper
.
serialize
(
joinPoint
.
getArgs
())));
}
catch
(
Exception
ex
)
{
ex
.
printStackTrace
();
}
return
start
;
}
/**
* 保存日志
*
* @param url
* @param joinPoint
* @param result
* @param resultEx
* @param start
*/
protected
void
responseLog
(
String
url
,
String
tag
,
ProceedingJoinPoint
joinPoint
,
long
start
,
Object
result
,
Exception
resultEx
)
{
try
{
String
name
=
joinPoint
.
getSignature
().
getName
();
// 处理结果
ResponseResult
responseResult
;
if
(
result
instanceof
ResponseResult
)
{
responseResult
=
(
ResponseResult
)
result
;
}
else
{
responseResult
=
ResponseResult
.
result
(
result
);
}
long
time
=
System
.
currentTimeMillis
()
-
start
;
if
(
resultEx
!=
null
)
{
Log
.
error
(
joinPoint
.
getSignature
().
getDeclaringType
(),
"%s [ %s ] time %d ms, error: %s"
,
tag
,
name
,
time
,
getMaxString
(
resultEx
.
getMessage
()));
}
else
{
Log
.
info
(
joinPoint
.
getSignature
().
getDeclaringType
(),
"%s [ %s ] time %d ms, result: %s"
,
tag
,
name
,
time
,
getMaxString
(
JsonHelper
.
serialize
(
responseResult
)));
}
// 日志请求不记录,防止死循环递归
boolean
isLog
=
applicationName
.
indexOf
(
CloudContans
.
LOG_MODULE
)
>=
0
||
name
.
indexOf
(
CloudContans
.
LOG_MODULE
)
>=
0
||
url
.
indexOf
(
CloudContans
.
LOG_MODULE
)
>=
0
;
if
(
isLog
)
{
return
;
}
// 正常请求不记录
if
(!
logAll
&&
responseResult
!=
null
&&
responseResult
.
getCode
()
==
ResultConstants
.
SUCCESS
)
{
return
;
}
LogVo
logVo
=
initLogInterVo
(
url
,
joinPoint
,
responseResult
);
logVo
.
setUseTime
((
int
)
time
);
addLog
(
logVo
);
}
catch
(
Exception
e
)
{
e
.
printStackTrace
();
}
finally
{
Log
.
threadCommit
();
}
}
/**
* 执行下一个函数,出现异常会继续执行
*
...
...
yzg-util-cloud/src/main/java/com/yanzuoguang/cloud/aop/FeignAspect.java
View file @
22522213
...
...
@@ -8,19 +8,18 @@ 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.slf4j.Logger
;
import
org.slf4j.LoggerFactory
;
import
org.springframework.stereotype.Component
;
/**
* 接口切面,用于验证接口回调返回参数
*
* @author 颜佐光
*/
@Aspect
@Component
public
class
FeignAspect
extends
HttpAspectUtil
{
public
class
FeignAspect
extends
BaseRequestAspect
{
private
static
final
Logger
logger
=
LoggerFactory
.
getLogger
(
FeignAspect
.
class
);
private
static
final
String
TAG
=
FeignAspect
.
class
.
getSimpleName
(
);
/**
* AOP的表达式
...
...
@@ -37,11 +36,11 @@ public class FeignAspect extends HttpAspectUtil {
*/
@Around
(
value
=
"feignAspect()"
)
public
Object
requestFeignAround
(
ProceedingJoinPoint
joinPoint
)
throws
Throwable
{
String
name
=
joinPoint
.
getSignature
().
getName
(
);
long
start
=
requestLog
(
TAG
,
joinPoint
);
Object
result
=
null
;
long
start
=
System
.
currentTimeMillis
();
Exception
ex
=
null
;
try
{
logger
.
info
(
"[ {} ] feign params is {}"
,
name
,
joinPoint
.
getArgs
());
result
=
joinPoint
.
proceed
();
// 假如是标准格式,则验证接口是否成功,不成功则抛出异常
...
...
@@ -52,20 +51,16 @@ public class FeignAspect extends HttpAspectUtil {
}
}
// 记录服务调用时间,请求日志
long
end
=
System
.
currentTimeMillis
();
logger
.
info
(
"[ {} ] feign time ({})ms, result is {}"
,
name
,
(
end
-
start
),
result
);
return
result
;
}
catch
(
CodeException
e
)
{
ex
=
e
;
throw
e
;
}
catch
(
Exception
e
)
{
long
end
=
System
.
currentTimeMillis
();
logger
.
error
(
"[ {} ] feign time ({})ms , error {}"
,
name
,
(
end
-
start
),
e
);
if
(
e
instanceof
CodeException
)
{
result
=
ExceptionHelper
.
getError
(
e
);
ex
=
e
;
throw
e
;
}
else
{
ResponseResult
responseResult
=
ExceptionHelper
.
getError
(
e
);
throw
new
CodeException
(
responseResult
.
getCode
(),
responseResult
.
getMessage
(),
responseResult
.
getTarget
());
}
}
finally
{
responseLog
(
TAG
,
joinPoint
.
getSignature
().
getName
(),
joinPoint
,
start
,
result
,
ex
);
}
}
...
...
yzg-util-cloud/src/main/java/com/yanzuoguang/cloud/aop/MqAspect.java
View file @
22522213
...
...
@@ -25,7 +25,7 @@ import org.springframework.stereotype.Component;
@Component
public
class
MqAspect
extends
BaseRequestAspect
{
private
static
final
Logger
logger
=
LoggerFactory
.
getLogger
(
MqAspect
.
class
);
private
static
final
String
TAG
=
MqAspect
.
class
.
getSimpleName
(
);
/**
* exec aop point aspect
...
...
@@ -42,50 +42,23 @@ public class MqAspect extends BaseRequestAspect {
*/
@Around
(
value
=
"mqAspect()"
)
public
Object
requestWebAround
(
ProceedingJoinPoint
joinPoint
)
throws
Throwable
{
Log
.
threadBegin
();
long
start
=
System
.
currentTimeMillis
();
String
name
=
joinPoint
.
getSignature
().
getName
();
logger
.
info
(
"[ {} ] request params is {}"
,
name
,
joinPoint
.
getArgs
());
long
start
=
requestLog
(
TAG
,
joinPoint
);
Object
result
=
null
;
Exception
ex
=
null
;
try
{
Object
ret
=
executeMethod
(
joinPoint
,
name
);
long
end
=
System
.
currentTimeMillis
();
logger
.
info
(
"[ {} ] time ({})ms, result is {}"
,
name
,
(
end
-
start
),
ret
);
return
ret
;
result
=
executeMethod
(
joinPoint
);
return
result
;
}
catch
(
Exception
e
)
{
long
end
=
System
.
currentTimeMillis
();
logger
.
error
(
"[ {} ] time ({})ms, is error {}"
,
name
,
(
end
-
start
),
e
);
// 消费消息出错
ResponseResult
responseResult
=
ExceptionHelper
.
getError
(
e
);
saveInterLogs
(
joinPoint
,
responseResult
);
Log
.
error
(
joinPoint
.
getSignature
().
getDeclaringType
(),
e
);
result
=
ExceptionHelper
.
getError
(
e
);
ex
=
e
;
throw
e
;
}
finally
{
Log
.
threadCommit
(
);
responseLog
(
TAG
,
joinPoint
.
getSignature
().
getName
(),
joinPoint
,
start
,
result
,
ex
);
}
}
private
Object
executeMethod
(
ProceedingJoinPoint
joinPoint
,
String
name
)
throws
Throwable
{
private
Object
executeMethod
(
ProceedingJoinPoint
joinPoint
)
throws
Throwable
{
return
joinPoint
.
proceed
();
}
/**
* deal with inter api logs
*
* @param joinPoint 请求参数
* @param responseResult 返回参数
*/
private
void
saveInterLogs
(
ProceedingJoinPoint
joinPoint
,
ResponseResult
responseResult
)
{
// 日志请求不记录,防止死循环递归
if
(
applicationName
.
indexOf
(
CloudContans
.
LOG_MODULE
)
>=
0
)
{
return
;
}
// 正常请求不记录
if
(!
logAll
&&
responseResult
.
getCode
()
==
ResultConstants
.
SUCCESS
)
{
return
;
}
LogVo
logVo
=
initLogInterVo
(
StringHelper
.
EMPTY
,
joinPoint
,
responseResult
);
addLog
(
logVo
);
}
}
yzg-util-cloud/src/main/java/com/yanzuoguang/cloud/aop/WebAspect.java
View file @
22522213
...
...
@@ -37,15 +37,14 @@ import java.lang.reflect.Type;
@Component
public
class
WebAspect
extends
BaseRequestAspect
{
private
static
final
String
TAG
=
WebAspect
.
class
.
getSimpleName
();
@Autowired
private
TokenServiceCall
tokenServiceCall
;
@Value
(
"${yzg.reqUrl:order,check,use}"
)
private
String
reqUrl
;
@Value
(
"${yzg.reqSize:5000}"
)
private
int
reqSize
;
/**
* exec aop point aspect
*/
...
...
@@ -61,62 +60,30 @@ public class WebAspect extends BaseRequestAspect {
*/
@Around
(
value
=
"webAspect()"
)
public
Object
requestWebAround
(
ProceedingJoinPoint
joinPoint
)
throws
Throwable
{
Log
.
threadBegin
();
// 用户数据库记录
ResponseResult
responseResult
=
null
;
long
start
=
System
.
currentTimeMillis
();
String
name
=
joinPoint
.
getSignature
().
getName
();
Ref
<
Boolean
>
flag
=
new
Ref
<>(
false
);
long
start
=
requestLog
(
TAG
,
joinPoint
);
Object
result
=
null
;
Exception
ex
=
null
;
Ref
<
Boolean
>
flag
=
new
Ref
<>(
false
);
try
{
Log
.
info
(
WebAspect
.
class
,
"[ %s ] request params is: %s"
,
name
,
this
.
getMaxString
(
JsonHelper
.
serialize
(
joinPoint
.
getArgs
())));
tokenServiceCall
.
tokenInit
();
Object
result
=
executeMethod
(
joinPoint
,
name
);
if
(
result
instanceof
ResponseResult
)
{
responseResult
=
(
ResponseResult
)
result
;
}
else
{
responseResult
=
ResponseResult
.
result
(
result
);
}
long
end
=
System
.
currentTimeMillis
();
result
=
executeMethod
(
joinPoint
);
tokenFinish
(
flag
);
return
result
;
}
catch
(
Exception
e
)
{
ex
=
e
;
res
ponseRes
ult
=
ExceptionHelper
.
getError
(
e
);
result
=
ExceptionHelper
.
getError
(
e
);
if
(
getReturnType
(
joinPoint
).
getTypeName
().
indexOf
(
ResponseResult
.
class
.
getName
())
>
-
1
)
{
return
res
ponseRes
ult
;
return
result
;
}
else
{
throw
e
;
}
}
finally
{
tokenFinish
(
flag
);
long
time
=
System
.
currentTimeMillis
()
-
start
;
if
(
ex
!=
null
)
{
Log
.
error
(
WebAspect
.
class
,
"[ %s ] time %d ms, error: %s"
,
name
,
time
,
getMaxString
(
ex
.
getMessage
()));
}
else
{
Log
.
info
(
WebAspect
.
class
,
"[ %s ] time %d ms, result is: %s"
,
name
,
time
,
getMaxString
(
JsonHelper
.
serialize
(
responseResult
)));
}
Log
.
threadCommit
();
saveInterLogs
(
joinPoint
,
responseResult
,
time
);
}
}
/**
* 获取JSON,当Json过长时,截断
*
* @param paraJson
* @return
*/
private
String
getMaxString
(
String
paraJson
)
{
if
(
paraJson
!=
null
&&
paraJson
.
length
()
>
reqSize
)
{
paraJson
=
paraJson
.
substring
(
0
,
reqSize
);
responseLog
(
TAG
,
HttpAspectUtil
.
getHttpRequestUrl
(),
joinPoint
,
start
,
result
,
ex
);
}
return
paraJson
;
}
/**
...
...
@@ -165,11 +132,10 @@ public class WebAspect extends BaseRequestAspect {
* 执行方法
*
* @param joinPoint 需要执行的方法
* @param name 方法名称
* @return 返回结果
* @throws Throwable
*/
private
Object
executeMethod
(
ProceedingJoinPoint
joinPoint
,
String
name
)
throws
Throwable
{
private
Object
executeMethod
(
ProceedingJoinPoint
joinPoint
)
throws
Throwable
{
boolean
dataArgs
=
joinPoint
.
getArgs
().
length
!=
1
||
joinPoint
.
getArgs
().
length
==
1
&&
(
joinPoint
.
getArgs
()[
0
]
instanceof
ServletResponse
||
joinPoint
.
getArgs
()[
0
]
instanceof
ServletRequest
);
...
...
@@ -211,26 +177,4 @@ public class WebAspect extends BaseRequestAspect {
return
result
;
}
}
/**
* deal with inter api logs
*
* @param joinPoint 请求参数
* @param responseResult 返回参数
*/
private
void
saveInterLogs
(
ProceedingJoinPoint
joinPoint
,
ResponseResult
responseResult
,
long
time
)
{
// 日志请求不记录,防止死循环递归
if
(
applicationName
.
indexOf
(
CloudContans
.
LOG_MODULE
)
>=
0
)
{
return
;
}
// 正常请求不记录
if
(!
logAll
&&
responseResult
!=
null
&&
responseResult
.
getCode
()
==
ResultConstants
.
SUCCESS
)
{
return
;
}
LogVo
logVo
=
initLogInterVo
(
HttpAspectUtil
.
getHttpRequestUrl
(),
joinPoint
,
responseResult
);
logVo
.
setUseTime
((
int
)
time
);
addLog
(
logVo
);
}
}
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment