Commit ba8c27ef authored by yanzg's avatar yanzg

将源码打包进jar包

parent 80f52bb6
...@@ -2,6 +2,7 @@ package com.yanzuoguang.cloud.aop; ...@@ -2,6 +2,7 @@ package com.yanzuoguang.cloud.aop;
import com.yanzuoguang.cloud.CloudConfig; import com.yanzuoguang.cloud.CloudConfig;
import com.yanzuoguang.cloud.aop.log.LogLocal; import com.yanzuoguang.cloud.aop.log.LogLocal;
import com.yanzuoguang.log.LogCountTime;
import com.yanzuoguang.util.exception.ExceptionHelper; import com.yanzuoguang.util.exception.ExceptionHelper;
import com.yanzuoguang.util.helper.JsonHelper; import com.yanzuoguang.util.helper.JsonHelper;
import com.yanzuoguang.util.helper.StringHelper; import com.yanzuoguang.util.helper.StringHelper;
...@@ -21,16 +22,16 @@ public class AspectLogResult { ...@@ -21,16 +22,16 @@ public class AspectLogResult {
private final CloudConfig cloudConfig; private final CloudConfig cloudConfig;
private final LogLocal logLocal; private final LogLocal logLocal;
private final AspectLogBody aspectLogBody; private final AspectLogBody aspectLogBody;
private final AspectLogCountTime aspectLogCountTime; private final LogCountTime logCountTime;
private final ResponseResult<String> responseDefault = ResponseResult.result("操作成功"); private final ResponseResult<String> responseDefault = ResponseResult.result("操作成功");
public AspectLogResult(CloudConfig cloudConfig, LogLocal logLocal, AspectLogBody aspectLogBody, AspectLogCountTime aspectLogCountTime) { public AspectLogResult(CloudConfig cloudConfig, LogLocal logLocal, AspectLogBody aspectLogBody, LogCountTime logCountTime) {
this.cloudConfig = cloudConfig; this.cloudConfig = cloudConfig;
this.logLocal = logLocal; this.logLocal = logLocal;
this.aspectLogBody = aspectLogBody; this.aspectLogBody = aspectLogBody;
this.aspectLogCountTime = aspectLogCountTime; this.logCountTime = logCountTime;
} }
/** /**
...@@ -59,7 +60,7 @@ public class AspectLogResult { ...@@ -59,7 +60,7 @@ public class AspectLogResult {
boolean isError = resultEx != null; boolean isError = resultEx != null;
boolean isLogDisplay = isLogCommon || isError; boolean isLogDisplay = isLogCommon || isError;
// 记录请求时间 // 记录请求时间
aspectLogCountTime.finish(fullUrl, time, isError); logCountTime.finish(fullUrl, time, isError);
// ThreadHelper.waitRun(WAIT_MAX, WAIT_ITEM, k -> StringHelper.isEmpty(log.getLogId())) // ThreadHelper.waitRun(WAIT_MAX, WAIT_ITEM, k -> StringHelper.isEmpty(log.getLogId()))
if (StringHelper.isEmpty(log.getLogId()) || !isLogDisplay) { if (StringHelper.isEmpty(log.getLogId()) || !isLogDisplay) {
return; return;
......
...@@ -2,6 +2,7 @@ package com.yanzuoguang.cloud.aop; ...@@ -2,6 +2,7 @@ package com.yanzuoguang.cloud.aop;
import com.yanzuoguang.cloud.CloudConfig; import com.yanzuoguang.cloud.CloudConfig;
import com.yanzuoguang.cloud.aop.log.LogLocal; import com.yanzuoguang.cloud.aop.log.LogLocal;
import com.yanzuoguang.log.LogCountTime;
import com.yanzuoguang.util.log.Log; import com.yanzuoguang.util.log.Log;
import com.yanzuoguang.util.vo.LogVo; import com.yanzuoguang.util.vo.LogVo;
import org.springframework.stereotype.Component; import org.springframework.stereotype.Component;
...@@ -17,13 +18,13 @@ public class AspectLogStart { ...@@ -17,13 +18,13 @@ public class AspectLogStart {
private final CloudConfig cloudConfig; private final CloudConfig cloudConfig;
private final LogLocal logLocal; private final LogLocal logLocal;
private final AspectLogBody aspectLogBody; private final AspectLogBody aspectLogBody;
private final AspectLogCountTime aspectLogCountTime; private final LogCountTime logCountTime;
public AspectLogStart(CloudConfig cloudConfig, LogLocal logLocal, AspectLogBody aspectLogBody, AspectLogCountTime aspectLogCountTime) { public AspectLogStart(CloudConfig cloudConfig, LogLocal logLocal, AspectLogBody aspectLogBody, LogCountTime logCountTime) {
this.cloudConfig = cloudConfig; this.cloudConfig = cloudConfig;
this.logLocal = logLocal; this.logLocal = logLocal;
this.aspectLogBody = aspectLogBody; this.aspectLogBody = aspectLogBody;
this.aspectLogCountTime = aspectLogCountTime; this.logCountTime = logCountTime;
} }
/** /**
...@@ -40,7 +41,7 @@ public class AspectLogStart { ...@@ -40,7 +41,7 @@ public class AspectLogStart {
public void requestLog(Class<?> cls, String tag, String url, Object requestBody, LogVo log, boolean logFlag) { public void requestLog(Class<?> cls, String tag, String url, Object requestBody, LogVo log, boolean logFlag) {
try { try {
String fullUrl = String.format("%s:%s", tag, url); String fullUrl = String.format("%s:%s", tag, url);
aspectLogCountTime.start(fullUrl); logCountTime.start(fullUrl);
String body = aspectLogBody.getBodyString(requestBody); String body = aspectLogBody.getBodyString(requestBody);
if (logFlag) { if (logFlag) {
......
...@@ -2,6 +2,7 @@ package com.yanzuoguang.db.impl; ...@@ -2,6 +2,7 @@ package com.yanzuoguang.db.impl;
import com.yanzuoguang.db.ConfigDb; import com.yanzuoguang.db.ConfigDb;
import com.yanzuoguang.db.DbExecute; import com.yanzuoguang.db.DbExecute;
import com.yanzuoguang.log.LogCountTime;
import com.yanzuoguang.util.helper.YzgTimeout; import com.yanzuoguang.util.helper.YzgTimeout;
import com.yanzuoguang.util.vo.MapRow; import com.yanzuoguang.util.vo.MapRow;
import com.yanzuoguang.util.vo.Ref; import com.yanzuoguang.util.vo.Ref;
...@@ -24,11 +25,13 @@ public class DbExecuteImpl implements DbExecute { ...@@ -24,11 +25,13 @@ public class DbExecuteImpl implements DbExecute {
private final JdbcTemplate jdbcTemplate; private final JdbcTemplate jdbcTemplate;
private final DbPrintSql printSql; private final DbPrintSql printSql;
private final ConfigDb configDb; private final ConfigDb configDb;
private final LogCountTime logCountTime;
public DbExecuteImpl(JdbcTemplate jdbcTemplate, DbPrintSql printSql, ConfigDb configDb) { public DbExecuteImpl(JdbcTemplate jdbcTemplate, DbPrintSql printSql, ConfigDb configDb, LogCountTime logCountTime) {
this.jdbcTemplate = jdbcTemplate; this.jdbcTemplate = jdbcTemplate;
this.printSql = printSql; this.printSql = printSql;
this.configDb = configDb; this.configDb = configDb;
this.logCountTime = logCountTime;
} }
public JdbcTemplate getJdbc() { public JdbcTemplate getJdbc() {
...@@ -151,12 +154,21 @@ public class DbExecuteImpl implements DbExecute { ...@@ -151,12 +154,21 @@ public class DbExecuteImpl implements DbExecute {
Ref<Integer> row = new Ref<>(0); Ref<Integer> row = new Ref<>(0);
long start = System.currentTimeMillis(); long start = System.currentTimeMillis();
sqlInfo.setSql(this.handleParas(sqlInfo.getSql())); sqlInfo.setSql(this.handleParas(sqlInfo.getSql()));
String fullUrl = String.format("Sql:%s:%s", sqlInfo.getTargetClass().getSimpleName(), sqlInfo.getSqlName());
logCountTime.start(fullUrl);
boolean isError = false;
try { try {
YzgTimeout.timeOut(sqlInfo.getTargetClass(), sqlInfo.getSqlName(), () -> { YzgTimeout.timeOut(sqlInfo.getTargetClass(), sqlInfo.getSqlName(), () -> {
sqlFunction.accept(row, start); sqlFunction.accept(row, start);
}); });
} catch (Exception ex) {
isError = true;
throw ex;
} finally { } finally {
printSql.print(sqlInfo, start, row.value); long time = System.currentTimeMillis() - start;
logCountTime.finish(fullUrl, time, isError);
printSql.print(sqlInfo, time, row.value);
} }
} }
......
...@@ -64,10 +64,10 @@ public class DbPrintSql { ...@@ -64,10 +64,10 @@ public class DbPrintSql {
* 打印SQL语句 * 打印SQL语句
* *
* @param sqlInfo sql语句嘻嘻你 * @param sqlInfo sql语句嘻嘻你
* @param start 开始执行的语句 * @param time 执行的时间
* @param row SQL语句 * @param row SQL语句
*/ */
public void print(SqlInfo sqlInfo, long start, int row) { public void print(SqlInfo sqlInfo, long time, int row) {
try { try {
// 日志表忽略打印 // 日志表忽略打印
if (!configDb.isPrintSql()) { if (!configDb.isPrintSql()) {
...@@ -79,8 +79,6 @@ public class DbPrintSql { ...@@ -79,8 +79,6 @@ public class DbPrintSql {
} }
} }
String sql = getStringSql(sqlInfo.getSql(), sqlInfo.getParas()); String sql = getStringSql(sqlInfo.getSql(), sqlInfo.getParas());
long end = System.currentTimeMillis();
long time = end - start;
// 打印SQL语句 // 打印SQL语句
String tag = String.format("%d row %d ms %s.%s", row, time, sqlInfo.getTargetClass().getSimpleName(), sqlInfo.getSqlName()); String tag = String.format("%d row %d ms %s.%s", row, time, sqlInfo.getTargetClass().getSimpleName(), sqlInfo.getSqlName());
Log.infoTag(DbPrintSql.class, tag, sql); Log.infoTag(DbPrintSql.class, tag, sql);
......
package com.yanzuoguang.log;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;
/**
* 整个模块的所有配置信息
*
* @author 颜佐光
*/
@Component
public class LogConfig {
public static final int DEFAULT_PRINT_TIME = 2 * 60 * 1000;
/**
* 外网地址
*/
@Value("${yzg.log.print.time:" + DEFAULT_PRINT_TIME + "}")
private int logCountTime;
public int getLogCountTime() {
return logCountTime;
}
}
package com.yanzuoguang.cloud.aop; package com.yanzuoguang.log;
import com.yanzuoguang.util.cache.MemoryCache; import com.yanzuoguang.util.cache.MemoryCache;
import com.yanzuoguang.util.helper.DateHelper; import com.yanzuoguang.util.helper.DateHelper;
...@@ -11,14 +11,14 @@ import org.springframework.stereotype.Component; ...@@ -11,14 +11,14 @@ import org.springframework.stereotype.Component;
* @author 颜佐光 * @author 颜佐光
*/ */
@Component @Component
public class AspectLogCountTime { public class LogCountTime {
private static final String URL_PARA_START = "?"; private static final String URL_PARA_START = "?";
/** /**
* 今日缓存,超过时间会自动清空 * 今日缓存,超过时间会自动清空
*/ */
private final MemoryCache<AspectUrlCountVo> todayMemoryCache = new MemoryCache<>(); private final MemoryCache<LogUrlCountVo> todayMemoryCache = new MemoryCache<>();
/** /**
* 今日日期 * 今日日期
*/ */
...@@ -28,7 +28,7 @@ public class AspectLogCountTime { ...@@ -28,7 +28,7 @@ public class AspectLogCountTime {
*/ */
private String todayTime; private String todayTime;
public MemoryCache<AspectUrlCountVo> getTodayMemoryCache() { public MemoryCache<LogUrlCountVo> getTodayMemoryCache() {
return todayMemoryCache; return todayMemoryCache;
} }
...@@ -56,14 +56,14 @@ public class AspectLogCountTime { ...@@ -56,14 +56,14 @@ public class AspectLogCountTime {
* @param url 地址 * @param url 地址
* @return 缓存对象 * @return 缓存对象
*/ */
private AspectUrlCountVo getCount(String url) { private LogUrlCountVo getCount(String url) {
// 初始化日期 // 初始化日期
initToday(); initToday();
if (url.contains(URL_PARA_START)) { if (url.contains(URL_PARA_START)) {
url = url.substring(0, url.indexOf(URL_PARA_START)); url = url.substring(0, url.indexOf(URL_PARA_START));
} }
// 缓存中获取对象 // 缓存中获取对象
AspectUrlCountVo ret = todayMemoryCache.get(url); LogUrlCountVo ret = todayMemoryCache.get(url);
if (ret != null) { if (ret != null) {
return ret; return ret;
} }
...@@ -73,7 +73,7 @@ public class AspectLogCountTime { ...@@ -73,7 +73,7 @@ public class AspectLogCountTime {
if (ret != null) { if (ret != null) {
return ret; return ret;
} }
ret = new AspectUrlCountVo(url); ret = new LogUrlCountVo(url);
todayMemoryCache.put(url, ret); todayMemoryCache.put(url, ret);
return ret; return ret;
} }
...@@ -85,7 +85,7 @@ public class AspectLogCountTime { ...@@ -85,7 +85,7 @@ public class AspectLogCountTime {
* @param url 请求地址 * @param url 请求地址
*/ */
public void start(String url) { public void start(String url) {
AspectUrlCountVo count = getCount(url); LogUrlCountVo count = getCount(url);
count.addStart(); count.addStart();
} }
...@@ -97,7 +97,7 @@ public class AspectLogCountTime { ...@@ -97,7 +97,7 @@ public class AspectLogCountTime {
* @param isError 是否错误 * @param isError 是否错误
*/ */
public void finish(String url, long time, boolean isError) { public void finish(String url, long time, boolean isError) {
AspectUrlCountVo count = getCount(url); LogUrlCountVo count = getCount(url);
count.addFinish(time, isError); count.addFinish(time, isError);
} }
} }
package com.yanzuoguang.cloud.aop; package com.yanzuoguang.log;
import com.yanzuoguang.cloud.CloudConfig;
import com.yanzuoguang.util.base.CollectionString; import com.yanzuoguang.util.base.CollectionString;
import com.yanzuoguang.util.thread.ThreadNext; import com.yanzuoguang.util.thread.ThreadNext;
import org.springframework.beans.factory.InitializingBean; import org.springframework.beans.factory.InitializingBean;
...@@ -17,14 +16,14 @@ import java.util.List; ...@@ -17,14 +16,14 @@ import java.util.List;
* @author 颜佐光 * @author 颜佐光
*/ */
@Component @Component
public class AspectLogPrint implements ThreadNext.Next, InitializingBean { public class LogPrint implements ThreadNext.Next, InitializingBean {
private final CloudConfig cloudConfig; private final LogConfig logConfig;
private final AspectLogCountTime aspectLogCountTime; private final LogCountTime logCountTime;
public AspectLogPrint(CloudConfig cloudConfig, AspectLogCountTime aspectLogCountTime) { public LogPrint(LogConfig logConfig, LogCountTime logCountTime) {
this.cloudConfig = cloudConfig; this.logConfig = logConfig;
this.aspectLogCountTime = aspectLogCountTime; this.logCountTime = logCountTime;
} }
@Override @Override
...@@ -35,22 +34,22 @@ public class AspectLogPrint implements ThreadNext.Next, InitializingBean { ...@@ -35,22 +34,22 @@ public class AspectLogPrint implements ThreadNext.Next, InitializingBean {
@Override @Override
public boolean next() { public boolean next() {
List<AspectUrlCountVo> rowList = new ArrayList<>(aspectLogCountTime.getTodayMemoryCache().getValues()); List<LogUrlCountVo> rowList = new ArrayList<>(logCountTime.getTodayMemoryCache().getValues());
if (rowList.isEmpty()) { if (rowList.isEmpty()) {
return true; return true;
} }
// 按照使用总时间升序排序 // 按照使用总时间升序排序
rowList.sort(Comparator.comparingLong(AspectUrlCountVo::getTotalTime)); rowList.sort(Comparator.comparingLong(LogUrlCountVo::getTotalTime));
// 逆转排序 // 逆转排序
Collections.reverse(rowList); Collections.reverse(rowList);
// 打印日志 // 打印日志
System.out.println(CollectionString.getCollectionString("接口从" + aspectLogCountTime.getTodayTime() + "时执行次数:", rowList)); System.out.println(CollectionString.getCollectionString("从" + logCountTime.getTodayTime() + "时执行次数:", rowList));
// 下次继续执行 // 下次继续执行
return true; return true;
} }
@Override @Override
public int getNextTime() { public int getNextTime() {
return cloudConfig.getLogCountTime(); return logConfig.getLogCountTime();
} }
} }
package com.yanzuoguang.cloud.aop; package com.yanzuoguang.log;
/** /**
* 次数统计 * 次数统计
* *
* @author 颜佐光 * @author 颜佐光
*/ */
public class AspectUrlCountVo { public class LogUrlCountVo {
private final Object lockStart = new Object(); private final Object lockStart = new Object();
private final Object lockFinish = new Object(); private final Object lockFinish = new Object();
...@@ -18,7 +18,7 @@ public class AspectUrlCountVo { ...@@ -18,7 +18,7 @@ public class AspectUrlCountVo {
private volatile long minTime = Long.MAX_VALUE; private volatile long minTime = Long.MAX_VALUE;
private volatile long maxTime; private volatile long maxTime;
public AspectUrlCountVo(String url) { public LogUrlCountVo(String url) {
this.url = url; this.url = url;
} }
......
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