Browse Source

update: common服务升级改造

zhongwei
liwen 5 months ago
parent
commit
d63e7d2847
  1. 1
      hzims-biz-common/src/main/java/com/hnac/hzims/common/config/BladeCommonConfiguration.java
  2. 6
      hzims-biz-common/src/main/java/com/hnac/hzims/common/config/RedisMessageConfig.java
  3. 66
      hzims-biz-common/src/main/java/com/hnac/hzims/common/launch/LauncherServiceImpl.java
  4. 269
      hzims-biz-common/src/main/java/com/hnac/hzims/common/logs/aop/SysLogAspect.java
  5. 2
      hzims-biz-common/src/main/java/com/hnac/hzims/common/service/UserAuthDataService.java
  6. 3
      hzims-biz-common/src/main/java/com/hnac/hzims/common/statistics/Charts.java
  7. 8
      hzims-biz-common/src/main/java/com/hnac/hzims/common/support/utils/Condition.java
  8. 1
      hzims-biz-common/src/main/java/com/hnac/hzims/common/utils/CacheUtil.java
  9. 1
      hzims-biz-common/src/main/java/com/hnac/hzims/common/utils/Condition.java

1
hzims-biz-common/src/main/java/com/hnac/hzims/common/config/BladeCommonConfiguration.java

@ -16,7 +16,6 @@
*/ */
package com.hnac.hzims.common.config; package com.hnac.hzims.common.config;
import lombok.AllArgsConstructor; import lombok.AllArgsConstructor;
import org.springframework.context.annotation.Configuration; import org.springframework.context.annotation.Configuration;

6
hzims-biz-common/src/main/java/com/hnac/hzims/common/config/RedisMessageConfig.java

@ -1,18 +1,13 @@
package com.hnac.hzims.common.config; package com.hnac.hzims.common.config;
import com.google.common.collect.Lists;
import lombok.AllArgsConstructor;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.ApplicationArguments; import org.springframework.boot.ApplicationArguments;
import org.springframework.boot.ApplicationRunner; import org.springframework.boot.ApplicationRunner;
import org.springframework.context.ApplicationContext; import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.support.GenericApplicationContext; import org.springframework.context.support.GenericApplicationContext;
import org.springframework.core.annotation.AnnotationUtils; import org.springframework.core.annotation.AnnotationUtils;
import org.springframework.data.redis.connection.RedisConnectionFactory; import org.springframework.data.redis.connection.RedisConnectionFactory;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.listener.PatternTopic; import org.springframework.data.redis.listener.PatternTopic;
import org.springframework.data.redis.listener.RedisMessageListenerContainer; import org.springframework.data.redis.listener.RedisMessageListenerContainer;
import org.springframework.data.redis.listener.adapter.MessageListenerAdapter; import org.springframework.data.redis.listener.adapter.MessageListenerAdapter;
@ -20,7 +15,6 @@ import org.springframework.stereotype.Component;
import org.springframework.util.ReflectionUtils; import org.springframework.util.ReflectionUtils;
import java.lang.reflect.Method; import java.lang.reflect.Method;
import java.util.ArrayList;
import java.util.Objects; import java.util.Objects;
import java.util.concurrent.atomic.AtomicLong; import java.util.concurrent.atomic.AtomicLong;

66
hzims-biz-common/src/main/java/com/hnac/hzims/common/launch/LauncherServiceImpl.java

@ -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));
}
}

269
hzims-biz-common/src/main/java/com/hnac/hzims/common/logs/aop/SysLogAspect.java

@ -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);
}
}
}
}
}

2
hzims-biz-common/src/main/java/com/hnac/hzims/common/service/UserAuthDataService.java

@ -1,8 +1,8 @@
package com.hnac.hzims.common.service; package com.hnac.hzims.common.service;
import com.hnac.hzims.common.logs.utils.StringUtils;
import lombok.AllArgsConstructor; import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j; import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.StringUtils;
import org.springblade.core.tool.api.R; import org.springblade.core.tool.api.R;
import org.springblade.system.user.entity.User; import org.springblade.system.user.entity.User;
import org.springblade.system.user.feign.IUserClient; import org.springblade.system.user.feign.IUserClient;

3
hzims-biz-common/src/main/java/com/hnac/hzims/common/statistics/Charts.java

@ -1,16 +1,13 @@
package com.hnac.hzims.common.statistics; package com.hnac.hzims.common.statistics;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiModel; import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty; import io.swagger.annotations.ApiModelProperty;
import lombok.Data; import lombok.Data;
import lombok.EqualsAndHashCode; import lombok.EqualsAndHashCode;
import java.io.Serializable; import java.io.Serializable;
import java.lang.reflect.Array;
import java.util.List; import java.util.List;
@ApiModel("统计图表") @ApiModel("统计图表")
@Data @Data
@EqualsAndHashCode @EqualsAndHashCode

8
hzims-biz-common/src/main/java/com/hnac/hzims/common/support/utils/Condition.java

@ -1,6 +1,5 @@
package com.hnac.hzims.common.support.utils; package com.hnac.hzims.common.support.utils;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper; import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper; import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.google.common.collect.Lists; import com.google.common.collect.Lists;
@ -8,13 +7,14 @@ import com.hnac.hzims.common.support.constants.Order;
import org.springblade.core.log.exception.ServiceException; import org.springblade.core.log.exception.ServiceException;
import org.springblade.core.mp.support.QueryField; import org.springblade.core.mp.support.QueryField;
import org.springblade.core.mp.support.SqlCondition; import org.springblade.core.mp.support.SqlCondition;
import org.springblade.core.tool.utils.BeanUtil;
import org.springblade.core.tool.utils.Func; import org.springblade.core.tool.utils.Func;
import org.springframework.util.Assert; import org.springframework.util.Assert;
import java.lang.reflect.Field; import java.lang.reflect.Field;
import java.util.*; import java.util.Arrays;
import java.util.function.Function; import java.util.List;
import java.util.Map;
import java.util.Optional;
public class Condition extends org.springblade.core.mp.support.Condition { public class Condition extends org.springblade.core.mp.support.Condition {

1
hzims-biz-common/src/main/java/com/hnac/hzims/common/utils/CacheUtil.java

@ -2,7 +2,6 @@ package com.hnac.hzims.common.utils;
import com.alibaba.fastjson.JSON; import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject; import com.alibaba.fastjson.JSONObject;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j; import lombok.extern.slf4j.Slf4j;
import org.springblade.core.tool.utils.Func; import org.springblade.core.tool.utils.Func;
import org.springblade.core.tool.utils.ObjectUtil; import org.springblade.core.tool.utils.ObjectUtil;

1
hzims-biz-common/src/main/java/com/hnac/hzims/common/utils/Condition.java

@ -5,7 +5,6 @@ import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import org.springblade.core.log.exception.ServiceException; import org.springblade.core.log.exception.ServiceException;
import org.springblade.core.mp.support.QueryField; import org.springblade.core.mp.support.QueryField;
import org.springblade.core.mp.support.SqlCondition; import org.springblade.core.mp.support.SqlCondition;
import org.springblade.core.tenant.mp.TenantEntity;
import org.springblade.core.tool.utils.Func; import org.springblade.core.tool.utils.Func;
import org.springblade.core.tool.utils.ObjectUtil; import org.springblade.core.tool.utils.ObjectUtil;

Loading…
Cancel
Save