模块拆分 init
This commit is contained in:
+159
-125
@@ -1,170 +1,204 @@
|
||||
# 部署时端口配置说明
|
||||
# 模块化部署端口说明
|
||||
|
||||
## 生产环境部署端口配置
|
||||
> 本文档描述的是 **当前模块化部署模式** 下的端口规划,不再以历史单体 `src.main:app` 作为默认前提。
|
||||
|
||||
本项目现在使用统一的端口配置文件 `.env`,部署时需要相应调整配置。
|
||||
当前推荐部署对象:
|
||||
|
||||
### Gunicorn 启动配置
|
||||
- gemold API
|
||||
- gemold Celery worker(无 HTTP 端口)
|
||||
- inventory API
|
||||
|
||||
**推荐方式(使用 .env 配置):**
|
||||
以下基础设施默认由服务器现有服务提供,不在本项目 compose 中重复部署:
|
||||
|
||||
- PostgreSQL
|
||||
- Redis
|
||||
- MinIO / RustFS(gemold 需要)
|
||||
|
||||
---
|
||||
|
||||
## 1. 推荐端口规划
|
||||
|
||||
| 组件 | 默认端口 | 说明 |
|
||||
|---|---:|---|
|
||||
| gemold API | 8000 | 模具分析后端 |
|
||||
| inventory API | 8001 | 进销存后端 |
|
||||
| PostgreSQL | 5432 | 共享数据库 |
|
||||
| Redis | 6379 | 共享队列/缓存 |
|
||||
| MinIO API | 9000 | 对象存储接口 |
|
||||
| MinIO Console | 9001 | 对象存储控制台 |
|
||||
|
||||
> Celery worker 不直接暴露 HTTP 端口。
|
||||
|
||||
---
|
||||
|
||||
## 2. 三种部署模式下的端口
|
||||
|
||||
### 2.1 gemold-only
|
||||
|
||||
- 对外开放:`8000`
|
||||
- 依赖:PostgreSQL、Redis、MinIO/RustFS
|
||||
- 可选:前置 Nginx 暴露 80/443
|
||||
|
||||
### 2.2 inventory-only
|
||||
|
||||
- 对外开放:`8001`
|
||||
- 依赖:PostgreSQL、Redis
|
||||
- 不要求对象存储
|
||||
|
||||
### 2.3 unified
|
||||
|
||||
两种常见实现:
|
||||
|
||||
1. **统一网关模式**
|
||||
- 外部只开放 80/443
|
||||
- 网关转发到 gemold / inventory
|
||||
2. **统一应用组合模式**
|
||||
- 统一后端监听单一端口
|
||||
- 后续组合层重构完成后更适合采用
|
||||
|
||||
当前阶段,如果需要统一对外,更推荐**网关统一**而不是继续依赖历史单体入口。
|
||||
|
||||
---
|
||||
|
||||
## 3. Docker Compose 端口来源
|
||||
|
||||
当前主部署文件:
|
||||
- [docker-compose.yml](../../docker-compose.yml)
|
||||
|
||||
关键端口映射:
|
||||
|
||||
- `MOLDINSIGHT_PORT` → gemold API 外部端口
|
||||
- `INVENTORY_PORT` → inventory API 外部端口
|
||||
|
||||
示例:
|
||||
|
||||
```env
|
||||
MOLDINSIGHT_PORT=8000
|
||||
INVENTORY_PORT=8001
|
||||
```
|
||||
|
||||
对应 compose 行为:
|
||||
|
||||
- gemold:`${MOLDINSIGHT_PORT:-8000}:8000`
|
||||
- inventory:`${INVENTORY_PORT:-8001}:8001`
|
||||
|
||||
---
|
||||
|
||||
## 4. 直接运行时的端口约定
|
||||
|
||||
### gemold-only
|
||||
|
||||
```bash
|
||||
# 创建启动脚本
|
||||
cat > start_production.sh << 'EOF'
|
||||
#!/bin/bash
|
||||
cd /opt/moldinsight/moldinsight_project
|
||||
source venv/bin/activate
|
||||
|
||||
# 从 .env 读取端口配置
|
||||
if [ -f .env ]; then
|
||||
PORT=$(grep '^PORT=' .env | cut -d'=' -f2)
|
||||
else
|
||||
PORT=8000
|
||||
fi
|
||||
|
||||
# 启动服务
|
||||
gunicorn src.main:app --workers 4 --worker-class uvicorn.workers.UvicornWorker --bind 0.0.0.0:${PORT}
|
||||
EOF
|
||||
|
||||
chmod +x start_production.sh
|
||||
./start_production.sh
|
||||
uvicorn src.entrypoints.moldinsight:app --host 0.0.0.0 --port 8000
|
||||
```
|
||||
|
||||
### systemd 服务配置
|
||||
### inventory-only
|
||||
|
||||
创建 `/etc/systemd/system/moldinsight.service`:
|
||||
|
||||
```ini
|
||||
[Unit]
|
||||
Description=MoldInsight Geometry Analysis Service
|
||||
After=network.target postgresql.service
|
||||
|
||||
[Service]
|
||||
Type=simple
|
||||
User=www-data
|
||||
Group=www-data
|
||||
WorkingDirectory=/opt/moldinsight/moldinsight_project
|
||||
Environment=PATH=/opt/moldinsight/moldinsight_project/venv/bin
|
||||
Environment="PORT=8000"
|
||||
ExecStart=/opt/moldinsight/moldinsight_project/venv/bin/gunicorn src.main:app --workers 4 --worker-class uvicorn.workers.UvicornWorker --bind 0.0.0.0:${PORT}
|
||||
Restart=always
|
||||
|
||||
[Install]
|
||||
WantedBy=multi-user.target
|
||||
```bash
|
||||
uvicorn src.entrypoints.inventory:app --host 0.0.0.0 --port 8001
|
||||
```
|
||||
|
||||
**重要:** 在 `[Service]` 部分添加 `Environment="PORT=8000"`,或在 `.env` 文件中配置 `PORT=8000`。
|
||||
如果改端口:
|
||||
- gemold 改 `--port`
|
||||
- inventory 改 `--port`
|
||||
- 同步更新 Nginx / 防火墙 / 前端 base URL
|
||||
|
||||
### Nginx 反向代理配置
|
||||
---
|
||||
|
||||
## 5. 前端联动
|
||||
|
||||
如果前端与后端分开部署,需要与前端环境变量保持一致。
|
||||
|
||||
建议前端支持:
|
||||
|
||||
### unified 模式
|
||||
```env
|
||||
VITE_API_BASE_URL=https://api.example.com
|
||||
```
|
||||
|
||||
### split 模式
|
||||
```env
|
||||
VITE_AUTH_API_BASE_URL=https://auth.example.com
|
||||
VITE_MOLDINSIGHT_API_BASE_URL=https://gemold.example.com
|
||||
VITE_INVENTORY_API_BASE_URL=https://inventory.example.com
|
||||
```
|
||||
|
||||
当前详细策略见:
|
||||
- [BACKEND_MODULARIZATION_BLUEPRINT.md](../BACKEND_MODULARIZATION_BLUEPRINT.md)
|
||||
|
||||
---
|
||||
|
||||
## 6. Nginx 示例
|
||||
|
||||
### gemold-only
|
||||
|
||||
```nginx
|
||||
upstream moldinsight_backend {
|
||||
server 127.0.0.1:8000; # 对应 .env 中的 PORT
|
||||
}
|
||||
|
||||
server {
|
||||
listen 80;
|
||||
server_name your-domain.com;
|
||||
server_name gemold.example.com;
|
||||
|
||||
location / {
|
||||
proxy_pass http://moldinsight_backend;
|
||||
proxy_pass http://127.0.0.1:8000;
|
||||
proxy_set_header Host $host;
|
||||
proxy_set_header X-Real-IP $remote_addr;
|
||||
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
|
||||
proxy_set_header X-Forwarded-Proto $scheme;
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
location /static {
|
||||
alias /opt/moldinsight/moldinsight_project/static;
|
||||
}
|
||||
### inventory-only
|
||||
|
||||
location /html_output {
|
||||
alias /opt/moldinsight/moldinsight_project/html_output;
|
||||
```nginx
|
||||
server {
|
||||
listen 80;
|
||||
server_name inventory.example.com;
|
||||
|
||||
location / {
|
||||
proxy_pass http://127.0.0.1:8001;
|
||||
proxy_set_header Host $host;
|
||||
proxy_set_header X-Real-IP $remote_addr;
|
||||
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
|
||||
proxy_set_header X-Forwarded-Proto $scheme;
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
### Docker 部署
|
||||
---
|
||||
|
||||
Docker 部署自动从 `.env` 读取配置,无需额外设置:
|
||||
## 7. 防火墙建议
|
||||
|
||||
如果不通过 Nginx 统一入口而是直接暴露服务端口,则应显式开放:
|
||||
|
||||
```bash
|
||||
# .env 文件配置
|
||||
PORT=8000 # 容器内端口
|
||||
HOST_PORT=8080 # 宿主机端口
|
||||
# gemold
|
||||
sudo ufw allow 8000/tcp
|
||||
|
||||
# 启动
|
||||
docker-compose up -d
|
||||
# inventory
|
||||
sudo ufw allow 8001/tcp
|
||||
```
|
||||
|
||||
### 修改生产环境端口
|
||||
生产环境更推荐:
|
||||
- 外部只开放 80/443
|
||||
- 内部仅开放 8000/8001 给 Nginx 或内网访问
|
||||
|
||||
1. **编辑 .env 文件**
|
||||
```bash
|
||||
PORT=9000 # 修改应用端口
|
||||
```
|
||||
---
|
||||
|
||||
2. **重启服务**
|
||||
```bash
|
||||
# systemd
|
||||
sudo systemctl restart moldinsight
|
||||
|
||||
# Docker
|
||||
docker-compose down && docker-compose up -d
|
||||
|
||||
# 手动启动
|
||||
./start_production.sh
|
||||
```
|
||||
|
||||
3. **更新 Nginx 配置(如果使用)**
|
||||
```nginx
|
||||
upstream moldinsight_backend {
|
||||
server 127.0.0.1:9000; # 更新为新端口
|
||||
}
|
||||
```
|
||||
```bash
|
||||
sudo nginx -t && sudo nginx -s reload
|
||||
```
|
||||
|
||||
### 防火墙配置
|
||||
|
||||
如果修改了端口,需要更新防火墙规则:
|
||||
## 8. 快速检查
|
||||
|
||||
```bash
|
||||
# UFW (Ubuntu/Debian)
|
||||
sudo ufw allow 9000/tcp
|
||||
sudo ufw delete allow 8000/tcp # 删除旧端口
|
||||
|
||||
# firewall-cmd (CentOS/RHEL)
|
||||
sudo firewall-cmd --permanent --add-port=9000/tcp
|
||||
sudo firewall-cmd --permanent --remove-port=8000/tcp
|
||||
sudo firewall-cmd --reload
|
||||
curl http://127.0.0.1:8000/health
|
||||
curl http://127.0.0.1:8001/health
|
||||
```
|
||||
|
||||
### 健康检查
|
||||
如果只部署单模块,只检查对应服务即可。
|
||||
|
||||
修改端口后,更新健康检查命令:
|
||||
---
|
||||
|
||||
```bash
|
||||
# 检查服务状态
|
||||
curl http://localhost:9000/health
|
||||
curl http://your-domain.com/health
|
||||
```
|
||||
## 9. 结论
|
||||
|
||||
## 快速参考
|
||||
在当前模块化架构下:
|
||||
|
||||
| 部署方式 | 端口配置位置 | 重启命令 |
|
||||
|---------|------------|---------|
|
||||
| 直接运行 | `.env` 中的 `PORT` | Ctrl+C 后重新运行 |
|
||||
| Gunicorn | `.env` 中的 `PORT` | `systemctl restart moldinsight` |
|
||||
| Docker | `.env` 中的 `PORT` 和 `HOST_PORT` | `docker-compose restart` |
|
||||
| Nginx代理 | Nginx配置中的 `proxy_pass` | `nginx -s reload` |
|
||||
|
||||
## 注意事项
|
||||
|
||||
⚠️ **重要:**
|
||||
1. 所有端口配置统一在 `.env` 文件中管理
|
||||
2. 修改端口后需要同步更新相关配置(Nginx、防火墙等)
|
||||
3. 确保新端口没有被其他服务占用
|
||||
4. 生产环境建议使用 Nginx 反向代理,对外提供 80/443 端口
|
||||
5. .env 文件不应提交到版本控制系统,使用 `.env.example` 作为模板
|
||||
- gemold 与 inventory 应视为**两个独立后端模块**
|
||||
- 端口应按模块分配,而不是继续沿用单体“一个后端一个端口”的思路
|
||||
- unified 更适合通过**组合层或网关**实现,而不是继续让历史单体入口承载全部语义
|
||||
|
||||
Reference in New Issue
Block a user