Files
gofly-platform-services/zlt-doc/启动问题修复说明.md

320 lines
8.8 KiB
Markdown
Raw Permalink Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
# File-Center 启动问题修复说明
## 问题描述
启动时出现以下错误:
```
Field s3Service in com.central.file.service.TimeWindowStorageService required a bean of type 'com.central.file.service.impl.S3Service' that could not be found.
```
以及:
```
Field fileService in com.central.file.controller.FileController required a single bean, but 2 were found:
- ossService: defined in file [...]
- s3Service: defined in file [...]
```
以及:
```
ClassNotFoundException: com.amazonaws.services.s3.model.S3ObjectInputStream
```
## 问题原因
1. `S3Service`使用了`@ConditionalOnProperty`注解,只有在`zlt.file-server.type=s3`时才会被创建
2. 但我们的时间窗口存储方案需要同时支持OSS和S3,所以需要修改条件
3. `TimeWindowStorageService`强依赖`S3Service`,导致启动失败
4. 同时启用OSS和S3服务后,两个服务都实现了`IFileService`接口,导致Bean冲突
5. `S3Template`的条件注解依赖于`zlt.file-server.type=s3`,与我们的需求冲突
## 架构分析
### 依赖关系
- `file-center`模块依赖`zlt-oss-spring-boot-starter`
- `zlt-oss-spring-boot-starter`包含AWS S3依赖(`aws-java-sdk-s3`)
- `zlt-oss-spring-boot-starter`的`OssAutoConfigure`自动导入`S3Template`
- `S3Template`的条件:`@ConditionalOnProperty(prefix = FileServerProperties.PREFIX, name = "type", havingValue = FileServerProperties.TYPE_S3)`
### 问题根源
`S3Template`只有在`zlt.file-server.type=s3`时才会被创建,但我们需要:
- `zlt.file-server.type=oss`(让OssService工作)
- 同时启用S3功能(让S3Service工作)
## 修复方案
### 1. 移除重复的AWS S3依赖
`zlt-oss-spring-boot-starter`已经包含了AWS S3依赖,无需在`file-center`中重复添加。
### 2. 创建自定义S3Template配置
在`StorageConfig`中创建自定义的S3Template,不依赖于`zlt.file-server.type`:
```java
@Bean("customS3Template")
@ConditionalOnClass(name = "com.amazonaws.services.s3.AmazonS3")
@ConditionalOnProperty(prefix = "zlt.file-server.s3", name = "enabled", havingValue = "true")
public S3Template customS3Template(FileServerProperties fileProperties) {
return new S3Template();
}
```
### 3. 修改S3Service条件注解
将`S3Service`的条件从:
```java
@ConditionalOnProperty(prefix = FileServerProperties.PREFIX, name = "type", havingValue = FileServerProperties.TYPE_S3)
```
改为:
```java
@ConditionalOnClass(name = "com.amazonaws.services.s3.AmazonS3")
@ConditionalOnProperty(prefix = "zlt.file-server.s3", name = "enabled", havingValue = "true", matchIfMissing = false)
```
### 4. 解决Bean冲突问题
#### 4.1 为OssService添加@Primary注解
```java
@Service
@Primary
@ConditionalOnProperty(prefix = FileServerProperties.PREFIX, name = "type", havingValue = FileServerProperties.TYPE_OSS)
public class OssService extends AbstractIFileService {
// ...
}
```
#### 4.2 为S3Service指定Bean名称
```java
@Service("s3FileService")
@ConditionalOnClass(name = "com.amazonaws.services.s3.AmazonS3")
@ConditionalOnProperty(prefix = "zlt.file-server.s3", name = "enabled", havingValue = "true", matchIfMissing = false)
public class S3Service extends AbstractIFileService {
@Resource(name = "customS3Template")
private S3Template s3Template;
// ...
}
```
#### 4.3 使用@Qualifier注解注入S3Service
在`TimeWindowStorageService`中:
```java
@Autowired(required = false)
@Qualifier("s3FileService")
private S3Service s3Service;
```
在`StorageHealthCheckService`中:
```java
@Autowired(required = false)
@Qualifier("s3FileService")
private S3Service s3Service;
```
### 5. 修改TimeWindowStorageService依赖注入
将`S3Service`的依赖改为可选注入:
```java
@Autowired(required = false)
@Qualifier("s3FileService")
private S3Service s3Service;
```
### 6. 添加配置支持
在`application.yml`中添加S3启用配置:
```yaml
zlt:
file-server:
type: oss # 主存储类型(用于上传)
s3:
enabled: true # 启用S3服务
access-key: ${zlt.s3.access-key:}
accessKeySecret: ${zlt.s3.accessKeySecret:}
endpoint: ${zlt.s3.endpoint:}
bucketName: ${zlt.s3.bucketName:}
# ... 其他配置
storage:
enable-time-window: true # 启用时间窗口存储
hot-data-days: 30 # 热数据保留天数
```
### 7. 创建存储配置类
创建`StorageConfig`类,确保自定义S3Template能够正确创建:
```java
@Configuration
public class StorageConfig {
@Bean("customS3Template")
@ConditionalOnClass(name = "com.amazonaws.services.s3.AmazonS3")
@ConditionalOnProperty(prefix = "zlt.file-server.s3", name = "enabled", havingValue = "true")
public S3Template customS3Template(FileServerProperties fileProperties) {
return new S3Template();
}
}
```
### 8. 添加健康检查服务
创建`StorageHealthCheckService`,在启动时检查存储服务状态:
```java
@Service
public class StorageHealthCheckService {
@EventListener(ApplicationReadyEvent.class)
public void checkStorageServices() {
// 检查OSS和S3服务状态
}
}
```
## 配置说明
### 完整配置示例
```yaml
zlt:
file-server:
type: oss # 主存储类型(用于上传)
s3:
enabled: true # 启用S3服务
access-key: ${zlt.s3.access-key:}
accessKeySecret: ${zlt.s3.accessKeySecret:}
endpoint: ${zlt.s3.endpoint:}
bucketName: ${zlt.s3.bucketName:}
# ... 其他配置
storage:
enable-time-window: true # 启用时间窗口智能存储
hot-data-days: 30 # 热数据保留天数
```
### 环境变量配置
确保以下环境变量已配置:
**OSS配置:**
- `zlt.oss.secretId`
- `zlt.oss.secretKey`
- `zlt.oss.endpoint`
- `zlt.oss.bucketName`
**S3/MinIO配置(可选):**
- `zlt.s3.access-key`
- `zlt.s3.accessKeySecret`
- `zlt.s3.endpoint`
- `zlt.s3.bucketName`
## 验证方法
### 1. 启动日志检查
启动后查看日志,应该看到:
```
=== 存储服务健康检查开始 ===
✓ OSS服务已启用
✓ S3/MinIO服务已启用
✓ 时间窗口智能存储服务已启用
- 热数据存储: OSS
- 冷数据存储: S3/MinIO
=== 存储服务健康检查完成 ===
```
### 2. 接口验证
访问健康检查接口:
```bash
GET /storage/health
```
应该返回:
```json
{
"code": 200,
"message": "获取成功",
"data": "存储服务状态:\n- OSS服务: 已启用\n- S3服务: 已启用\n- 时间窗口存储: 已启用"
}
```
### 3. 统计信息接口
访问统计信息接口:
```bash
GET /storage/statistics
```
应该返回:
```json
{
"code": 200,
"message": "获取成功",
"data": "时间窗口存储服务 - 热数据天数: 30, 启用状态: true, S3服务: 已启用"
}
```
## 故障排除
### 1. S3Service未启用
**现象:** 日志显示"S3/MinIO服务未启用"
**解决:** 检查配置`zlt.file-server.s3.enabled=true`
### 2. S3Template创建失败
**现象:** S3Service创建失败
**解决:** 检查S3配置参数是否正确,特别是endpoint和认证信息
### 3. 时间窗口存储未启用
**现象:** 智能存储功能不工作
**解决:** 检查配置`zlt.storage.enable-time-window=true`
### 4. Bean冲突问题
**现象:** 启动时出现"expected single matching bean but found 2"错误
**解决:**
- 确保OssService有`@Primary`注解
- 确保S3Service有`@Service("s3FileService")`注解
- 确保使用`@Qualifier("s3FileService")`注入S3Service
### 5. 依赖问题
**现象:** `ClassNotFoundException: com.amazonaws.services.s3.model.S3ObjectInputStream`
**解决:**
- 确保`zlt-oss-spring-boot-starter`依赖正确引入
- 重新编译项目:`mvn clean compile`
- 如果不需要S3功能,设置`zlt.file-server.s3.enabled=false`
## 注意事项
1. **兼容性:** 修改后的配置支持同时使用OSS和S3,不影响现有功能
2. **性能:** 时间窗口存储会根据文件创建时间自动选择存储位置
3. **成本:** 热数据存储在OSS,冷数据迁移到MinIO,可有效降低成本
4. **监控:** 通过健康检查接口可以实时监控存储服务状态
5. **Bean管理:** 使用`@Primary`和`@Qualifier`确保Bean注入的正确性
6. **依赖管理:** 利用`zlt-oss-spring-boot-starter`的现有依赖,避免重复
7. **配置灵活性:** 通过自定义S3Template实现与原有配置的兼容
## 架构优势
1. **模块化设计:** 利用现有的`zlt-oss-spring-boot-starter`模块
2. **依赖复用:** 避免重复添加AWS S3依赖
3. **配置灵活:** 支持同时使用OSS和S3,也可仅使用OSS
4. **向后兼容:** 不影响现有的OSS功能
5. **扩展性强:** 可以轻松添加其他存储类型
## 下一步
1. 配置MinIO服务器(如需要)
2. 设置定时迁移任务
3. 监控存储使用情况
4. 根据业务需求调整热数据天数
5. 参考`S3-MinIO配置说明.md`进行详细配置