更新数据同步

This commit is contained in:
2025-11-17 14:08:14 +08:00
parent 603c12cea2
commit add23676b2
23 changed files with 810 additions and 759 deletions

View File

@@ -2,21 +2,14 @@ package com.mini.capi.api.job;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.mini.capi.biz.domain.BizDeviceInfo;
import com.mini.capi.biz.domain.BizMonitorAccount;
import com.mini.capi.biz.domain.BizMonitorHost;
import com.mini.capi.biz.domain.BizServerInfo;
import com.mini.capi.biz.service.BizDeviceInfoService;
import com.mini.capi.biz.service.BizMonitorAccountService;
import com.mini.capi.biz.service.BizMonitorHostService;
import com.mini.capi.biz.service.BizServerInfoService;
import com.mini.capi.biz.domain.*;
import com.mini.capi.biz.service.*;
import com.mini.capi.model.ApiResult;
import com.mini.capi.model.info.CpuInfo;
import com.mini.capi.model.info.DiskInfo;
import com.mini.capi.model.info.ServerInfo;
import com.mini.capi.utils.NetworkUtils;
import com.mini.capi.utils.SystemInfoUtil;
import com.mini.capi.utils.vId;
import com.mini.capi.model.info.TableTree;
import com.mini.capi.utils.*;
import jakarta.annotation.Resource;
import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor;
import org.springframework.util.StringUtils;
@@ -47,10 +40,22 @@ public class jobController {
@Resource
private BizDeviceInfoService bizDeviceInfoService;
@Resource
private BizDbConfigService bizDbConfigService;
@Resource
private DataTableInfoService dataTableInfoService;
@Resource
private DataTableFieldService dataTableFieldService;
// 注入配置好的线程池
@Resource(name = "hostMonitorExecutor")
private ThreadPoolTaskExecutor hostMonitorExecutor;
private static final LoggerUtils logger = LoggerUtils.getInstance();
/**
* 主机在线状态检测
*/
@@ -71,7 +76,7 @@ public class jobController {
}
bizMonitorHostService.update(monitorHost, updateWrapper);
} catch (Exception e) {
System.out.println(e.getMessage());
logger.error(e.getMessage());
}
}, hostMonitorExecutor); // 指定使用配置的线程池
futures.add(future);
@@ -86,66 +91,89 @@ public class jobController {
}
/**
* 数据表同步
*/
@GetMapping("getJobDataTableMarge")
public ApiResult<?> getJobDataTableMarge() {
List<BizDbConfig> configs = bizDbConfigService.list();
List<CompletableFuture<Void>> futures = new ArrayList<>(configs.size());
for (BizDbConfig config : configs) {
CompletableFuture<Void> future = CompletableFuture.runAsync(() -> {
try {
List<TableTree> tableTrees = MysqlUtils.getTableTrees(config);
for (TableTree tableTree : tableTrees) {
DataTableInfo tableInfo = tableTree.getTableInfo();
List<DataTableField> tableFields = tableTree.getTableFields();
for (DataTableField field : tableFields) {
dataTableFieldService.save(field);
}
dataTableInfoService.save(tableInfo);
}
} catch (Exception e) {
logger.error(e.getMessage());
}
}, hostMonitorExecutor); // 指定使用配置的线程池
futures.add(future);
}
try {
CompletableFuture.allOf(futures.toArray(new CompletableFuture[0]))
.get(60, TimeUnit.SECONDS); // 超时时间可根据业务调整
return ApiResult.success();
} catch (Exception e) {
return ApiResult.error(101, e.getMessage());
}
}
public void syncServerInfo(BizMonitorHost host) {
try {
QueryWrapper<BizMonitorAccount> queryWrapper = new QueryWrapper<>();
queryWrapper.eq("host_id", host.getHostId()).eq("ssh_username", "ogsapp");
BizMonitorAccount account = bizMonitorAccountService.getOne(queryWrapper);
if (account != null) {
CpuInfo cpuInfo = SystemInfoUtil.getCpuMemUsage(host.getIpAddress(), account.getSshPort(), account.getSshUsername(), account.getSshPassword());
ServerInfo info = SystemInfoUtil.getServerBasicInfo(host.getIpAddress(), account.getSshPort(), account.getSshUsername(), account.getSshPassword(), host.getIpAddress());
List<DiskInfo> diskInfos = SystemInfoUtil.getDiskInfos(host.getIpAddress(), account.getSshPort(), account.getSshUsername(), account.getSshPassword());
syncDeviceInfo(host, diskInfos);
Optional<BizServerInfo> serverInfoOpt = Optional.ofNullable(
bizServerInfoService.getOne(new QueryWrapper<BizServerInfo>().eq("host_id", host.getHostId()))
);
BizServerInfo serverInfo = serverInfoOpt.orElseGet(() -> {
BizServerInfo newInfo = new BizServerInfo();
newInfo.setHostId(host.getHostId()); // 初始化唯一标识
return newInfo;
});
serverInfo.setUptime(info.getUptime());
serverInfo.setOs(info.getOs());
serverInfo.setKernelVersion(info.getKernelVersion());
serverInfo.setHostname(info.getHostname());
serverInfo.setIpAddress(info.getIpAddress());
serverInfo.setCpuModel(info.getCpuModel());
serverInfo.setMemoryTotal(info.getMemoryTotal());
serverInfo.setCpuUsage(cpuInfo.getCpuUsage());
serverInfo.setMemoryUsage(cpuInfo.getMemoryUsage());
serverInfo.setLastOnlineTime(LocalDateTime.now());
UpdateWrapper<BizServerInfo> updateWrapper = new UpdateWrapper<>();
updateWrapper.eq("host_id", host.getHostId());
bizServerInfoService.saveOrUpdate(serverInfo, updateWrapper);
}
// 查询账号,不存在直接返回
QueryWrapper<BizMonitorAccount> accountQuery = new QueryWrapper<>();
accountQuery.eq("host_id", host.getHostId()).eq("ssh_username", "ogsapp");
BizMonitorAccount account = bizMonitorAccountService.getOne(accountQuery);
if (account == null) return;
// 获取服务器信息
CpuInfo cpuInfo = SystemInfoUtil.getCpuMemUsage(host.getIpAddress(), account.getSshPort(), account.getSshUsername(), account.getSshPassword());
ServerInfo info = SystemInfoUtil.getServerBasicInfo(host.getIpAddress(), account.getSshPort(), account.getSshUsername(), account.getSshPassword(), host.getIpAddress());
List<DiskInfo> diskInfos = SystemInfoUtil.getDiskInfos(host.getIpAddress(), account.getSshPort(), account.getSshUsername(), account.getSshPassword());
syncDeviceInfo(host, diskInfos);
// 查询是否存在对应记录
QueryWrapper<BizServerInfo> queryWrapper = new QueryWrapper<>();
queryWrapper.eq("host_id", host.getHostId());
BizServerInfo serverInfo = bizServerInfoService.getOne(queryWrapper);
bizServerInfoService.removeById(serverInfo.getId());
serverInfo.setUptime(info.getUptime());
serverInfo.setOs(info.getOs());
serverInfo.setKernelVersion(info.getKernelVersion());
serverInfo.setHostname(info.getHostname());
serverInfo.setIpAddress(info.getIpAddress());
serverInfo.setCpuModel(info.getCpuModel());
serverInfo.setMemoryTotal(info.getMemoryTotal());
serverInfo.setCpuUsage(cpuInfo.getCpuUsage());
serverInfo.setMemoryUsage(cpuInfo.getMemoryUsage());
serverInfo.setLastOnlineTime(LocalDateTime.now());
serverInfo.setHostId(host.getHostId()); // 新对象初始化唯一标识
bizServerInfoService.save(serverInfo);
} catch (Exception e) {
System.out.print(e.getMessage());
logger.error(e.getMessage());
}
}
public void syncDeviceInfo(BizMonitorHost host, List<DiskInfo> diskInfos) {
for (DiskInfo diskInfo : diskInfos) {
Optional<BizDeviceInfo> deviceInfoOpt = Optional.ofNullable(
bizDeviceInfoService.getOne(new QueryWrapper<BizDeviceInfo>().eq("host_id", host.getHostId()).eq("device", diskInfo.getDevice()).eq("mount_point", diskInfo.getMountPoint()))
);
BizDeviceInfo deviceInfo = deviceInfoOpt.orElseGet(() -> {
BizDeviceInfo newInfo = new BizDeviceInfo();
newInfo.setHostId(host.getHostId()); // 初始化唯一标识
return newInfo;
});
QueryWrapper<BizDeviceInfo> queryWrapper = new QueryWrapper<>();
queryWrapper.eq("host_id", host.getHostId()).eq("device", diskInfo.getDevice()).eq("mount_point", diskInfo.getMountPoint());
BizDeviceInfo deviceInfo = bizDeviceInfoService.getOne(queryWrapper);
bizDeviceInfoService.removeById(deviceInfo.getId());
deviceInfo.setDevice(diskInfo.getDevice());
deviceInfo.setMountPoint(diskInfo.getMountPoint());
deviceInfo.setTotalSize(diskInfo.getTotalSize());
deviceInfo.setUsedSize(diskInfo.getUsedSize());
deviceInfo.setUsageRate(diskInfo.getUsageRate());
deviceInfo.setLastOnlineTime(LocalDateTime.now());
UpdateWrapper<BizDeviceInfo> updateWrapper = new UpdateWrapper<>();
updateWrapper.eq("host_id", host.getHostId()).eq("device", diskInfo.getDevice()).eq("mount_point", diskInfo.getMountPoint());
bizDeviceInfoService.saveOrUpdate(deviceInfo, updateWrapper);
deviceInfo.setHostId(host.getHostId());
bizDeviceInfoService.save(deviceInfo);
}
}
}

