backend/resource-service/README.zh.md
Resource Service 是一个基于 Spring Boot 3.2.4 构建的资源管理服务,专门用于处理文件上传、下载、存储和管理。该服务集成了 S3 兼容的对象存储(MinIO)、MySQL 数据库、Redis 缓存等基础设施,提供完整的文件生命周期管理功能。
/file/upload)/file/upload-video)/file/share-file-upload)/file/download)| 组件 | 技术选型 | 版本要求 |
|---|---|---|
| 后端框架 | Spring Boot | 3.2.4 |
| Java | Java | 21 |
| 数据库 | MySQL + MyBatis-Plus | 8.0.28 / 3.5.5 |
| 对象存储 | AWS S3 SDK | 2.17.102 |
| 连接池 | Druid | 1.2.16 |
| 缓存 | Redis | - |
| 构建工具 | Maven | 3.6+ |
| 容器化 | Docker | - |
| 日志框架 | Logback | - |
resource-service/
├── src/main/java/com/iflytek/rpa/resource/
│ ├── ResourceApplication.java # Spring Boot 应用启动类
│ ├── common/ # 公共组件
│ │ ├── exp/ # 异常处理
│ │ │ ├── GlobalExceptionHandler.java
│ │ │ └── ServiceException.java
│ │ └── response/ # 响应封装
│ │ ├── AppResponse.java
│ │ └── ErrorCodeEnum.java
│ └── file/ # 文件管理模块
│ ├── config/ # 配置类
│ │ └── S3Config.java # S3 存储配置
│ ├── controller/ # 控制器
│ │ └── FileController.java # 文件操作控制器
│ ├── dao/ # 数据访问层
│ │ ├── FileMapper.java
│ │ └── FileMapper.xml
│ ├── entity/ # 实体类
│ │ ├── enums/ # 枚举类
│ │ │ └── FileType.java
│ │ ├── vo/ # 视图对象
│ │ │ └── ShareFileUploadVo.java
│ │ └── File.java # 文件实体
│ ├── service/ # 服务层
│ │ ├── impl/ # 服务实现
│ │ │ └── FileServiceImpl.java
│ │ └── FileService.java # 文件服务接口
│ └── utils/ # 工具类
│ └── IdWorker.java # ID 生成器
├── src/main/resources/
│ ├── application.yml # 主配置文件
│ ├── application-local.yml # 本地环境配置
│ └── logback-delayed.xml # 日志配置
├── Dockerfile # Docker 镜像构建
├── pom.xml # Maven 项目配置
└── README.md # 项目说明文档
git clone <repository-url>
cd resource-service
创建 .env 文件,配置必要的环境变量:
# 数据库配置
DATABASE_HOST=localhost
DATABASE_PORT=3306
DATABASE_NAME=resource_service
DATABASE_USERNAME=your_db_username
DATABASE_PASSWORD=your_db_password
# Redis 配置
REDIS_HOST=localhost
REDIS_PORT=6379
REDIS_DB=0
REDIS_PASSWORD=your_redis_password
# MinIO/S3 配置
MINIO_URL=http://localhost:9000
MINIO_BUCKET=resource-bucket
MINIO_AK=your_access_key
MINIO_SK=your_secret_key
mvn clean package -DskipTests
java -jar target/resource-0.0.1-SNAPSHOT.jar --spring.profiles.active=local
访问 http://localhost:8030/api/resource 验证服务是否正常启动。
docker build -t resource-service:latest .
docker run -d --name resource-service \
-p 8030:8030 \
-e DATABASE_HOST=your_db_host \
-e DATABASE_USERNAME=your_db_username \
-e DATABASE_PASSWORD=your_db_password \
-e REDIS_HOST=your_redis_host \
-e MINIO_URL=your_minio_url \
-e MINIO_AK=your_access_key \
-e MINIO_SK=your_secret_key \
resource-service:latest
POST /api/resource/file/upload
curl -X POST "http://localhost:8030/api/resource/file/upload" \
-H "Content-Type: multipart/form-data" \
-F "file=@/path/to/your/file.jpg"
响应格式:
{
"code": 200,
"message": "success",
"data": "file-uuid-12345"
}
POST /api/resource/file/upload-video
curl -X POST "http://localhost:8030/api/resource/file/upload-video" \
-H "Content-Type: multipart/form-data" \
-F "file=@/path/to/your/video.mp4"
POST /api/resource/file/share-file-upload
curl -X POST "http://localhost:8030/api/resource/file/share-file-upload" \
-H "Content-Type: multipart/form-data" \
-F "file=@/path/to/your/large-file.zip"
响应格式:
{
"code": 200,
"message": "success",
"data": {
"fileId": "file-uuid-12345",
"type": 1,
"filename": "large-file.zip"
}
}
GET /api/resource/file/download?fileId=file-uuid-12345
curl -X GET "http://localhost:8030/api/resource/file/download?fileId=file-uuid-12345" \
-o downloaded-file.jpg
| 配置项 | 默认值 | 说明 |
|---|---|---|
spring.servlet.multipart.max-file-size | 50MB | 普通文件最大上传大小 |
spring.servlet.multipart.max-request-size | 110MB | 请求最大大小 |
spring.servlet.multipart.file-size-threshold | 100MB | 文件大小阈值 |
| 配置项 | 说明 |
|---|---|
spring.datasource.url | MySQL 连接 URL |
spring.datasource.username | 数据库用户名 |
spring.datasource.password | 数据库密码 |
spring.datasource.druid.* | Druid 连接池配置 |
| 配置项 | 说明 |
|---|---|
amazonaws.s3.url | S3 服务地址 |
amazonaws.s3.bucket | 存储桶名称 |
amazonaws.s3.accessKey | 访问密钥 |
amazonaws.s3.secretKey | 秘密密钥 |
amazonaws.s3.maxConnections | 最大连接数 |
# 运行所有测试
mvn test
# 运行特定测试类
mvn test -Dtest=FileServiceTest
# 编译检查
mvn compile
# 代码格式化
mvn spotless:apply
# 实时查看应用日志
tail -f logs/application.log
logging.level.* 配置2024-01-01 10:30:15.123 INFO [http-nio-8030-exec-1] c.i.r.r.f.s.i.FileServiceImpl : 文件上传成功,文件ID: file-uuid-12345
2024-01-01 10:30:16.456 INFO [http-nio-8030-exec-2] c.i.r.r.f.s.i.FileServiceImpl : 文件下载成功,文件ID: file-uuid-12345
A: 检查以下几点:
A: 在 application.yml 中修改以下配置:
spring:
servlet:
multipart:
max-file-size: 100MB
max-request-size: 150MB
A: 可以通过以下方式监控:
A:
A: