Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
63 changes: 27 additions & 36 deletions docker-compose.dev.yml
Original file line number Diff line number Diff line change
@@ -1,3 +1,21 @@
# ============================================================
# Virtual Path MES - 后端服务开发/生产编排
# 与 docker-compose.yml(基础设施)叠加使用:
# docker compose -f docker-compose.yml -f docker-compose.dev.yml up -d --build
# 生产再加 -f docker-compose.prod.yml
# ============================================================

x-common-env: &common-env
SPRING_PROFILES_ACTIVE: docker
MYSQL_HOST: mysql
MYSQL_PORT: 3306
MYSQL_USERNAME: root
MYSQL_PASSWORD: ${MYSQL_ROOT_PASSWORD:-123455}
# 与 application.yml 的 ${REDIS_HOST:localhost} 对应(勿用 SPRING_REDIS_HOST)
REDIS_HOST: redis
REDIS_PORT: 6379
JWT_SECRET: ${JWT_SECRET:-MesDevOnlyJwtSecret-ChangeMe-InProd-2026}

services:
influxdb:
image: influxdb:2.7-alpine
Expand Down Expand Up @@ -33,13 +51,7 @@ services:
ports:
- "8081:8081"
environment:
SPRING_PROFILES_ACTIVE: docker
MYSQL_HOST: mysql
MYSQL_PORT: 3306
MYSQL_USERNAME: root
MYSQL_PASSWORD: ${MYSQL_ROOT_PASSWORD:-123455}
SPRING_REDIS_HOST: redis
JWT_SECRET: ${JWT_SECRET:-MesDevOnlyJwtSecret-ChangeMe-InProd-2026}
<<: *common-env
depends_on:
mysql:
condition: service_healthy
Expand Down Expand Up @@ -81,12 +93,7 @@ services:
ports:
- "8082:8082"
environment:
MYSQL_HOST: mysql
MYSQL_PORT: 3306
MYSQL_USERNAME: root
MYSQL_PASSWORD: ${MYSQL_ROOT_PASSWORD:-123455}
SPRING_REDIS_HOST: redis
JWT_SECRET: ${JWT_SECRET:-MesDevOnlyJwtSecret-ChangeMe-InProd-2026}
<<: *common-env
depends_on:
mysql:
condition: service_healthy
Expand All @@ -104,13 +111,7 @@ services:
ports:
- "8083:8083"
environment:
SPRING_PROFILES_ACTIVE: docker
MYSQL_HOST: mysql
MYSQL_PORT: 3306
MYSQL_USERNAME: root
MYSQL_PASSWORD: ${MYSQL_ROOT_PASSWORD:-123455}
SPRING_REDIS_HOST: redis
JWT_SECRET: ${JWT_SECRET:-MesDevOnlyJwtSecret-ChangeMe-InProd-2026}
<<: *common-env
depends_on:
mysql:
condition: service_healthy
Expand All @@ -128,20 +129,12 @@ services:
ports:
- "8084:8084"
environment:
MYSQL_HOST: mysql
MYSQL_PORT: 3306
MYSQL_USERNAME: root
MYSQL_PASSWORD: ${MYSQL_ROOT_PASSWORD:-123455}
SPRING_REDIS_HOST: redis
SPRING_KAFKA_BOOTSTRAP_SERVERS: kafka:9092
JWT_SECRET: ${JWT_SECRET:-MesDevOnlyJwtSecret-ChangeMe-InProd-2026}
<<: *common-env
depends_on:
mysql:
condition: service_healthy
redis:
condition: service_healthy
kafka:
condition: service_healthy
networks:
- mes-network

