14 KiB
14 KiB
冷热数据同步与业务访问逻辑详解
系统架构概览
┌─────────────────────────────────────────────────────────────────┐
│ 业务应用层 │
│ ┌─────────────┐ ┌─────────────┐ ┌─────────────┐ │
│ │ 订单中心 │ │ 设备中心 │ │ 其他业务 │ │
│ │ OrderCenter │ │DeviceCenter │ │ 应用模块 │ │
│ └─────────────┘ └─────────────┘ └─────────────┘ │
└─────────────────────────────────────────────────────────────────┘
│
▼
┌─────────────────────────────────────────────────────────────────┐
│ 文件中心服务 │
│ ┌─────────────┐ ┌─────────────┐ ┌─────────────┐ │
│ │ 文件访问 │ │ 智能存储 │ │ 同步服务 │ │
│ │FileController│ │TimeWindow │ │HotDataSync │ │
│ │ │ │StorageService│ │Service │ │
│ └─────────────┘ └─────────────┘ └─────────────┘ │
└─────────────────────────────────────────────────────────────────┘
│
▼
┌─────────────────────────────────────────────────────────────────┐
│ 存储服务层 │
│ ┌─────────────┐ ┌─────────────┐ ┌─────────────┐ │
│ │ OSS服务 │ │ S3服务 │ │ COS服务 │ │
│ │OssService │ │S3Service │ │CosService │ │
│ │ │ │EnhancedS3 │ │ │ │
│ └─────────────┘ └─────────────┘ └─────────────┘ │
└─────────────────────────────────────────────────────────────────┘
│
▼
┌─────────────────────────────────────────────────────────────────┐
│ 存储基础设施 │
│ ┌─────────────┐ ┌─────────────┐ ┌─────────────┐ │
│ │ 阿里云OSS │ │ MinIO/S3 │ │ 腾讯云COS │ │
│ │ (热数据) │ │ (冷数据) │ │ (备选存储) │ │
│ └─────────────┘ └─────────────┘ └─────────────┘ │
└─────────────────────────────────────────────────────────────────┘
核心组件详解
1. 文件访问控制器 (FileController)
职责: 提供统一的文件访问接口,支持业务应用的文件操作
核心方法:
@RestController
public class FileController {
// 文件上传
@PostMapping("/files-anon")
public FileInfo upload(@RequestParam("file") MultipartFile file)
// 智能获取文件访问URL
@GetMapping("/file/{id}")
public String getFileViewUrl(@PathVariable String id) {
return timeWindowStorageService.getSmartViewUrl(id);
}
// 通过路径获取文件URL
@GetMapping("/files/path")
public String getViewUrlByPath(@RequestParam String path) {
return timeWindowStorageService.getSmartViewUrlByPath(path);
}
}
访问流程:
- 业务应用调用文件访问接口
- 控制器接收请求参数(文件ID或路径)
- 调用智能存储服务获取访问URL
- 返回可访问的文件URL给业务应用
2. 时间窗口智能存储服务 (TimeWindowStorageService)
职责: 根据文件创建时间智能选择存储位置,实现冷热数据分离
核心逻辑:
@Service
public class TimeWindowStorageService {
// 智能获取文件访问URL
public String getSmartViewUrl(String fileId) {
// 1. 获取文件信息
FileInfo fileInfo = ossService.getFileInfo(fileId);
// 2. 判断存储类型
String storageType = getStorageTypeByTime(fileInfo);
// 3. 根据存储类型获取URL
return getViewUrlByStorageType(fileInfo, storageType);
}
// 根据时间判断存储类型
private String getStorageTypeByTime(FileInfo fileInfo) {
if (!enableTimeWindow) {
return "OSS"; // 时间窗口功能关闭,使用默认OSS存储
}
// 计算文件创建时间距离现在的天数
long daysDiff = ChronoUnit.DAYS.between(createTime, now);
if (daysDiff <= hotDataDays) {
return "OSS"; // 热数据,使用OSS
} else {
return "S3"; // 冷数据,使用S3(MinIO)
}
}
}
存储策略:
- 热数据 (≤30天): 存储在OSS,访问速度快
- 冷数据 (>30天): 迁移到S3/MinIO,降低成本
- 降级机制: 如果首选存储失败,自动降级到备选存储
3. 热数据同步服务 (HotDataSyncService)
职责: 自动识别热数据并将其同步到S3存储,实现数据备份和成本优化
核心功能:
@Service
public class HotDataSyncService {
// 定时同步任务
private void performScheduledSync() {
// 1. 获取热数据文件列表
List<FileInfo> hotFiles = getHotDataFiles();
// 2. 分批处理
for (List<FileInfo> batch : batches) {
syncExecutor.submit(() -> processBatch(batch));
}
}
// 同步单个文件到S3
public boolean syncFileToS3(FileInfo fileInfo) {
// 1. 检查文件是否已在S3
if (isFileInS3(fileInfo)) return true;
// 2. 从OSS获取文件URL
String ossUrl = ossService.getViewUrlByPath(fileInfo.getPath());
// 3. 下载并上传到S3
return enhancedS3Service.downloadAndUpload(ossUrl, s3Path);
}
}
同步策略:
- 定时同步: 每30分钟执行一次批量同步
- 手动触发: 支持手动触发同步任务
- 重试机制: 失败时自动重试3次
- 异步处理: 不阻塞主业务流程
4. 文件迁移服务 (FileMigrationService)
职责: 提供文件迁移管理功能,支持手动和批量迁移
核心方法:
@Service
public class FileMigrationService {
// 批量迁移到S3
public int batchMigrateToS3(List<String> fileIds) {
int successCount = 0;
for (String fileId : fileIds) {
if (migrateFileToS3(fileId)) {
successCount++;
}
}
return successCount;
}
// 根据时间窗口迁移冷数据
public int migrateColdDataByDays(int days) {
LocalDateTime cutoffDate = LocalDateTime.now().minusDays(days);
// 查询并迁移指定天数前的文件
}
}
业务访问流程详解
1. 文件上传流程
业务应用 → FileController.upload() → IFileService.upload() →
存储服务(OssService/S3Service) → 存储基础设施 → 返回FileInfo
详细步骤:
- 业务应用调用文件上传接口
- 文件控制器接收MultipartFile
- 根据配置选择存储服务(默认OSS)
- 上传文件到存储基础设施
- 保存文件信息到数据库
- 返回包含文件ID的FileInfo对象
2. 文件访问流程
业务应用 → FileController.getFileViewUrl() → TimeWindowStorageService.getSmartViewUrl() →
存储类型判断 → 获取访问URL → 返回可访问的URL
详细步骤:
- 业务应用传入文件ID请求访问URL
- 智能存储服务根据文件创建时间判断存储类型
- 优先从对应存储获取访问URL
- 如果失败,降级到备选存储
- 返回可访问的文件URL
3. 热数据同步流程
定时任务 → HotDataSyncService.performScheduledSync() →
获取热数据文件 → 分批同步 → EnhancedS3Service.downloadAndUpload() →
更新文件存储信息
详细步骤:
- 定时任务触发同步
- 查询需要同步的热数据文件
- 分批处理文件同步
- 从OSS下载文件并上传到S3
- 更新数据库中的文件存储信息
数据流转逻辑
1. 文件生命周期管理
文件上传 → OSS存储 → 热数据识别 → 同步到S3 → 冷数据归档
↓ ↓ ↓ ↓ ↓
业务访问 快速访问 智能判断 成本优化 长期存储
2. 存储策略切换
新文件 → OSS (热数据) → 30天后 → S3 (冷数据)
↓ ↓ ↓ ↓
快速访问 高性能存储 自动迁移 低成本存储
3. 访问降级机制
首选存储失败 → 检查备选存储 → 返回备选URL → 记录降级日志
↓ ↓ ↓ ↓
网络异常 存储可用性 服务连续性 监控告警
配置管理
1. 存储配置
zlt:
file-server:
type: oss # 默认存储类型
s3:
enabled: true # 启用S3服务
access-key: your-key
accessKeySecret: your-secret
endpoint: http://minio:9000
bucketName: your-bucket
oss:
secretId: your-oss-id
secretKey: your-oss-key
endpoint: https://oss-cn-beijing.aliyuncs.com
bucketName: your-oss-bucket
2. 同步配置
zlt:
storage:
hot-sync:
enabled: true # 启用热数据同步
batch-size: 100 # 批次大小
interval-minutes: 30 # 同步间隔
access-threshold: 10 # 访问阈值
max-threads: 5 # 最大线程数
hot-data-days: 7 # 热数据天数
enable-time-window: true # 启用时间窗口存储
hot-data-days: 30 # 热数据保留天数
监控和运维
1. 健康检查
@GetMapping("/storage/health")
public Result<String> getStorageHealth() {
return Result.succeed(storageHealthCheckService.getStorageStatus());
}
2. 统计信息
@GetMapping("/storage/statistics")
public Result<String> getStorageStatistics() {
return Result.succeed(timeWindowStorageService.getStorageStatistics());
}
3. 同步状态监控
@GetMapping("/sync/status")
public Map<String, Object> getSyncStatus() {
return Map.of(
"running", hotDataSyncService.isRunning(),
"statistics", hotDataSyncService.getSyncStatistics(),
"config", hotDataSyncService.getConfigInfo()
);
}
业务集成示例
1. 订单中心集成
@RestController
public class OrderFileController {
@Autowired
private FileCenterService fileCenterService;
@GetMapping("/order/{orderNo}")
public Result<List<OrderFile>> getOrderFiles(@PathVariable String orderNo) {
List<OrderFile> orderFiles = orderFileService.getOrderFileByOrderNo(orderNo);
// 为每个文件获取访问URL
for (OrderFile orderFile : orderFiles) {
String viewUrl = fileCenterService.getFileViewUrl(orderFile.getFileId());
orderFile.setViewUrl(viewUrl);
}
return Result.succeed(orderFiles);
}
}
2. 设备中心集成
@RestController
public class IaFileController {
@Autowired
private FileCenterService fileCenterService;
@GetMapping("/{id}")
public Result<IaFile> getFile(@PathVariable String id) {
IaFile iaFile = iaFileService.getById(id);
// 获取文件访问URL
String viewUrl = fileCenterService.getFileViewUrl(iaFile.getFileId());
iaFile.setViewUrl(viewUrl);
return Result.succeed(iaFile);
}
}
性能优化策略
1. 缓存机制
- 文件访问URL缓存
- 存储类型判断缓存
- 文件信息缓存
2. 异步处理
- 文件同步异步执行
- 批量操作异步处理
- 日志记录异步写入
3. 连接池管理
- 数据库连接池
- HTTP连接池
- 存储服务连接池
故障处理机制
1. 存储服务故障
- 自动降级到备选存储
- 重试机制
- 告警通知
2. 网络异常
- 连接超时处理
- 重试策略
- 降级访问
3. 数据一致性
- 事务管理
- 回滚机制
- 数据校验
总结
整个冷热数据同步与业务访问系统实现了:
- 智能存储管理: 根据文件生命周期自动选择最优存储位置
- 无缝业务集成: 提供统一的文件访问接口,业务应用无需关心存储细节
- 自动化运维: 定时同步、健康检查、状态监控
- 高可用设计: 降级机制、重试策略、故障恢复
- 成本优化: 热数据高性能存储,冷数据低成本存储
通过这套完整的解决方案,企业可以实现文件存储的智能化管理,在保证业务性能的同时有效控制存储成本。