Files
SupplierSystem/deploy/PRODUCTION.md
T
Ubuntu deb3e72750
CI / server typecheck + tests (push) Canceled after 0s
CI / web typecheck + tests + miniprogram syntax (push) Canceled after 0s
CI / eslint (观察项,C7 骨架) (push) Canceled after 0s
CI / npm audit (critical 硬拦) (push) Canceled after 0s
chore: 以 AI 生鲜配送系统 当前版本作为 SupplierSystem 起点
来源仓库:jxc/ai-fresh-delivery
来源提交:f0b1a180e2deb7e4673b9d1c8542619f7bc0f73d
快照树:  09f679334ff38f0ff25e907961c94a8223bcaedd(与来源 HEAD 工作树逐字节一致,3801 个文件)

按约定本仓库不带任何历史提交:来源仓库的历史中含真实凭据
(生产 SSH 私钥 deploy/id_rsa_tencent、server/.env、.env.tencent/.env.production/.env.gitea、
deploy/小程序AppSecret.txt,其中 MP_APPSECRET / S3 AccessKey 经哈希比对与线上同值),
整包推送会把这些凭据永久带入本仓库,故只取「当前版本」的单提交快照。
供应商系统后续开发自本提交开始。
2026-10-08 17:35:43 +08:00

167 lines
8.8 KiB
Markdown
Raw 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.
# 腾讯云生产环境
> 资源全量清单(CVM/CLB/COS/托管 PG/DNS/证书/安全组 + 风险项)见 [docs/manual/04-腾讯云资源清单.md](../docs/manual/04-腾讯云资源清单.md)(2026-09-15 只读 API 实测)。
## 服务器
| 角色 | 实例 | 公网 IP | 内网 IP | 说明 |
|------|------|---------|---------|------|
| test-h5 | ins-9zdly5wm | 119.45.34.106 | 10.206.16.10 | **当前线上前端** Nginx |
| test-h51 | ins-hvq9s3gi | 175.27.253.206 | 10.206.16.15 | **CLB `/api` 后端之一**(与 test-api 双活,systemd `ai-fresh-api` :3001);其 `:80` 前端为 2026-09-09 旧构建、**不在 `/` 规则内**;另跑 Gitea:3000 / Semaphore:3010 |
| test-api | ins-dn3v0znu | 146.56.223.139 | 10.206.16.9 | **当前线上 API** :3001 |
| test-api1 | ins-bzgxnhke | 118.195.186.82 | 10.206.16.14 | **不在任何 CLB 规则内**(原备机身份已撤销,现为开发机 / DSH Harness 宿主;未跑 `ai-fresh-api`) |
| CLB | lb-3g4yhjra / aideiver | CNAME 见下 | — | 七层负载;证书 xdjchuanhui.com |
| 数据库 | 经 wan-clb | 1.13.62.2 | — | PostgreSQL(勿与业务 CLB 混淆) |
## 接入拓扑(2026-09-21 实测更正:前端单活 + API 双活)
浏览器 → `https://www.xdjchuanhui.com`
→ DNS CNAME → `lb-3g4yhjra-4f9thqng7wo1xvae.clb.nj-tencentclb.net`
→ CLB HTTPS:443
 • `/` → **仅 test-h5**(`ins-9zdly5wm`):80 —— 前端单活
 • `/api` → **test-api(`ins-dn3v0znu`):3001 + test-h51(`ins-hvq9s3gi`):3001**,权重各 10 —— **API 双活**