Expand All @@ -154,20 +147,18 @@ services:
ports:
- "8085:8085"
environment:
MYSQL_HOST: mysql
MYSQL_PORT: 3306
MYSQL_USERNAME: root
MYSQL_PASSWORD: ${MYSQL_ROOT_PASSWORD:-123455}
SPRING_REDIS_HOST: redis
SPRING_KAFKA_BOOTSTRAP_SERVERS: kafka:9092
<<: *common-env
# 与 application.yml 的 ${KAFKA_BOOTSTRAP_SERVERS:localhost:9092} 对应
KAFKA_BOOTSTRAP_SERVERS: kafka:9092
INFLUXDB_URL: http://influxdb:8086
INFLUXDB_TOKEN: ${INFLUXDB_TOKEN:-kUVlFGXR2YxkzRUzn9bW9dJazYkigOU6GyYlKLdGsF7ekBLLz9URVnOLMmFZViLy}
JWT_SECRET: ${JWT_SECRET:-MesDevOnlyJwtSecret-ChangeMe-InProd-2026}
depends_on:
mysql:
condition: service_healthy
redis:
condition: service_healthy
kafka:
condition: service_healthy
influxdb:
condition: service_healthy
networks:
Expand Down
9 changes: 8 additions & 1 deletion docker-compose.prod.yml
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,12 @@ services:
REDIS_PORT: 6379
JWT_SECRET: ${JWT_SECRET:-}
ZHIPU_API_KEY: ${ZHIPU_API_KEY:-}
healthcheck:
test: ["CMD", "python", "-c", "import urllib.request; urllib.request.urlopen('http://localhost:8087/api/v1/health', timeout=3)"]
interval: 30s
timeout: 10s
retries: 3
start_period: 40s
depends_on:
mysql:
condition: service_healthy
Expand All @@ -41,7 +47,8 @@ services:
dockerfile: Dockerfile
args:
VITE_API_BASE_URL: /api
VITE_AI_SERVICE_URL: /api/ai
# 相对 /ai(与 baseURL /api 拼接为 /api/ai/**;写 /api/ai 会双重前缀)
VITE_AI_SERVICE_URL: /ai
container_name: mes-frontend
restart: always
ports:
Expand Down
6 changes: 4 additions & 2 deletions docker-compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -79,8 +79,10 @@ services:
environment:
KAFKA_NODE_ID: 1
KAFKA_LISTENER_SECURITY_PROTOCOL_MAP: PLAINTEXT:PLAINTEXT,PLAINTEXT_internal:PLAINTEXT
KAFKA_LISTENERS: PLAINTEXT://0.0.0.0:9092,PLAINTEXT_internal://localhost:9093
KAFKA_ADVERTISED_LISTENERS: PLAINTEXT://localhost:9092,PLAINTEXT_internal://localhost:9093
KAFKA_LISTENERS: PLAINTEXT://0.0.0.0:9092,PLAINTEXT_internal://0.0.0.0:9093
# advertised 用容器服务名 kafka,容器内客户端才能正确连接
# (之前用 localhost 导致 dashboard 连不上 broker)
KAFKA_ADVERTISED_LISTENERS: PLAINTEXT://kafka:9092,PLAINTEXT_internal://kafka:9093
KAFKA_INTER_BROKER_LISTENER_NAME: PLAINTEXT
KAFKA_OFFSETS_TOPIC_REPLICATION_FACTOR: 1
KAFKA_TRANSACTION_STATE_LOG_REPLICATION_FACTOR: 1
Expand Down
127 changes: 113 additions & 14 deletions docs/DEPLOYMENT.md
Original file line number Diff line number Diff line change
Expand Up @@ -116,37 +116,53 @@ docker compose up -d
depends_on:
mysql:
condition: service_healthy
redis:
condition: service_healthy
networks:
- mes-network
# 注意:AI 服务的 kafka bootstrap 在 config.yaml 硬编码为 localhost:9092,
# 若启用 Kafka 预测链路,需改为 kafka:9092(见 §5.1 注)

mes-frontend:
build:
context: ./mes-frontend
dockerfile: Dockerfile
args:
VITE_API_BASE_URL: /api
VITE_AI_SERVICE_URL: /api/ai
container_name: mes-frontend
restart: always
ports:
- "3000:3000"
environment:
VITE_API_BASE_URL: /api
VITE_AI_SERVICE_URL: /api/ai
depends_on:
- mes-gateway
networks:
- mes-network
```

**注 1(AI 服务 Kafka)**:`mes-ai-service/config.yaml` 里 `kafka.bootstrap_servers: "localhost:9092"` 与 `redis.host: "localhost"` 为硬编码。数据库/Redis 连接已支持环境变量覆盖(`MYSQL_HOST`/`REDIS_HOST`,见 `conversation_store.py`/`redis_store.py`),但 **Kafka 未做环境变量覆盖**。若 AI 服务需要消费 Kafka 设备数据,需改 `config.yaml` 为 `${KAFKA_BOOTSTRAP_SERVERS}` 或直接改为 `kafka:9092`(compose 服务名)。不需要 Kafka 预测链路时忽略即可。
**注 1(AI 服务连接)**:`mes-ai-service` 的数据库/Redis 连接已支持环境变量覆盖(`MYSQL_HOST`/`MYSQL_USERNAME`/`MYSQL_PASSWORD`/`MYSQL_DATABASE`/`REDIS_HOST`/`REDIS_PORT`,见 `conversation_store.py`/`redis_store.py`),compose 已注入。AI 服务不消费 Kafka(config.yaml 中 kafka 配置未在代码使用),无需 Kafka 环境变量。

**注 2(前端构建变量)**:Vite 的 `VITE_*` 变量在**构建时**注入,Dockerfile 里 `RUN npm run build` 读不到 compose 的 `environment`。正确做法:
- 在 `mes-frontend/.env.production` 写入 `VITE_API_BASE_URL=/api`、`VITE_AI_SERVICE_URL=/api/ai`(随镜像打包),或
- 在 compose `build.args` 传入 ARG 并在 Dockerfile 中 export
- 在 compose `build.args` 传入 ARG 并在 Dockerfile 中 export(当前 Dockerfile 已支持)

**注 3(CORS)**:`mes-gateway/application-docker.yml` 的 `CORS_ALLOWED_ORIGINS` 已预设 localhost + `https://*.reality-blog.asia` + `https://virtual-path-mes.reality-blog.asia`。生产通过网关统一代理同源,前端请求一般不发跨域;若用独立域名直连网关,设置环境变量 `CORS_ALLOWED_ORIGINS: https://your.domain,https://*.your-domain.com`(逗号分隔多域,支持通配)。

### 5.2 AI 服务依赖(已按代码精简,CPU-only)

AI 服务依赖已按 `src/` 实际 import 精简,**不含 torch/CUDA**(原 requirements 中的 chromadb/sentence-transformers/skl2onnx 从未被代码使用,已移除):

- 运行时:`requirements.txt`(fastapi/uvicorn/numpy/pandas/scikit-learn/lightgbm/xgboost/onnxruntime/redis/pymysql/httpx/zhipuai 等,镜像仅 ~几百 MB)
- 知识库为 TF-IDF(numpy/scikit-learn),模型推理为 LightGBM/XGBoost/ONNX Runtime(CPU)
- 训练并导出 ONNX 为可选项(`train_models.py` 内 try/except 包裹,缺失时自动 fallback 保存 pkl):`pip install onnxmltools onnx`

```bash
docker compose -f docker-compose.yml -f docker-compose.dev.yml -f docker-compose.prod.yml build mes-ai-service
```

**注 3(CORS)**:`mes-gateway/application-docker.yml` 的 `CORS_ALLOWED_ORIGINS` 默认仅 `localhost:3000,localhost:5173`。生产通过网关统一代理同源,前端请求不发跨域,一般无需改;若用独立域名直连网关,设置 `CORS_ALLOWED_ORIGINS: https://your.domain`。
- 宿主机构建失败时,可改用宿主机直接运行:创建 `.venv` 后 `pip install -r requirements.txt`,再 `python -m src.main`(8087)
- 容器自带健康检查(`/api/v1/health`,无需鉴权);数据表启动时自动创建(`CREATE TABLE IF NOT EXISTS`),无需手工导 SQL
- MySQL/Redis 不可用时自动降级(对话历史缓存不可用,LLM/预测/分析不受影响);端口可用 `AI_PORT` 环境变量覆盖

### 5.2 动态设备展示数据源【关键】
### 5.3 动态设备展示数据源【关键】

本地链路依赖 **WPF 桌面模拟器**(`mes-device-simulator-wpf`,仅 Windows)。Linux 服务器需要 headless 模拟器。两个方案:

Expand All @@ -159,7 +175,7 @@ docker compose up -d

> 建议先走**方案 A**:写一个 Python 脚本模拟 10~20 台设备,直接调 dashboard HTTP 接口,最快看到 3D 孪生动起来。

### 5.3 数据库初始化
### 5.4 数据库初始化

首次部署需导入 SQL(`sql/init.sql` + 各 `V*.sql` 迁移,或从现有 MySQL 导出)。MySQL 容器启动后:

Expand Down Expand Up @@ -226,8 +242,91 @@ docker compose down -v # 停止并清数据卷(慎用
## 9. 待办(需代码改动,确认后实施)

1. ✅ **新增 `docker-compose.prod.yml`**:加入 mes-frontend + mes-ai-service(含 build ARG 与 WS 配置)——**已完成**
2. **新增 `scripts/device-simulator.py`**:headless 设备模拟器(方案 A,调 dashboard HTTP 接口)——**待实施**
3. **(可选)** AI 服务 `config.yaml` 的 Kafka/Redis 改为环境变量支持(当前 AI 未用 Kafka,Redis/MySQL 已支持 env 覆盖,暂无需改)
4. **(可选)** 为 .NET 网关补 Dockerfile(方案 B 才需要)
2. ✅ **AI 服务依赖精简**:移除从未使用的 chromadb/sentence-transformers/skl2onnx,单一 `requirements.txt`(无 torch/CUDA)——**已完成**
3. ✅ **统一环境变量**:compose `x-common-env` 锚点、6 个 Dockerfile 默认 `SPRING_PROFILES_ACTIVE=docker`、Redis/Kafka 变量名与代码一致——**已完成**
4. ✅ **CORS 多域/通配**:预设 `https://*.reality-blog.asia` + 隧道域名——**已完成**
5. ✅ **数据库迁移**:`sql/V14__add_device_type.sql`(幂等补 `device_type` 列)——**已完成**
6. ✅ **API 健壮性**:批量创建返回 created/skipped/errors 明细;SQL 语法异常友好提示——**已完成**
7. **新增 `scripts/device-simulator.py`**:headless 设备模拟器(方案 A,调 dashboard HTTP 接口)——**待实施**
8. **(可选)** 为 .NET 网关补 Dockerfile(方案 B 才需要)

