一个基于 Rust + Actix Web + rbatis + MySQL + Redis 的微服务项目。
football/
├── business/ # 业务服务 (端口: 8080)
│ ├── src/
│ │ ├── main.rs # 服务入口
│ │ ├── handlers.rs # 请求处理器
│ │ └── routes.rs # 路由配置
│ └── Cargo.toml
├── manage/ # 管理服务 (端口: 8081)
│ ├── src/
│ │ ├── main.rs # 服务入口
│ │ ├── handlers.rs # 请求处理器
│ │ └── routes.rs # 路由配置
│ └── Cargo.toml
├── common/ # 公共库
│ ├── src/
│ │ ├── error.rs # 错误定义
│ │ ├── config.rs # 配置管理
│ │ └── utils.rs # 工具函数
│ └── Cargo.toml
├── orm/ # 数据库ORM层
│ ├── src/
│ │ ├── db.rs # MySQL 连接管理
│ │ ├── cache.rs # Redis 缓存管理
│ │ ├── entities.rs # 实体定义
│ │ └── repositories.rs # 仓储层
│ └── Cargo.toml
├── migrations/ # 数据库迁移文件
├── config.toml # 配置文件
├── docker-compose.yml # Docker 编排文件
└── Cargo.toml # Workspace 配置
- Web框架: Actix Web 4.9
- 异步运行时: Tokio
- ORM: rbatis 4.5
- 数据库: MySQL 8.0
- 缓存: Redis 7
- 序列化: Serde
- 日志: log + env_logger
- 配置: config + dotenv
- Rust 1.70+
- MySQL 8.0+
- Redis 7+
- Docker & Docker Compose (可选)
# 启动 MySQL 和 Redis
docker-compose up -d
# 查看服务状态
docker-compose ps
# 查看日志
docker-compose logs -f# 复制环境变量示例文件
cp env.example .env
# 编辑 .env 文件,修改配置
vim .env# 连接到 MySQL
mysql -h 127.0.0.1 -u root -p
# 执行迁移脚本
source migrations/001_create_users_table.sql# 构建所有服务
cargo build
# 或者构建特定服务
cargo build --bin football-business
cargo build --bin football-manage# 设置日志级别
export RUST_LOG=info
# 运行服务
cargo run --bin football-business# 在新终端运行
export RUST_LOG=info
export SERVER_PORT=8081
cargo run --bin football-manage# 服务信息
curl http://localhost:8080/
# 健康检查
curl http://localhost:8080/health
# 设置缓存
curl -X POST http://localhost:8080/api/cache/set \
-H "Content-Type: application/json" \
-d '{"key": "test_key", "value": "test_value"}'
# 获取缓存
curl http://localhost:8080/api/cache/get/test_key# 服务信息
curl http://localhost:8081/
# 健康检查
curl http://localhost:8081/health
# 获取所有用户
curl http://localhost:8081/api/users
# 根据ID获取用户
curl http://localhost:8081/api/users/1
# 根据用户名获取用户
curl http://localhost:8081/api/users/username/admin项目使用 Workspace 统一管理依赖,在根目录的 Cargo.toml 中添加:
[workspace.dependencies]
your-dependency = "version"然后在子 crate 的 Cargo.toml 中引用:
[dependencies]
your-dependency = { workspace = true }- 在
orm/src/entities.rs中定义实体:
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct YourEntity {
pub id: i64,
pub name: String,
pub created_at: DateTime<Utc>,
}- 在
orm/src/repositories.rs中实现仓储:
pub struct YourRepository;
impl YourRepository {
pub async fn find_by_name(&self, name: &str) -> AppResult<Option<YourEntity>> {
let rb = get_db();
let sql = "SELECT * FROM your_table WHERE name = ? LIMIT 1";
let result = rb.query_decode(sql, vec![name.into()]).await?;
Ok(result)
}
}- 在
handlers.rs中添加处理器函数:
pub async fn your_handler() -> impl Responder {
HttpResponse::Ok().json(ApiResponse::success("OK"))
}- 在
routes.rs中注册路由:
cfg.route("/your-path", web::get().to(handlers::your_handler));| 变量名 | 说明 | 默认值 |
|---|---|---|
| SERVER_HOST | 服务器地址 | 0.0.0.0 |
| SERVER_PORT | 服务器端口 | 8080 |
| DATABASE_URL | MySQL 连接字符串 | mysql://root:password@localhost:3306/football |
| DATABASE_MAX_CONNECTIONS | 数据库最大连接数 | 10 |
| REDIS_URL | Redis 连接字符串 | redis://localhost:6379 |
| REDIS_POOL_SIZE | Redis 连接池大小 | 10 |
| LOG_LEVEL | 日志级别 | info |
| RUN_MODE | 运行模式 | development |
config.toml: 开发环境配置config.production.toml: 生产环境配置
使用 RUN_MODE 环境变量切换配置:
export RUN_MODE=production
cargo runcargo fmtcargo clippycargo test# Business 服务
docker build -t football-business:latest -f business/Dockerfile .
# Manage 服务
docker build -t football-manage:latest -f manage/Dockerfile .docker-compose up -d- business/: 业务服务,提供核心业务 API
- manage/: 管理服务,提供后台管理 API
- common/: 公共库,包含错误处理、配置管理、工具函数
- orm/: ORM 层,包含数据库和缓存操作
- migrations/: 数据库迁移脚本
GET /- 服务信息GET /health- 健康检查POST /api/cache/set- 设置缓存GET /api/cache/get/{key}- 获取缓存
GET /- 服务信息GET /health- 健康检查GET /api/users- 获取所有用户GET /api/users/{id}- 根据ID获取用户GET /api/users/username/{username}- 根据用户名获取用户
检查 MySQL 是否启动:
docker-compose ps mysql查看 MySQL 日志:
docker-compose logs mysql检查 Redis 是否启动:
docker-compose ps redis清理并重新构建:
cargo clean
cargo buildMIT OR Apache-2.0