5.3 KiB
5.3 KiB
快速开始指南
30秒快速了解
你现在有了一个零停机部署系统:
- 蓝实例运行在 端口 5000
- 绿实例运行在 端口 5001
- Nginx 总是指向活跃实例
- 部署时自动切换到另一个端口,客户端无感知
第一步:初始化(只需一次)
1. 创建目录
mkdir D:\EPproject\LabelReplaceServer\deployment\{blue,green,backups,logs,scripts,config}
2. 复制配置文件
将这些文件复制到相应位置:
deployment-config.json→deployment/config/build-and-publish.ps1→deployment/scripts/health-check.ps1→deployment/scripts/deploy-blue-green.ps1→deployment/scripts/stop-instance.ps1→deployment/scripts/rollback.ps1→deployment/scripts/
3. 第一次启动蓝实例
# 编译到blue目录
D:\EPproject\LabelReplaceServer\deployment\scripts\build-and-publish.ps1 `
-OutputDirectory "D:\EPproject\LabelReplaceServer\deployment\blue" `
-Version "1.0.0"
# 启动蓝实例(在Command Prompt或PowerShell中)
cd D:\EPproject\LabelReplaceServer\deployment\blue
set DEPLOYMENT_INSTANCE=blue
set SHUTDOWN_TIMEOUT=30
CONTROLLER.exe
4. 验证运行
# 打开另一个PowerShell
curl http://localhost:5000/api/health
# 应该看到:
# {"status":"healthy","instance":"blue","port":5000,...}
5. 配置Nginx
编辑你的Nginx配置文件,将以下代码写入:
upstream backend {
server 127.0.0.1:5000 max_fails=2 fail_timeout=10s;
server 127.0.0.1:5001 backup;
}
server {
listen 80;
location / {
proxy_pass http://backend;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
}
}
然后重启Nginx:
nginx -s reload
6. 初始化状态
# 创建初始状态文件
$state = @{
activeInstance = "blue"
lastUpdate = Get-Date -Format "yyyy-MM-dd HH:mm:ss"
version = "1.0.0"
} | ConvertTo-Json |
Set-Content -Path "D:\EPproject\LabelReplaceServer\deployment\instance_state.json" -Force
完成!初始化结束。 ✓
第二步:日常部署(每次更新时)
一行命令部署新版本
D:\EPproject\LabelReplaceServer\deployment\scripts\deploy-blue-green.ps1 -Version "1.1.0"
部署脚本会自动:
- ✓ 编译新版本
- ✓ 部署到非活跃实例
- ✓ 启动新实例
- ✓ 验证健康状态
- ✓ 切换流量
- ✓ 关闭旧实例
部署过程中:
- 你的客户端继续可以访问 ✓
- 无需手动停止应用 ✓
- 无需手动启动应用 ✓
问题排查
健康检查失败?
# 查看新启动的实例是否在运行
Get-Process CONTROLLER
# 手动测试健康检查
curl http://localhost:5000/api/health
curl http://localhost:5001/api/health
# 查看详细日志
Get-Content "D:\EPproject\LabelReplaceServer\deployment\logs\deployment.log" -Tail 100
需要回滚?
# 一条命令回滚到上一个版本
D:\EPproject\LabelReplaceServer\deployment\scripts\rollback.ps1
关键文件位置
| 文件/目录 | 说明 |
|---|---|
deployment/blue/ |
蓝实例应用(端口5000) |
deployment/green/ |
绿实例应用(端口5001) |
deployment/logs/deployment.log |
部署日志 |
deployment/instance_state.json |
当前活跃实例信息 |
deployment/scripts/ |
所有部署脚本 |
监控命令
# 查看当前活跃实例
Get-Content "D:\EPproject\LabelReplaceServer\deployment\instance_state.json" | ConvertFrom-Json
# 查看最近部署日志
Get-Content "D:\EPproject\LabelReplaceServer\deployment\logs\deployment.log" -Tail 50
# 测试两个实例
Write-Host "蓝实例 (5000):" -ForegroundColor Green
curl http://localhost:5000/api/version
Write-Host "绿实例 (5001):" -ForegroundColor Cyan
curl http://localhost:5001/api/version
# 查看进程
Get-Process CONTROLLER
工作流示例
场景:从版本1.0.0升级到1.1.0
# 当前状态:蓝实例(1.0.0) 活跃,绿实例待命
# 1. 执行部署
.\deploy-blue-green.ps1 -Version "1.1.0"
# 脚本自动执行:
# ✓ 编译新版本到绿实例
# ✓ 启动绿实例(1.1.0)
# ✓ 健康检查通过
# ✓ Nginx切换到绿实例
# ✓ 关闭蓝实例(1.0.0)
# 2. 验证部署成功
curl http://localhost:5001/api/version
# 返回:version: "1.1.0"
# 3. 客户端自动使用新版本
# 无需任何操作!
场景:发现问题需要回滚
# 当前状态:绿实例(1.1.0) 活跃,蓝实例待命
# 1. 执行回滚
.\rollback.ps1
# 脚本自动执行:
# ✓ 从备份恢复蓝实例(1.0.0)
# ✓ 启动蓝实例
# ✓ 健康检查通过
# ✓ Nginx切换到蓝实例
# ✓ 关闭绿实例(1.1.0)
# 2. 验证回滚成功
curl http://localhost:5000/api/version
# 返回:version: "1.0.0"
# 完成!客户端已切换回旧版本
下一步
详细信息请查看:
- 完整指南:
deployment/DEPLOYMENT_GUIDE.md - 部署配置:
deployment/config/deployment-config.json - 应用配置:
src/CONTROLLER/appsettings.json
有问题?检查日志文件:deployment/logs/deployment.log
祝部署顺利!🚀