> 当前项目**可直接部署**;headless 设备模拟器(#7)是启用"动态设备展示"的关键,需要时请告知。

---

## 10. Nginx 与 Cloudflare Tunnel 说明

### 10.1 架构(前端容器 nginx 是唯一入口)

```
浏览器 ──https──▶ Cloudflare ──http(Tunnel)──▶ mes-frontend(nginx:3000)
├── 静态资源 (dist)
├── /api/** ──▶ mes-gateway:9090
│ └─ 各微服务 / AI
└── /health (健康检查)
```

- **服务器不需要额外装 nginx**:`mes-frontend` 容器内置 `nginx:alpine`(配置见 `mes-frontend/nginx.conf`),已含:
- Vue history 路由 fallback(`try_files ... /index.html`)
- 静态资源 30 天缓存 + gzip
- `/api/` 反代到网关(含 WebSocket 升级,路径 `/api/ws/dashboard`)
- Cloudflare Flexible SSL 的 `X-Forwarded-Proto` 透传(后端据此判断协议)
- `client_max_body_size 20m`、`/health` 探活

**API 路径拼接规范**(防止双重 `/api` 前缀):

```
前端 axios baseURL: VITE_API_BASE_URL=/api (全局唯一前缀来源)
AI 模块: VITE_AI_SERVICE_URL=/ai (相对路径,禁止写 /api/ai)
→ 实际请求: /api/ai/api/v1/** → 网关 Path=/api/ai/** + StripPrefix=2 → AI 服务 /api/v1/**
```

### 10.2 宿主机上已装的 nginx 处理

服务器若已装宿主机 nginx(占用 80),二选一:

**方案 A(推荐,最简单):直接停用** —— Tunnel 直连 3000,宿主机 nginx 不需要:

```bash
sudo systemctl stop nginx && sudo systemctl disable nginx
```

**方案 B:只做 80 → 3000 转发**(如需直连 80 端口访问):

```nginx
# /etc/nginx/conf.d/mes.conf
server {
listen 80 default_server;
server_name _;
location / {
proxy_pass http://127.0.0.1:3000;
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;
# WebSocket
proxy_http_version 1.1;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection "upgrade";
proxy_read_timeout 3600s;
}
}
sudo nginx -t && sudo systemctl reload nginx
```

### 10.3 Cloudflare 设置

- `cloudflared` 隧道指向服务器 **`localhost:3000`**(容器 nginx,非 80)
- **SSL/TLS 模式必须设为 Flexible**(CF 代理 HTTPS → 源站 HTTP),否则源站需要证书
- 前端与网关同源(`/api` 代理),CORS 默认已放行该域名,无需额外配置;换域名时更新 `CORS_ALLOWED_ORIGINS`

### 10.4 常见问题排查

> 当前项目**可直接部署**;headless 设备模拟器(#2)是启用"动态设备展示"的关键,需要时请告知。
| 症状 | 原因 | 处理 |
|------|------|------|
| 页面 404(刷新子路由) | 容器 nginx 未生效 history fallback | 确认用 `mes-frontend/nginx.conf`(Dockerfile 已 COPY) |
| WS 实时数据断连/不刷新 | Upgrade 头未透传 | 确认 nginx.conf 有 `map $http_upgrade` 段(已内置) |
| 上传/导入报 413 | body 超限 | 已内置 `client_max_body_size 20m`,按需调大 |
| 502 Bad Gateway | 网关容器未就绪/挂了 | `docker compose logs -f mes-gateway`;确认网关已健康 |
| 登录后无限重定向/安全头报 http | Flexible SSL 下协议被覆盖为 http | 已内置 `X-Forwarded-Proto` 透传;检查是否用旧版 nginx.conf |
| 全站 521/522 | Tunnel 指错端口或容器没起 | `cloudflared` 指向 3000;`curl http://localhost:3000/health` |
6 changes: 6 additions & 0 deletions mes-ai-service/.dockerignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
.venv/
__pycache__/
*.pyc
.idea/
.git/
tests/
22 changes: 19 additions & 3 deletions mes-ai-service/Dockerfile
Original file line number Diff line number Diff line change
@@ -1,9 +1,25 @@
FROM python:3.11-slim
# Virtual Path MES - AI 服务镜像(CPU-only,无 torch/CUDA)
# 构建:docker build -t mes-ai-service .
# Python 版本与本地开发环境一致(README 技术栈为 3.12)
FROM python:3.12-slim

# pip 镜像源(国内服务器默认清华源,海外可用 --build-arg PIP_INDEX=https://pypi.org/simple 覆盖)
ARG PIP_INDEX=https://pypi.tuna.tsinghua.edu.cn/simple

WORKDIR /app

COPY requirements.txt .
RUN pip install --no-cache-dir -r requirements.txt
RUN pip install --no-cache-dir --upgrade pip -i ${PIP_INDEX} && \
pip install --no-cache-dir -r requirements.txt -i ${PIP_INDEX}

COPY src/ ./src/
COPY knowledge/ ./knowledge/
COPY config.yaml ./config.yaml
RUN mkdir -p models tests

EXPOSE 8087

# slim 镜像无 curl,用标准库 urllib 做健康检查(/api/v1/health 无需鉴权)
HEALTHCHECK --interval=30s --timeout=5s --retries=3 --start-period=30s \
CMD python -c "import urllib.request; urllib.request.urlopen('http://localhost:8087/api/v1/health', timeout=3)" || exit 1

CMD ["python", "-m", "src.main"]
Loading
Loading