Commit 2110a4da authored by yanzg's avatar yanzg

日志处理服务

parent 0bd070a2
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());
}
}
} }
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;
......
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;
......
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;
/** /**
* 字符串帮主类 * 字符串帮主类
......
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;
......
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;
......
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();
}
}
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;
}
}
...@@ -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>
......
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();
}
}
//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.Aspect; import org.aspectj.lang.annotation.Pointcut;
//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
//@Aspect @Component
//@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);
// } }
// } }
//
//
//} }
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);
}
//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;
// } }
//} }
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment