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
Hide 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 {
String
exception
=
local
+
"/n"
+
"开始:"
+
start
+
" 结束:"
+
end
+
"总长:"
+
allsize
+
"/n"
+
str
;
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
;
import
com.yanzuoguang.util.exception.CodeException
;
import
com.yanzuoguang.util.exception.ExceptionHelper
;
import
java.sql.Timestamp
;
import
java.text.ParseException
;
...
...
yzg-util-base/src/main/java/com/yanzuoguang/util/helper/EnumHelper.java
View file @
2110a4da
package
com
.
yanzuoguang
.
util
.
helper
;
import
com.yanzuoguang.util.exception.ExceptionHelper
;
import
java.lang.reflect.Method
;
import
java.util.HashMap
;
...
...
yzg-util-base/src/main/java/com/yanzuoguang/util/helper/StringHelper.java
View file @
2110a4da
package
com
.
yanzuoguang
.
util
.
helper
;
import
com.yanzuoguang.util.exception.CodeException
;
import
com.yanzuoguang.util.exception.ExceptionHelper
;
import
java.io.UnsupportedEncodingException
;
import
java.security.MessageDigest
;
...
...
@@ -9,8 +10,6 @@ import java.util.Arrays;
import
java.util.Date
;
import
java.util.List
;
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
;
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
java.util.ArrayList
;
...
...
yzg-util-base/src/main/java/com/yanzuoguang/util/thread/ThreadHelper.java
View file @
2110a4da
package
com
.
yanzuoguang
.
util
.
thread
;
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.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 @@
</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>
<dependencies>
<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;
//
//import com.tourbida.sys.common.AbstractValidateAspect
;
//import com.tourbida.sys
.exception.CodeException;
//import com.tourbida.sys.vo.ResponseResult
;
//import com.tourbida.sys.vo.ResponseResultBuilder
;
//import com.tourbida.sys.web.BaseController
;
//import org.aspectj.lang.ProceedingJoinPoint
;
//import org.aspectj.lang.annotation.Around
;
//import org.aspectj.lang.annotation.Aspec
t;
//import org.aspectj.lang.annotation.Pointcut
;
//import org.slf4j.Logger
;
//import org.slf4j.LoggerFactory
;
//import org.springframework.stereotype.Component;
/
/
///**
// * 接口切面,用于验证接口回调返回参数
// */
//@Aspec
t
//@Component
//public class FeignAspect extends AbstractValidateAspect {
//
// private static final Logger logger = LoggerFactory.getLogger(FeignAspect.class);
//
// /**
// * AOP的表达式
// */
// @Pointcut("execution(* com.panding..feign..*(..))")
// public void feignAspect() {
// }
//
// /**
// * 执行环形切面
// *
// * @param joinPoint
// * @return
// */
// @Around(value = "feignAspect()")
// public Object requestFeignAround(ProceedingJoinPoint joinPoint) throws Throwable {
// String name = joinPoint.getSignature().getName()
;
// Object result = null
;
// long start = System.currentTimeMillis();
// try {
// logger.info("=================[ {} ] feign params is {} =================", name, joinPoint.getArgs()
);
// result = joinPoint.proceed();
//
// // 假如是标准格式,则验证接口是否成功,不成功则抛出异常
// if (result instanceof ResponseResult) {
// ResponseResult responseResult = (ResponseResult) result;
// if (!ResponseResultBuilder.SUCCESS.equals(responseResult.getCode())) {
// throw new CodeException(responseResult.getCode(), responseResult.getMessage());
//
}
// }
// return result;
// } catch (CodeException ex) {
// logger.info("=================[ {} ] feign is error {} =================", name, ex
);
// result = new ResponseResult(ex.getCode(), ex.getMessage())
;
// throw ex;
// } catch (Exception e) {
// ResponseResult responseResult = BaseController.getError(logger,
"=================[ " + name + " ] feign is error {} =================", e);
//
result = responseResult;
//
throw new CodeException(responseResult.getCode(), responseResult.getMessage());
//
} finally {
//
// 记录服务调用时间,请求日志
//
long end = System.currentTimeMillis();
//
logger.info("=================[ {} ] feign time ({})ms, result is {} =================", name, (end - start), result);
//
}
//
}
//
//
//
}
package
com
.
yanzuoguang
.
cloud
.
aop
;
import
com.yanzuoguang.util.contants.ResultConstants
;
import
com.yanzuoguang.util
.exception.CodeException
;
import
com.yanzuoguang.util.exception.ExceptionHelper
;
import
com.yanzuoguang.util.vo.ResponseResult
;
import
org.aspectj.lang.ProceedingJoinPoint
;
import
org.aspectj.lang.annotation.Around
;
import
org.aspectj.lang.annotation.Aspect
;
import
org.aspectj.lang.annotation.Pointcu
t
;
import
org.slf4j.Logger
;
import
org.slf4j.LoggerFactory
;
import
org.springframework.stereotype.Component
;
/
**
* 接口切面,用于验证接口回调返回参数
*/
@Aspect
@Componen
t
public
class
FeignAspect
extends
AbstractValidateAspect
{
private
static
final
Logger
logger
=
LoggerFactory
.
getLogger
(
FeignAspect
.
class
);
/**
* AOP的表达式
*/
@Pointcut
(
"execution(* *..feign..*(..))"
)
public
void
feignAspect
()
{
}
/**
* 执行环形切面
*
* @param joinPoint
* @return
*/
@Around
(
value
=
"feignAspect()"
)
public
Object
requestFeignAround
(
ProceedingJoinPoint
joinPoint
)
throws
Throwable
{
String
name
=
joinPoint
.
getSignature
().
getName
();
Object
result
=
null
;
long
start
=
System
.
currentTimeMillis
()
;
try
{
logger
.
info
(
"=================[ {} ] feign params is {} ================="
,
name
,
joinPoint
.
getArgs
());
result
=
joinPoint
.
proceed
(
);
// 假如是标准格式,则验证接口是否成功,不成功则抛出异常
if
(
result
instanceof
ResponseResult
)
{
ResponseResult
responseResult
=
(
ResponseResult
)
result
;
if
(!
ResultConstants
.
SUCCESS
.
equals
(
responseResult
.
getCode
()))
{
throw
new
CodeException
(
responseResult
.
getCode
(),
responseResult
.
getMessage
());
}
}
return
result
;
}
catch
(
CodeException
ex
)
{
logger
.
info
(
"=================[ {} ] feign is error {} ================="
,
name
,
ex
);
result
=
new
ResponseResult
(
ex
.
getCode
(),
ex
.
getMessage
()
);
throw
ex
;
}
catch
(
Exception
e
)
{
ResponseResult
responseResult
=
ExceptionHelper
.
getError
(
e
);
logger
.
error
(
"=================[ "
+
name
+
" ] feign is error {} ================="
,
e
);
result
=
responseResult
;
throw
new
CodeException
(
responseResult
.
getCode
(),
responseResult
.
getMessage
());
}
finally
{
// 记录服务调用时间,请求日志
long
end
=
System
.
currentTimeMillis
();
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;
//
//
import java.util.Date;
//
/
//
**
//
* 请求缓存的数据
//
*/
//
public class RequestCacheResult {
//
//
/**
//
* 构造函数
//
*/
//
public RequestCacheResult(String reqID) {
//
this.date = new Date();
//
this.reqID = reqID;
//
this.result = null;
//
}
//
//
/**
//
* 请求编号
//
*/
//
private String reqID;
//
//
/**
//
* 结果
//
*/
//
private Object result;
//
//
/**
//
* 时间
//
*/
//
private Date date;
//
//
/**
//
* 获取毫秒数
//
*
//
* @return
//
*/
//
public long getMillsecond() {
//
return new Date().getTime() - date.getTime();
//
}
//
//
/**
//
* 获取结果
//
*
//
* @return
//
*/
//
public Object getResult() {
//
return result;
//
}
//
//
/**
//
* 返回结果
//
*
//
* @param result
//
*/
//
public void setResult(Object result) {
//
this.date = new Date();
//
this.result = result;
//
}
//
}
package
com
.
yanzuoguang
.
cloud
.
aop
;
import
java.util.Date
;
/**
* 请求缓存的数据
*/
public
class
RequestCacheResult
{
/**
* 构造函数
*/
public
RequestCacheResult
(
String
reqID
)
{
this
.
date
=
new
Date
();
this
.
reqID
=
reqID
;
this
.
result
=
null
;
}
/**
* 请求编号
*/
private
String
reqID
;
/**
* 结果
*/
private
Object
result
;
/**
* 时间
*/
private
Date
date
;
/**
* 获取毫秒数
*
* @return
*/
public
long
getMillsecond
()
{
return
new
Date
().
getTime
()
-
date
.
getTime
();
}
/**
* 获取结果
*
* @return
*/
public
Object
getResult
()
{
return
result
;
}
/**
* 返回结果
*
* @param result
*/
public
void
setResult
(
Object
result
)
{
this
.
date
=
new
Date
();
this
.
result
=
result
;
}
}
yzg-util-cloud/src/main/java/com/yanzuoguang/cloud/aop/WebAspect.java
View file @
2110a4da
//package com.yanzuoguang.cloud.aop;
//
//import com.alibaba.fastjson.JSON;
//import com.tourbida.sys.common.AbstractValidateAspect;
//import com.tourbida.sys.common.utils.ThreadNext;
//import com.tourbida.sys.core.Config;
//import com.tourbida.sys.core.cache.LocalCacheData;
//import com.tourbida.sys.core.util.JSONHelper;
//import com.tourbida.sys.core.util.Log;
//import com.tourbida.sys.core.util.ObjectHelper;
//import com.tourbida.sys.core.util.StringHelper;
//import com.tourbida.sys.feign.LogFeign;
//import com.tourbida.sys.vo.LogVo;
//import com.tourbida.sys.vo.ResponseResult;
//import com.tourbida.sys.vo.ResponseResultBuilder;
//import com.tourbida.sys.web.BaseController;
//import org.aspectj.lang.ProceedingJoinPoint;
//import org.aspectj.lang.Signature;
//import org.aspectj.lang.annotation.Around;
//import org.aspectj.lang.annotation.Aspect;
//import org.aspectj.lang.annotation.Pointcut;
//import org.aspectj.lang.reflect.MethodSignature;
//import org.slf4j.Logger;
//import org.slf4j.LoggerFactory;
//import org.springframework.beans.factory.annotation.Value;
//import org.springframework.stereotype.Component;
//
//import javax.servlet.ServletRequest;
//import javax.servlet.ServletResponse;
//import java.lang.reflect.Method;
//import java.lang.reflect.Type;
//import java.util.concurrent.LinkedBlockingQueue;
//
///**
// * LogsAspect(接口请求日志切面)
// *
// * @author: Kang
// * @time: 2018年04月25日 11:43
// */
//@Aspect
//@Component
//public class WebAspect extends AbstractValidateAspect implements ThreadNext.Next {
//
// private static final Logger logger = LoggerFactory.getLogger(WebAspect.class);
//
// @Value("${spring.application.name}")
// private String applicationName;
//
// @Value("${config.logAll:false}")
// private boolean logAll = false;
//
// @Value("${config.cacheTime:120}")
// private int cacheTime = 120;
//
// private LogFeign logInterFeign;
//
// /**
// * 缓存队列
// */
// private volatile LinkedBlockingQueue<LogVo> cache = new LinkedBlockingQueue<LogVo>();
//
// // 缓存的每次请求的结果
// private LocalCacheData<RequestCacheResult> _cacheResult = new LocalCacheData<RequestCacheResult>(cacheTime);
//
// public WebAspect() {
// ThreadNext.start(logger, this, "save log error");
// }
//
// /**
// * exec aop point aspect
// */
// @Pointcut("execution(* com.tourbida..web..*(..))")
// public void webAspect() {
// }
//
// /**
// * 获取返回的至类型
// *
// * @param joinPoint
// * @return
// * @throws NoSuchMethodException
// */
// private Type getReturnType(ProceedingJoinPoint joinPoint) {
// //获取返回值类型
// Signature s = joinPoint.getSignature();
// MethodSignature ms = (MethodSignature) s;
// Method m = ms.getMethod();
// Type t = m.getAnnotatedReturnType().getType();
// return t;
// }
//
// /**
// * 执行环形切面
// *
// * @param joinPoint
// * @return
// */
// @Around(value = "webAspect()")
// public Object requestWebAround(ProceedingJoinPoint joinPoint) throws Throwable {
// Log.threadBegin();
// ResponseResult responseResult = null; // 用户数据库记录
// long start = System.currentTimeMillis();
// String name = joinPoint.getSignature().getName();
// try {
// logger.info("=================[ {} ] request params is {} =================", name, joinPoint.getArgs());
// Object result = executeMethod(joinPoint, name);
// if (result instanceof ResponseResult) {
// responseResult = (ResponseResult) result;
// } else {
// responseResult = ResponseResult.result(result);
// }
// return result;
// } catch (Exception e) {
// responseResult = BaseController.getError(logger, "=================[ " + name + " ] is error {} =================", e);
// if (getReturnType(joinPoint).getTypeName().indexOf(ResponseResult.class.getName()) > -1) {
// return responseResult;
// } else {
// throw e;
// }
// } finally {
// long end = System.currentTimeMillis();
// Log.threadCommit();
// saveInterLogs(joinPoint, responseResult);
// logger.info("=================[ {} ] time ({})ms, result is {} =================", name, (end - start), responseResult);
// }
// }
//
// private Object executeMethod(ProceedingJoinPoint joinPoint, String name) throws Throwable {
// if (joinPoint.getArgs().length != 1
// || joinPoint.getArgs().length == 1 &&
// (joinPoint.getArgs()[0] instanceof ServletResponse || joinPoint.getArgs()[0] instanceof ServletRequest)
// ) {
// return joinPoint.proceed();
// } else {
// // 获取请求编号
// Object firstArgs = joinPoint.getArgs().length > 0 ? joinPoint.getArgs()[0] : null;
// String reqId = StringHelper.GetLastString(
// ObjectHelper.GetString(firstArgs, "__req"),
// ObjectHelper.GetString(firstArgs, "reqId"),
// ObjectHelper.GetString(firstArgs, "repeatCode"),
// ObjectHelper.GetString(firstArgs, "thirdOrderId")
// );
// if (StringHelper.IsEmpty(reqId)) {
// reqId = StringHelper.md5(JSONHelper.SerializeObject(firstArgs));
// } else {
// // 请求编号和公司编号挂钩
// reqId = StringHelper.GetID(ObjectHelper.GetString(firstArgs, "companyId"), reqId);
// }
// String reqFull = StringHelper.GetID(reqId, getHttpRequestUrl());
// RequestCacheResult req = _cacheResult.get(reqFull, new RequestCacheResult(reqFull));
// // 缓存的键值对
// if (req.getResult() == null) {
// synchronized (req) {
// if (req.getResult() == null) {
// try {
// req.setResult(joinPoint.proceed());
// } catch (Exception ex) {
// req.setResult(ex);
// }
// }
// }
// }
// Object result = req.getResult();
// if (result instanceof Throwable) {
// throw (Throwable) result;
// }
// return result;
// }
// }
//
// /**
// * deal with inter api logs
// *
// * @param joinPoint 请求参数
// * @param responseResult 返回参数
// */
// private void saveInterLogs(ProceedingJoinPoint joinPoint, ResponseResult responseResult) {
// // 日志请求不记录,防止死循环递归
// if (applicationName.indexOf("log") >= 0) {
// return;
// }
// // 正常请求不记录
// if (!logAll && responseResult.getCode() == ResponseResultBuilder.SUCCESS) {
// return;
// }
// LogVo logVo = initLogInterVo(getHttpRequestUrl(), joinPoint, responseResult);
// addLog(logVo);
// }
//
// /**
// * 添加日志到缓存中,并不是立即添加,而是通过线程执行,防止对环境造成影响
// *
// * @param logVo
// */
// public void addLog(LogVo logVo) {
// cache.add(logVo);
// }
//
// /**
// * 初始化日志Vo
// *
// * @param url 请求路径
// * @param joinPoint 请求参数
// * @param responseResult 返回参数
// * @return
// */
// private LogVo initLogInterVo(String url, ProceedingJoinPoint joinPoint, ResponseResult responseResult) {
// LogVo logInterVo = new LogVo();
// logInterVo.setLogId(StringHelper.GetNewID());
// logInterVo.setLogSources(applicationName);//平台名
// logInterVo.setInterUrl(url);//请求URL
// Object para = joinPoint.getArgs();
// if (joinPoint.getArgs().length == 1) {
// para = joinPoint.getArgs()[0];
// }
// logInterVo.setContent(JSON.toJSONString(para));//请求参数
// logInterVo.setResult(JSON.toJSONString(responseResult));//返回参数
// logInterVo.setStatus(responseResult.getCode() == ResponseResultBuilder.SUCCESS ? 1 : 0);
// return logInterVo;
// }
//
// /**
// * 执行下一个函数,出现异常会继续执行
// *
// * @return 是否继续执行
// */
// @Override
// public boolean next() throws Exception {
// while (cache.size() > 0) {
// LogVo item = cache.poll();
// if (item != null) {
// if (logInterFeign == null) {
// logInterFeign = Config.Context.getBean(LogFeign.class);
// }
// logInterFeign.saveInterLogs(item);
// }
// }
// return true;
// }
//
// /**
// * 沉睡时间
// *
// * @return
// */
// @Override
// public int getNextTime() {
// return 1000;
// }
//}
package
com
.
yanzuoguang
.
cloud
.
aop
;
import
com.alibaba.fastjson.JSON
;
import
com.yanzuoguang.util.cache.MemoryCache
;
import
com.yanzuoguang.util.contants.ResultConstants
;
import
com.yanzuoguang.util.exception.ExceptionHelper
;
import
com.yanzuoguang.util.helper.JSONHelper
;
import
com.yanzuoguang.util.helper.StringHelper
;
import
com.yanzuoguang.util.log.Log
;
import
com.yanzuoguang.util.obj.ObjectHelper
;
import
com.yanzuoguang.util.thread.ThreadNext
;
import
com.yanzuoguang.util.vo.LogVo
;
import
com.yanzuoguang.util.vo.ResponseResult
;
import
org.aspectj.lang.ProceedingJoinPoint
;
import
org.aspectj.lang.Signature
;
import
org.aspectj.lang.annotation.Around
;
import
org.aspectj.lang.annotation.Aspect
;
import
org.aspectj.lang.annotation.Pointcut
;
import
org.aspectj.lang.reflect.MethodSignature
;
import
org.slf4j.Logger
;
import
org.slf4j.LoggerFactory
;
import
org.springframework.beans.factory.annotation.Autowired
;
import
org.springframework.beans.factory.annotation.Value
;
import
org.springframework.context.ApplicationContext
;
import
org.springframework.stereotype.Component
;
import
javax.servlet.ServletRequest
;
import
javax.servlet.ServletResponse
;
import
java.lang.reflect.Method
;
import
java.lang.reflect.Type
;
import
java.util.concurrent.LinkedBlockingQueue
;
/**
* LogsAspect(接口请求日志切面)
*
* @author: Kang
* @time: 2018年04月25日 11:43
*/
@Aspect
@Component
public
class
WebAspect
extends
AbstractValidateAspect
implements
ThreadNext
.
Next
{
private
static
final
Logger
logger
=
LoggerFactory
.
getLogger
(
WebAspect
.
class
);
@Value
(
"${spring.application.name}"
)
private
String
applicationName
;
@Value
(
"${yzg.logAll:false}"
)
private
boolean
logAll
=
false
;
@Value
(
"${yzg.cacheTime:120}"
)
private
int
cacheTime
=
120
;
@Autowired
private
ApplicationContext
context
;
private
LogFeign
logFeign
;
/**
* 缓存队列
*/
private
volatile
LinkedBlockingQueue
<
LogVo
>
cache
=
new
LinkedBlockingQueue
<
LogVo
>();
// 缓存的每次请求的结果
private
MemoryCache
<
RequestCacheResult
>
_cacheResult
=
new
MemoryCache
<>(
cacheTime
);
public
WebAspect
()
{
ThreadNext
.
start
(
this
,
"save log error"
);
}
/**
* exec aop point aspect
*/
@Pointcut
(
"execution(* *..web..*(..))"
)
public
void
webAspect
()
{
}
/**
* 获取返回的至类型
*
* @param joinPoint
* @return
* @throws NoSuchMethodException
*/
private
Type
getReturnType
(
ProceedingJoinPoint
joinPoint
)
{
//获取返回值类型
Signature
s
=
joinPoint
.
getSignature
();
MethodSignature
ms
=
(
MethodSignature
)
s
;
Method
m
=
ms
.
getMethod
();
Type
t
=
m
.
getAnnotatedReturnType
().
getType
();
return
t
;
}
/**
* 执行环形切面
*
* @param joinPoint
* @return
*/
@Around
(
value
=
"webAspect()"
)
public
Object
requestWebAround
(
ProceedingJoinPoint
joinPoint
)
throws
Throwable
{
Log
.
threadBegin
();
ResponseResult
responseResult
=
null
;
// 用户数据库记录
long
start
=
System
.
currentTimeMillis
();
String
name
=
joinPoint
.
getSignature
().
getName
();
try
{
logger
.
info
(
"=================[ {} ] request params is {} ================="
,
name
,
joinPoint
.
getArgs
());
Object
result
=
executeMethod
(
joinPoint
,
name
);
if
(
result
instanceof
ResponseResult
)
{
responseResult
=
(
ResponseResult
)
result
;
}
else
{
responseResult
=
ResponseResult
.
result
(
result
);
}
return
result
;
}
catch
(
Exception
e
)
{
logger
.
error
(
"=================[ "
+
name
+
" ] is error {} ================="
,
e
);
responseResult
=
ExceptionHelper
.
getError
(
e
);
if
(
getReturnType
(
joinPoint
).
getTypeName
().
indexOf
(
ResponseResult
.
class
.
getName
())
>
-
1
)
{
return
responseResult
;
}
else
{
throw
e
;
}
}
finally
{
long
end
=
System
.
currentTimeMillis
();
Log
.
threadCommit
();
saveInterLogs
(
joinPoint
,
responseResult
);
logger
.
info
(
"=================[ {} ] time ({})ms, result is {} ================="
,
name
,
(
end
-
start
),
responseResult
);
}
}
private
Object
executeMethod
(
ProceedingJoinPoint
joinPoint
,
String
name
)
throws
Throwable
{
if
(
joinPoint
.
getArgs
().
length
!=
1
||
joinPoint
.
getArgs
().
length
==
1
&&
(
joinPoint
.
getArgs
()[
0
]
instanceof
ServletResponse
||
joinPoint
.
getArgs
()[
0
]
instanceof
ServletRequest
)
)
{
return
joinPoint
.
proceed
();
}
else
{
// 获取请求编号
Object
firstArgs
=
joinPoint
.
getArgs
().
length
>
0
?
joinPoint
.
getArgs
()[
0
]
:
null
;
String
reqId
=
ObjectHelper
.
getString
(
firstArgs
,
"reqId"
);
if
(
StringHelper
.
isEmpty
(
reqId
))
{
reqId
=
StringHelper
.
md5
(
JSONHelper
.
serialize
(
firstArgs
));
}
else
{
// 请求编号和公司编号挂钩
reqId
=
StringHelper
.
getId
(
ObjectHelper
.
getString
(
firstArgs
,
"companyId"
),
reqId
);
}
String
reqFull
=
StringHelper
.
getId
(
reqId
,
getHttpRequestUrl
());
RequestCacheResult
req
=
_cacheResult
.
get
(
reqFull
,
new
RequestCacheResult
(
reqFull
));
// 缓存的键值对
if
(
req
.
getResult
()
==
null
)
{
synchronized
(
req
)
{
if
(
req
.
getResult
()
==
null
)
{
try
{
req
.
setResult
(
joinPoint
.
proceed
());
}
catch
(
Exception
ex
)
{
req
.
setResult
(
ex
);
}
}
}
}
Object
result
=
req
.
getResult
();
if
(
result
instanceof
Throwable
)
{
throw
(
Throwable
)
result
;
}
return
result
;
}
}
/**
* deal with inter api logs
*
* @param joinPoint 请求参数
* @param responseResult 返回参数
*/
private
void
saveInterLogs
(
ProceedingJoinPoint
joinPoint
,
ResponseResult
responseResult
)
{
// 日志请求不记录,防止死循环递归
if
(
applicationName
.
indexOf
(
"log"
)
>=
0
)
{
return
;
}
// 正常请求不记录
if
(!
logAll
&&
responseResult
.
getCode
()
==
ResultConstants
.
SUCCESS
)
{
return
;
}
LogVo
logVo
=
initLogInterVo
(
getHttpRequestUrl
(),
joinPoint
,
responseResult
);
addLog
(
logVo
);
}
/**
* 添加日志到缓存中,并不是立即添加,而是通过线程执行,防止对环境造成影响
*
* @param logVo
*/
public
void
addLog
(
LogVo
logVo
)
{
cache
.
add
(
logVo
);
}
/**
* 初始化日志Vo
*
* @param url 请求路径
* @param joinPoint 请求参数
* @param responseResult 返回参数
* @return
*/
private
LogVo
initLogInterVo
(
String
url
,
ProceedingJoinPoint
joinPoint
,
ResponseResult
responseResult
)
{
LogVo
logInterVo
=
new
LogVo
();
logInterVo
.
setLogId
(
StringHelper
.
getNewID
());
logInterVo
.
setLogSources
(
applicationName
);
//平台名
logInterVo
.
setInterUrl
(
url
);
//请求URL
Object
para
=
joinPoint
.
getArgs
();
if
(
joinPoint
.
getArgs
().
length
==
1
)
{
para
=
joinPoint
.
getArgs
()[
0
];
}
logInterVo
.
setContent
(
JSON
.
toJSONString
(
para
));
//请求参数
logInterVo
.
setResult
(
JSON
.
toJSONString
(
responseResult
));
//返回参数
logInterVo
.
setStatus
(
responseResult
.
getCode
()
==
ResultConstants
.
SUCCESS
?
1
:
0
);
return
logInterVo
;
}
/**
* 执行下一个函数,出现异常会继续执行
*
* @return 是否继续执行
*/
@Override
public
boolean
next
()
throws
Exception
{
while
(
cache
.
size
()
>
0
)
{
LogVo
item
=
cache
.
poll
();
if
(
item
!=
null
)
{
if
(
logFeign
==
null
)
{
logFeign
=
context
.
getBean
(
LogFeign
.
class
);
}
logFeign
.
save
(
item
);
}
}
return
true
;
}
/**
* 沉睡时间
*
* @return
*/
@Override
public
int
getNextTime
()
{
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