liwen
5 months ago
9 changed files with 5 additions and 352 deletions
@ -1,66 +0,0 @@
|
||||
/* |
||||
* Copyright (c) 2018-2028, Chill Zhuang All rights reserved. |
||||
* |
||||
* Redistribution and use in source and binary forms, with or without |
||||
* modification, are permitted provided that the following conditions are met: |
||||
* |
||||
* Redistributions of source code must retain the above copyright notice, |
||||
* this list of conditions and the following disclaimer. |
||||
* Redistributions in binary form must reproduce the above copyright |
||||
* notice, this list of conditions and the following disclaimer in the |
||||
* documentation and/or other materials provided with the distribution. |
||||
* Neither the name of the dreamlu.net developer nor the names of its |
||||
* contributors may be used to endorse or promote products derived from |
||||
* this software without specific prior written permission. |
||||
* Author: Chill 庄骞 (smallchill@163.com) |
||||
*/ |
||||
package com.hnac.hzims.common.launch; |
||||
|
||||
import com.hnac.hzims.common.constant.LauncherConstant; |
||||
import org.springblade.core.auto.service.AutoService; |
||||
import org.springblade.core.launch.constant.AppConstant; |
||||
import org.springblade.core.launch.service.LauncherService; |
||||
import org.springblade.core.launch.utils.PropsUtil; |
||||
import org.springframework.boot.builder.SpringApplicationBuilder; |
||||
|
||||
import java.util.Properties; |
||||
|
||||
/** |
||||
* 启动参数拓展 |
||||
* |
||||
* @author smallchil |
||||
*/ |
||||
@AutoService(LauncherService.class) |
||||
public class LauncherServiceImpl implements LauncherService { |
||||
|
||||
@Override |
||||
public void launcher(SpringApplicationBuilder builder, String appName, String profile, boolean isLocalDev) { |
||||
Properties props = System.getProperties(); |
||||
// 通用注册
|
||||
PropsUtil.setProperty(props, "spring.cloud.nacos.discovery.server-addr", LauncherConstant.nacosAddr(profile)); |
||||
PropsUtil.setProperty(props, "spring.cloud.nacos.config.server-addr", LauncherConstant.nacosAddr(profile)); |
||||
PropsUtil.setProperty(props, "spring.cloud.sentinel.transport.dashboard", LauncherConstant.sentinelAddr(profile)); |
||||
|
||||
// dubbo注册
|
||||
PropsUtil.setProperty(props, "dubbo.application.name", appName); |
||||
PropsUtil.setProperty(props, "dubbo.application.qos.enable", "false"); |
||||
PropsUtil.setProperty(props, "dubbo.protocol.name", "dubbo"); |
||||
PropsUtil.setProperty(props, "dubbo.registry.address", "nacos://" + LauncherConstant.nacosAddr(profile)); |
||||
PropsUtil.setProperty(props, "dubbo.version", AppConstant.APPLICATION_VERSION); |
||||
PropsUtil.setProperty(props, "dubbo.scan.base-packages", AppConstant.BASE_PACKAGES); |
||||
|
||||
// seata注册地址
|
||||
PropsUtil.setProperty(props, "seata.service.grouplist.default", LauncherConstant.seataAddr(profile)); |
||||
// seata注册group格式
|
||||
PropsUtil.setProperty(props, "seata.tx-service-group", LauncherConstant.seataServiceGroup(appName)); |
||||
// seata配置服务group
|
||||
PropsUtil.setProperty(props, "seata.service.vgroup-mapping.".concat(LauncherConstant.seataServiceGroup(appName)), LauncherConstant.DEFAULT_MODE); |
||||
// seata注册模式配置
|
||||
// PropsUtil.setProperty(props, "seata.registry.type", LauncherConstant.NACOS_MODE);
|
||||
// PropsUtil.setProperty(props, "seata.registry.nacos.server-addr", LauncherConstant.nacosAddr(profile));
|
||||
// PropsUtil.setProperty(props, "seata.config.type", LauncherConstant.NACOS_MODE);
|
||||
// PropsUtil.setProperty(props, "seata.config.nacos.server-addr", LauncherConstant.nacosAddr(profile));
|
||||
|
||||
} |
||||
|
||||
} |
@ -1,269 +0,0 @@
|
||||
package com.hnac.hzims.common.logs.aop; |
||||
|
||||
import com.alibaba.fastjson.JSON; |
||||
import com.baomidou.mybatisplus.core.toolkit.ObjectUtils; |
||||
import com.hnac.hzims.common.logs.annotation.OperationAnnotation; |
||||
import com.hnac.hzims.common.logs.consumer.SysLogQueue; |
||||
import com.hnac.hzims.common.logs.to.SysLogTo; |
||||
import com.hnac.hzims.common.logs.enums.BusinessStatus; |
||||
import com.hnac.hzims.common.logs.utils.StringUtils; |
||||
import com.hnac.hzims.common.logs.utils.WebIpUtils; |
||||
import lombok.extern.slf4j.Slf4j; |
||||
import org.aspectj.lang.JoinPoint; |
||||
import org.aspectj.lang.annotation.*; |
||||
import org.aspectj.lang.reflect.CodeSignature; |
||||
import org.springblade.core.launch.props.BladeProperties; |
||||
import org.springblade.core.launch.server.ServerInfo; |
||||
import org.springblade.core.secure.utils.AuthUtil; |
||||
import org.springblade.core.tool.constant.BladeConstant; |
||||
import org.springblade.core.tool.utils.Func; |
||||
import org.springblade.core.tool.utils.WebUtil; |
||||
import org.springframework.beans.factory.annotation.Autowired; |
||||
import org.springframework.core.NamedThreadLocal; |
||||
import org.springframework.stereotype.Component; |
||||
import org.springframework.util.StopWatch; |
||||
import org.springframework.web.context.request.RequestAttributes; |
||||
import org.springframework.web.context.request.RequestContextHolder; |
||||
import org.springframework.web.context.request.ServletRequestAttributes; |
||||
|
||||
import javax.servlet.http.HttpServletRequest; |
||||
import java.lang.reflect.Method; |
||||
import java.time.LocalDateTime; |
||||
import java.util.HashMap; |
||||
import java.util.Map; |
||||
|
||||
/** |
||||
* @author dfy |
||||
* @date 2023/3/29 |
||||
* @description: 操作日志切面处理类 |
||||
*/ |
||||
//@Aspect
|
||||
//@Component
|
||||
@Slf4j |
||||
public class SysLogAspect { |
||||
|
||||
|
||||
@Autowired |
||||
private ServerInfo serverInfo; |
||||
|
||||
|
||||
@Autowired |
||||
private BladeProperties bladeProperties; |
||||
|
||||
|
||||
@Autowired |
||||
private SysLogQueue sysLogQueue; |
||||
|
||||
|
||||
//private StopWatch stopWatch = new StopWatch();
|
||||
|
||||
|
||||
private static final ThreadLocal<Long> TIME_THREADLOCAL = new NamedThreadLocal<>("Cost Time"); |
||||
|
||||
|
||||
/** |
||||
* 设置操作日志切入点 在注解的位置切入代码 |
||||
*/ |
||||
@Pointcut("@annotation(com.hnac.hzims.common.logs.annotation.OperationAnnotation)") |
||||
public void logPointCut() { |
||||
} |
||||
|
||||
/** |
||||
* @param joinPoint |
||||
* @Description 前置通知 方法调用前触发 记录开始时间,从session中获取操作人 |
||||
*/ |
||||
@Before(value = "logPointCut()") |
||||
public void before(JoinPoint joinPoint) { |
||||
// if (!stopWatch.isRunning()) {
|
||||
// stopWatch.start();
|
||||
// }
|
||||
//startTime = System.currentTimeMillis();
|
||||
TIME_THREADLOCAL.set(System.currentTimeMillis()); |
||||
log.info("前置通知"); |
||||
} |
||||
|
||||
/** |
||||
* @param joinPoint |
||||
* @Description 后置通知 方法调用后触发 记录结束时间 ,操作人 ,入参等 |
||||
*/ |
||||
@AfterReturning(value = "logPointCut()", returning = "jsonResult") |
||||
public void after(JoinPoint joinPoint, Object jsonResult) { |
||||
log.info("=========返回通知=============="); |
||||
try { |
||||
handleLog(joinPoint, jsonResult, null); |
||||
} catch (Exception e) { |
||||
log.error("异常信息:{}", e.getMessage()); |
||||
e.printStackTrace(); |
||||
} finally { |
||||
TIME_THREADLOCAL.remove(); |
||||
} |
||||
} |
||||
|
||||
|
||||
/** |
||||
* @param joinPoint |
||||
* @return |
||||
* @Description 获取入参方法参数 |
||||
*/ |
||||
public Map<String, Object> getNameAndValue(JoinPoint joinPoint) { |
||||
Map<String, Object> param = new HashMap<>(16); |
||||
Object[] paramValues = joinPoint.getArgs(); |
||||
String[] paramNames = ((CodeSignature) joinPoint.getSignature()).getParameterNames(); |
||||
for (int i = 0; i < paramNames.length; i++) { |
||||
if (paramValues[i] instanceof Integer || paramValues[i] instanceof String) { |
||||
param.put(paramNames[i], paramValues[i]); |
||||
} |
||||
} |
||||
return param; |
||||
} |
||||
|
||||
|
||||
/** |
||||
* @Description: 获取request |
||||
*/ |
||||
public HttpServletRequest getHttpServletRequest() { |
||||
RequestAttributes ra = RequestContextHolder.getRequestAttributes(); |
||||
ServletRequestAttributes sra = (ServletRequestAttributes) ra; |
||||
HttpServletRequest request = sra.getRequest(); |
||||
return request; |
||||
} |
||||
|
||||
|
||||
/** |
||||
* @param joinPoint |
||||
* @Description 异常通知 |
||||
*/ |
||||
@AfterThrowing(pointcut = "logPointCut()", throwing = "e") |
||||
public void throwing(JoinPoint joinPoint, Exception e) { |
||||
log.info("=========异常通知=============="); |
||||
try { |
||||
handleLog(joinPoint, null, e); |
||||
} catch (Exception exception) { |
||||
log.error("异常信息:{}", exception.getMessage()); |
||||
e.printStackTrace(); |
||||
} finally { |
||||
TIME_THREADLOCAL.remove(); |
||||
} |
||||
} |
||||
|
||||
|
||||
/** |
||||
* 转换request 请求参数 |
||||
* |
||||
* @param paramMap request获取的参数数组 |
||||
*/ |
||||
public Map<String, String> converMap(Map<String, String[]> paramMap) { |
||||
Map<String, String> rtnMap = new HashMap<String, String>(); |
||||
for (String key : paramMap.keySet()) { |
||||
rtnMap.put(key, paramMap.get(key)[0]); |
||||
} |
||||
return rtnMap; |
||||
} |
||||
|
||||
/** |
||||
* 转换异常信息为字符串 |
||||
* |
||||
* @param exceptionName 异常名称 |
||||
* @param exceptionMessage 异常信息 |
||||
* @param elements 堆栈信息 |
||||
*/ |
||||
public String stackTraceToString(String exceptionName, String exceptionMessage, StackTraceElement[] elements) { |
||||
StringBuffer strbuff = new StringBuffer(); |
||||
for (StackTraceElement stet : elements) { |
||||
strbuff.append(stet + "\n"); |
||||
} |
||||
String message = exceptionName + ":" + exceptionMessage + "\n\t" + strbuff; |
||||
return message; |
||||
} |
||||
|
||||
|
||||
/** |
||||
* 处理操作日志结果集 |
||||
* |
||||
* @param joinPoint |
||||
* @param jsonResult |
||||
* @param exception |
||||
*/ |
||||
private void handleLog(JoinPoint joinPoint, Object jsonResult, Exception exception) { |
||||
String targetName = joinPoint.getTarget().getClass().getName(); |
||||
String methodName = joinPoint.getSignature().getName(); |
||||
Object[] arguments = joinPoint.getArgs(); |
||||
Class<?> targetClass = null; |
||||
try { |
||||
targetClass = Class.forName(targetName); |
||||
} catch (ClassNotFoundException e) { |
||||
e.printStackTrace(); |
||||
} |
||||
Method[] methods = targetClass.getMethods(); |
||||
String title; |
||||
String action; |
||||
String businessType; |
||||
String operatorType; |
||||
String moduleName; |
||||
Class<?>[] clazzs; |
||||
for (Method method : methods) { |
||||
if (method.getName().equals(methodName)) { |
||||
clazzs = method.getParameterTypes(); |
||||
if (clazzs != null && clazzs.length == arguments.length && method.getAnnotation(OperationAnnotation.class) != null) { |
||||
// 获取请求的类名
|
||||
String className = joinPoint.getTarget().getClass().getName(); |
||||
methodName = className + "." + methodName; |
||||
HttpServletRequest request = getHttpServletRequest(); |
||||
String requestPath = request.getServletPath(); |
||||
OperationAnnotation annotation = method.getAnnotation(OperationAnnotation.class); |
||||
title = annotation.title(); |
||||
action = annotation.action(); |
||||
businessType = annotation.businessType().getValue(); |
||||
operatorType = annotation.operatorType().getValue(); |
||||
moduleName = annotation.moduleName(); |
||||
// 获取当前用户信息
|
||||
String userName = AuthUtil.getUserAccount(request); |
||||
Long userId = AuthUtil.getUserId(request); |
||||
SysLogTo sysLog = new SysLogTo(); |
||||
if (StringUtils.isBlank(userName) && userId == -1) { |
||||
userName = "当前用户未登录"; |
||||
} |
||||
sysLog.setModuleName(moduleName); |
||||
sysLog.setOperationUserName(userName); |
||||
sysLog.setOperationUserId(userId); |
||||
sysLog.setTenantId(Func.toStr(AuthUtil.getTenantId(), BladeConstant.ADMIN_TENANT_ID)); |
||||
sysLog.setLocalIp(WebIpUtils.getIpAddr(request));// 请求IP
|
||||
// if (stopWatch.isRunning()) {
|
||||
// stopWatch.stop();
|
||||
// }
|
||||
long endTime = System.currentTimeMillis(); |
||||
sysLog.setCostTime((endTime - TIME_THREADLOCAL.get()) + "ms"); |
||||
sysLog.setPath(requestPath); |
||||
sysLog.setTitle(title); |
||||
sysLog.setAction(action); |
||||
sysLog.setOperatorType(operatorType); |
||||
sysLog.setBusinessType(businessType); |
||||
sysLog.setParameter(getNameAndValue(joinPoint).toString()); |
||||
sysLog.setMethodClass(className); |
||||
sysLog.setMethodName(methodName); |
||||
sysLog.setServerName(bladeProperties.getName()); |
||||
sysLog.setServerHost(serverInfo.getHostName()); |
||||
sysLog.setServerIp(serverInfo.getIpWithPort()); |
||||
sysLog.setEnv(bladeProperties.getEnv()); |
||||
sysLog.setMethod(request.getMethod()); |
||||
sysLog.setUserAgent(request.getHeader(WebUtil.USER_AGENT_HEADER)); |
||||
sysLog.setOperationTime(LocalDateTime.now()); |
||||
|
||||
//返回结果集
|
||||
if (ObjectUtils.isNotEmpty(jsonResult)) { |
||||
sysLog.setStatus(BusinessStatus.SUCCESS.getKey()); |
||||
sysLog.setJsonResult(StringUtils.substring(JSON.toJSONString(jsonResult), 0, 2000)); |
||||
} |
||||
|
||||
//异常结果集
|
||||
if (exception != null) { |
||||
sysLog.setStatus(BusinessStatus.FAIL.getKey()); |
||||
sysLog.setErrorMsg(StringUtils.substring(exception.getMessage(), 0, 2000)); |
||||
} |
||||
//保存到阻塞队列里
|
||||
sysLogQueue.add(sysLog); |
||||
} |
||||
} |
||||
} |
||||
} |
||||
} |
Loading…
Reference in new issue