Merge branch 'feature_lsl' of gitee.com:L-Shilin/go-fly-platform-services into feature_dzc

This commit is contained in:
2025-10-15 01:26:54 +08:00
5 changed files with 129 additions and 107 deletions
@@ -120,7 +120,9 @@ public class FileController {
PageResult<FileInfo> pageResult = fileService.findList(params);
if(pageResult.getCount() >0){
for (FileInfo fileInfo: pageResult.getData()){
fileInfo.setUrl(timeWindowStorageService.getSmartViewUrlByPath(fileInfo.getName()));
String url = timeWindowStorageService.getSmartViewUrlByPath(fileInfo.getName());
log.info("文件查询URL: name={}, url={}", fileInfo.getName(), url);
fileInfo.setUrl(url);
}
}
return pageResult;
@@ -136,17 +138,23 @@ public class FileController {
@GetMapping("/files/{id}")
public Result getViewUrl(@PathVariable String id) {
return Result.succeed(timeWindowStorageService.getSmartViewUrl(id),"获取成功");
String url = timeWindowStorageService.getSmartViewUrl(id);
log.info("文件查询URL: id={}, url={}", id, url);
return Result.succeed(url,"获取成功");
}
@GetMapping("/file/path")
public Result getFileViewUrlByPath(@RequestParam String path) {
return Result.succeed(timeWindowStorageService.getSmartViewUrlByPath(path),"获取成功");
String url = timeWindowStorageService.getSmartViewUrlByPath(path);
log.info("文件查询URL: path={}, url={}", path, url);
return Result.succeed(url,"获取成功");
}
@GetMapping("/files/path")
public String getViewUrlByPath(@RequestParam String path) {
return timeWindowStorageService.getSmartViewUrlByPath(path);
String url = timeWindowStorageService.getSmartViewUrlByPath(path);
log.info("文件查询URL: path={}, url={}", path, url);
return url;
}
@GetMapping("/files/path/style")
@@ -4,6 +4,7 @@ import com.central.common.model.FileInfo;
import com.central.file.service.impl.OssService;
import com.central.file.service.impl.S3Service;
import com.central.file.service.impl.EnhancedS3Service;
import com.central.file.mapper.FileMapper;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
@@ -12,6 +13,7 @@ import org.springframework.stereotype.Service;
import java.time.LocalDateTime;
import java.time.temporal.ChronoUnit;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
/**
* 时间窗口智能存储服务
@@ -36,15 +38,17 @@ public class TimeWindowStorageService {
@Qualifier("enhancedS3FileService")
private EnhancedS3Service enhancedS3Service;
@Autowired(required = false)
private OssToS3SyncService ossToS3SyncService;
// 自动同步逻辑已取消,不再注入同步服务
@Autowired
private FileMapper fileMapper;
// 配置参数
@Value("${zlt.storage.hot-data-days:30}")
private int hotDataDays; // 热数据保留天数,默认30天
@Value("${zlt.storage.enable-time-window:true}")
private boolean enableTimeWindow; // 是否启用时间窗口存储
// @Value("${zlt.storage.enable-time-window:true}")
private boolean enableTimeWindow = true; // 是否启用时间窗口存储
/**
* 智能获取文件访问URL
@@ -64,6 +68,8 @@ public class TimeWindowStorageService {
// 2. 判断存储类型
String storageType = getStorageTypeByTime(fileInfo);
log.info("时间窗口判定: fileId={} createTime={} hotDataDays={} enableTimeWindow={} -> storageType={}",
fileInfo.getId(), fileInfo.getCreateTime(), hotDataDays, enableTimeWindow, storageType);
// 3. 根据存储类型获取URL
String viewUrl = getViewUrlByStorageType(fileInfo, storageType);
@@ -86,22 +92,62 @@ public class TimeWindowStorageService {
*/
public String getSmartViewUrlByPath(String filePath) {
try {
// 1. 根据路径特征判断存储类型
if (isS3Path(filePath)) {
log.debug("S3路径,直接查询S3: path={}", filePath);
return queryFromS3ByPath(filePath);
} else if (isOssPath(filePath)) {
log.debug("OSS路径,直接查询OSS: path={}", filePath);
return ossService.getViewUrlByPath(filePath);
// 先按路径查询数据库,若查到则走时间窗口策略
FileInfo fileInfo = getFileInfoByPath(filePath);
if (fileInfo != null) {
String storageType = getStorageTypeByTime(fileInfo);
log.info("[PATH] 时间窗口判定: path={} createTime={} hotDataDays={} enableTimeWindow={} -> storageType={}",
filePath, fileInfo.getCreateTime(), hotDataDays, enableTimeWindow, storageType);
String url = getViewUrlByStorageType(fileInfo, storageType);
log.info("[PATH] 按时间窗口获取URL: path={} storageType={} url={}", filePath, storageType, url);
return url;
}
// 2. 路径特征不明确,尝试智能查询
log.debug("路径特征不明确,尝试智能查询: path={}", filePath);
return getViewUrlByFallback(filePath);
} catch (Exception e) {
log.error("智能存储查询失败: path={}, error={}", filePath, e.getMessage(), e);
// 若DB无记录,退回统一路径策略:S3优先,其次OSS
log.warn("[PATH] 数据库未找到文件信息,按统一策略查询: path={}", filePath);
// 1) 先尝试S3(不触发任何自动同步)
String s3Url = queryFromS3ByPath(filePath);
if (s3Url != null && !s3Url.isEmpty()) {
log.debug("S3查询成功: path={}", filePath);
return s3Url;
}
// 2) 再尝试OSS
try {
String ossUrl = ossService.getViewUrlByPath(filePath);
if (ossUrl != null && !ossUrl.isEmpty()) {
log.debug("OSS查询成功: path={}", filePath);
return ossUrl;
}
} catch (Exception e) {
log.warn("OSS查询失败: path={}, error={}", filePath, e.getMessage());
}
log.warn("路径查询失败(OSS/S3均未命中): path={}", filePath);
return "";
} catch (Exception e) {
log.error("智能存储路径查询异常: path={}, error={}", filePath, e.getMessage(), e);
return "";
}
}
private FileInfo getFileInfoByPath(String filePath) {
try {
if (filePath == null || filePath.isEmpty()) {
return null;
}
QueryWrapper<FileInfo> wrapper = new QueryWrapper<>();
wrapper.eq("path", filePath).last("limit 1");
FileInfo info = fileMapper.selectOne(wrapper);
if (info == null) {
// 某些历史数据可能将 name 作为相对路径,可兼容一次
wrapper = new QueryWrapper<>();
wrapper.eq("name", filePath).last("limit 1");
info = fileMapper.selectOne(wrapper);
}
return info;
} catch (Exception ex) {
log.warn("按路径查询文件信息异常: path={}, error={}", filePath, ex.getMessage());
return null;
}
}
@@ -155,7 +201,9 @@ public class TimeWindowStorageService {
}
// S3查询失败,降级到OSS
log.warn("S3查询失败,降级到OSS: fileId={}", fileInfo.getId());
return queryFromOss(fileInfo);
String ossUrl = queryFromOss(fileInfo);
log.info("OSS降级查询结果: fileId={}, url={}", fileInfo.getId(), ossUrl);
return ossUrl;
} else {
// 热数据,优先从OSS查询
log.info("热数据,优先从OSS查询: fileId={}", fileInfo.getId());
@@ -166,7 +214,9 @@ public class TimeWindowStorageService {
}
// OSS查询失败,降级到S3
log.warn("OSS查询失败,降级到S3: fileId={}", fileInfo.getId());
return queryFromS3(fileInfo);
String s3Url = queryFromS3(fileInfo);
log.info("S3降级查询结果: fileId={}, url={}", fileInfo.getId(), s3Url);
return s3Url;
}
}
@@ -175,7 +225,13 @@ public class TimeWindowStorageService {
*/
private String queryFromOss(FileInfo fileInfo) {
try {
return ossService.getViewUrlByPath(fileInfo.getPath());
String url = ossService.getViewUrlByPath(fileInfo.getPath());
if (url == null || url.isEmpty()) {
log.warn("从OSS查询为空: fileId={} path={}", fileInfo.getId(), fileInfo.getPath());
} else {
log.debug("从OSS获取URL: fileId={} url={}", fileInfo.getId(), url);
}
return url;
} catch (Exception e) {
log.warn("从OSS查询失败: fileId={}, error={}", fileInfo.getId(), e.getMessage());
return "";
@@ -190,10 +246,22 @@ public class TimeWindowStorageService {
// 优先使用EnhancedS3Service
if (enhancedS3Service != null) {
log.debug("使用EnhancedS3Service查询: fileId={}, path={}", fileInfo.getId(), fileInfo.getPath());
return enhancedS3Service.getViewUrlByPath(fileInfo.getPath());
String url = enhancedS3Service.getViewUrlByPath(fileInfo.getPath());
if (url == null || url.isEmpty()) {
log.warn("从S3查询为空(Enhanced): fileId={} path={}", fileInfo.getId(), fileInfo.getPath());
} else {
log.debug("从S3获取URL(Enhanced): fileId={} url={}", fileInfo.getId(), url);
}
return url;
} else if (s3Service != null) {
log.debug("使用S3Service查询: fileId={}, path={}", fileInfo.getId(), fileInfo.getPath());
return s3Service.getViewUrlByPath(fileInfo.getPath());
String url = s3Service.getViewUrlByPath(fileInfo.getPath());
if (url == null || url.isEmpty()) {
log.warn("从S3查询为空(基础): fileId={} path={}", fileInfo.getId(), fileInfo.getPath());
} else {
log.debug("从S3获取URL(基础): fileId={} url={}", fileInfo.getId(), url);
}
return url;
} else {
log.warn("S3服务未配置,无法查询S3: fileId={}", fileInfo.getId());
return "";
@@ -212,21 +280,7 @@ public class TimeWindowStorageService {
// 优先使用EnhancedS3Service
if (enhancedS3Service != null) {
log.debug("使用EnhancedS3Service查询: path={}", filePath);
String s3Url = enhancedS3Service.getViewUrlByPath(filePath);
// 如果S3中没有找到文件,尝试从OSS同步
if ((s3Url == null || s3Url.isEmpty()) && ossToS3SyncService != null) {
log.info("S3中未找到文件,尝试从OSS同步: path={}", filePath);
boolean syncResult = ossToS3SyncService.syncFileToS3(filePath);
if (syncResult) {
log.info("OSS同步到S3成功,重新获取URL: path={}", filePath);
s3Url = enhancedS3Service.getViewUrlByPath(filePath);
} else {
log.warn("OSS同步到S3失败: path={}", filePath);
}
}
return s3Url;
return enhancedS3Service.getViewUrlByPath(filePath);
} else if (s3Service != null) {
log.debug("使用S3Service查询: path={}", filePath);
return s3Service.getViewUrlByPath(filePath);
@@ -243,61 +297,19 @@ public class TimeWindowStorageService {
/**
* 降级查询策略
*/
private String getViewUrlByFallback(String filePath) {
// 先尝试OSS
try {
String ossUrl = ossService.getViewUrlByPath(filePath);
if (ossUrl != null && !ossUrl.isEmpty()) {
log.debug("OSS降级查询成功: path={}", filePath);
return ossUrl;
}
} catch (Exception e) {
log.warn("OSS降级查询失败: path={}, error={}", filePath, e.getMessage());
}
// 再尝试S3
try {
String s3Url = queryFromS3ByPath(filePath);
if (s3Url != null && !s3Url.isEmpty()) {
log.debug("S3降级查询成功: path={}", filePath);
return s3Url;
}
} catch (Exception e) {
log.warn("S3降级查询失败: path={}, error={}", filePath, e.getMessage());
}
log.warn("所有存储查询都失败: path={}", filePath);
return "";
}
// 删除未使用的降级方法以消除告警
// private String getViewUrlByFallback(String filePath) { return ""; }
/**
* 判断是否为S3路径
*/
private boolean isS3Path(String path) {
return path != null && (
path.startsWith("s3://") ||
path.contains("minio") ||
path.startsWith("cold-storage") ||
path.contains("S3") ||
path.startsWith("hot-sync/") || // 兼容旧的hot-sync前缀
// 只有明确包含S3标识的路径才认为是S3路径
path.contains("s3://") ||
path.contains("minio://")
);
}
// 删除未使用的方法以消除告警
// private boolean isS3Path(String path) { return false; }
/**
* 判断是否为OSS路径
*/
private boolean isOssPath(String path) {
return path != null && (
path.startsWith("oss://") ||
path.contains("aliyun") ||
path.contains("ali-oss") ||
path.contains("OSS") ||
path.startsWith("https://goflyapp.oss-cn-beijing.aliyuncs.com/")
);
}
// private boolean isOssPath(String path) { return false; }
/**
* 检查文件是否需要迁移
@@ -1,7 +1,6 @@
package com.central.file.service.impl;
import cn.hutool.core.util.StrUtil;
import com.central.common.constant.CommonConstant;
import com.central.common.model.FileInfo;
import com.central.oss.model.ObjectInfo;
import com.central.oss.properties.FileServerProperties;
@@ -336,7 +335,7 @@ public class EnhancedS3Service extends AbstractIFileService {
private S3Object parsePath(String path) {
S3Object s3Object = new S3Object();
if (StrUtil.isNotEmpty(path)) {
// 处理s3://bucket/object格式
// 1) 显式 s3://bucket/object 格式
if (path.startsWith("s3://")) {
String[] parts = path.substring(5).split("/", 2);
if (parts.length >= 2) {
@@ -346,23 +345,25 @@ public class EnhancedS3Service extends AbstractIFileService {
s3Object.bucketName = fileServerProperties.getS3().getBucketName();
s3Object.objectName = decodeUrl(parts[0]);
}
} else if (path.startsWith("hot-sync/")) {
// 处理hot-sync前缀的路径(兼容旧格式)
s3Object.bucketName = fileServerProperties.getS3().getBucketName();
s3Object.objectName = decodeUrl(path.substring("hot-sync/".length())); // 去掉hot-sync/前缀并解码
} else {
// 处理相对路径或无前缀路径
int splitIndex = path.lastIndexOf(CommonConstant.PATH_SPLIT);
if (splitIndex != -1) {
s3Object.bucketName = path.substring(0, splitIndex);
s3Object.objectName = decodeUrl(path.substring(splitIndex + 1));
} else {
// 如果没有分隔符,使用配置的bucket
s3Object.bucketName = fileServerProperties.getS3().getBucketName();
s3Object.objectName = decodeUrl(path);
}
return s3Object;
}
// 2) 兼容历史 hot-sync/ 前缀:对象名去掉前缀,bucket 用配置
if (path.startsWith("hot-sync/")) {
s3Object.bucketName = fileServerProperties.getS3().getBucketName();
s3Object.objectName = decodeUrl(path.substring("hot-sync/".length()));
return s3Object;
}
// 3) 统一相对路径策略:bucket 固定取配置,objectName 使用完整 path
// 这样与 OSS 目录保持一致:同一个 filePath 在 S3 作为对象键
s3Object.bucketName = fileServerProperties.getS3().getBucketName();
s3Object.objectName = decodeUrl(path);
return s3Object;
}
// path 为空时,退回配置 bucket,空对象名
s3Object.bucketName = fileServerProperties.getS3().getBucketName();
s3Object.objectName = "";
return s3Object;
}
@@ -8,7 +8,8 @@ import lombok.Getter;
* @author lsl create by 2025/1/10
*/
@Getter
public enum OrderStatus implements ZltEnum {
public enum
OrderStatus implements ZltEnum {
UNPAID(0,"待支付"),
PAID(1, "已支付排队中"),
CANCELED(2, "已取消"),
@@ -29,7 +29,7 @@ zlt.fdfs.trackerList=${zlt.fdfs.web-url}:22122
zlt.s3.access-key=Hq78CaYg2BI6ZfxCRMKI
zlt.s3.accessKeySecret=DJCt5q9K7GQ2zcBEfr3twb1vQ6BgUW1LKFlI5NSw
zlt.s3.endpoint=http://220.205.16.27:9000
zlt.s3.bucketName=goflyapp
zlt.s3.bucketName=goflymedia
zlt.s3.region=us-east-1
zlt.oss.secretId=LTAI5tSNV1RvexXpMMCZhxSe