View File

@@ -1,4 +1,4 @@
package com.mini.capi.api.sys;
package com.mini.capi.api.service;
import com.mini.capi.config.component.AppNetworkInfo;
import com.mini.capi.config.component.AppPathInfo;
@@ -23,7 +23,7 @@ public class sysService {
public RunInfo getRunInfo() throws Exception {
RunInfo runInfo = RunInfo.builder()
return RunInfo.builder()
.runtimeInfo(RuntimeInfo.builder()
.appName(appRuntimeInfo.getAppName())
.serverPort(appRuntimeInfo.getServerPort())
@@ -49,6 +49,5 @@ public class sysService {
.allIps(appNetworkInfo.getAllIps())
.build())
.build();
return runInfo;
}
}

View File

@@ -1,8 +1,7 @@
package com.mini.capi.api.sys;
import com.mini.capi.config.component.*;
import com.mini.capi.api.service.sysService;
import com.mini.capi.model.ApiResult;
import com.mini.capi.model.info.*;
import jakarta.annotation.Resource;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;

View File

@@ -0,0 +1,18 @@
package com.mini.capi.biz.controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
/**
* <p>
* 数据库连接配置表 前端控制器
* </p>
*
* @author gaoxq
* @since 2025-11-17
*/
@RestController
@RequestMapping("/biz/bizDbConfig")
public class BizDbConfigController {
}

View File

@@ -0,0 +1,98 @@
package com.mini.capi.biz.domain;
import com.baomidou.mybatisplus.annotation.IdType;
import com.baomidou.mybatisplus.annotation.TableField;
import com.baomidou.mybatisplus.annotation.TableId;
import com.baomidou.mybatisplus.annotation.TableName;
import java.io.Serializable;
import java.time.LocalDateTime;
import lombok.Getter;
import lombok.Setter;
/**
* <p>
* 数据库连接配置表
* </p>
*
* @author gaoxq
* @since 2025-11-17
*/
@Getter
@Setter
@TableName("biz_db_config")
public class BizDbConfig implements Serializable {
private static final long serialVersionUID = 1L;
/**
* 创建时间
*/
@TableField("create_time")
private LocalDateTime createTime;
/**
* 主键ID
*/
@TableId(value = "id", type = IdType.AUTO)
private String id;
/**
* 数据库类型
*/
@TableField("db_type")
private String dbType;
/**
* 数据库名称
*/
@TableField("db_name")
private String dbName;
/**
* IP地址
*/
@TableField("db_ip")
private String dbIp;
/**
* 端口
*/
@TableField("db_port")
private Integer dbPort;
/**
* 账号
*/
@TableField("db_username")
private String dbUsername;
/**
* 密码
*/
@TableField("db_password")
private String dbPassword;
/**
* 配置描述
*/
@TableField("description")
private String description;
/**
* 是否启用1=启用0=禁用
*/
@TableField("is_enabled")
private Integer isEnabled;
/**
* schema名称
*/
@TableField("schema_name")
private String schemaName;
/**
* 更新时间
*/
@TableField("update_time")
private LocalDateTime updateTime;
}

View File

@@ -15,7 +15,7 @@ import lombok.Setter;
* </p>
*
* @author gaoxq
* @since 2025-11-16
* @since 2025-11-17
*/
@Getter
@Setter
@@ -42,6 +42,18 @@ public class DataTableField implements Serializable {
@TableField("table_id")
private String tableId;
/**
* 数据来源
*/
@TableField("data_source")
private String dataSource;
/**
* 数据表名称
*/
@TableField("table_name")
private String tableName;
/**
* 字段序号(表示字段在表中的顺序)
*/
@@ -64,7 +76,7 @@ public class DataTableField implements Serializable {
* 字段长度如varchar(50)中的50数值型可表示精度
*/
@TableField("field_length")
private Integer fieldLength;
private Long fieldLength;
/**
* 字段说明
@@ -72,6 +84,12 @@ public class DataTableField implements Serializable {
@TableField("field_remark")
private String fieldRemark;
/**
* 分区日期
*/
@TableField("ds")
private String ds;
/**
* 租户id
*/

View File

@@ -16,7 +16,7 @@ import lombok.Setter;
* </p>
*
* @author gaoxq
* @since 2025-11-16
* @since 2025-11-17
*/
@Getter
@Setter
@@ -79,6 +79,24 @@ public class DataTableInfo implements Serializable {
@TableField("update_time")
private LocalDateTime updateTime;
/**
* 备注信息
*/
@TableField("remarks")
private String remarks;
/**
* 数据标识
*/
@TableField("db_id")
private String dbId;
/**
* 分区日期
*/
@TableField("ds")
private String ds;
/**
* 租户id
*/
@@ -102,10 +120,4 @@ public class DataTableInfo implements Serializable {
*/
@TableField("f_flow_state")
private Integer fFlowState;
/**
* 备注信息
*/
@TableField("remarks")
private String remarks;
}

View File

@@ -0,0 +1,16 @@
package com.mini.capi.biz.mapper;
import com.mini.capi.biz.domain.BizDbConfig;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
/**
* <p>
* 数据库连接配置表 Mapper 接口
* </p>
*
* @author gaoxq
* @since 2025-11-17
*/
public interface BizDbConfigMapper extends BaseMapper<BizDbConfig> {
}

View File

@@ -9,7 +9,7 @@ import com.baomidou.mybatisplus.core.mapper.BaseMapper;
* </p>
*
* @author gaoxq
* @since 2025-11-16
* @since 2025-11-17
*/
public interface DataTableFieldMapper extends BaseMapper<DataTableField> {

View File

@@ -9,7 +9,7 @@ import com.baomidou.mybatisplus.core.mapper.BaseMapper;
* </p>
*
* @author gaoxq
* @since 2025-11-16
* @since 2025-11-17
*/
public interface DataTableInfoMapper extends BaseMapper<DataTableInfo> {

View File

@@ -0,0 +1,16 @@
package com.mini.capi.biz.service;
import com.mini.capi.biz.domain.BizDbConfig;
import com.baomidou.mybatisplus.extension.service.IService;
/**
* <p>
* 数据库连接配置表 服务类
* </p>
*
* @author gaoxq
* @since 2025-11-17
*/
public interface BizDbConfigService extends IService<BizDbConfig> {
}

View File

@@ -0,0 +1,20 @@
package com.mini.capi.biz.service.impl;
import com.mini.capi.biz.domain.BizDbConfig;
import com.mini.capi.biz.mapper.BizDbConfigMapper;
import com.mini.capi.biz.service.BizDbConfigService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;
/**
* <p>
* 数据库连接配置表 服务实现类
* </p>
*
* @author gaoxq
* @since 2025-11-17
*/
@Service
public class BizDbConfigServiceImpl extends ServiceImpl<BizDbConfigMapper, BizDbConfig> implements BizDbConfigService {
}

View File

@@ -1,27 +1,22 @@
package com.mini.capi.biz;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.mini.capi.api.sys.sysService;
import com.mini.capi.api.service.sysService;
import com.mini.capi.biz.domain.*;
import com.mini.capi.biz.service.*;
import com.mini.capi.model.info.CpuInfo;
import com.mini.capi.model.info.DiskInfo;
import com.mini.capi.model.info.RunInfo;
import com.mini.capi.model.info.ServerInfo;
import com.mini.capi.utils.DateUtils;
import com.mini.capi.utils.SqlUtils;
import com.mini.capi.utils.SystemInfoUtil;
import com.mini.capi.utils.vDate;
import jakarta.annotation.Resource;
import jakarta.servlet.http.HttpSession;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.GetMapping;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;
import java.util.stream.Collectors;
@Controller
public class viewController {
@@ -74,7 +69,9 @@ public class viewController {
return "redirect:/swagger-ui/index.html";
}
/**
* 首页
*/
@GetMapping("/biz/index")
public String getUserHome(Model model, HttpSession session) throws Exception {
RunInfo runInfo = service.getRunInfo();
@@ -96,6 +93,9 @@ public class viewController {
return "index";
}
/**
* 主机运行状态
*/
@GetMapping("/biz/getServerInfo")
public String getServerInfo(Model model, String hostId) {
// 1. 查询服务器信息确保不为null
@@ -113,37 +113,38 @@ public class viewController {
return "server";
}
@GetMapping("/biz/getFieldDetail")
public String getFieldDetail(Model model, String tableId) {
DataTableInfo tableInfo = dataTableInfoService.getById(tableId);
QueryWrapper<DataTableField> queryWrapper = new QueryWrapper<>();
queryWrapper.eq("table_id", tableId);
queryWrapper.orderByAsc("field_order");
List<DataTableField> fields = dataTableFieldService.list(queryWrapper);
String createSql = SqlUtils.CreateTableSql(tableInfo, fields);
String selectSql = SqlUtils.SelectSqlComments(tableInfo, fields);
model.addAttribute("createSql", createSql != null ? createSql : null);
model.addAttribute("selectSql", selectSql != null ? selectSql : null);
model.addAttribute("tableInfo", tableInfo != null ? tableInfo : new DataTableInfo());
model.addAttribute("fields", fields != null ? fields : Collections.emptyList());
return "field";
}
/**
* 数据地图
*/
@GetMapping("/biz/dataMap")
public String getDataMap(Model model) {
List<DataTableInfo> tables = dataTableInfoService.list();
Collections.sort(tables,
Comparator.nullsLast(Comparator.comparing(DataTableInfo::getCreateTime)).reversed()
);
QueryWrapper<DataTableInfo> queryWrapper = new QueryWrapper<>();
queryWrapper.eq("ds", DateUtils.dsValue());
List<DataTableInfo> tables = dataTableInfoService.list(queryWrapper);
tables.sort(Comparator.nullsLast(Comparator.comparing(DataTableInfo::getCreateTime)).reversed());
model.addAttribute("tables", tables);
return "data";
}
/**
* 数据地图明细
*/
@GetMapping("/biz/getFieldDetail")
public String getFieldDetail(Model model, String tableId) {
DataTableInfo tableInfo = dataTableInfoService.getById(tableId);
QueryWrapper<DataTableField> queryWrapper = new QueryWrapper<>();
queryWrapper.eq("table_id", tableId).eq("ds", DateUtils.dsValue());
queryWrapper.orderByAsc("field_order");
List<DataTableField> fields = dataTableFieldService.list(queryWrapper);
String createSql = SqlUtils.CreateTableSql(tableInfo, fields);
String selectSql = SqlUtils.SelectSqlComments(tableInfo, fields);
model.addAttribute("createSql", createSql);
model.addAttribute("selectSql", selectSql);
model.addAttribute("tableInfo", tableInfo);
model.addAttribute("fields", fields);
return "field";
}
/**
* 文档中心
*/
@@ -158,7 +159,9 @@ public class viewController {
return "file";
}
/**
* 系统管理
*/
@GetMapping("/biz/system")
public String getSystem(Model model) {

View File

@@ -0,0 +1,24 @@
package com.mini.capi.model.info;
import com.mini.capi.biz.domain.DataTableField;
import com.mini.capi.biz.domain.DataTableInfo;
import lombok.Data;
import java.io.Serializable;
import java.util.List;
@Data
public class TableTree implements Serializable {
DataTableInfo tableInfo;
List<DataTableField> tableFields;
public TableTree() {
}
public TableTree(DataTableInfo tableInfo, List<DataTableField> tableFields) {
this.tableInfo = tableInfo;
this.tableFields = tableFields;
}
}

View File

@@ -29,7 +29,7 @@ public class demo {
.pathInfo(Collections.singletonMap(OutputFile.xml, System.getProperty("user.dir") + "/src/main/resources/mapper"));
})
.strategyConfig(builder -> {
builder.addInclude("biz_base_notice_view")
builder.addInclude("data_table_info,data_table_field")
.addTablePrefix("biz_,erp_")
.entityBuilder()
.enableLombok()

View File

@@ -1,7 +1,6 @@
package com.mini.capi.utils;
import java.time.LocalDate;
import java.time.format.DateTimeFormatter;
@@ -10,6 +9,7 @@ public class DateUtils {
// 日期格式化器(线程安全,复用提升性能)
private static final DateTimeFormatter DAY_FORMATTER = DateTimeFormatter.ISO_LOCAL_DATE; // yyyy-MM-dd
private static final DateTimeFormatter MONTH_FORMATTER = DateTimeFormatter.ofPattern("yyyy-MM"); // yyyy-MM
private static final DateTimeFormatter DATE_FORMATTER = DateTimeFormatter.ofPattern("yyyyMMdd");
public static String calculateStartCycleCode(String cycleType) {
@@ -63,4 +63,9 @@ public class DateUtils {
return String.format("%d-Q%d", date.getYear(), quarter);
}
public static String dsValue() {
LocalDate currentDate = LocalDate.now();
// 格式化日期为yyyymmdd
return currentDate.format(DATE_FORMATTER);
}
}

View File

@@ -0,0 +1,195 @@
package com.mini.capi.utils;
import java.io.*;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.concurrent.locks.ReentrantLock;
public class LoggerUtils {
// 日志级别
public enum Level {
DEBUG, INFO, WARN, ERROR
}
// 单例实例
private static volatile LoggerUtils instance;
// 日志文件根路径(默认:/ogsapp/logs/cApi
private String baseLogPath;
// 日期格式文件名yyyyMMdd日志内容时间戳yyyy-MM-dd HH:mm:ss.SSS
private final SimpleDateFormat fileDateFormat = new SimpleDateFormat("yyyyMMdd");
private final SimpleDateFormat logDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss.SSS");
// 线程安全锁
private final ReentrantLock lock = new ReentrantLock();
// 禁止外部实例化
private LoggerUtils(String baseLogPath) {
this.baseLogPath = baseLogPath;
initLogDir(); // 初始化日志目录
}
/**
* 获取单例实例(使用默认日志目录:/ogsapp/logs/cApi
*/
public static LoggerUtils getInstance() {
return getInstance("/ogsapp/logs/cApi");
}
/**
* 获取单例实例(自定义日志路径)
*
* @param baseLogPath 日志根目录(支持相对路径或绝对路径)
*/
public static LoggerUtils getInstance(String baseLogPath) {
if (instance == null) {
synchronized (LoggerUtils.class) {
if (instance == null) {
instance = new LoggerUtils(baseLogPath);
}
}
}
return instance;
}
/**
* 初始化日志目录(若不存在则创建多级目录)
*/
private void initLogDir() {
try {
Files.createDirectories(Paths.get(baseLogPath));
} catch (IOException e) {
throw new RuntimeException("初始化日志目录失败:" + baseLogPath, e);
}
}
/**
* 获取当前日志文件路径按日期拆分baseLogPath/log_yyyyMMdd.txt
*/
private String getCurrentLogFilePath(Level level) {
String fileName = level + "_" + fileDateFormat.format(new Date()) + ".log";
return baseLogPath + File.separator + fileName;
}
// ------------------------------ 日志方法(支持多类型可变参数) ------------------------------
/**
* 记录DEBUG级别日志支持1到多个任意类型参数
*/
public void debug(Object... messages) {
log(Level.DEBUG, joinMessages(messages), null);
}
/**
* 记录INFO级别日志支持1到多个任意类型参数
*/
public void info(Object... messages) {
log(Level.INFO, joinMessages(messages), null);
}
/**
* 记录WARN级别日志支持1到多个任意类型参数
*/
public void warn(Object... messages) {
log(Level.WARN, joinMessages(messages), null);
}
/**
* 记录WARN级别日志支持1到多个任意类型参数+异常)
*/
public void warn(Object[] messages, Throwable throwable) {
log(Level.WARN, joinMessages(messages), throwable);
}
/**
* 记录ERROR级别日志支持1到多个任意类型参数
*/
public void error(Object... messages) {
log(Level.ERROR, joinMessages(messages), null);
}
/**
* 记录ERROR级别日志支持1到多个任意类型参数+异常)
*/
public void error(Object[] messages, Throwable throwable) {
log(Level.ERROR, joinMessages(messages), throwable);
}
// ------------------------------ 核心方法 ------------------------------
/**
* 核心日志写入逻辑
*/
private void log(Level level, String message, Throwable throwable) {
// 构建日志内容
StringBuilder logContent = new StringBuilder();
logContent.append("[").append(logDateFormat.format(new Date())).append("] "); // 时间戳
logContent.append("[").append(level.name()).append("] "); // 日志级别
logContent.append("[Thread-").append(Thread.currentThread().getId()).append("] "); // 线程ID
logContent.append(message); // 拼接后的消息
// 追加异常堆栈信息(如果有)
if (throwable != null) {
logContent.append("\n").append(getStackTrace(throwable));
}
logContent.append("\n"); // 每条日志换行
// 加锁写入文件(保证线程安全)
lock.lock();
try (BufferedWriter writer = new BufferedWriter(
new OutputStreamWriter(
new FileOutputStream(getCurrentLogFilePath(level), true), // 追加模式
StandardCharsets.UTF_8 // 避免中文乱码
)
)) {
writer.write(logContent.toString());
writer.flush();
} catch (IOException e) {
System.err.println("日志写入失败:" + e.getMessage());
} finally {
lock.unlock();
}
}
/**
* 拼接多类型可变参数为单个字符串自动转换任意类型为字符串处理null
*
* @param messages 1到多个任意类型参数不可为空数组
* @return 拼接后的字符串
*/
private String joinMessages(Object... messages) {
if (messages == null || messages.length == 0) {
throw new IllegalArgumentException("日志消息至少需要1个参数");
}
StringBuilder sb = new StringBuilder();
for (Object msg : messages) {
// 任意类型转换为字符串null转为"null"其他类型调用String.valueOf()
sb.append(String.valueOf(msg));
}
return sb.toString();
}
/**
* 异常堆栈信息转为字符串
*/
private String getStackTrace(Throwable throwable) {
StringWriter sw = new StringWriter();
PrintWriter pw = new PrintWriter(sw);
throwable.printStackTrace(pw);
return sw.toString();
}
/**
* 动态修改日志路径
*/
public void setBaseLogPath(String baseLogPath) {
this.baseLogPath = baseLogPath;
initLogDir();
}
}

View File

@@ -0,0 +1,215 @@
package com.mini.capi.utils;
import com.mini.capi.biz.domain.BizDbConfig;
import com.mini.capi.biz.domain.DataTableField;
import com.mini.capi.biz.domain.DataTableInfo;
import com.mini.capi.model.info.TableTree;
import java.math.BigDecimal;
import java.math.RoundingMode;
import java.sql.*;
import java.time.LocalDateTime;
import java.time.ZoneId;
import java.util.*;
import java.util.Date;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class MysqlUtils {
private static final LoggerUtils logger = LoggerUtils.getInstance();
// 需要排除的系统数据库
private static final List<String> SYSTEM_DATABASES = Arrays.asList(
"information_schema", "mysql", "performance_schema", "sys", "test"
);
// 提取字段长度的正则表达式如varchar(50) -> 50
private static final Pattern LENGTH_PATTERN = Pattern.compile("\\((\\d+)\\)");
/**
* 封装获取MySQL数据库连接
*/
private static Connection getConnection(String ip, Integer port, String username, String password) throws Exception {
String driver = "com.mysql.cj.jdbc.Driver";
String jdbcUrl = String.format(
"jdbc:mysql://%s:%d/information_schema?useSSL=false&serverTimezone=UTC&allowPublicKeyRetrieval=true",
ip, port
);
Class.forName(driver); // 加载驱动
return DriverManager.getConnection(jdbcUrl, username, password);
}
/**
* 获取指定MySQL连接的所有非系统库表结构信息
*
* @param ip MySQL服务器IP
* @param port 端口默认3306
* @param username 用户名
* @param password 密码
* @return 数据库名 -> 表信息列表(包含字段)的映射
* @throws Exception 连接或查询异常
*/
public static Map<String, List<DataTableInfo>> getMysqlSchemaInfo(Connection conn) throws Exception {
Map<String, List<DataTableInfo>> result = new HashMap<>();
// 1. 获取所有非系统数据库
List<String> databases = getNonSystemDatabases(conn);
logger.info("获取到非系统数据库数量:", databases.size());
// 2. 遍历数据库,获取表和字段信息
for (String dbName : databases) {
List<DataTableInfo> tableInfos = getTablesByDatabase(conn, dbName);
result.put(dbName, tableInfos);
}
return result;
}
/**
* 获取所有非系统数据库
*/
private static List<String> getNonSystemDatabases(Connection conn) throws SQLException {
List<String> databases = new ArrayList<>();
String sql = "SELECT SCHEMA_NAME FROM SCHEMATA WHERE SCHEMA_NAME NOT IN ("
+ String.join(",", Collections.nCopies(SYSTEM_DATABASES.size(), "?")) + ")";
try (PreparedStatement ps = conn.prepareStatement(sql)) {
for (int i = 0; i < SYSTEM_DATABASES.size(); i++) {
ps.setString(i + 1, SYSTEM_DATABASES.get(i));
}
try (ResultSet rs = ps.executeQuery()) {
while (rs.next()) {
databases.add(rs.getString("SCHEMA_NAME"));
}
}
}
return databases;
}
/**
* 获取指定数据库下的所有表信息(包含字段)
*/
private static List<DataTableInfo> getTablesByDatabase(Connection conn, String dbName) throws SQLException {
List<DataTableInfo> tableInfos = new ArrayList<>();
String tableSql = "SELECT " +
"TABLE_NAME, TABLE_COMMENT, CREATE_TIME, UPDATE_TIME, " +
"DATA_LENGTH, INDEX_LENGTH, TABLE_ROWS " +
"FROM TABLES WHERE TABLE_SCHEMA = ?";
try (PreparedStatement tablePs = conn.prepareStatement(tableSql)) {
tablePs.setString(1, dbName);
try (ResultSet tableRs = tablePs.executeQuery()) {
while (tableRs.next()) {
DataTableInfo tableInfo = buildDataTableInfo(tableRs, dbName);
List<DataTableField> fields = getFieldsByTable(conn, dbName, tableInfo.getTableName());
fields.forEach(field -> field.setTableId(tableInfo.getTableId()));
tableInfos.add(tableInfo);
}
}
}
return tableInfos;
}
/**
* 构建DataTableInfo实体
*/
private static DataTableInfo buildDataTableInfo(ResultSet tableRs, String dbName) throws SQLException {
DataTableInfo tableInfo = new DataTableInfo();
tableInfo.setTableId(vId.getUid());
tableInfo.setTableName(tableRs.getString("TABLE_NAME"));
tableInfo.setTableComment(tableRs.getString("TABLE_COMMENT"));
long dataLength = tableRs.getLong("DATA_LENGTH");
long indexLength = tableRs.getLong("INDEX_LENGTH");
BigDecimal tableSize = BigDecimal.valueOf((dataLength + indexLength) / 1024.0 / 1024.0)
.setScale(2, RoundingMode.HALF_UP);
tableInfo.setTableSize(tableSize);
tableInfo.setDataSource(dbName);
tableInfo.setDataRows(tableRs.getLong("TABLE_ROWS"));
Date createDate = tableRs.getTimestamp("CREATE_TIME");
if (createDate != null) {
tableInfo.setCreateTime(createDate.toInstant().atZone(ZoneId.systemDefault()).toLocalDateTime());
}
Date updateDate = tableRs.getTimestamp("UPDATE_TIME");
if (updateDate != null) {
tableInfo.setUpdateTime(updateDate.toInstant().atZone(ZoneId.systemDefault()).toLocalDateTime());
}
tableInfo.setDs(DateUtils.dsValue());
return tableInfo;
}
/**
* 获取指定表的字段信息
*/
private static List<DataTableField> getFieldsByTable(Connection conn, String dbName, String tableName) throws SQLException {
List<DataTableField> fields = new ArrayList<>();
String fieldSql = "SELECT " +
"TABLE_SCHEMA,TABLE_NAME,COLUMN_NAME, COLUMN_TYPE, COLUMN_COMMENT, " +
"ORDINAL_POSITION, CHARACTER_MAXIMUM_LENGTH " +
"FROM COLUMNS WHERE TABLE_SCHEMA = ? AND TABLE_NAME = ? " +
"ORDER BY ORDINAL_POSITION";
try (PreparedStatement fieldPs = conn.prepareStatement(fieldSql)) {
fieldPs.setString(1, dbName);
fieldPs.setString(2, tableName);
try (ResultSet fieldRs = fieldPs.executeQuery()) {
while (fieldRs.next()) {
fields.add(buildDataTableField(fieldRs));
}
}
}
return fields;
}
/**
* 构建DataTableField实体
*/
private static DataTableField buildDataTableField(ResultSet fieldRs) throws SQLException {
DataTableField field = new DataTableField();
field.setFieldId(vId.getUid());
field.setDataSource(fieldRs.getString("TABLE_SCHEMA"));
field.setTableName(fieldRs.getString("TABLE_NAME"));
field.setFieldName(fieldRs.getString("COLUMN_NAME"));
String fieldType = fieldRs.getString("COLUMN_TYPE");
field.setFieldType(fieldType);
field.setFieldOrder(fieldRs.getInt("ORDINAL_POSITION"));
field.setFieldRemark(fieldRs.getString("COLUMN_COMMENT"));
Long length = fieldRs.getLong("CHARACTER_MAXIMUM_LENGTH");
if (length == 0 || fieldRs.wasNull()) {
length = extractLengthFromType(fieldType);
}
field.setFieldLength(length);
field.setCreateTime(LocalDateTime.now());
field.setDs(DateUtils.dsValue());
return field;
}
/**
* 从字段类型中提取长度如int(11) -> 11
*/
private static Long extractLengthFromType(String fieldType) {
if (fieldType == null) return null;
Matcher matcher = LENGTH_PATTERN.matcher(fieldType);
if (matcher.find()) {
try {
return Long.parseLong(matcher.group(1));
} catch (NumberFormatException e) {
logger.warn("提取字段长度失败,类型", fieldType, e);
}
}
return null;
}
public static List<TableTree> getTableTrees(BizDbConfig dbConfig) {
List<TableTree> tableTrees = new ArrayList<>();
try {
Connection conn = getConnection(dbConfig.getDbIp(), dbConfig.getDbPort(), dbConfig.getDbUsername(), dbConfig.getDbPassword());
Map<String, List<DataTableInfo>> schemaInfo = MysqlUtils.getMysqlSchemaInfo(conn);
for (Map.Entry<String, List<DataTableInfo>> entry : schemaInfo.entrySet()) {
for (DataTableInfo tableInfo : entry.getValue()) {
tableInfo.setDbId(dbConfig.getId());
List<DataTableField> dataTableFields = getFieldsByTable(conn, entry.getKey(), tableInfo.getTableName());
dataTableFields.stream().forEach(tableField -> tableField.setTableId(tableInfo.getTableId()));
tableTrees.add(new TableTree(tableInfo, dataTableFields));
}
}
} catch (Exception e) {
logger.error(e.getMessage());
}
return tableTrees;
}
}

View File

@@ -5,7 +5,6 @@ import java.math.BigDecimal;
import java.math.RoundingMode;
public class vF {
@@ -32,4 +31,8 @@ public class vF {
}
public static boolean isNew(Object o) {
return o == null;
}
}