topfans/backend/deploy/README.md
2026-04-07 22:29:48 +08:00

119 lines
4.2 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.

# deploy/ 目录说明
本目录包含 TopFans 后端的生产部署配置模板,采用**分层环境变量**策略管理多服务配置。
## 目录结构
```
deploy/
├── envs/ # 环境变量模板(按服务拆分)
│ ├── common.env # 所有服务共享DB、日志级别等
│ ├── user.env # User Service 私有配置
│ ├── social.env # Social Service 私有配置
│ ├── gallery.env # Gallery Service 私有配置
│ ├── asset.env # Asset Service 私有配置(含 OSS
│ ├── activity.env # Activity Service 私有配置
│ └── gateway.env # Gateway 私有配置(含 JWT、下游地址
└── systemd/ # systemd 服务文件模板
├── topfans-user.service
├── topfans-social.service
├── topfans-gallery.service
├── topfans-asset.service
├── topfans-activity.service
└── topfans-gateway.service
```
## 服务端口
| 服务 | 端口 |
|---|---|
| Gateway | 8080 |
| User Service | 20000 |
| Gallery Service | 20001 |
| Social Service | 20002 |
| Asset Service | 20003 |
| Activity Service | 20004 |
## 配置优先级
```
CLI flag > 环境变量ENV > 代码硬编码默认值
```
各服务 `main.go` 中的 flag 默认值均从对应环境变量读取,因此:
- **单机部署**: 只需填写 `/etc/topfans/*.env`,无需传任何启动参数
- **多机部署**: 仅修改目标服务器上的 env 文件中的地址,代码不变
## 快速部署步骤
### 1. 安装配置文件
```bash
# 创建服务器上的配置目录
sudo mkdir -p /etc/topfans
sudo chown topfans:topfans /etc/topfans
sudo chmod 750 /etc/topfans
# 复制模板并按实际环境修改
sudo cp deploy/envs/common.env /etc/topfans/common.env
sudo cp deploy/envs/user.env /etc/topfans/user.env
sudo cp deploy/envs/social.env /etc/topfans/social.env
sudo cp deploy/envs/gallery.env /etc/topfans/gallery.env
sudo cp deploy/envs/asset.env /etc/topfans/asset.env
sudo cp deploy/envs/activity.env /etc/topfans/activity.env
sudo cp deploy/envs/gateway.env /etc/topfans/gateway.env
# 填写真实密码、OSS 密钥、JWT Secret 等
sudo vim /etc/topfans/common.env
sudo vim /etc/topfans/asset.env
sudo vim /etc/topfans/gateway.env
# 保护文件权限
sudo chmod 640 /etc/topfans/*.env
sudo chown topfans:topfans /etc/topfans/*.env
```
### 2. 安装 systemd 服务文件
```bash
sudo cp deploy/systemd/*.service /etc/systemd/system/
sudo systemctl daemon-reload
sudo systemctl enable topfans-user topfans-gallery topfans-social \
topfans-asset topfans-activity topfans-gateway
```
### 3. 编译并启动
```bash
# 编译(输出到 /opt/topfans/
cd gateway && go build -ldflags="-s -w" -o /opt/topfans/gateway .
cd services/userService && go build -ldflags="-s -w" -o /opt/topfans/user-service .
cd services/galleryService && go build -ldflags="-s -w" -o /opt/topfans/gallery-service .
cd services/socialService && go build -ldflags="-s -w" -o /opt/topfans/social-service .
cd services/assetService && go build -ldflags="-s -w" -o /opt/topfans/asset-service .
cd services/activityService && go build -ldflags="-s -w" -o /opt/topfans/activity-service .
# 按依赖顺序启动
sudo systemctl start topfans-user topfans-asset
sudo systemctl start topfans-gallery topfans-social topfans-activity
sudo systemctl start topfans-gateway
```
## 多机部署
只需修改目标服务器上的 env 文件,将 `localhost` 替换为对应服务器的**内网 IP**
```ini
# 示例应用服务器1 上的 gateway.envAsset/Gallery 在应用服务器210.0.0.21
DUBBO_GALLERY_SERVICE_URL=tri://10.0.0.21:20001
DUBBO_ASSET_SERVICE_URL=tri://10.0.0.21:20003
```
详细步骤参见 [docs/服务器部署指南.md](../docs/服务器部署指南.md)。
## 注意事项
- **不要将真实密钥提交到 Git**`/etc/topfans/*.env` 应仅存在于服务器本地
- `deploy/envs/*.env` 中的值仅为示例模板,提交到仓库时不应包含真实密码
- `deploy/systemd/*.service` 假设二进制位于 `/opt/topfans/`,如部署路径不同请修改 `WorkingDirectory``ExecStart`