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
2110a4da
Commit
2110a4da
authored
Mar 21, 2019
by
yanzg
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
日志处理服务
parent
0bd070a2
Changes
14
Show whitespace changes
Inline
Side-by-side
Showing
14 changed files
with
677 additions
and
387 deletions
+677
-387
ExceptionHelper.java
.../java/com/yanzuoguang/util/exception/ExceptionHelper.java
+22
-1
DateHelper.java
...src/main/java/com/yanzuoguang/util/helper/DateHelper.java
+1
-0
EnumHelper.java
...src/main/java/com/yanzuoguang/util/helper/EnumHelper.java
+2
-0
StringHelper.java
...c/main/java/com/yanzuoguang/util/helper/StringHelper.java
+1
-2
RunPlan.java
...se/src/main/java/com/yanzuoguang/util/thread/RunPlan.java
+1
-1
ThreadHelper.java
...c/main/java/com/yanzuoguang/util/thread/ThreadHelper.java
+1
-1
ThreadNext.java
...src/main/java/com/yanzuoguang/util/thread/ThreadNext.java
+60
-0
LogVo.java
...til-base/src/main/java/com/yanzuoguang/util/vo/LogVo.java
+101
-0
pom.xml
yzg-util-cloud/pom.xml
+60
-0
AbstractValidateAspect.java
...ava/com/yanzuoguang/cloud/aop/AbstractValidateAspect.java
+30
-0
FeignAspect.java
.../src/main/java/com/yanzuoguang/cloud/aop/FeignAspect.java
+71
-71
LogFeign.java
...oud/src/main/java/com/yanzuoguang/cloud/aop/LogFeign.java
+17
-0
RequestCacheResult.java
...in/java/com/yanzuoguang/cloud/aop/RequestCacheResult.java
+61
-61
WebAspect.java
...ud/src/main/java/com/yanzuoguang/cloud/aop/WebAspect.java
+249
-250
No files found.
yzg-util-base/src/main/java/com/yanzuoguang/util/
helper
/ExceptionHelper.java
→
yzg-util-base/src/main/java/com/yanzuoguang/util/
exception
/ExceptionHelper.java
View file @
2110a4da
package
com
.
yanzuoguang
.
util
.
helper
;
package
com
.
yanzuoguang
.
util
.
exception
;
import
com.yanzuoguang.util.contants.ResultConstants
;
import
com.yanzuoguang.util.vo.ResponseResult
;
import
java.util.logging.Logger
;
/**
/**
* 异常处理帮助类
* 异常处理帮助类
...
@@ -41,4 +46,20 @@ public class ExceptionHelper {
...
@@ -41,4 +46,20 @@ public class ExceptionHelper {
String
exception
=
local
+
"/n"
+
"开始:"
+
start
+
" 结束:"
+
end
+
"总长:"
+
allsize
+
"/n"
+
str
;
String
exception
=
local
+
"/n"
+
"开始:"
+
start
+
" 结束:"
+
end
+
"总长:"
+
allsize
+
"/n"
+
str
;
throw
new
Exception
(
exception
);
throw
new
Exception
(
exception
);
}
}
/**
* 根据异常获取返回数据
*
* @param e 异常信息
* @return 返回的数据
*/
public
static
ResponseResult
getError
(
Exception
e
)
{
if
(
e
instanceof
CodeException
)
{
CodeException
code
=
(
CodeException
)
e
;
return
new
ResponseResult
(
code
.
getCode
(),
code
.
getMessage
());
}
else
{
return
new
ResponseResult
(
ResultConstants
.
UNKNOW_ERROR
,
e
.
getMessage
());
}
}
}
}
yzg-util-base/src/main/java/com/yanzuoguang/util/helper/DateHelper.java
View file @
2110a4da
package
com
.
yanzuoguang
.
util
.
helper
;
package
com
.
yanzuoguang
.
util
.
helper
;
import
com.yanzuoguang.util.exception.CodeException
;
import
com.yanzuoguang.util.exception.CodeException
;
import
com.yanzuoguang.util.exception.ExceptionHelper
;
import
java.sql.Timestamp
;
import
java.sql.Timestamp
;
import
java.text.ParseException
;
import
java.text.ParseException
;
...
...
yzg-util-base/src/main/java/com/yanzuoguang/util/helper/EnumHelper.java
View file @
2110a4da
package
com
.
yanzuoguang
.
util
.
helper
;
package
com
.
yanzuoguang
.
util
.
helper
;
import
com.yanzuoguang.util.exception.ExceptionHelper
;
import
java.lang.reflect.Method
;
import
java.lang.reflect.Method
;
import
java.util.HashMap
;
import
java.util.HashMap
;
...
...
yzg-util-base/src/main/java/com/yanzuoguang/util/helper/StringHelper.java
View file @
2110a4da
package
com
.
yanzuoguang
.
util
.
helper
;
package
com
.
yanzuoguang
.
util
.
helper
;
import
com.yanzuoguang.util.exception.CodeException
;
import
com.yanzuoguang.util.exception.CodeException
;
import
com.yanzuoguang.util.exception.ExceptionHelper
;
import
java.io.UnsupportedEncodingException
;
import
java.io.UnsupportedEncodingException
;
import
java.security.MessageDigest
;
import
java.security.MessageDigest
;
...
@@ -9,8 +10,6 @@ import java.util.Arrays;
...
@@ -9,8 +10,6 @@ import java.util.Arrays;
import
java.util.Date
;
import
java.util.Date
;
import
java.util.List
;
import
java.util.List
;
import
java.util.UUID
;
import
java.util.UUID
;
import
java.util.regex.Matcher
;
import
java.util.regex.Pattern
;
/**
/**
* 字符串帮主类
* 字符串帮主类
...
...
yzg-util-base/src/main/java/com/yanzuoguang/util/thread/RunPlan.java
View file @
2110a4da
package
com
.
yanzuoguang
.
util
.
thread
;
package
com
.
yanzuoguang
.
util
.
thread
;
import
com.yanzuoguang.util.helper.Event
;
import
com.yanzuoguang.util.helper.Event
;
import
com.yanzuoguang.util.
helper
.ExceptionHelper
;
import
com.yanzuoguang.util.
exception
.ExceptionHelper
;
import
com.yanzuoguang.util.helper.StringHelper
;
import
com.yanzuoguang.util.helper.StringHelper
;
import
java.util.ArrayList
;
import
java.util.ArrayList
;
...
...
yzg-util-base/src/main/java/com/yanzuoguang/util/thread/ThreadHelper.java
View file @
2110a4da
package
com
.
yanzuoguang
.
util
.
thread
;
package
com
.
yanzuoguang
.
util
.
thread
;
import
com.yanzuoguang.util.helper.DateHelper
;
import
com.yanzuoguang.util.helper.DateHelper
;
import
com.yanzuoguang.util.
helper
.ExceptionHelper
;
import
com.yanzuoguang.util.
exception
.ExceptionHelper
;
import
java.util.Date
;
import
java.util.Date
;
import
java.util.concurrent.ExecutorService
;
import
java.util.concurrent.ExecutorService
;
...
...
yzg-util-base/src/main/java/com/yanzuoguang/util/thread/ThreadNext.java
0 → 100644
View file @
2110a4da
package
com
.
yanzuoguang
.
util
.
thread
;
import
com.yanzuoguang.util.helper.StringHelper
;
import
com.yanzuoguang.util.log.Log
;
/**
* 线程执行类
*/
public
class
ThreadNext
{
public
interface
Next
{
/**
* 执行下一个函数,出现异常会继续执行
*
* @return 是否继续执行
*/
boolean
next
()
throws
Exception
;
/**
* 沉睡时间
*
* @return
*/
int
getNextTime
();
}
public
static
void
start
(
Next
next
,
String
error
)
{
if
(
next
==
null
)
{
return
;
}
new
Thread
(
new
Runnable
()
{
@Override
public
void
run
()
{
boolean
isExecute
=
true
;
Exception
frontEx
=
null
;
while
(
isExecute
)
{
try
{
isExecute
=
next
.
next
();
}
catch
(
Exception
ex
)
{
if
(
frontEx
==
null
||
!(
frontEx
.
getClass
()
==
ex
.
getClass
()
&&
StringHelper
.
compare
(
frontEx
.
getMessage
(),
ex
.
getMessage
())))
{
Log
.
error
(
ThreadNext
.
class
,
ex
);
}
frontEx
=
ex
;
}
if
(
isExecute
)
{
try
{
int
max
=
Math
.
max
(
next
.
getNextTime
(),
100
);
Thread
.
sleep
(
max
);
}
catch
(
InterruptedException
e
)
{
e
.
printStackTrace
();
}
}
else
{
break
;
}
}
}
}).
start
();
}
}
yzg-util-base/src/main/java/com/yanzuoguang/util/vo/LogVo.java
0 → 100644
View file @
2110a4da
package
com
.
yanzuoguang
.
util
.
vo
;
/**
* 日志实例
*/
public
class
LogVo
extends
BaseReqVo
{
private
static
final
long
serialVersionUID
=
-
8629960247077620458L
;
/**
* 日志id
*/
private
String
logId
;
/**
* 工程实例名
*/
private
String
logSources
;
/**
* 请求接口地址
*/
private
String
interUrl
;
/**
* 请求内容
*/
private
String
content
;
/**
* 返回参数
*/
private
String
result
;
/**
* 接口处理状态,是否有异常
*/
private
int
status
;
/**
* 创建时间
*/
private
String
createDate
;
public
String
getLogId
()
{
return
logId
;
}
public
void
setLogId
(
String
logId
)
{
this
.
logId
=
logId
;
}
public
String
getLogSources
()
{
return
logSources
;
}
public
void
setLogSources
(
String
logSources
)
{
this
.
logSources
=
logSources
;
}
public
String
getInterUrl
()
{
return
interUrl
;
}
public
void
setInterUrl
(
String
interUrl
)
{
this
.
interUrl
=
interUrl
;
}
public
String
getContent
()
{
return
content
;
}
public
void
setContent
(
String
content
)
{
this
.
content
=
content
;
}
public
String
getResult
()
{
return
result
;
}
public
void
setResult
(
String
result
)
{
this
.
result
=
result
;
}
public
int
getStatus
()
{
return
status
;
}
public
void
setStatus
(
int
status
)
{
this
.
status
=
status
;
}
public
String
getCreateDate
()
{
return
createDate
;
}
public
void
setCreateDate
(
String
createDate
)
{
this
.
createDate
=
createDate
;
}
}
yzg-util-cloud/pom.xml
View file @
2110a4da
...
@@ -23,6 +23,66 @@
...
@@ -23,6 +23,66 @@
</properties>
</properties>
<dependencies>
<dependency>
<groupId>
junit
</groupId>
<artifactId>
junit
</artifactId>
<scope>
test
</scope>
</dependency>
<!-- 感知服务器端配置变化 -->
<dependency>
<groupId>
org.springframework.boot
</groupId>
<artifactId>
spring-boot-starter-actuator
</artifactId>
</dependency>
<dependency>
<groupId>
org.springframework.boot
</groupId>
<artifactId>
spring-boot-starter-aop
</artifactId>
</dependency>
<dependency>
<groupId>
org.springframework.boot
</groupId>
<artifactId>
spring-boot-starter-web
</artifactId>
</dependency>
<dependency>
<groupId>
org.springframework.cloud
</groupId>
<artifactId>
spring-cloud-starter-config
</artifactId>
</dependency>
<dependency>
<groupId>
org.springframework.cloud
</groupId>
<artifactId>
spring-cloud-starter-netflix-eureka-client
</artifactId>
</dependency>
<dependency>
<groupId>
org.springframework.cloud
</groupId>
<artifactId>
spring-cloud-starter-openfeign
</artifactId>
</dependency>
<dependency>
<groupId>
org.springframework.boot
</groupId>
<artifactId>
spring-boot-devtools
</artifactId>
<optional>
true
</optional>
</dependency>
<!-- HTTP请求组件 -->
<dependency>
<groupId>
org.apache.httpcomponents
</groupId>
<artifactId>
httpclient
</artifactId>
</dependency>
<dependency>
<groupId>
org.apache.httpcomponents
</groupId>
<artifactId>
httpmime
</artifactId>
</dependency>
<dependency>
<groupId>
${project.groupId}
</groupId>
<artifactId>
yzg-util-base
</artifactId>
<version>
${project.version}
</version>
</dependency>
</dependencies>
<dependencyManagement>
<dependencyManagement>
<dependencies>
<dependencies>
<dependency>
<dependency>
...
...
yzg-util-cloud/src/main/java/com/yanzuoguang/cloud/aop/AbstractValidateAspect.java
0 → 100644
View file @
2110a4da
package
com
.
yanzuoguang
.
cloud
.
aop
;
import
org.springframework.web.context.request.RequestContextHolder
;
import
org.springframework.web.context.request.ServletRequestAttributes
;
import
javax.servlet.http.HttpServletRequest
;
/**
* 虚拟拦截类
*/
public
abstract
class
AbstractValidateAspect
{
/**
* 获取 http request
*
* @return
*/
protected
static
HttpServletRequest
getRequest
()
{
return
((
ServletRequestAttributes
)
RequestContextHolder
.
getRequestAttributes
()).
getRequest
();
}
/**
* 获取 http request url
*
* @return
*/
protected
static
String
getHttpRequestUrl
()
{
return
getRequest
().
getServletPath
();
}
}
yzg-util-cloud/src/main/java/com/yanzuoguang/cloud/aop/FeignAspect.java
View file @
2110a4da
//
package com.yanzuoguang.cloud.aop;
package
com
.
yanzuoguang
.
cloud
.
aop
;
//
//import com.tourbida.sys.common.AbstractValidateAspect
;
import
com.yanzuoguang.util.contants.ResultConstants
;
//import com.tourbida.sys
.exception.CodeException;
import
com.yanzuoguang.util
.exception.CodeException
;
//import com.tourbida.sys.vo.ResponseResult
;
import
com.yanzuoguang.util.exception.ExceptionHelper
;
//import com.tourbida.sys.vo.ResponseResultBuilder
;
import
com.yanzuoguang.util.vo.ResponseResult
;
//import com.tourbida.sys.web.BaseController
;
import
org.aspectj.lang.ProceedingJoinPoint
;
//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.Aspec
t;
import
org.aspectj.lang.annotation.Pointcu
t
;
//import org.aspectj.lang.annotation.Pointcut
;
import
org.slf4j.Logger
;
//import org.slf4j.Logger
;
import
org.slf4j.LoggerFactory
;
//import org.slf4j.LoggerFactory
;
import
org.springframework.stereotype.Component
;
//import org.springframework.stereotype.Component;
/
/
/
**
///**
* 接口切面,用于验证接口回调返回参数
// * 接口切面,用于验证接口回调返回参数
*/
// */
@Aspect
//@Aspec
t
@Componen
t
//@Component
public
class
FeignAspect
extends
AbstractValidateAspect
{
//public class FeignAspect extends AbstractValidateAspect {
//
private
static
final
Logger
logger
=
LoggerFactory
.
getLogger
(
FeignAspect
.
class
);
// private static final Logger logger = LoggerFactory.getLogger(FeignAspect.class);
//
/**
// /**
* AOP的表达式
// * AOP的表达式
*/
// */
@Pointcut
(
"execution(* *..feign..*(..))"
)
// @Pointcut("execution(* com.panding..feign..*(..))")
public
void
feignAspect
()
{
// public void feignAspect() {
}
// }
//
/**
// /**
* 执行环形切面
// * 执行环形切面
*
// *
* @param joinPoint
// * @param joinPoint
* @return
// * @return
*/
// */
@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
();
// String name = joinPoint.getSignature().getName()
;
Object
result
=
null
;
// Object result = null
;
long
start
=
System
.
currentTimeMillis
()
;
// long start = System.currentTimeMillis();
try
{
// try {
logger
.
info
(
"=================[ {} ] feign params is {} ================="
,
name
,
joinPoint
.
getArgs
());
// logger.info("=================[ {} ] feign params is {} =================", name, joinPoint.getArgs()
);
result
=
joinPoint
.
proceed
(
);
// result = joinPoint.proceed();
//
// 假如是标准格式,则验证接口是否成功,不成功则抛出异常
// // 假如是标准格式,则验证接口是否成功,不成功则抛出异常
if
(
result
instanceof
ResponseResult
)
{
// if (result instanceof ResponseResult) {
ResponseResult
responseResult
=
(
ResponseResult
)
result
;
// ResponseResult responseResult = (ResponseResult) result;
if
(!
ResultConstants
.
SUCCESS
.
equals
(
responseResult
.
getCode
()))
{
// if (!ResponseResultBuilder.SUCCESS.equals(responseResult.getCode())) {
throw
new
CodeException
(
responseResult
.
getCode
(),
responseResult
.
getMessage
());
// throw new CodeException(responseResult.getCode(), responseResult.getMessage());
}
//
}
}
// }
return
result
;
// return result;
}
catch
(
CodeException
ex
)
{
// } catch (CodeException ex) {
logger
.
info
(
"=================[ {} ] feign is error {} ================="
,
name
,
ex
);
// logger.info("=================[ {} ] feign is error {} =================", name, ex
);
result
=
new
ResponseResult
(
ex
.
getCode
(),
ex
.
getMessage
()
);
// result = new ResponseResult(ex.getCode(), ex.getMessage())
;
throw
ex
;
// throw ex;
}
catch
(
Exception
e
)
{
// } catch (Exception e) {
ResponseResult
responseResult
=
ExceptionHelper
.
getError
(
e
);
// ResponseResult responseResult = BaseController.getError(logger,
"=================[ " + name + " ] feign is error {} =================", e);
logger
.
error
(
"=================[ "
+
name
+
" ] feign is error {} ================="
,
e
);
//
result = responseResult;
result
=
responseResult
;
//
throw new CodeException(responseResult.getCode(), responseResult.getMessage());
throw
new
CodeException
(
responseResult
.
getCode
(),
responseResult
.
getMessage
());
//
} finally {
}
finally
{
//
// 记录服务调用时间,请求日志
// 记录服务调用时间,请求日志
//
long end = System.currentTimeMillis();
long
end
=
System
.
currentTimeMillis
();
//
logger.info("=================[ {} ] feign time ({})ms, result is {} =================", name, (end - start), result);
logger
.
info
(
"=================[ {} ] feign time ({})ms, result is {} ================="
,
name
,
(
end
-
start
),
result
);
//
}
}
//
}
}
//
//
//
}
}
yzg-util-cloud/src/main/java/com/yanzuoguang/cloud/aop/LogFeign.java
0 → 100644
View file @
2110a4da
package
com
.
yanzuoguang
.
cloud
.
aop
;
import
com.yanzuoguang.util.vo.LogVo
;
import
com.yanzuoguang.util.vo.ResponseResult
;
/**
* 调用外置服务保存日志对象
*/
public
interface
LogFeign
{
/**
* 保存日志对象
*
* @param log 需要保存的日志对象
*/
ResponseResult
<
String
>
save
(
LogVo
log
);
}
yzg-util-cloud/src/main/java/com/yanzuoguang/cloud/aop/RequestCacheResult.java
View file @
2110a4da
//
package com.yanzuoguang.cloud.aop;
package
com
.
yanzuoguang
.
cloud
.
aop
;
//
//
import java.util.Date;
import
java.util.Date
;
//
/
//
**
/**
//
* 请求缓存的数据
* 请求缓存的数据
//
*/
*/
//
public class RequestCacheResult {
public
class
RequestCacheResult
{
//
//
/**
/**
//
* 构造函数
* 构造函数
//
*/
*/
//
public RequestCacheResult(String reqID) {
public
RequestCacheResult
(
String
reqID
)
{
//
this.date = new Date();
this
.
date
=
new
Date
();
//
this.reqID = reqID;
this
.
reqID
=
reqID
;
//
this.result = null;
this
.
result
=
null
;
//
}
}
//
//
/**
/**
//
* 请求编号
* 请求编号
//
*/
*/
//
private String reqID;
private
String
reqID
;
//
//
/**
/**
//
* 结果
* 结果
//
*/
*/
//
private Object result;
private
Object
result
;
//
//
/**
/**
//
* 时间
* 时间
//
*/
*/
//
private Date date;
private
Date
date
;
//
//
/**
/**
//
* 获取毫秒数
* 获取毫秒数
//
*
*
//
* @return
* @return
//
*/
*/
//
public long getMillsecond() {
public
long
getMillsecond
()
{
//
return new Date().getTime() - date.getTime();
return
new
Date
().
getTime
()
-
date
.
getTime
();
//
}
}
//
//
/**
/**
//
* 获取结果
* 获取结果
//
*
*
//
* @return
* @return
//
*/
*/
//
public Object getResult() {
public
Object
getResult
()
{
//
return result;
return
result
;
//
}
}
//
//
/**
/**
//
* 返回结果
* 返回结果
//
*
*
//
* @param result
* @param result
//
*/
*/
//
public void setResult(Object result) {
public
void
setResult
(
Object
result
)
{
//
this.date = new Date();
this
.
date
=
new
Date
();
//
this.result = result;
this
.
result
=
result
;
//
}
}
//
}
}
yzg-util-cloud/src/main/java/com/yanzuoguang/cloud/aop/WebAspect.java
View file @
2110a4da
//package com.yanzuoguang.cloud.aop;
package
com
.
yanzuoguang
.
cloud
.
aop
;
//
//import com.alibaba.fastjson.JSON;
import
com.alibaba.fastjson.JSON
;
//import com.tourbida.sys.common.AbstractValidateAspect;
import
com.yanzuoguang.util.cache.MemoryCache
;
//import com.tourbida.sys.common.utils.ThreadNext;
import
com.yanzuoguang.util.contants.ResultConstants
;
//import com.tourbida.sys.core.Config;
import
com.yanzuoguang.util.exception.ExceptionHelper
;
//import com.tourbida.sys.core.cache.LocalCacheData;
import
com.yanzuoguang.util.helper.JSONHelper
;
//import com.tourbida.sys.core.util.JSONHelper;
import
com.yanzuoguang.util.helper.StringHelper
;
//import com.tourbida.sys.core.util.Log;
import
com.yanzuoguang.util.log.Log
;
//import com.tourbida.sys.core.util.ObjectHelper;
import
com.yanzuoguang.util.obj.ObjectHelper
;
//import com.tourbida.sys.core.util.StringHelper;
import
com.yanzuoguang.util.thread.ThreadNext
;
//import com.tourbida.sys.feign.LogFeign;
import
com.yanzuoguang.util.vo.LogVo
;
//import com.tourbida.sys.vo.LogVo;
import
com.yanzuoguang.util.vo.ResponseResult
;
//import com.tourbida.sys.vo.ResponseResult;
import
org.aspectj.lang.ProceedingJoinPoint
;
//import com.tourbida.sys.vo.ResponseResultBuilder;
import
org.aspectj.lang.Signature
;
//import com.tourbida.sys.web.BaseController;
import
org.aspectj.lang.annotation.Around
;
//import org.aspectj.lang.ProceedingJoinPoint;
import
org.aspectj.lang.annotation.Aspect
;
//import org.aspectj.lang.Signature;
import
org.aspectj.lang.annotation.Pointcut
;
//import org.aspectj.lang.annotation.Around;
import
org.aspectj.lang.reflect.MethodSignature
;
//import org.aspectj.lang.annotation.Aspect;
import
org.slf4j.Logger
;
//import org.aspectj.lang.annotation.Pointcut;
import
org.slf4j.LoggerFactory
;
//import org.aspectj.lang.reflect.MethodSignature;
import
org.springframework.beans.factory.annotation.Autowired
;
//import org.slf4j.Logger;
import
org.springframework.beans.factory.annotation.Value
;
//import org.slf4j.LoggerFactory;
import
org.springframework.context.ApplicationContext
;
//import org.springframework.beans.factory.annotation.Value;
import
org.springframework.stereotype.Component
;
//import org.springframework.stereotype.Component;
//
import
javax.servlet.ServletRequest
;
//import javax.servlet.ServletRequest;
import
javax.servlet.ServletResponse
;
//import javax.servlet.ServletResponse;
import
java.lang.reflect.Method
;
//import java.lang.reflect.Method;
import
java.lang.reflect.Type
;
//import java.lang.reflect.Type;
import
java.util.concurrent.LinkedBlockingQueue
;
//import java.util.concurrent.LinkedBlockingQueue;
//
/**
///**
* LogsAspect(接口请求日志切面)
// * LogsAspect(接口请求日志切面)
*
// *
* @author: Kang
// * @author: Kang
* @time: 2018年04月25日 11:43
// * @time: 2018年04月25日 11:43
*/
// */
@Aspect
//@Aspect
@Component
//@Component
public
class
WebAspect
extends
AbstractValidateAspect
implements
ThreadNext
.
Next
{
//public class WebAspect extends AbstractValidateAspect implements ThreadNext.Next {
//
private
static
final
Logger
logger
=
LoggerFactory
.
getLogger
(
WebAspect
.
class
);
// private static final Logger logger = LoggerFactory.getLogger(WebAspect.class);
//
@Value
(
"${spring.application.name}"
)
// @Value("${spring.application.name}")
private
String
applicationName
;
// private String applicationName;
//
@Value
(
"${yzg.logAll:false}"
)
// @Value("${config.logAll:false}")
private
boolean
logAll
=
false
;
// private boolean logAll = false;
//
@Value
(
"${yzg.cacheTime:120}"
)
// @Value("${config.cacheTime:120}")
private
int
cacheTime
=
120
;
// private int cacheTime = 120;
//
@Autowired
// private LogFeign logInterFeign;
private
ApplicationContext
context
;
//
// /**
private
LogFeign
logFeign
;
// * 缓存队列
// */
/**
// private volatile LinkedBlockingQueue<LogVo> cache = new LinkedBlockingQueue<LogVo>();
* 缓存队列
//
*/
// // 缓存的每次请求的结果
private
volatile
LinkedBlockingQueue
<
LogVo
>
cache
=
new
LinkedBlockingQueue
<
LogVo
>();
// private LocalCacheData<RequestCacheResult> _cacheResult = new LocalCacheData<RequestCacheResult>(cacheTime);
//
// 缓存的每次请求的结果
// public WebAspect() {
private
MemoryCache
<
RequestCacheResult
>
_cacheResult
=
new
MemoryCache
<>(
cacheTime
);
// ThreadNext.start(logger, this, "save log error");
// }
public
WebAspect
()
{
//
ThreadNext
.
start
(
this
,
"save log error"
);
// /**
}
// * exec aop point aspect
// */
/**
// @Pointcut("execution(* com.tourbida..web..*(..))")
* exec aop point aspect
// public void webAspect() {
*/
// }
@Pointcut
(
"execution(* *..web..*(..))"
)
//
public
void
webAspect
()
{
// /**
}
// * 获取返回的至类型
// *
/**
// * @param joinPoint
* 获取返回的至类型
// * @return
*
// * @throws NoSuchMethodException
* @param joinPoint
// */
* @return
// private Type getReturnType(ProceedingJoinPoint joinPoint) {
* @throws NoSuchMethodException
// //获取返回值类型
*/
// Signature s = joinPoint.getSignature();
private
Type
getReturnType
(
ProceedingJoinPoint
joinPoint
)
{
// MethodSignature ms = (MethodSignature) s;
//获取返回值类型
// Method m = ms.getMethod();
Signature
s
=
joinPoint
.
getSignature
();
// Type t = m.getAnnotatedReturnType().getType();
MethodSignature
ms
=
(
MethodSignature
)
s
;
// return t;
Method
m
=
ms
.
getMethod
();
// }
Type
t
=
m
.
getAnnotatedReturnType
().
getType
();
//
return
t
;
// /**
}
// * 执行环形切面
// *
/**
// * @param joinPoint
* 执行环形切面
// * @return
*
// */
* @param joinPoint
// @Around(value = "webAspect()")
* @return
// public Object requestWebAround(ProceedingJoinPoint joinPoint) throws Throwable {
*/
// Log.threadBegin();
@Around
(
value
=
"webAspect()"
)
// ResponseResult responseResult = null; // 用户数据库记录
public
Object
requestWebAround
(
ProceedingJoinPoint
joinPoint
)
throws
Throwable
{
// long start = System.currentTimeMillis();
Log
.
threadBegin
();
// String name = joinPoint.getSignature().getName();
ResponseResult
responseResult
=
null
;
// 用户数据库记录
// try {
long
start
=
System
.
currentTimeMillis
();
// logger.info("=================[ {} ] request params is {} =================", name, joinPoint.getArgs());
String
name
=
joinPoint
.
getSignature
().
getName
();
// Object result = executeMethod(joinPoint, name);
try
{
// if (result instanceof ResponseResult) {
logger
.
info
(
"=================[ {} ] request params is {} ================="
,
name
,
joinPoint
.
getArgs
());
// responseResult = (ResponseResult) result;
Object
result
=
executeMethod
(
joinPoint
,
name
);
// } else {
if
(
result
instanceof
ResponseResult
)
{
// responseResult = ResponseResult.result(result);
responseResult
=
(
ResponseResult
)
result
;
// }
}
else
{
// return result;
responseResult
=
ResponseResult
.
result
(
result
);
// } catch (Exception e) {
}
// responseResult = BaseController.getError(logger, "=================[ " + name + " ] is error {} =================", e);
return
result
;
// if (getReturnType(joinPoint).getTypeName().indexOf(ResponseResult.class.getName()) > -1) {
}
catch
(
Exception
e
)
{
// return responseResult;
logger
.
error
(
"=================[ "
+
name
+
" ] is error {} ================="
,
e
);
// } else {
// throw e;
responseResult
=
ExceptionHelper
.
getError
(
e
);
// }
if
(
getReturnType
(
joinPoint
).
getTypeName
().
indexOf
(
ResponseResult
.
class
.
getName
())
>
-
1
)
{
// } finally {
return
responseResult
;
// long end = System.currentTimeMillis();
}
else
{
// Log.threadCommit();
throw
e
;
// saveInterLogs(joinPoint, responseResult);
}
// logger.info("=================[ {} ] time ({})ms, result is {} =================", name, (end - start), responseResult);
}
finally
{
// }
long
end
=
System
.
currentTimeMillis
();
// }
Log
.
threadCommit
();
//
saveInterLogs
(
joinPoint
,
responseResult
);
// private Object executeMethod(ProceedingJoinPoint joinPoint, String name) throws Throwable {
logger
.
info
(
"=================[ {} ] time ({})ms, result is {} ================="
,
name
,
(
end
-
start
),
responseResult
);
// if (joinPoint.getArgs().length != 1
}
// || joinPoint.getArgs().length == 1 &&
}
// (joinPoint.getArgs()[0] instanceof ServletResponse || joinPoint.getArgs()[0] instanceof ServletRequest)
// ) {
private
Object
executeMethod
(
ProceedingJoinPoint
joinPoint
,
String
name
)
throws
Throwable
{
// return joinPoint.proceed();
if
(
joinPoint
.
getArgs
().
length
!=
1
// } else {
||
joinPoint
.
getArgs
().
length
==
1
&&
// // 获取请求编号
(
joinPoint
.
getArgs
()[
0
]
instanceof
ServletResponse
||
joinPoint
.
getArgs
()[
0
]
instanceof
ServletRequest
)
// Object firstArgs = joinPoint.getArgs().length > 0 ? joinPoint.getArgs()[0] : null;
)
{
// String reqId = StringHelper.GetLastString(
return
joinPoint
.
proceed
();
// ObjectHelper.GetString(firstArgs, "__req"),
}
else
{
// ObjectHelper.GetString(firstArgs, "reqId"),
// 获取请求编号
// ObjectHelper.GetString(firstArgs, "repeatCode"),
Object
firstArgs
=
joinPoint
.
getArgs
().
length
>
0
?
joinPoint
.
getArgs
()[
0
]
:
null
;
// ObjectHelper.GetString(firstArgs, "thirdOrderId")
String
reqId
=
ObjectHelper
.
getString
(
firstArgs
,
"reqId"
);
// );
if
(
StringHelper
.
isEmpty
(
reqId
))
{
// if (StringHelper.IsEmpty(reqId)) {
reqId
=
StringHelper
.
md5
(
JSONHelper
.
serialize
(
firstArgs
));
// reqId = StringHelper.md5(JSONHelper.SerializeObject(firstArgs));
}
else
{
// } else {
// 请求编号和公司编号挂钩
// // 请求编号和公司编号挂钩
reqId
=
StringHelper
.
getId
(
ObjectHelper
.
getString
(
firstArgs
,
"companyId"
),
reqId
);
// reqId = StringHelper.GetID(ObjectHelper.GetString(firstArgs, "companyId"), reqId);
}
// }
String
reqFull
=
StringHelper
.
getId
(
reqId
,
getHttpRequestUrl
());
// String reqFull = StringHelper.GetID(reqId, getHttpRequestUrl());
RequestCacheResult
req
=
_cacheResult
.
get
(
reqFull
,
new
RequestCacheResult
(
reqFull
));
// RequestCacheResult req = _cacheResult.get(reqFull, new RequestCacheResult(reqFull));
// 缓存的键值对
// // 缓存的键值对
if
(
req
.
getResult
()
==
null
)
{
// if (req.getResult() == null) {
synchronized
(
req
)
{
// synchronized (req) {
if
(
req
.
getResult
()
==
null
)
{
// if (req.getResult() == null) {
try
{
// try {
req
.
setResult
(
joinPoint
.
proceed
());
// req.setResult(joinPoint.proceed());
}
catch
(
Exception
ex
)
{
// } catch (Exception ex) {
req
.
setResult
(
ex
);
// req.setResult(ex);
}
// }
}
// }
}
// }
}
// }
Object
result
=
req
.
getResult
();
// Object result = req.getResult();
if
(
result
instanceof
Throwable
)
{
// if (result instanceof Throwable) {
throw
(
Throwable
)
result
;
// throw (Throwable) result;
}
// }
return
result
;
// return result;
}
// }
}
// }
//
/**
// /**
* deal with inter api logs
// * deal with inter api logs
*
// *
* @param joinPoint 请求参数
// * @param joinPoint 请求参数
* @param responseResult 返回参数
// * @param responseResult 返回参数
*/
// */
private
void
saveInterLogs
(
ProceedingJoinPoint
joinPoint
,
ResponseResult
responseResult
)
{
// private void saveInterLogs(ProceedingJoinPoint joinPoint, ResponseResult responseResult) {
// 日志请求不记录,防止死循环递归
// // 日志请求不记录,防止死循环递归
if
(
applicationName
.
indexOf
(
"log"
)
>=
0
)
{
// if (applicationName.indexOf("log") >= 0) {
return
;
// return;
}
// }
// 正常请求不记录
// // 正常请求不记录
if
(!
logAll
&&
responseResult
.
getCode
()
==
ResultConstants
.
SUCCESS
)
{
// if (!logAll && responseResult.getCode() == ResponseResultBuilder.SUCCESS) {
return
;
// return;
}
// }
LogVo
logVo
=
initLogInterVo
(
getHttpRequestUrl
(),
joinPoint
,
responseResult
);
// LogVo logVo = initLogInterVo(getHttpRequestUrl(), joinPoint, responseResult);
addLog
(
logVo
);
// addLog(logVo);
}
// }
//
/**
// /**
* 添加日志到缓存中,并不是立即添加,而是通过线程执行,防止对环境造成影响
// * 添加日志到缓存中,并不是立即添加,而是通过线程执行,防止对环境造成影响
*
// *
* @param logVo
// * @param logVo
*/
// */
public
void
addLog
(
LogVo
logVo
)
{
// public void addLog(LogVo logVo) {
cache
.
add
(
logVo
);
// cache.add(logVo);
}
// }
//
/**
// /**
* 初始化日志Vo
// * 初始化日志Vo
*
// *
* @param url 请求路径
// * @param url 请求路径
* @param joinPoint 请求参数
// * @param joinPoint 请求参数
* @param responseResult 返回参数
// * @param responseResult 返回参数
* @return
// * @return
*/
// */
private
LogVo
initLogInterVo
(
String
url
,
ProceedingJoinPoint
joinPoint
,
ResponseResult
responseResult
)
{
// private LogVo initLogInterVo(String url, ProceedingJoinPoint joinPoint, ResponseResult responseResult) {
LogVo
logInterVo
=
new
LogVo
();
// LogVo logInterVo = new LogVo();
logInterVo
.
setLogId
(
StringHelper
.
getNewID
());
// logInterVo.setLogId(StringHelper.GetNewID());
logInterVo
.
setLogSources
(
applicationName
);
//平台名
// logInterVo.setLogSources(applicationName);//平台名
logInterVo
.
setInterUrl
(
url
);
//请求URL
// logInterVo.setInterUrl(url);//请求URL
Object
para
=
joinPoint
.
getArgs
();
// Object para = joinPoint.getArgs();
if
(
joinPoint
.
getArgs
().
length
==
1
)
{
// if (joinPoint.getArgs().length == 1) {
para
=
joinPoint
.
getArgs
()[
0
];
// para = joinPoint.getArgs()[0];
}
// }
logInterVo
.
setContent
(
JSON
.
toJSONString
(
para
));
//请求参数
// logInterVo.setContent(JSON.toJSONString(para));//请求参数
logInterVo
.
setResult
(
JSON
.
toJSONString
(
responseResult
));
//返回参数
// logInterVo.setResult(JSON.toJSONString(responseResult));//返回参数
logInterVo
.
setStatus
(
responseResult
.
getCode
()
==
ResultConstants
.
SUCCESS
?
1
:
0
);
// logInterVo.setStatus(responseResult.getCode() == ResponseResultBuilder.SUCCESS ? 1 : 0);
return
logInterVo
;
// return logInterVo;
}
// }
//
/**
// /**
* 执行下一个函数,出现异常会继续执行
// * 执行下一个函数,出现异常会继续执行
*
// *
* @return 是否继续执行
// * @return 是否继续执行
*/
// */
@Override
// @Override
public
boolean
next
()
throws
Exception
{
// public boolean next() throws Exception {
while
(
cache
.
size
()
>
0
)
{
// while (cache.size() > 0) {
LogVo
item
=
cache
.
poll
();
// LogVo item = cache.poll();
if
(
item
!=
null
)
{
// if (item != null) {
if
(
logFeign
==
null
)
{
// if (logInterFeign == null) {
logFeign
=
context
.
getBean
(
LogFeign
.
class
);
// logInterFeign = Config.Context.getBean(LogFeign.class);
}
// }
logFeign
.
save
(
item
);
// logInterFeign.saveInterLogs(item);
}
// }
}
// }
return
true
;
// return true;
}
// }
//
/**
// /**
* 沉睡时间
// * 沉睡时间
*
// *
* @return
// * @return
*/
// */
@Override
// @Override
public
int
getNextTime
()
{
// public int getNextTime() {
return
1000
;
// return 1000;
}
// }
}
//}
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