业务目标: 多平台多账户 Delta 中性对冲刷量交易系统
技术架构: Node.js + TypeScript + PM2 + 健康检查 + 结构化日志
src/infrastructure/health//health, /health/summary, /health/live, /health/ready)examples/health_check_test.ts - ✅ 通过src/utils/ProductionLogger.tsexamples/production_logger_test.ts - ✅ 通过ecosystem.config.jssrc/main-production.tsscripts/production-setup.sh - 自动化部署| 运行方式 | 命令 | 适用场景 |
|---|---|---|
| 完整系统 | yarn start:complete |
开发测试、演示 |
| 生产版本 | yarn start:prod |
生产环境直接运行 |
| PM2管理 | yarn deploy:production |
生产环境高可用 |
# 检查 Node.js 版本
node --version # 需要 18+
# 安装 PM2 (如果未安装)
npm install -g pm2
# 克隆项目并安装依赖
git clone <your-repo>
cd binance-api
yarn install
# 复制环境变量模板
cp env.example .env
# 配置必要的环境变量
export NODE_ENV=production
export LOG_LEVEL=info
export LOG_DIR=./logs
export HEALTH_CHECK_PORT=3001
export ENABLE_HEALTH_API=true
export ENABLE_STRUCTURED_LOGGING=true
# 配置交易所 API 密钥
export PACIFICA_PRIVATE_KEY=your_pacifica_private_key
export ASTER_ORDER_USER=your_aster_user_address
export ASTER_API_KEY=your_aster_api_key
export ASTER_API_SECRET=your_aster_api_secret
# 运行自动化部署脚本
chmod +x scripts/production-setup.sh
./scripts/production-setup.sh
# 构建项目
yarn build
# 启动生产环境
pm2 start ecosystem.config.js --env production
# 查看状态
pm2 list
pm2 logs trading-hedge-system
# 完整健康检查
curl http://localhost:3001/health
# 健康状态摘要
curl http://localhost:3001/health/summary
# 存活性检查 (Liveness Probe)
curl http://localhost:3001/health/live
# 就绪性检查 (Readiness Probe)
curl http://localhost:3001/health/ready
# 查看实时日志
pm2 logs trading-hedge-system
# 查看系统状态
./scripts/system-status.sh
# 清理旧日志
./scripts/cleanup-logs.sh
# 查看进程列表
pm2 list
# 重启系统
pm2 restart trading-hedge-system
# 停止系统
pm2 stop trading-hedge-system
# 查看监控面板
pm2 monit
# 重载配置
pm2 reload ecosystem.config.js --env production
// ecosystem.config.js - 主要配置
{
name: 'trading-hedge-system',
instances: 1, // 单实例运行
max_memory_restart: '1G', // 内存限制
min_uptime: '30s', // 最小运行时间
max_restarts: 5, // 最大重启次数
kill_timeout: 30000, // 优雅关闭超时
health_check: {
endpoint: 'http://localhost:3001/health',
interval: 30000 // 30秒健康检查
}
}
// 生产日志配置
{
level: 'info', // 日志级别
enableFile: true, // 文件日志
logDir: './logs', // 日志目录
maxFileSize: 100, // 100MB 文件轮转
enableRotation: true, // 启用轮转
enableAudit: true // 审计日志
}
// 健康检查配置
{
port: 3001, // API 端口
enableDetailedResponse: true, // 详细响应
checkInterval: 30000 // 检查间隔
}
| 指标 | 当前状态 | 目标状态 | 状态 |
|---|---|---|---|
| 健康检查系统 | ✅ 完成 | ✅ 完成 | 🟢 |
| 结构化日志 | ✅ 完成 | ✅ 完成 | 🟢 |
| PM2 配置 | ✅ 完成 | ✅ 完成 | 🟢 |
| 生产主程序 | ✅ 完成 | ✅ 完成 | 🟢 |
| 自动化部署 | ✅ 完成 | ✅ 完成 | 🟢 |
# 未来集成 Prometheus + Grafana
# 当前使用 PM2 内置监控
pm2 monit
健康检查失败
# 检查服务状态
curl http://localhost:3001/health
pm2 logs trading-hedge-system
内存使用过高
# 检查内存使用
pm2 monit
# 重启进程
pm2 restart trading-hedge-system
交易所连接失败
# 检查网络连接
ping api.pacifica.fi
# 验证 API 密钥
echo $PACIFICA_PRIVATE_KEY
日志文件过大
# 清理日志
./scripts/cleanup-logs.sh
# 检查轮转配置
# 1. 备份当前版本
cp -r . ../backup-$(date +%Y%m%d)
# 2. 更新代码
git pull origin main
# 3. 安装依赖和构建
yarn install
yarn build
# 4. 重载 PM2 配置
pm2 reload ecosystem.config.js --env production
# 5. 验证部署
curl http://localhost:3001/health
# 如果升级失败,快速回滚
pm2 stop trading-hedge-system
cp -r ../backup-YYYYMMDD/* .
pm2 start ecosystem.config.js --env production
系统状态: 生产就绪 ✅ 部署时间: < 5分钟 预期可用性: 99.5%+
🎉 恭喜! 对冲刷量交易系统已准备好上生产!