⚠️ **API 是两台**:只发一台会让同一个 CLB 后面同时跑两个版本 → **后端必须两台都发**(见下「后端发布」)。`ins-bzgxnhke`(118.195.186.82)不在任何规则内。
2026-09-10 的单活冻结只对**前端**仍成立;API 侧后来又被挂回第二台,[20260910-prod-single-h5-api.md](../docs/design/20260910-prod-single-h5-api.md) 描述的「仅 test-api」已非现状。**不要把 h51 加进 `/` 规则**——它的前端是 2026-09-09 旧构建。
历史双活设计仍见 `docs/design/20260802-clb-dual-h5-api-zero-downtime.md`(已非现状)。
前端 `config.js` 保持 `apiBase: '/api'`。
### 两台 API 的共享面(决定「为什么只需跑一次迁移」)
| 共享物 | 实测取值 | 含义 |
|---|---|---|
| 生产库 | 两台 `.env` 的 `DATABASE_URL` 同为 `1.13.62.2:20538` | 迁移只需在一台执行;数据天然一致 |
| Redis | test-api = `127.0.0.1:6379`(本机容器);h51 = `10.206.16.9:6379`(= test-api 内网)→ **同一实例** | `queryCacheService` 查询缓存跨两台一致;`withDistributedLock` 调度锁互斥 → 定时任务/竞价 tick 不会各跑一遍 |
| 对象存储 | 同用 COS `sku-imge-1343576026` | 图片/附件一致 |
⚠️ 代价:**Redis 单点在 test-api**。test-api 故障会连带 h51 的缓存与锁不可用(h51 对 test-api 有内网耦合,并非完全对等)。
### 核验拓扑(只读,不需要控制台)
用 `server/scripts/tencent-lib.mjs` 调 CLB `DescribeListeners` + `DescribeTargets`(`LoadBalancerId=lb-3g4yhjra`、`region=ap-nanjing`)。
⚠️ 响应包在 **`Response`** 里(不是 `Result`)——`docs/manual/04-腾讯云资源清单.md` §2 曾因按 `Result` 解析而误判为「权限不足、无法列举」。
## 后端发布(两台都要发)
```bash
# 本地构建 server → 打包 dist + package.json + migrations/*.sql → 上传到目标机 ~/deploy-staging/
# 主库那台跑迁移 + 重启:
ssh -i deploy/id_rsa_tencent ubuntu@146.56.223.139 'bash /opt/ai-fresh-delivery/scripts/prod-apply-backend.sh'
# 第二台共用同一生产库,跳过迁移避免并发 DDL:
ssh -i deploy/id_rsa_tencent ubuntu@175.27.253.206 'SKIP_DB_MIGRATE=1 bash /opt/ai-fresh-delivery/scripts/prod-apply-backend.sh'
```
- test-h51 原先没有该脚本,2026-09-21 已装上仓库版(`scripts/prod-apply-backend.sh`,md5 `72c2cb55…`)。
- 逐台替换 + 重启,**两台之间存在几十秒的版本不一致窗口**;要绝对一致须先从 CLB 摘一台(改 CLB 属生产变更,须明确批准)。
- 前端只发 test-h5。
## 密钥配置
```powershell
copy .env.tencent.example .env.tencent
# 编辑 .env.tencent,填入 CAM 子账号 SecretId / SecretKey
```
`SecretKey.csv` 已废弃,勿再提交仓库。
## 生产迁移治理
若 deploy 时出现 `operator does not exist: character varying = app_role` 或 pending 迁移堆积,在 test-api 上执行:
```powershell
# 本机:上传最新 migrations + 治理脚本后
ssh -i deploy\id_rsa_tencent ubuntu@146.56.223.139 "bash /opt/ai-fresh-delivery/scripts/prod-run-migration-governance.sh"
```
脚本会:跳过已废弃的 RLS 中间迁移 → 补跑 pending → 验证 `node migrate.js` 全绿。
## 权威部署入口(请只用下列脚本)
**现状(2026-09-21 实测):前端单活 + API 双活。**
⚠️ `deploy-prod-all.ps1` 只覆盖「test-h5 + test-api」,**在当前双活 API 下会漏掉 test-h51**,导致 CLB 后面两个版本并存 → 后端发布请用下面「后端发布」的 `prod-apply-backend.sh`(两台都跑)。
`deploy-prod-rolling.ps1`(`ensure-dual-api` / `ensure-dual-h5`)与 `deploy-prod-dual.ps1` 仍属**历史停用**脚本:它们会动 CLB 注册关系(把机器挂进/摘下流量),在没想清楚当前拓扑前不要跑。
| 场景 | 方式 | 说明 |
|------|------|------|
| **现行:后端发布(两台)** | `scripts/prod-apply-backend.sh`(两台各跑一次,第二台带 `SKIP_DB_MIGRATE=1`) | 见上文「后端发布」;构建 → 部署 → 主 API 迁移 → 重启;**不覆盖线上 LLM 配置** |
| 仅前端 | `.\scripts\deploy-frontend-prod.ps1` 或 tar+`cp -a` 覆盖 test-h5 | 单机 H5(test-h5);**不要发 h51**(其前端是旧构建) |
| 仅同步线上模型 | `.\scripts\deploy-prod-llm-env.ps1` | 才用本机 `server/.env` 写 `LLM_*` / `SPEECH_API_KEY` |
| 历史:`deploy-prod-all.ps1` | 单活时代入口 | 只发 test-h5 + test-api;**双活 API 下不完整**,勿单独使用 |
| 滚动不停机 | `.\scripts\deploy-prod-rolling.ps1` | **停用**(会改 CLB 注册关系) |
| 双机短窗 | `.\scripts\deploy-prod-dual.ps1` | **停用**(同上) |
其余 `deploy*.ps1` / `deploy*.sh` / `deploy-split.ps1` 等为历史脚本,**默认勿用**(易打错机器或漏迁移)。
```powershell
# 当前拓扑:只发线上这一套
.\scripts\deploy-prod-all.ps1
```
`deploy-prod-all.ps1` 会:**构建前端 + 后端 → 部署 test-h5 → 部署 test-api → 迁移 → 重启 API**。有短重启窗属预期。⚠️ 它**不含 test-h51**,在 API 双活的现状下会漏一台(另见上文「后端发布」)。
仅更新前端:
```powershell
.\scripts\deploy-frontend-prod.ps1
```
```powershell
node server/scripts/tencent-prod-scan.mjs
node server/scripts/tencent-remote-ops.mjs
```
## SSH 一键修复(推荐)
1. 将腾讯云下载的私钥放到 **`deploy/id_rsa_tencent`**(已在 `.gitignore`,不会提交 Git)
2. Windows 上限制权限(可选):
```powershell
icacls deploy\id_rsa_tencent /inheritance:r /grant:r "$env:USERNAME:(R)"
```
3. 从本机执行:
```powershell
.\scripts\ssh-prod.ps1 -Target both
# 或仅 API: .\scripts\ssh-prod.ps1 -Target api
```
在每台 CVM 上也可手动执行:
```bash
cd /root/canteen-delivery2 # 按实际路径调整
git pull
bash scripts/prod-on-server.sh h5 # test-h5 上
bash scripts/prod-on-server.sh api # test-api 上(含 Nginx)
```
脚本会:重建 Docker → 跑 migrate(bcrypt 列补齐)→ 配置 Nginx → 本地登录测试。
## 数据库每日备份
备份脚本:`scripts/prod-backup-db.sh`(pg_dump custom 格式 + 完整性校验 + 14 天本地保留 + 可选异地复制)。在 test-api 上安装一次:
```bash
sudo mkdir -p /opt/backups/ai-fresh-delivery
sudo cp scripts/prod-backup-db.sh /opt/ai-fresh-delivery/scripts/
sudo chmod +x /opt/ai-fresh-delivery/scripts/prod-backup-db.sh
(sudo crontab -l 2>/dev/null; echo '40 3 * * * /opt/ai-fresh-delivery/scripts/prod-backup-db.sh >> /opt/backups/ai-fresh-delivery/backup.log 2>&1') | sudo crontab -
```
恢复方式见脚本头部注释;每季度至少做一次恢复演练。
## 当前已知问题
1. **test-api 登录失败**:生产代码仍为旧版 `crypt()` 校验,需 `git pull` 重建镜像(含 bcrypt 修复)。
2. **test-api 80 端口**:默认 Nginx 欢迎页,需执行 `setup-prod-api-nginx.sh`。
3. **TAT 远程命令**:若超时,请在 [自动化助手](https://console.cloud.tencent.com/tat) 为实例安装/启用 Agent,或使用 SSH。
## 访问地址
- 管理端:https://www.xdjchuanhui.com/
- API:同源 `/api`(CLB → **test-api + test-h51 双后端**,权重各 10)
默认账号请使用运维下发口令;文档中的历史示例口令上线前必须轮换,勿在公网文档长期明文保留。