模块拆分 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 更适合通过**组合层或网关**实现,而不是继续让历史单体入口承载全部语义
|
||||
|
||||
+300
-178
@@ -1,177 +1,315 @@
|
||||
# MoldInsight Linux 部署指南
|
||||
# geMoldInsight Linux 部署指南
|
||||
|
||||
## 系统要求
|
||||
- Linux 系统 (Ubuntu 20.04+ / CentOS 8+)
|
||||
- Python 3.8+
|
||||
- PostgreSQL 12+
|
||||
> 本文档描述的是 **当前模块化架构** 下的 Linux 部署方式,而不是历史单体 `src.main:app` 方案。
|
||||
|
||||
当前项目支持三种部署模式:
|
||||
|
||||
- **unified**:gemold + inventory 统一部署
|
||||
- **gemold-only**:仅部署模具分析后端
|
||||
- **inventory-only**:仅部署进销存后端
|
||||
|
||||
项目保持:
|
||||
|
||||
- **同一 Git 仓库**
|
||||
- **同一 PostgreSQL 数据库**
|
||||
- **共享认证体系**
|
||||
- **按模块独立部署**
|
||||
- **复用服务器上已存在的 PostgreSQL / Redis / RustFS(或 MinIO 兼容存储)**
|
||||
|
||||
详细架构蓝图见:
|
||||
- [BACKEND_MODULARIZATION_BLUEPRINT.md](../BACKEND_MODULARIZATION_BLUEPRINT.md)
|
||||
|
||||
---
|
||||
|
||||
## 1. 系统要求
|
||||
|
||||
### 基础要求
|
||||
- Linux(Ubuntu 22.04+ / Debian 12+ / CentOS Stream 9+)
|
||||
- Python 3.12
|
||||
- Git
|
||||
- 服务器上已可访问的 PostgreSQL 15+
|
||||
- 服务器上已可访问的 Redis 7+
|
||||
|
||||
## 1. 环境准备
|
||||
### 按模块附加要求
|
||||
|
||||
#### gemold / unified 需要
|
||||
- 服务器上已可访问的 MinIO 或 RustFS 兼容对象存储
|
||||
- PythonOCC 运行环境
|
||||
- Celery worker(推荐与 gemold 一起部署)
|
||||
|
||||
#### inventory-only 需要
|
||||
- PostgreSQL
|
||||
- Redis
|
||||
- 不依赖 MinIO / RustFS
|
||||
- 不依赖 Celery worker
|
||||
|
||||
---
|
||||
|
||||
## 2. 代码获取与目录约定
|
||||
|
||||
### 安装系统依赖
|
||||
```bash
|
||||
# Ubuntu/Debian
|
||||
sudo apt update
|
||||
sudo apt install python3 python3-pip python3-venv postgresql postgresql-contrib git
|
||||
|
||||
# CentOS/RHEL
|
||||
sudo yum update
|
||||
sudo yum install python3 python3-pip postgresql postgresql-server git
|
||||
```
|
||||
|
||||
### 配置PostgreSQL
|
||||
```bash
|
||||
# 启动PostgreSQL服务
|
||||
sudo systemctl start postgresql
|
||||
sudo systemctl enable postgresql
|
||||
|
||||
# 创建数据库和用户
|
||||
sudo -u postgres psql
|
||||
```
|
||||
|
||||
在PostgreSQL中执行:
|
||||
```sql
|
||||
CREATE DATABASE moldinsight;
|
||||
CREATE USER molduser WITH PASSWORD 'moldpassword';
|
||||
GRANT ALL PRIVILEGES ON DATABASE moldinsight TO molduser;
|
||||
\q
|
||||
```
|
||||
|
||||
## 2. 项目部署
|
||||
|
||||
### 克隆或复制项目
|
||||
```bash
|
||||
# 如果使用Git
|
||||
cd /opt
|
||||
sudo git clone <your-repo-url> moldinsight
|
||||
sudo chown -R $USER:$USER moldinsight
|
||||
cd moldinsight
|
||||
|
||||
# 或者直接复制项目文件到Linux服务器
|
||||
sudo git clone <your-repo-url> geMoldInsight
|
||||
sudo chown -R $USER:$USER geMoldInsight
|
||||
cd geMoldInsight
|
||||
```
|
||||
|
||||
### 创建Python虚拟环境
|
||||
后续示例默认项目位于:
|
||||
|
||||
```bash
|
||||
cd moldinsight_project
|
||||
python3 -m venv venv
|
||||
source venv/bin/activate
|
||||
/opt/geMoldInsight
|
||||
```
|
||||
|
||||
### 安装依赖
|
||||
---
|
||||
|
||||
## 3. Python 环境准备
|
||||
|
||||
```bash
|
||||
python3.12 -m venv .venv
|
||||
source .venv/bin/activate
|
||||
pip install --upgrade pip
|
||||
pip install -r requirements.txt
|
||||
```
|
||||
|
||||
## 3. 环境配置
|
||||
> 如果需要 gemold 分析能力,请额外准备 PythonOCC 运行环境。该依赖通常通过 conda 或预构建运行镜像提供,而不是直接由 pip 安装。
|
||||
|
||||
---
|
||||
|
||||
## 4. 环境变量配置
|
||||
|
||||
复制模板:
|
||||
|
||||
### 修改环境配置文件
|
||||
编辑 `.env` 文件:
|
||||
```bash
|
||||
nano .env
|
||||
cp .env.example .env
|
||||
```
|
||||
|
||||
修改为Linux环境的配置:
|
||||
```env
|
||||
# 数据库配置(Linux环境)
|
||||
DATABASE_URL=postgresql+asyncpg://molduser:moldpassword@localhost:5432/moldinsight
|
||||
或部署场景参考:
|
||||
|
||||
# 服务配置
|
||||
DEBUG=false
|
||||
```bash
|
||||
cp deploy/.env.example .env
|
||||
```
|
||||
|
||||
至少需要配置(并指向服务器上已存在的基础设施):
|
||||
|
||||
```env
|
||||
HOST=0.0.0.0
|
||||
PORT=8000
|
||||
|
||||
# Redis配置(可选)
|
||||
REDIS_HOST=localhost
|
||||
DB_HOST=127.0.0.1
|
||||
DB_PORT=5432
|
||||
DB_NAME=moldinsight
|
||||
DB_USER=moldinsight_user
|
||||
DB_PASSWORD=change-me
|
||||
|
||||
REDIS_HOST=127.0.0.1
|
||||
REDIS_PORT=6379
|
||||
REDIS_PASSWORD=
|
||||
|
||||
# Kafka配置(可选)
|
||||
KAFKA_BOOTSTRAP_SERVERS=localhost:9092
|
||||
KAFKA_SECURITY_PROTOCOL=PLAINTEXT
|
||||
SECRET_KEY=change-me-in-production
|
||||
ADMIN_USERNAME=admin
|
||||
ADMIN_PASSWORD=change-me
|
||||
|
||||
RUSTFS_ENDPOINT=http://127.0.0.1:9000
|
||||
RUSTFS_ACCESS_KEY=minioadmin
|
||||
RUSTFS_SECRET_KEY=minioadmin
|
||||
```
|
||||
|
||||
### 创建必要的目录
|
||||
说明:
|
||||
- `RUSTFS_*` 仅 **gemold / unified** 模式需要
|
||||
- `inventory-only` 可不使用对象存储
|
||||
- 当前配置读取实现见 [settings.py](../../src/shared/config/settings.py)
|
||||
|
||||
---
|
||||
|
||||
## 5. 数据库初始化
|
||||
|
||||
项目当前使用 Alembic 管理迁移,应用启动时也会执行初始化流程。
|
||||
|
||||
首次部署建议:
|
||||
|
||||
1. 确保服务器上的 PostgreSQL / Redis / 对象存储服务已可访问
|
||||
2. 启动应用前确认 `.env` 中的 `DB_HOST` / `REDIS_HOST` / `RUSTFS_ENDPOINT` 指向真实服务地址
|
||||
3. 让应用执行初始化/升级逻辑
|
||||
|
||||
相关实现参考:
|
||||
- [init_db.py](../../src/shared/database/init_db.py)
|
||||
|
||||
> 当前项目是 **单数据库** 设计,因此 unified / gemold-only / inventory-only 都连接到同一个数据库与同一 migration head。
|
||||
|
||||
---
|
||||
|
||||
## 6. 启动方式
|
||||
|
||||
## 6.1 inventory-only
|
||||
|
||||
```bash
|
||||
mkdir -p uploads html_output logs
|
||||
chmod 755 uploads html_output logs
|
||||
source .venv/bin/activate
|
||||
uvicorn src.entrypoints.inventory:app --host 0.0.0.0 --port 8001
|
||||
```
|
||||
|
||||
## 4. 启动服务
|
||||
适合:
|
||||
- 只提供进销存能力
|
||||
- 不需要模具分析与异步任务
|
||||
|
||||
---
|
||||
|
||||
## 6.2 gemold-only
|
||||
|
||||
### 开发模式启动
|
||||
```bash
|
||||
cd moldinsight_project
|
||||
source venv/bin/activate
|
||||
python src/main.py
|
||||
source .venv/bin/activate
|
||||
uvicorn src.entrypoints.moldinsight:app --host 0.0.0.0 --port 8000
|
||||
```
|
||||
|
||||
### 生产环境启动(使用Gunicorn)
|
||||
适合:
|
||||
- 单独部署模具分析能力
|
||||
- 文件上传 / 分析 / 导出 / 批量分析
|
||||
|
||||
### gemold Celery worker
|
||||
|
||||
建议同时启动 worker:
|
||||
|
||||
```bash
|
||||
# 安装Gunicorn
|
||||
pip install gunicorn uvloop httptools
|
||||
|
||||
# 启动服务
|
||||
cd moldinsight_project
|
||||
source venv/bin/activate
|
||||
gunicorn src.main:app --workers 4 --worker-class uvicorn.workers.UvicornWorker --bind 0.0.0.0:8000
|
||||
source .venv/bin/activate
|
||||
celery -A src.celery_app.celery_app worker --loglevel=info
|
||||
```
|
||||
|
||||
## 5. 系统服务配置(可选)
|
||||
> gemold 的异步处理链路依赖 Celery + Redis;若只启动 HTTP 服务而不启动 worker,上传分析任务可能无法完整处理。
|
||||
|
||||
---
|
||||
|
||||
## 6.3 unified
|
||||
|
||||
当前仓库仍保留历史统一入口 [main.py](../../src/main.py),但它更适合作为**过渡参考**,不建议作为长期标准入口。
|
||||
|
||||
在正式完成组合层重构前,如需统一部署,可优先使用反向代理或部署编排层统一暴露 gemold 与 inventory;后续会演进为显式 `unified_app.py`。
|
||||
|
||||
蓝图参考:
|
||||
- [BACKEND_MODULARIZATION_BLUEPRINT.md](../BACKEND_MODULARIZATION_BLUEPRINT.md)
|
||||
|
||||
---
|
||||
|
||||
## 7. systemd 部署示例
|
||||
|
||||
## 7.1 inventory-only 服务
|
||||
|
||||
创建:
|
||||
|
||||
### 创建systemd服务文件
|
||||
```bash
|
||||
sudo nano /etc/systemd/system/moldinsight.service
|
||||
sudo nano /etc/systemd/system/gemold-inventory.service
|
||||
```
|
||||
|
||||
添加以下内容:
|
||||
```ini
|
||||
[Unit]
|
||||
Description=MoldInsight Geometry Analysis Service
|
||||
After=network.target postgresql.service
|
||||
Description=geMoldInsight Inventory API
|
||||
After=network.target postgresql.service redis.service
|
||||
|
||||
[Service]
|
||||
Type=simple
|
||||
User=www-data
|
||||
Group=www-data
|
||||
WorkingDirectory=/opt/moldinsight/moldinsight_project
|
||||
Environment=PATH=/opt/moldinsight/moldinsight_project/venv/bin
|
||||
ExecStart=/opt/moldinsight/moldinsight_project/venv/bin/gunicorn src.main:app --workers 4 --worker-class uvicorn.workers.UvicornWorker --bind 0.0.0.0:8000
|
||||
WorkingDirectory=/opt/geMoldInsight
|
||||
Environment=PATH=/opt/geMoldInsight/.venv/bin
|
||||
ExecStart=/opt/geMoldInsight/.venv/bin/uvicorn src.entrypoints.inventory:app --host 0.0.0.0 --port 8001
|
||||
Restart=always
|
||||
RestartSec=5
|
||||
|
||||
[Install]
|
||||
WantedBy=multi-user.target
|
||||
```
|
||||
|
||||
### 启用并启动服务
|
||||
启用:
|
||||
|
||||
```bash
|
||||
sudo systemctl daemon-reload
|
||||
sudo systemctl enable moldinsight
|
||||
sudo systemctl start moldinsight
|
||||
sudo systemctl status moldinsight
|
||||
sudo systemctl enable gemold-inventory
|
||||
sudo systemctl start gemold-inventory
|
||||
```
|
||||
|
||||
## 6. Nginx反向代理配置(可选)
|
||||
---
|
||||
|
||||
## 7.2 gemold-only API 服务
|
||||
|
||||
创建:
|
||||
|
||||
### 安装Nginx
|
||||
```bash
|
||||
# Ubuntu/Debian
|
||||
sudo apt install nginx
|
||||
|
||||
# CentOS/RHEL
|
||||
sudo yum install nginx
|
||||
sudo nano /etc/systemd/system/gemold-moldinsight.service
|
||||
```
|
||||
|
||||
### 创建Nginx配置文件
|
||||
```ini
|
||||
[Unit]
|
||||
Description=geMoldInsight MoldInsight API
|
||||
After=network.target postgresql.service redis.service
|
||||
|
||||
[Service]
|
||||
Type=simple
|
||||
User=www-data
|
||||
Group=www-data
|
||||
WorkingDirectory=/opt/geMoldInsight
|
||||
Environment=PATH=/opt/geMoldInsight/.venv/bin
|
||||
ExecStart=/opt/geMoldInsight/.venv/bin/uvicorn src.entrypoints.moldinsight:app --host 0.0.0.0 --port 8000
|
||||
Restart=always
|
||||
RestartSec=5
|
||||
|
||||
[Install]
|
||||
WantedBy=multi-user.target
|
||||
```
|
||||
|
||||
---
|
||||
|
||||
## 7.3 gemold Celery worker 服务
|
||||
|
||||
创建:
|
||||
|
||||
```bash
|
||||
sudo nano /etc/nginx/sites-available/moldinsight
|
||||
sudo nano /etc/systemd/system/gemold-celery.service
|
||||
```
|
||||
|
||||
添加以下内容:
|
||||
```ini
|
||||
[Unit]
|
||||
Description=geMoldInsight MoldInsight Celery Worker
|
||||
After=network.target postgresql.service redis.service
|
||||
|
||||
[Service]
|
||||
Type=simple
|
||||
User=www-data
|
||||
Group=www-data
|
||||
WorkingDirectory=/opt/geMoldInsight
|
||||
Environment=PATH=/opt/geMoldInsight/.venv/bin
|
||||
ExecStart=/opt/geMoldInsight/.venv/bin/celery -A src.celery_app.celery_app worker --loglevel=info
|
||||
Restart=always
|
||||
RestartSec=5
|
||||
|
||||
[Install]
|
||||
WantedBy=multi-user.target
|
||||
```
|
||||
|
||||
---
|
||||
|
||||
## 8. Nginx 反向代理示例
|
||||
|
||||
### 8.1 inventory-only
|
||||
|
||||
```nginx
|
||||
server {
|
||||
listen 80;
|
||||
server_name your-domain.com;
|
||||
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;
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
### 8.2 gemold-only
|
||||
|
||||
```nginx
|
||||
server {
|
||||
listen 80;
|
||||
server_name gemold.example.com;
|
||||
|
||||
location / {
|
||||
proxy_pass http://127.0.0.1:8000;
|
||||
@@ -180,102 +318,86 @@ server {
|
||||
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;
|
||||
expires 30d;
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
### 启用站点并重启Nginx
|
||||
```bash
|
||||
sudo ln -s /etc/nginx/sites-available/moldinsight /etc/nginx/sites-enabled/
|
||||
sudo nginx -t
|
||||
sudo systemctl restart nginx
|
||||
```
|
||||
### 8.3 unified(示意)
|
||||
|
||||
## 7. 防火墙配置
|
||||
统一对外时,可由网关做模块路由,也可直接暴露统一组合后的应用入口。组合层标准化完成前,更推荐通过部署编排或网关来实现统一对外访问。
|
||||
|
||||
---
|
||||
|
||||
## 9. 验证部署
|
||||
|
||||
### 9.1 健康检查
|
||||
|
||||
inventory-only:
|
||||
|
||||
```bash
|
||||
# Ubuntu/Debian (ufw)
|
||||
sudo ufw allow 80
|
||||
sudo ufw allow 8000
|
||||
sudo ufw allow ssh
|
||||
sudo ufw enable
|
||||
|
||||
# CentOS/RHEL (firewalld)
|
||||
sudo firewall-cmd --permanent --add-port=80/tcp
|
||||
sudo firewall-cmd --permanent --add-port=8000/tcp
|
||||
sudo firewall-cmd --permanent --add-service=ssh
|
||||
sudo firewall-cmd --reload
|
||||
curl http://127.0.0.1:8001/health
|
||||
```
|
||||
|
||||
## 8. 验证部署
|
||||
gemold-only:
|
||||
|
||||
### 检查服务状态
|
||||
```bash
|
||||
# 检查应用服务
|
||||
curl http://localhost:8000/health
|
||||
|
||||
# 检查数据库连接
|
||||
sudo -u postgres psql -d moldinsight -c "SELECT version();"
|
||||
curl http://127.0.0.1:8000/health
|
||||
```
|
||||
|
||||
### 测试文件上传
|
||||
访问 `http://your-server-ip:8000` 上传STP文件测试功能。
|
||||
### 9.2 基础能力验证
|
||||
|
||||
## 9. 故障排除
|
||||
#### inventory-only
|
||||
- 登录接口可用
|
||||
- `/api/products` 返回数据
|
||||
- `/api/inventory` 返回数据
|
||||
|
||||
### 常见问题
|
||||
#### gemold-only
|
||||
- 登录接口可用
|
||||
- `/api/upload` 可访问
|
||||
- 上传后 worker 能正常消费任务
|
||||
- `/api/status/{task_id}` 能看到进度
|
||||
|
||||
1. **数据库连接失败**
|
||||
- 检查PostgreSQL服务状态:`sudo systemctl status postgresql`
|
||||
- 验证数据库连接:`psql -h localhost -U molduser -d moldinsight`
|
||||
---
|
||||
|
||||
2. **端口被占用**
|
||||
- 检查端口使用:`netstat -tulpn | grep 8000`
|
||||
- 修改端口或停止占用进程
|
||||
## 10. 常见问题
|
||||
|
||||
3. **权限问题**
|
||||
- 确保目录权限正确:`chmod 755 uploads html_output logs`
|
||||
- 检查文件所有者:`ls -la`
|
||||
### 1. 为什么不再推荐 `src.main:app`?
|
||||
因为当前项目已演进为模块化结构,`src.main:app` 更适合作为过渡兼容入口,而不是长期部署标准。应优先围绕 [entrypoints/](../../src/entrypoints/) 部署。
|
||||
|
||||
4. **依赖安装失败**
|
||||
- 更新pip:`pip install --upgrade pip`
|
||||
- 使用国内镜像:`pip install -r requirements.txt -i https://pypi.tuna.tsinghua.edu.cn/simple`
|
||||
### 2. inventory-only 为什么不需要对象存储?
|
||||
因为对象存储主要服务于 gemold 分析产物(HTML、导出文件等)。纯 inventory 部署不需要这部分基础设施。
|
||||
|
||||
## 10. 备份和恢复
|
||||
### 3. gemold-only 为什么建议同时部署 Celery?
|
||||
因为模具分析任务通常走异步处理链路,仅启动 API 而不启动 worker,会影响上传后的任务处理。
|
||||
|
||||
---
|
||||
|
||||
## 11. Docker Compose 说明
|
||||
|
||||
当前 [docker-compose.yml](../../docker-compose.yml) 仅启动:
|
||||
|
||||
- `moldinsight`
|
||||
- `moldinsight-celery`
|
||||
- `inventory`
|
||||
|
||||
它**不会**再拉起:
|
||||
|
||||
- PostgreSQL
|
||||
- Redis
|
||||
- MinIO
|
||||
|
||||
这些基础设施应由服务器现有服务提供,并通过 `.env` 传入连接信息。
|
||||
|
||||
示例:
|
||||
|
||||
### 数据库备份
|
||||
```bash
|
||||
# 备份数据库
|
||||
sudo -u postgres pg_dump moldinsight > moldinsight_backup.sql
|
||||
|
||||
# 恢复数据库
|
||||
sudo -u postgres psql -d moldinsight < moldinsight_backup.sql
|
||||
docker compose --profile full up -d
|
||||
```
|
||||
|
||||
### 文件备份
|
||||
```bash
|
||||
# 备份上传的文件和配置
|
||||
tar -czf moldinsight_backup.tar.gz uploads/ html_output/ .env requirements.txt
|
||||
```
|
||||
---
|
||||
|
||||
## 快速启动脚本
|
||||
## 12. 推荐阅读
|
||||
|
||||
创建启动脚本 `start.sh`:
|
||||
```bash
|
||||
#!/bin/bash
|
||||
cd /opt/moldinsight/moldinsight_project
|
||||
source venv/bin/activate
|
||||
python src/main.py
|
||||
```
|
||||
|
||||
赋予执行权限:
|
||||
```bash
|
||||
chmod +x start.sh
|
||||
./start.sh
|
||||
```
|
||||
|
||||
现在您的MoldInsight项目已经可以在Linux环境下正常运行!
|
||||
- [README.md](../../README.md)
|
||||
- [BACKEND_MODULARIZATION_BLUEPRINT.md](../BACKEND_MODULARIZATION_BLUEPRINT.md)
|
||||
- [DEPLOY_PORT.md](./DEPLOY_PORT.md)
|
||||
- [PORT_CONFIG.md](./PORT_CONFIG.md)
|
||||
|
||||
+145
-78
@@ -1,107 +1,174 @@
|
||||
# 端口配置说明
|
||||
# 端口配置说明(模块化架构)
|
||||
|
||||
## ⚠️ 重要提示
|
||||
> 本文档说明当前 geMoldInsight 在**模块化部署**下的端口配置方式。
|
||||
|
||||
本项目现在采用**统一端口配置管理**,所有端口配置集中在一个地方:
|
||||
当前架构中应区分:
|
||||
|
||||
**唯一修改端口的地方:** `.env` 文件中的端口配置部分
|
||||
- **gemold API 端口**
|
||||
- **inventory API 端口**
|
||||
- **数据库/Redis/对象存储端口**
|
||||
- **前端访问地址**
|
||||
|
||||
## 配置说明
|
||||
不再推荐把所有部署场景都抽象成“单应用单端口”。
|
||||
|
||||
编辑项目根目录下的 `.env` 文件:
|
||||
---
|
||||
|
||||
```bash
|
||||
# ================================
|
||||
# 端口配置 - 唯一修改端口的地方
|
||||
# ================================
|
||||
# 应用端口(容器内端口)
|
||||
PORT=8000
|
||||
# Docker映射到宿主机的端口(docker-compose使用)
|
||||
HOST_PORT=10001
|
||||
# ================================
|
||||
## 1. 当前主配置位置
|
||||
|
||||
对于 Docker Compose 部署,当前主要端口配置来源于:
|
||||
|
||||
- [docker-compose.yml](../../docker-compose.yml)
|
||||
- `.env` / `deploy/.env.example`
|
||||
|
||||
注意:该 compose 文件仅负责项目应用容器,不负责 PostgreSQL / Redis / 对象存储容器。
|
||||
|
||||
核心环境变量:
|
||||
|
||||
```env
|
||||
MOLDINSIGHT_PORT=8000
|
||||
INVENTORY_PORT=8001
|
||||
```
|
||||
|
||||
### 端口含义
|
||||
其余基础设施通常为:
|
||||
|
||||
| 变量 | 用途 | 默认值 | 说明 |
|
||||
|------|------|--------|------|
|
||||
| `PORT` | 应用监听端口 | 8000 | FastAPI/Uvicorn 服务监听的端口 |
|
||||
| `HOST_PORT` | 宿主机映射端口 | 10001 | Docker Compose 映射到宿主机的端口 |
|
||||
|
||||
### 使用场景
|
||||
|
||||
#### 1. 本地直接运行(Python)
|
||||
```bash
|
||||
python src/main.py
|
||||
```env
|
||||
DB_PORT=5432
|
||||
REDIS_PORT=6379
|
||||
```
|
||||
服务将在 `http://localhost:8000` 启动(使用 `PORT` 配置)
|
||||
|
||||
#### 2. Docker Compose 运行
|
||||
```bash
|
||||
docker-compose up
|
||||
对象存储常见端口(由服务器既有服务提供):
|
||||
|
||||
```env
|
||||
RUSTFS_ENDPOINT=http://localhost:9000
|
||||
```
|
||||
服务将在 `http://localhost:10001` 访问(使用 `HOST_PORT` 配置)
|
||||
容器内部使用 `PORT` 配置的端口(8000)
|
||||
|
||||
#### 3. 修改端口
|
||||
---
|
||||
|
||||
**场景A:只想修改外部访问端口(Docker)**
|
||||
```bash
|
||||
# .env 文件
|
||||
PORT=8000 # 容器内不变
|
||||
HOST_PORT=8080 # 宿主机改为8080
|
||||
## 2. 各端口的含义
|
||||
|
||||
| 变量 / 端口 | 用途 |
|
||||
|---|---|
|
||||
| `MOLDINSIGHT_PORT` | gemold API 宿主机暴露端口 |
|
||||
| `INVENTORY_PORT` | inventory API 宿主机暴露端口 |
|
||||
| `DB_PORT` | PostgreSQL 端口 |
|
||||
| `REDIS_PORT` | Redis 端口 |
|
||||
| `9000` | MinIO/RustFS S3 兼容 API |
|
||||
| `9001` | MinIO 控制台 |
|
||||
|
||||
---
|
||||
|
||||
## 3. 推荐配置方式
|
||||
|
||||
### 3.1 gemold-only
|
||||
|
||||
```env
|
||||
MOLDINSIGHT_PORT=8000
|
||||
DB_PORT=5432
|
||||
REDIS_PORT=6379
|
||||
```
|
||||
访问地址:`http://localhost:8080`
|
||||
|
||||
**场景B:修改应用端口(容器内/本地运行)**
|
||||
```bash
|
||||
# .env 文件
|
||||
PORT=9000 # 应用改为9000
|
||||
HOST_PORT=10001 # 宿主机映射到10001
|
||||
### 3.2 inventory-only
|
||||
|
||||
```env
|
||||
INVENTORY_PORT=8001
|
||||
DB_PORT=5432
|
||||
REDIS_PORT=6379
|
||||
```
|
||||
- 本地运行:`http://localhost:9000`
|
||||
- Docker运行:`http://localhost:10001` (映射到容器内9000)
|
||||
|
||||
**场景C:同时修改两个端口**
|
||||
```bash
|
||||
# .env 文件
|
||||
PORT=9000
|
||||
HOST_PORT=9000
|
||||
### 3.3 full / 模块同时部署
|
||||
|
||||
```env
|
||||
MOLDINSIGHT_PORT=8000
|
||||
INVENTORY_PORT=8001
|
||||
DB_PORT=5432
|
||||
REDIS_PORT=6379
|
||||
```
|
||||
- 本地运行:`http://localhost:9000`
|
||||
- Docker运行:`http://localhost:9000`
|
||||
|
||||
## 配置文件说明
|
||||
---
|
||||
|
||||
### 配置读取优先级
|
||||
## 4. 为什么不再强调单一 `PORT` 变量
|
||||
|
||||
1. **`config/settings.py`** - 从 `.env` 读取 `PORT` 和 `HOST`
|
||||
2. **`src/main.py`** - 从 `settings` 获取端口配置
|
||||
3. **`docker-compose.yml`** - 从 `.env` 读取 `HOST_PORT` 和 `PORT`
|
||||
历史单体部署通常只有一个后端入口,因此 `PORT=8000` 足够。
|
||||
|
||||
### 相关文件
|
||||
当前项目已经是:
|
||||
- gemold 独立入口
|
||||
- inventory 独立入口
|
||||
- unified 作为组合模式而非默认单体入口
|
||||
|
||||
- **`.env`** - ⭐ 唯一需要修改的配置文件
|
||||
- **`config/settings.py`** - 配置读取逻辑(无需修改)
|
||||
- **`src/main.py`** - 使用配置启动服务(无需修改)
|
||||
- **`docker-compose.yml`** - Docker端口映射(自动读取 `.env`)
|
||||
- **`start.sh` / `start_fixed.sh`** - 启动脚本(自动读取 `.env`)
|
||||
因此端口配置必须模块化:
|
||||
|
||||
## 常见问题
|
||||
- gemold 一个端口
|
||||
- inventory 一个端口
|
||||
- 如果 unified 对外存在,可以由网关统一暴露 80/443
|
||||
|
||||
### Q: 为什么 Docker 宿主机端口和应用端口分开配置?
|
||||
A: 这样可以灵活调整容器端口而不影响外部访问,也避免端口冲突。
|
||||
这比继续强行把所有模式压成一个 `PORT` 更清晰,也更符合真实部署方式。
|
||||
|
||||
### Q: 修改后需要重启吗?
|
||||
A: 是的,修改 `.env` 后需要重启服务才能生效:
|
||||
- 本地运行:Ctrl+C 停止后重新 `python src/main.py`
|
||||
- Docker: `docker-compose down && docker-compose up`
|
||||
---
|
||||
|
||||
### Q: 如何避免端口冲突?
|
||||
A: 确保 `HOST_PORT` 不与其他服务冲突,可以使用 `netstat -an | grep <端口>` 检查端口占用情况。
|
||||
## 5. 直接运行与 Compose 的区别
|
||||
|
||||
### Q: 可以使用 80 端口吗?
|
||||
A: 可以,但需要管理员权限:
|
||||
- Linux/Mac: 使用 sudo
|
||||
- Docker: 需要容器有足够权限
|
||||
- 生产环境建议使用反向代理(如 Nginx)
|
||||
### 直接运行
|
||||
|
||||
gemold:
|
||||
|
||||
```bash
|
||||
uvicorn src.entrypoints.moldinsight:app --port 8000
|
||||
```
|
||||
|
||||
inventory:
|
||||
|
||||
```bash
|
||||
uvicorn src.entrypoints.inventory:app --port 8001
|
||||
```
|
||||
|
||||
### Docker Compose
|
||||
|
||||
Compose 通过端口映射暴露服务:
|
||||
|
||||
- gemold → `${MOLDINSIGHT_PORT}:8000`
|
||||
- inventory → `${INVENTORY_PORT}:8001`
|
||||
|
||||
当前实际定义见:
|
||||
- [docker-compose.yml](../../docker-compose.yml)
|
||||
|
||||
---
|
||||
|
||||
## 6. 与前端配置的关系
|
||||
|
||||
前端是否使用 unified / split deployment,会影响前端 API 地址配置。
|
||||
|
||||
### unified
|
||||
- 一个 API 基地址
|
||||
|
||||
### split
|
||||
- gemold 与 inventory 各自基地址
|
||||
|
||||
因此,修改后端端口后,可能还需要同步:
|
||||
|
||||
- 前端 `.env`
|
||||
- Nginx 反向代理
|
||||
- 浏览器访问地址
|
||||
|
||||
---
|
||||
|
||||
## 7. 推荐实践
|
||||
|
||||
1. **本地开发**
|
||||
- gemold:8000
|
||||
- inventory:8001
|
||||
|
||||
2. **服务器部署**
|
||||
- 外网只暴露 80/443
|
||||
- Nginx 反代到 8000 / 8001
|
||||
|
||||
3. **不要继续把所有部署模式都写成 `src.main:app + PORT=8000`**
|
||||
- 这已不符合当前架构
|
||||
|
||||
---
|
||||
|
||||
## 8. 关联文档
|
||||
|
||||
- [LINUX_SETUP.md](./LINUX_SETUP.md)
|
||||
- [DEPLOY_PORT.md](./DEPLOY_PORT.md)
|
||||
- [BACKEND_MODULARIZATION_BLUEPRINT.md](../BACKEND_MODULARIZATION_BLUEPRINT.md)
|
||||
- [README.md](../../README.md)
|
||||
|
||||
@@ -1,192 +1,39 @@
|
||||
# 端口配置重构总结
|
||||
# 端口配置历史说明(已被模块化部署文档取代)
|
||||
|
||||
## 修改内容
|
||||
本文件保留为历史说明。
|
||||
|
||||
本次重构将项目的端口配置统一到 `.env` 文件中,确保整个项目只有一个地方需要修改端口。
|
||||
它所描述的“单体应用单一端口配置”思路,已经不再能完整代表当前 geMoldInsight 的模块化架构。
|
||||
|
||||
## 修改的文件
|
||||
当前项目已演进为:
|
||||
|
||||
### 1. ⭐ `.env` - 唯一配置入口
|
||||
**变更:** 添加了统一的端口配置区域
|
||||
```bash
|
||||
# ================================
|
||||
# 端口配置 - 唯一修改端口的地方
|
||||
# ================================
|
||||
# 应用端口(容器内端口)
|
||||
PORT=8000
|
||||
# Docker映射到宿主机的端口(docker-compose使用)
|
||||
HOST_PORT=10001
|
||||
# ================================
|
||||
```
|
||||
- gemold 模块可独立部署
|
||||
- inventory 模块可独立部署
|
||||
- unified 作为组合模式存在
|
||||
- gemold 与 inventory 应分别考虑端口与网关暴露方式
|
||||
|
||||
### 2. `src/main.py`
|
||||
**变更:** 从硬编码的环境变量读取改为从 `config.settings` 读取
|
||||
```python
|
||||
# 修改前
|
||||
host = os.getenv('HOST', '0.0.0.0')
|
||||
port = int(os.getenv('PORT', '8000'))
|
||||
因此,端口配置的权威说明已转移到以下文档:
|
||||
|
||||
# 修改后
|
||||
from config.settings import settings
|
||||
# ...
|
||||
host=settings.HOST,
|
||||
port=settings.PORT
|
||||
```
|
||||
- [LINUX_SETUP.md](./LINUX_SETUP.md)
|
||||
- [DEPLOY_PORT.md](./DEPLOY_PORT.md)
|
||||
- [PORT_CONFIG.md](./PORT_CONFIG.md)
|
||||
- [BACKEND_MODULARIZATION_BLUEPRINT.md](../BACKEND_MODULARIZATION_BLUEPRINT.md)
|
||||
|
||||
### 3. `docker-compose.yml`
|
||||
**变更:** 端口映射从硬编码改为从环境变量读取
|
||||
```yaml
|
||||
# 修改前
|
||||
ports:
|
||||
- "10001:8000"
|
||||
environment:
|
||||
- PORT=8000
|
||||
---
|
||||
|
||||
# 修改后
|
||||
ports:
|
||||
- "${HOST_PORT:-10001}:${CONTAINER_PORT:-8000}"
|
||||
environment:
|
||||
- HOST=${HOST:-0.0.0.0}
|
||||
- PORT=${CONTAINER_PORT:-8000}
|
||||
```
|
||||
## 当前结论
|
||||
|
||||
### 4. `start.sh` 和 `start_fixed.sh`
|
||||
**变更:** 自动从 `.env` 读取端口并显示正确的访问地址
|
||||
```bash
|
||||
# 添加
|
||||
PORT=$(grep '^PORT=' .env 2>/dev/null | cut -d'=' -f2 || echo '8000')
|
||||
echo "🌐 服务将在 http://localhost:${PORT} 启动"
|
||||
```
|
||||
1. 不再默认以历史单体 `src.main:app` 作为部署中心。
|
||||
2. 不再假设整个系统只有一个后端端口。
|
||||
3. gemold 与 inventory 应按模块分别规划端口。
|
||||
4. unified 更适合通过组合层或网关统一暴露,而不是继续沿用旧单体部署语义。
|
||||
|
||||
### 5. `README.md`
|
||||
**变更:** 更新访问说明,提示端口配置位置
|
||||
---
|
||||
|
||||
### 6. 新增文件
|
||||
- `.env.example` - 配置文件模板
|
||||
- `PORT_CONFIG.md` - 端口配置详细说明
|
||||
- `DEPLOY_PORT.md` - 部署时端口配置指南
|
||||
## 建议
|
||||
|
||||
## 配置读取流程
|
||||
如果你正在查找当前有效的端口/部署方式,请不要继续参考旧的单体端口说明,而应直接查看:
|
||||
|
||||
```
|
||||
.env 文件
|
||||
↓
|
||||
config/settings.py (读取 PORT 和 HOST)
|
||||
↓
|
||||
src/main.py (使用 settings.PORT)
|
||||
↓
|
||||
uvicorn 启动服务
|
||||
```
|
||||
|
||||
Docker 部署流程:
|
||||
```
|
||||
.env 文件
|
||||
↓
|
||||
docker-compose.yml (读取 HOST_PORT 和 PORT)
|
||||
↓
|
||||
容器映射和内部启动
|
||||
```
|
||||
|
||||
## 如何修改端口
|
||||
|
||||
### 方法 1:修改应用端口
|
||||
```bash
|
||||
# 编辑 .env
|
||||
PORT=9000 # 修改此行
|
||||
```
|
||||
- 本地运行:`http://localhost:9000`
|
||||
- Docker运行:需同时修改 `HOST_PORT=9000`
|
||||
|
||||
### 方法 2:修改 Docker 外部访问端口
|
||||
```bash
|
||||
# 编辑 .env
|
||||
HOST_PORT=8080 # 修改此行(PORT 保持不变)
|
||||
```
|
||||
- Docker运行:`http://localhost:8080`
|
||||
- 容器内仍使用 PORT 配置的端口
|
||||
|
||||
## 测试验证
|
||||
|
||||
### 测试 1:本地运行
|
||||
```bash
|
||||
# 修改 .env 中的 PORT
|
||||
PORT=9999
|
||||
|
||||
# 启动服务
|
||||
python src/main.py
|
||||
|
||||
# 验证
|
||||
curl http://localhost:9999/health
|
||||
```
|
||||
|
||||
### 测试 2:Docker 运行
|
||||
```bash
|
||||
# 修改 .env
|
||||
PORT=8000
|
||||
HOST_PORT=9999
|
||||
|
||||
# 启动容器
|
||||
docker-compose up -d
|
||||
|
||||
# 验证
|
||||
curl http://localhost:9999/health
|
||||
```
|
||||
|
||||
### 测试 3:启动脚本
|
||||
```bash
|
||||
# 修改 .env
|
||||
PORT=8888
|
||||
|
||||
# 运行启动脚本
|
||||
./start.sh
|
||||
|
||||
# 检查输出是否显示正确的端口
|
||||
```
|
||||
|
||||
## 注意事项
|
||||
|
||||
1. ✅ 所有端口配置集中在 `.env` 文件
|
||||
2. ✅ 无需修改代码文件即可更改端口
|
||||
3. ✅ 支持本地运行和 Docker 部署两种场景
|
||||
4. ✅ 提供了详细的配置文档
|
||||
5. ⚠️ 修改端口后需要重启服务
|
||||
6. ⚠️ Docker 部署时需要同时考虑容器内外端口
|
||||
7. ⚠️ 确保新端口没有被占用
|
||||
|
||||
## 文件清单
|
||||
|
||||
### 修改的文件
|
||||
- `.env` - 添加端口配置区域
|
||||
- `src/main.py` - 统一使用 settings 配置
|
||||
- `docker-compose.yml` - 支持环境变量配置端口
|
||||
- `start.sh` - 自动读取和显示端口
|
||||
- `start_fixed.sh` - 自动读取和显示端口
|
||||
- `README.md` - 更新访问说明
|
||||
|
||||
### 新增的文件
|
||||
- `.env.example` - 配置模板
|
||||
- `PORT_CONFIG.md` - 端口配置详细说明
|
||||
- `DEPLOY_PORT.md` - 部署配置指南
|
||||
- `PORT_REFACTOR_SUMMARY.md` - 本文档
|
||||
|
||||
## 回滚方案
|
||||
|
||||
如果需要回滚,按以下步骤操作:
|
||||
|
||||
```bash
|
||||
git checkout -- src/main.py
|
||||
git checkout -- docker-compose.yml
|
||||
git checkout -- start.sh start_fixed.sh
|
||||
git checkout -- .env
|
||||
git checkout -- README.md
|
||||
|
||||
# 删除新增文件
|
||||
rm .env.example PORT_CONFIG.md DEPLOY_PORT.md PORT_REFACTOR_SUMMARY.md
|
||||
```
|
||||
|
||||
## 联系支持
|
||||
|
||||
如有问题,请查看:
|
||||
- `PORT_CONFIG.md` - 端口配置详细说明
|
||||
- `DEPLOY_PORT.md` - 部署配置指南
|
||||
- `.env.example` - 配置示例
|
||||
- [README.md](../../README.md)
|
||||
- [LINUX_SETUP.md](./LINUX_SETUP.md)
|
||||
- [DEPLOY_PORT.md](./DEPLOY_PORT.md)
|
||||
- [PORT_CONFIG.md](./PORT_CONFIG.md)
|
||||
|
||||
Reference in New Issue
Block a user