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
Hide 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
;
package
com
.
yanzuoguang
.
cloud
.
aop
;
import
com.alibaba.fastjson.JSON
;
import
com.alibaba.fastjson.JSON
;
import
com.yanzuoguang.cloud.CloudContans
;
import
com.yanzuoguang.util.cache.MemoryCache
;
import
com.yanzuoguang.util.cache.MemoryCache
;
import
com.yanzuoguang.util.contants.ResultConstants
;
import
com.yanzuoguang.util.contants.ResultConstants
;
import
com.yanzuoguang.util.helper.JsonHelper
;
import
com.yanzuoguang.util.helper.StringHelper
;
import
com.yanzuoguang.util.helper.StringHelper
;
import
com.yanzuoguang.util.log.Log
;
import
com.yanzuoguang.util.thread.ThreadNext
;
import
com.yanzuoguang.util.thread.ThreadNext
;
import
com.yanzuoguang.util.vo.LogVo
;
import
com.yanzuoguang.util.vo.LogVo
;
import
com.yanzuoguang.util.vo.ResponseResult
;
import
com.yanzuoguang.util.vo.ResponseResult
;
...
@@ -21,6 +24,7 @@ import java.util.concurrent.LinkedBlockingQueue;
...
@@ -21,6 +24,7 @@ import java.util.concurrent.LinkedBlockingQueue;
/**
/**
* 基本处理拦截类
* 基本处理拦截类
*
* @author 颜佐光
* @author 颜佐光
*/
*/
public
class
BaseRequestAspect
implements
ThreadNext
.
Next
{
public
class
BaseRequestAspect
implements
ThreadNext
.
Next
{
...
@@ -35,6 +39,9 @@ public class BaseRequestAspect implements ThreadNext.Next {
...
@@ -35,6 +39,9 @@ public class BaseRequestAspect implements ThreadNext.Next {
@Value
(
"${yzg.cacheTime:120}"
)
@Value
(
"${yzg.cacheTime:120}"
)
protected
int
cacheTime
=
120
;
protected
int
cacheTime
=
120
;
@Value
(
"${yzg.reqSize:5000}"
)
private
int
reqSize
;
@Autowired
@Autowired
protected
ApplicationContext
context
;
protected
ApplicationContext
context
;
...
@@ -96,6 +103,92 @@ public class BaseRequestAspect implements ThreadNext.Next {
...
@@ -96,6 +103,92 @@ public class BaseRequestAspect implements ThreadNext.Next {
return
logInterVo
;
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;
...
@@ -8,19 +8,18 @@ import org.aspectj.lang.ProceedingJoinPoint;
import
org.aspectj.lang.annotation.Around
;
import
org.aspectj.lang.annotation.Around
;
import
org.aspectj.lang.annotation.Aspect
;
import
org.aspectj.lang.annotation.Aspect
;
import
org.aspectj.lang.annotation.Pointcut
;
import
org.aspectj.lang.annotation.Pointcut
;
import
org.slf4j.Logger
;
import
org.slf4j.LoggerFactory
;
import
org.springframework.stereotype.Component
;
import
org.springframework.stereotype.Component
;
/**
/**
* 接口切面,用于验证接口回调返回参数
* 接口切面,用于验证接口回调返回参数
*
* @author 颜佐光
* @author 颜佐光
*/
*/
@Aspect
@Aspect
@Component
@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的表达式
* AOP的表达式
...
@@ -37,11 +36,11 @@ public class FeignAspect extends HttpAspectUtil {
...
@@ -37,11 +36,11 @@ public class FeignAspect extends HttpAspectUtil {
*/
*/
@Around
(
value
=
"feignAspect()"
)
@Around
(
value
=
"feignAspect()"
)
public
Object
requestFeignAround
(
ProceedingJoinPoint
joinPoint
)
throws
Throwable
{
public
Object
requestFeignAround
(
ProceedingJoinPoint
joinPoint
)
throws
Throwable
{
String
name
=
joinPoint
.
getSignature
().
getName
(
);
long
start
=
requestLog
(
TAG
,
joinPoint
);
Object
result
=
null
;
Object
result
=
null
;
long
start
=
System
.
currentTimeMillis
();
Exception
ex
=
null
;
try
{
try
{
logger
.
info
(
"[ {} ] feign params is {}"
,
name
,
joinPoint
.
getArgs
());
result
=
joinPoint
.
proceed
();
result
=
joinPoint
.
proceed
();
// 假如是标准格式,则验证接口是否成功,不成功则抛出异常
// 假如是标准格式,则验证接口是否成功,不成功则抛出异常
...
@@ -52,20 +51,16 @@ public class FeignAspect extends HttpAspectUtil {
...
@@ -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
;
return
result
;
}
catch
(
CodeException
e
)
{
ex
=
e
;
throw
e
;
}
catch
(
Exception
e
)
{
}
catch
(
Exception
e
)
{
long
end
=
System
.
currentTimeMillis
();
result
=
ExceptionHelper
.
getError
(
e
);
logger
.
error
(
"[ {} ] feign time ({})ms , error {}"
,
name
,
(
end
-
start
),
e
);
ex
=
e
;
if
(
e
instanceof
CodeException
)
{
throw
e
;
throw
e
;
}
finally
{
}
else
{
responseLog
(
TAG
,
joinPoint
.
getSignature
().
getName
(),
joinPoint
,
start
,
result
,
ex
);
ResponseResult
responseResult
=
ExceptionHelper
.
getError
(
e
);
throw
new
CodeException
(
responseResult
.
getCode
(),
responseResult
.
getMessage
(),
responseResult
.
getTarget
());
}
}
}
}
}
...
...
yzg-util-cloud/src/main/java/com/yanzuoguang/cloud/aop/MqAspect.java
View file @
22522213
...
@@ -25,7 +25,7 @@ import org.springframework.stereotype.Component;
...
@@ -25,7 +25,7 @@ import org.springframework.stereotype.Component;
@Component
@Component
public
class
MqAspect
extends
BaseRequestAspect
{
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
* exec aop point aspect
...
@@ -42,50 +42,23 @@ public class MqAspect extends BaseRequestAspect {
...
@@ -42,50 +42,23 @@ public class MqAspect extends BaseRequestAspect {
*/
*/
@Around
(
value
=
"mqAspect()"
)
@Around
(
value
=
"mqAspect()"
)
public
Object
requestWebAround
(
ProceedingJoinPoint
joinPoint
)
throws
Throwable
{
public
Object
requestWebAround
(
ProceedingJoinPoint
joinPoint
)
throws
Throwable
{
Log
.
threadBegin
();
long
start
=
requestLog
(
TAG
,
joinPoint
);
long
start
=
System
.
currentTimeMillis
();
Object
result
=
null
;
String
name
=
joinPoint
.
getSignature
().
getName
();
Exception
ex
=
null
;
logger
.
info
(
"[ {} ] request params is {}"
,
name
,
joinPoint
.
getArgs
());
try
{
try
{
Object
ret
=
executeMethod
(
joinPoint
,
name
);
result
=
executeMethod
(
joinPoint
);
long
end
=
System
.
currentTimeMillis
();
return
result
;
logger
.
info
(
"[ {} ] time ({})ms, result is {}"
,
name
,
(
end
-
start
),
ret
);
return
ret
;
}
catch
(
Exception
e
)
{
}
catch
(
Exception
e
)
{
long
end
=
System
.
currentTimeMillis
();
logger
.
error
(
"[ {} ] time ({})ms, is error {}"
,
name
,
(
end
-
start
),
e
);
// 消费消息出错
// 消费消息出错
ResponseResult
responseResult
=
ExceptionHelper
.
getError
(
e
);
result
=
ExceptionHelper
.
getError
(
e
);
saveInterLogs
(
joinPoint
,
responseResult
);
ex
=
e
;
Log
.
error
(
joinPoint
.
getSignature
().
getDeclaringType
(),
e
);
throw
e
;
throw
e
;
}
finally
{
}
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
();
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;
...
@@ -37,15 +37,14 @@ import java.lang.reflect.Type;
@Component
@Component
public
class
WebAspect
extends
BaseRequestAspect
{
public
class
WebAspect
extends
BaseRequestAspect
{
private
static
final
String
TAG
=
WebAspect
.
class
.
getSimpleName
();
@Autowired
@Autowired
private
TokenServiceCall
tokenServiceCall
;
private
TokenServiceCall
tokenServiceCall
;
@Value
(
"${yzg.reqUrl:order,check,use}"
)
@Value
(
"${yzg.reqUrl:order,check,use}"
)
private
String
reqUrl
;
private
String
reqUrl
;
@Value
(
"${yzg.reqSize:5000}"
)
private
int
reqSize
;
/**
/**
* exec aop point aspect
* exec aop point aspect
*/
*/
...
@@ -61,64 +60,32 @@ public class WebAspect extends BaseRequestAspect {
...
@@ -61,64 +60,32 @@ public class WebAspect extends BaseRequestAspect {
*/
*/
@Around
(
value
=
"webAspect()"
)
@Around
(
value
=
"webAspect()"
)
public
Object
requestWebAround
(
ProceedingJoinPoint
joinPoint
)
throws
Throwable
{
public
Object
requestWebAround
(
ProceedingJoinPoint
joinPoint
)
throws
Throwable
{
Log
.
threadBegin
();
// 用户数据库记录
// 用户数据库记录
ResponseResult
responseResult
=
null
;
long
start
=
requestLog
(
TAG
,
joinPoint
);
long
start
=
System
.
currentTimeMillis
();
Object
result
=
null
;
String
name
=
joinPoint
.
getSignature
().
getName
();
Ref
<
Boolean
>
flag
=
new
Ref
<>(
false
);
Exception
ex
=
null
;
Exception
ex
=
null
;
Ref
<
Boolean
>
flag
=
new
Ref
<>(
false
);
try
{
try
{
Log
.
info
(
WebAspect
.
class
,
"[ %s ] request params is: %s"
,
name
,
this
.
getMaxString
(
JsonHelper
.
serialize
(
joinPoint
.
getArgs
())));
tokenServiceCall
.
tokenInit
();
tokenServiceCall
.
tokenInit
();
result
=
executeMethod
(
joinPoint
);
Object
result
=
executeMethod
(
joinPoint
,
name
);
if
(
result
instanceof
ResponseResult
)
{
responseResult
=
(
ResponseResult
)
result
;
}
else
{
responseResult
=
ResponseResult
.
result
(
result
);
}
long
end
=
System
.
currentTimeMillis
();
tokenFinish
(
flag
);
tokenFinish
(
flag
);
return
result
;
return
result
;
}
catch
(
Exception
e
)
{
}
catch
(
Exception
e
)
{
ex
=
e
;
ex
=
e
;
res
ponseRes
ult
=
ExceptionHelper
.
getError
(
e
);
result
=
ExceptionHelper
.
getError
(
e
);
if
(
getReturnType
(
joinPoint
).
getTypeName
().
indexOf
(
ResponseResult
.
class
.
getName
())
>
-
1
)
{
if
(
getReturnType
(
joinPoint
).
getTypeName
().
indexOf
(
ResponseResult
.
class
.
getName
())
>
-
1
)
{
return
res
ponseRes
ult
;
return
result
;
}
else
{
}
else
{
throw
e
;
throw
e
;
}
}
}
finally
{
}
finally
{
tokenFinish
(
flag
);
tokenFinish
(
flag
);
long
time
=
System
.
currentTimeMillis
()
-
start
;
responseLog
(
TAG
,
HttpAspectUtil
.
getHttpRequestUrl
(),
joinPoint
,
start
,
result
,
ex
);
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
);
}
return
paraJson
;
}
/**
/**
* 执行结束函数
* 执行结束函数
*
*
...
@@ -165,11 +132,10 @@ public class WebAspect extends BaseRequestAspect {
...
@@ -165,11 +132,10 @@ public class WebAspect extends BaseRequestAspect {
* 执行方法
* 执行方法
*
*
* @param joinPoint 需要执行的方法
* @param joinPoint 需要执行的方法
* @param name 方法名称
* @return 返回结果
* @return 返回结果
* @throws Throwable
* @throws Throwable
*/
*/
private
Object
executeMethod
(
ProceedingJoinPoint
joinPoint
,
String
name
)
throws
Throwable
{
private
Object
executeMethod
(
ProceedingJoinPoint
joinPoint
)
throws
Throwable
{
boolean
dataArgs
=
joinPoint
.
getArgs
().
length
!=
1
boolean
dataArgs
=
joinPoint
.
getArgs
().
length
!=
1
||
joinPoint
.
getArgs
().
length
==
1
&&
||
joinPoint
.
getArgs
().
length
==
1
&&
(
joinPoint
.
getArgs
()[
0
]
instanceof
ServletResponse
||
joinPoint
.
getArgs
()[
0
]
instanceof
ServletRequest
);
(
joinPoint
.
getArgs
()[
0
]
instanceof
ServletResponse
||
joinPoint
.
getArgs
()[
0
]
instanceof
ServletRequest
);
...
@@ -211,26 +177,4 @@ public class WebAspect extends BaseRequestAspect {
...
@@ -211,26 +177,4 @@ public class WebAspect extends BaseRequestAspect {
return
result
;
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