mirror of
https://github.com/NoFxAiOS/nofx.git
synced 2026-07-05 03:50:59 +08:00
Feat: Update docs
- 重构文档结构 - 更新文档内容 - 制定roadmap - 提供中/EN 双语文档
This commit is contained in:
127
docs/getting-started/README.md
Normal file
127
docs/getting-started/README.md
Normal file
@@ -0,0 +1,127 @@
|
||||
# 🚀 Getting Started with NOFX
|
||||
|
||||
**Language:** [English](README.md) | [中文](README.zh-CN.md)
|
||||
|
||||
This section contains all the documentation you need to get NOFX up and running.
|
||||
|
||||
## 📋 Deployment Options
|
||||
|
||||
Choose the method that best fits your needs:
|
||||
|
||||
### 🐳 Docker Deployment (Recommended)
|
||||
|
||||
**Best for:** Beginners, quick setup, production deployments
|
||||
|
||||
- **English:** [docker-deploy.en.md](docker-deploy.en.md)
|
||||
- **中文:** [docker-deploy.zh-CN.md](docker-deploy.zh-CN.md)
|
||||
|
||||
**Pros:**
|
||||
- ✅ One-command setup
|
||||
- ✅ All dependencies included
|
||||
- ✅ Easy to update and manage
|
||||
- ✅ Isolated environment
|
||||
|
||||
**Quick Start:**
|
||||
```bash
|
||||
cp config.example.jsonc config.json
|
||||
./start.sh start --build
|
||||
```
|
||||
|
||||
---
|
||||
|
||||
### 🔧 PM2 Deployment
|
||||
|
||||
**Best for:** Advanced users, development, custom setups
|
||||
|
||||
- **English:** [pm2-deploy.en.md](pm2-deploy.en.md)
|
||||
- **中文:** [pm2-deploy.md](pm2-deploy.md)
|
||||
|
||||
**Pros:**
|
||||
- ✅ Direct process control
|
||||
- ✅ Better for development
|
||||
- ✅ Lower resource usage
|
||||
- ✅ More flexible
|
||||
|
||||
**Quick Start:**
|
||||
```bash
|
||||
go build -o nofx
|
||||
cd web && npm install && npm run build
|
||||
pm2 start ecosystem.config.js
|
||||
```
|
||||
|
||||
---
|
||||
|
||||
## 🤖 AI Configuration
|
||||
|
||||
### Custom AI Providers
|
||||
|
||||
- **English:** [custom-api.en.md](custom-api.en.md)
|
||||
- **中文:** [custom-api.md](custom-api.md)
|
||||
|
||||
Use custom AI models or third-party OpenAI-compatible APIs:
|
||||
- Custom DeepSeek endpoints
|
||||
- Self-hosted models
|
||||
- Other LLM providers
|
||||
|
||||
---
|
||||
|
||||
## 🔑 Prerequisites
|
||||
|
||||
Before starting, ensure you have:
|
||||
|
||||
### For Docker Method:
|
||||
- ✅ Docker 20.10+
|
||||
- ✅ Docker Compose V2
|
||||
|
||||
### For Manual Method:
|
||||
- ✅ Go 1.21+
|
||||
- ✅ Node.js 18+
|
||||
- ✅ TA-Lib library
|
||||
- ✅ PM2 (optional)
|
||||
|
||||
---
|
||||
|
||||
## 📚 Next Steps
|
||||
|
||||
After deployment:
|
||||
|
||||
1. **Configure AI Models** → Web interface at http://localhost:3000
|
||||
2. **Set Up Exchange** → Add Binance/Hyperliquid credentials
|
||||
3. **Create Traders** → Combine AI models with exchanges
|
||||
4. **Start Trading** → Monitor performance in dashboard
|
||||
|
||||
---
|
||||
|
||||
## ⚠️ Important Notes
|
||||
|
||||
**Before Trading:**
|
||||
- ⚠️ Test on testnet first
|
||||
- ⚠️ Start with small amounts
|
||||
- ⚠️ Understand the risks
|
||||
- ⚠️ Read [Security Policy](../../SECURITY.md)
|
||||
|
||||
**API Keys:**
|
||||
- 🔑 Never commit API keys to git
|
||||
- 🔑 Use environment variables
|
||||
- 🔑 Restrict IP access
|
||||
- 🔑 Enable 2FA on exchanges
|
||||
|
||||
---
|
||||
|
||||
## 🆘 Troubleshooting
|
||||
|
||||
**Common Issues:**
|
||||
|
||||
1. **Docker build fails** → Check Docker version, update to 20.10+
|
||||
2. **TA-Lib not found** → `brew install ta-lib` (macOS) or `apt-get install libta-lib0-dev` (Ubuntu)
|
||||
3. **Port 8080 in use** → Change `API_PORT` in .env file
|
||||
4. **Frontend won't connect** → Check backend is running on port 8080
|
||||
|
||||
**Need more help?**
|
||||
- 📖 [FAQ](../guides/faq.zh-CN.md)
|
||||
- 💬 [Telegram Community](https://t.me/nofx_dev_community)
|
||||
- 🐛 [GitHub Issues](https://github.com/tinkle-community/nofx/issues)
|
||||
|
||||
---
|
||||
|
||||
[← Back to Documentation Home](../README.md)
|
||||
125
docs/getting-started/README.zh-CN.md
Normal file
125
docs/getting-started/README.zh-CN.md
Normal file
@@ -0,0 +1,125 @@
|
||||
# 🚀 NOFX 快速开始
|
||||
|
||||
本节包含让 NOFX 运行起来所需的所有文档。
|
||||
|
||||
## 📋 部署选项
|
||||
|
||||
选择最适合您的方式:
|
||||
|
||||
### 🐳 Docker 部署(推荐)
|
||||
|
||||
**适合:** 初学者、快速部署、生产环境
|
||||
|
||||
- **中文文档:** [docker-deploy.zh-CN.md](docker-deploy.zh-CN.md)
|
||||
- **English:** [docker-deploy.en.md](docker-deploy.en.md)
|
||||
|
||||
**优势:**
|
||||
- ✅ 一键启动
|
||||
- ✅ 包含所有依赖
|
||||
- ✅ 易于更新和管理
|
||||
- ✅ 隔离环境
|
||||
|
||||
**快速开始:**
|
||||
```bash
|
||||
cp config.example.jsonc config.json
|
||||
./start.sh start --build
|
||||
```
|
||||
|
||||
---
|
||||
|
||||
### 🔧 PM2 部署
|
||||
|
||||
**适合:** 进阶用户、开发环境、自定义设置
|
||||
|
||||
- **中文文档:** [pm2-deploy.md](pm2-deploy.md)
|
||||
- **English:** [pm2-deploy.en.md](pm2-deploy.en.md)
|
||||
|
||||
**优势:**
|
||||
- ✅ 直接进程控制
|
||||
- ✅ 更适合开发
|
||||
- ✅ 资源占用更低
|
||||
- ✅ 更灵活
|
||||
|
||||
**快速开始:**
|
||||
```bash
|
||||
go build -o nofx
|
||||
cd web && npm install && npm run build
|
||||
pm2 start ecosystem.config.js
|
||||
```
|
||||
|
||||
---
|
||||
|
||||
## 🤖 AI 配置
|
||||
|
||||
### 自定义 AI 提供商
|
||||
|
||||
- **中文文档:** [custom-api.md](custom-api.md)
|
||||
- **English:** [custom-api.en.md](custom-api.en.md)
|
||||
|
||||
使用自定义 AI 模型或第三方 OpenAI 兼容 API:
|
||||
- 自定义 DeepSeek 端点
|
||||
- 本地部署的模型
|
||||
- 其他 LLM 提供商
|
||||
|
||||
---
|
||||
|
||||
## 🔑 环境要求
|
||||
|
||||
开始之前,请确保已安装:
|
||||
|
||||
### Docker 方式:
|
||||
- ✅ Docker 20.10+
|
||||
- ✅ Docker Compose V2
|
||||
|
||||
### 手动部署方式:
|
||||
- ✅ Go 1.21+
|
||||
- ✅ Node.js 18+
|
||||
- ✅ TA-Lib 库
|
||||
- ✅ PM2(可选)
|
||||
|
||||
---
|
||||
|
||||
## 📚 下一步
|
||||
|
||||
部署完成后:
|
||||
|
||||
1. **配置 AI 模型** → 访问 Web 界面 http://localhost:3000
|
||||
2. **设置交易所** → 添加 Binance/Hyperliquid 凭证
|
||||
3. **创建交易员** → 将 AI 模型与交易所结合
|
||||
4. **开始交易** → 在仪表板中监控表现
|
||||
|
||||
---
|
||||
|
||||
## ⚠️ 重要提示
|
||||
|
||||
**交易前:**
|
||||
- ⚠️ 先在测试网测试
|
||||
- ⚠️ 从小金额开始
|
||||
- ⚠️ 了解风险
|
||||
- ⚠️ 阅读[安全策略](../../SECURITY.md)
|
||||
|
||||
**API 密钥:**
|
||||
- 🔑 永远不要提交 API 密钥到 git
|
||||
- 🔑 使用环境变量
|
||||
- 🔑 限制 IP 访问
|
||||
- 🔑 在交易所启用 2FA
|
||||
|
||||
---
|
||||
|
||||
## 🆘 故障排除
|
||||
|
||||
**常见问题:**
|
||||
|
||||
1. **Docker 构建失败** → 检查 Docker 版本,更新到 20.10+
|
||||
2. **找不到 TA-Lib** → `brew install ta-lib` (macOS) 或 `apt-get install libta-lib0-dev` (Ubuntu)
|
||||
3. **端口 8080 被占用** → 在 .env 文件中更改 `API_PORT`
|
||||
4. **前端无法连接** → 检查后端是否在端口 8080 上运行
|
||||
|
||||
**需要更多帮助?**
|
||||
- 📖 [常见问题](../guides/faq.zh-CN.md)
|
||||
- 💬 [Telegram 社区](https://t.me/nofx_dev_community)
|
||||
- 🐛 [GitHub Issues](https://github.com/tinkle-community/nofx/issues)
|
||||
|
||||
---
|
||||
|
||||
[← 返回文档首页](../README.md)
|
||||
207
docs/getting-started/custom-api.en.md
Normal file
207
docs/getting-started/custom-api.en.md
Normal file
@@ -0,0 +1,207 @@
|
||||
# Custom AI API Usage Guide
|
||||
|
||||
## Features
|
||||
|
||||
NOFX now supports using any OpenAI-compatible API format, including:
|
||||
- OpenAI official API (gpt-4o, gpt-4-turbo, etc.)
|
||||
- OpenRouter (access to multiple models)
|
||||
- Locally deployed models (Ollama, LM Studio, etc.)
|
||||
- Other OpenAI-compatible API services
|
||||
|
||||
## Configuration Method
|
||||
|
||||
~~Add trader using custom API in `config.json` (deprecated):~~
|
||||
|
||||
*Note: Custom APIs and traders are now configured through the Web interface. config.json only retains basic settings.*
|
||||
|
||||
```json
|
||||
{
|
||||
"traders": [
|
||||
{
|
||||
"id": "trader_custom",
|
||||
"name": "My Custom AI Trader",
|
||||
"ai_model": "custom",
|
||||
"exchange": "binance",
|
||||
|
||||
"binance_api_key": "your_binance_api_key",
|
||||
"binance_secret_key": "your_binance_secret_key",
|
||||
|
||||
"custom_api_url": "https://api.openai.com/v1",
|
||||
"custom_api_key": "sk-your-openai-api-key",
|
||||
"custom_model_name": "gpt-4o",
|
||||
|
||||
"initial_balance": 1000,
|
||||
"scan_interval_minutes": 3
|
||||
}
|
||||
]
|
||||
}
|
||||
```
|
||||
|
||||
## Configuration Fields
|
||||
|
||||
| Field | Type | Required | Description |
|
||||
|-------|------|----------|-------------|
|
||||
| `ai_model` | string | ✅ | Set to `"custom"` to enable custom API |
|
||||
| `custom_api_url` | string | ✅ | API Base URL (without `/chat/completions`). Special usage: If ending with `#`, use full URL (no auto path append) |
|
||||
| `custom_api_key` | string | ✅ | API key |
|
||||
| `custom_model_name` | string | ✅ | Model name (e.g. `gpt-4o`, `claude-3-5-sonnet`, etc.) |
|
||||
|
||||
## Usage Examples
|
||||
|
||||
### 1. OpenAI Official API
|
||||
|
||||
```json
|
||||
{
|
||||
"ai_model": "custom",
|
||||
"custom_api_url": "https://api.openai.com/v1",
|
||||
"custom_api_key": "sk-proj-xxxxx",
|
||||
"custom_model_name": "gpt-4o"
|
||||
}
|
||||
```
|
||||
|
||||
### 2. OpenRouter
|
||||
|
||||
```json
|
||||
{
|
||||
"ai_model": "custom",
|
||||
"custom_api_url": "https://openrouter.ai/api/v1",
|
||||
"custom_api_key": "sk-or-xxxxx",
|
||||
"custom_model_name": "anthropic/claude-3.5-sonnet"
|
||||
}
|
||||
```
|
||||
|
||||
### 3. Local Ollama
|
||||
|
||||
```json
|
||||
{
|
||||
"ai_model": "custom",
|
||||
"custom_api_url": "http://localhost:11434/v1",
|
||||
"custom_api_key": "ollama",
|
||||
"custom_model_name": "llama3.1:70b"
|
||||
}
|
||||
```
|
||||
|
||||
### 4. Azure OpenAI
|
||||
|
||||
```json
|
||||
{
|
||||
"ai_model": "custom",
|
||||
"custom_api_url": "https://your-resource.openai.azure.com/openai/deployments/your-deployment",
|
||||
"custom_api_key": "your-azure-api-key",
|
||||
"custom_model_name": "gpt-4"
|
||||
}
|
||||
```
|
||||
|
||||
### 5. Using Full Custom Path (append #)
|
||||
|
||||
For certain special API endpoints that already include the full path (including `/chat/completions` or other custom paths), you can append `#` at the end of the URL to force using the full URL:
|
||||
|
||||
```json
|
||||
{
|
||||
"ai_model": "custom",
|
||||
"custom_api_url": "https://api.example.com/v2/ai/chat/completions#",
|
||||
"custom_api_key": "your-api-key",
|
||||
"custom_model_name": "custom-model"
|
||||
}
|
||||
```
|
||||
|
||||
**Note**: The `#` will be automatically removed, and the actual request will be sent to `https://api.example.com/v2/ai/chat/completions`
|
||||
|
||||
## Compatibility Requirements
|
||||
|
||||
Custom APIs must:
|
||||
1. Support OpenAI Chat Completions format
|
||||
2. Accept `POST` requests to `/chat/completions` endpoint (or append `#` at URL end for custom path)
|
||||
3. Support `Authorization: Bearer {api_key}` authentication
|
||||
4. Return standard OpenAI response format
|
||||
|
||||
## Important Notes
|
||||
|
||||
1. **URL Format**: `custom_api_url` should be the Base URL, system will auto-append `/chat/completions`
|
||||
- ✅ Correct: `https://api.openai.com/v1`
|
||||
- ❌ Wrong: `https://api.openai.com/v1/chat/completions`
|
||||
- 🔧 **Special usage**: If you need to use a full custom path (without auto-appending `/chat/completions`), append `#` at the URL end
|
||||
- Example: `https://api.example.com/custom/path/chat/completions#`
|
||||
- System will automatically remove `#` and use the full URL directly
|
||||
|
||||
2. **Model Name**: Ensure `custom_model_name` exactly matches the model name supported by your API provider
|
||||
|
||||
3. **API Key**: Some locally deployed models may not require a real API key, you can fill in any string
|
||||
|
||||
4. **Timeout Settings**: Default timeout is 120 seconds, may need adjustment if model response is slow
|
||||
|
||||
## Multi-AI Comparison Trading
|
||||
|
||||
You can configure multiple traders with different AIs for comparison:
|
||||
|
||||
```json
|
||||
{
|
||||
"traders": [
|
||||
{
|
||||
"id": "deepseek_trader",
|
||||
"ai_model": "deepseek",
|
||||
"deepseek_key": "sk-xxxxx",
|
||||
...
|
||||
},
|
||||
{
|
||||
"id": "gpt4_trader",
|
||||
"ai_model": "custom",
|
||||
"custom_api_url": "https://api.openai.com/v1",
|
||||
"custom_api_key": "sk-xxxxx",
|
||||
"custom_model_name": "gpt-4o",
|
||||
...
|
||||
},
|
||||
{
|
||||
"id": "claude_trader",
|
||||
"ai_model": "custom",
|
||||
"custom_api_url": "https://openrouter.ai/api/v1",
|
||||
"custom_api_key": "sk-or-xxxxx",
|
||||
"custom_model_name": "anthropic/claude-3.5-sonnet",
|
||||
...
|
||||
}
|
||||
]
|
||||
}
|
||||
```
|
||||
|
||||
## Troubleshooting
|
||||
|
||||
### Issue: Configuration Validation Failed
|
||||
|
||||
**Error Message**: `使用自定义API时必须配置custom_api_url` (custom_api_url must be configured when using custom API)
|
||||
|
||||
**Solution**: After setting `ai_model: "custom"`, ensure you also configure:
|
||||
- `custom_api_url`
|
||||
- `custom_api_key`
|
||||
- `custom_model_name`
|
||||
|
||||
### Issue: API Call Failed
|
||||
|
||||
**Possible Causes**:
|
||||
1. URL format error
|
||||
- Normal usage: Should not include `/chat/completions` (system will auto-append)
|
||||
- Special usage: If full path is needed, remember to append `#` at URL end
|
||||
2. Invalid API key
|
||||
3. Incorrect model name
|
||||
4. Network connection issues
|
||||
|
||||
**Debug Method**: Check error messages in logs, usually includes HTTP status code and error details
|
||||
|
||||
## Backward Compatibility
|
||||
|
||||
Existing `deepseek` and `qwen` configurations are unaffected and can continue to be used:
|
||||
|
||||
```json
|
||||
{
|
||||
"ai_model": "deepseek",
|
||||
"deepseek_key": "sk-xxxxx"
|
||||
}
|
||||
```
|
||||
|
||||
Or
|
||||
|
||||
```json
|
||||
{
|
||||
"ai_model": "qwen",
|
||||
"qwen_key": "sk-xxxxx"
|
||||
}
|
||||
```
|
||||
207
docs/getting-started/custom-api.md
Normal file
207
docs/getting-started/custom-api.md
Normal file
@@ -0,0 +1,207 @@
|
||||
# 自定义 AI API 使用指南
|
||||
|
||||
## 功能说明
|
||||
|
||||
现在 NOFX 支持使用任何 OpenAI 格式兼容的 API,包括:
|
||||
- OpenAI 官方 API (gpt-4o, gpt-4-turbo 等)
|
||||
- OpenRouter (可访问多种模型)
|
||||
- 本地部署的模型 (Ollama, LM Studio 等)
|
||||
- 其他兼容 OpenAI 格式的 API 服务
|
||||
|
||||
## 配置方式
|
||||
|
||||
在 `config.json` 中添加使用自定义 API 的 trader(~~已弃用~~):
|
||||
|
||||
*注意:现在通过Web界面配置自定义API和交易员,config.json仅保留基础设置*
|
||||
|
||||
```json
|
||||
{
|
||||
"traders": [
|
||||
{
|
||||
"id": "trader_custom",
|
||||
"name": "My Custom AI Trader",
|
||||
"ai_model": "custom",
|
||||
"exchange": "binance",
|
||||
|
||||
"binance_api_key": "your_binance_api_key",
|
||||
"binance_secret_key": "your_binance_secret_key",
|
||||
|
||||
"custom_api_url": "https://api.openai.com/v1",
|
||||
"custom_api_key": "sk-your-openai-api-key",
|
||||
"custom_model_name": "gpt-4o",
|
||||
|
||||
"initial_balance": 1000,
|
||||
"scan_interval_minutes": 3
|
||||
}
|
||||
]
|
||||
}
|
||||
```
|
||||
|
||||
## 配置字段说明
|
||||
|
||||
| 字段 | 类型 | 必需 | 说明 |
|
||||
|-----|------|------|------|
|
||||
| `ai_model` | string | ✅ | 设置为 `"custom"` 启用自定义 API |
|
||||
| `custom_api_url` | string | ✅ | API 的 Base URL (不含 `/chat/completions`)。特殊用法:如果以 `#` 结尾,则使用完整 URL(不自动添加路径) |
|
||||
| `custom_api_key` | string | ✅ | API 密钥 |
|
||||
| `custom_model_name` | string | ✅ | 模型名称 (如 `gpt-4o`, `claude-3-5-sonnet` 等) |
|
||||
|
||||
## 使用示例
|
||||
|
||||
### 1. OpenAI 官方 API
|
||||
|
||||
```json
|
||||
{
|
||||
"ai_model": "custom",
|
||||
"custom_api_url": "https://api.openai.com/v1",
|
||||
"custom_api_key": "sk-proj-xxxxx",
|
||||
"custom_model_name": "gpt-4o"
|
||||
}
|
||||
```
|
||||
|
||||
### 2. OpenRouter
|
||||
|
||||
```json
|
||||
{
|
||||
"ai_model": "custom",
|
||||
"custom_api_url": "https://openrouter.ai/api/v1",
|
||||
"custom_api_key": "sk-or-xxxxx",
|
||||
"custom_model_name": "anthropic/claude-3.5-sonnet"
|
||||
}
|
||||
```
|
||||
|
||||
### 3. 本地 Ollama
|
||||
|
||||
```json
|
||||
{
|
||||
"ai_model": "custom",
|
||||
"custom_api_url": "http://localhost:11434/v1",
|
||||
"custom_api_key": "ollama",
|
||||
"custom_model_name": "llama3.1:70b"
|
||||
}
|
||||
```
|
||||
|
||||
### 4. Azure OpenAI
|
||||
|
||||
```json
|
||||
{
|
||||
"ai_model": "custom",
|
||||
"custom_api_url": "https://your-resource.openai.azure.com/openai/deployments/your-deployment",
|
||||
"custom_api_key": "your-azure-api-key",
|
||||
"custom_model_name": "gpt-4"
|
||||
}
|
||||
```
|
||||
|
||||
### 5. 使用完整自定义路径(末尾添加 #)
|
||||
|
||||
对于某些特殊的 API 端点,如果已经包含完整路径(包括 `/chat/completions` 或其他自定义路径),可以在 URL 末尾添加 `#` 来强制使用完整 URL:
|
||||
|
||||
```json
|
||||
{
|
||||
"ai_model": "custom",
|
||||
"custom_api_url": "https://api.example.com/v2/ai/chat/completions#",
|
||||
"custom_api_key": "your-api-key",
|
||||
"custom_model_name": "custom-model"
|
||||
}
|
||||
```
|
||||
|
||||
**注意**:`#` 会被自动去除,实际请求会发送到 `https://api.example.com/v2/ai/chat/completions`
|
||||
|
||||
## 兼容性要求
|
||||
|
||||
自定义 API 必须:
|
||||
1. 支持 OpenAI Chat Completions 格式
|
||||
2. 接受 `POST` 请求到 `/chat/completions` 端点(或在 URL 末尾添加 `#` 以使用自定义路径)
|
||||
3. 支持 `Authorization: Bearer {api_key}` 认证
|
||||
4. 返回标准的 OpenAI 响应格式
|
||||
|
||||
## 注意事项
|
||||
|
||||
1. **URL 格式**:`custom_api_url` 应该是 Base URL,系统会自动添加 `/chat/completions`
|
||||
- ✅ 正确:`https://api.openai.com/v1`
|
||||
- ❌ 错误:`https://api.openai.com/v1/chat/completions`
|
||||
- 🔧 **特殊用法**:如果需要使用完整的自定义路径(不自动添加 `/chat/completions`),可以在 URL 末尾添加 `#`
|
||||
- 例如:`https://api.example.com/custom/path/chat/completions#`
|
||||
- 系统会自动去掉 `#` 并直接使用该完整 URL
|
||||
|
||||
2. **模型名称**:确保 `custom_model_name` 与 API 提供商支持的模型名称完全一致
|
||||
|
||||
3. **API 密钥**:某些本地部署的模型可能不需要真实的 API 密钥,可以填写任意字符串
|
||||
|
||||
4. **超时设置**:默认超时时间为 120 秒,如果模型响应较慢可能需要调整
|
||||
|
||||
## 多 AI 对比交易
|
||||
|
||||
你可以同时配置多个不同 AI 的 trader 进行对比:
|
||||
|
||||
```json
|
||||
{
|
||||
"traders": [
|
||||
{
|
||||
"id": "deepseek_trader",
|
||||
"ai_model": "deepseek",
|
||||
"deepseek_key": "sk-xxxxx",
|
||||
...
|
||||
},
|
||||
{
|
||||
"id": "gpt4_trader",
|
||||
"ai_model": "custom",
|
||||
"custom_api_url": "https://api.openai.com/v1",
|
||||
"custom_api_key": "sk-xxxxx",
|
||||
"custom_model_name": "gpt-4o",
|
||||
...
|
||||
},
|
||||
{
|
||||
"id": "claude_trader",
|
||||
"ai_model": "custom",
|
||||
"custom_api_url": "https://openrouter.ai/api/v1",
|
||||
"custom_api_key": "sk-or-xxxxx",
|
||||
"custom_model_name": "anthropic/claude-3.5-sonnet",
|
||||
...
|
||||
}
|
||||
]
|
||||
}
|
||||
```
|
||||
|
||||
## 故障排除
|
||||
|
||||
### 问题:配置验证失败
|
||||
|
||||
**错误信息**:`使用自定义API时必须配置custom_api_url`
|
||||
|
||||
**解决方案**:确保设置了 `ai_model: "custom"` 后,同时配置了:
|
||||
- `custom_api_url`
|
||||
- `custom_api_key`
|
||||
- `custom_model_name`
|
||||
|
||||
### 问题:API 调用失败
|
||||
|
||||
**可能原因**:
|
||||
1. URL 格式错误
|
||||
- 普通用法:不应包含 `/chat/completions`(系统会自动添加)
|
||||
- 特殊用法:如果需要完整路径,记得在 URL 末尾添加 `#`
|
||||
2. API 密钥无效
|
||||
3. 模型名称错误
|
||||
4. 网络连接问题
|
||||
|
||||
**调试方法**:查看日志中的错误信息,通常会包含 HTTP 状态码和错误详情
|
||||
|
||||
## 向后兼容性
|
||||
|
||||
现有的 `deepseek` 和 `qwen` 配置完全不受影响,可以继续使用:
|
||||
|
||||
```json
|
||||
{
|
||||
"ai_model": "deepseek",
|
||||
"deepseek_key": "sk-xxxxx"
|
||||
}
|
||||
```
|
||||
|
||||
或
|
||||
|
||||
```json
|
||||
{
|
||||
"ai_model": "qwen",
|
||||
"qwen_key": "sk-xxxxx"
|
||||
}
|
||||
```
|
||||
473
docs/getting-started/docker-deploy.en.md
Normal file
473
docs/getting-started/docker-deploy.en.md
Normal file
@@ -0,0 +1,473 @@
|
||||
# 🐳 Docker One-Click Deployment Guide
|
||||
|
||||
This guide will help you quickly deploy the NOFX AI Trading Competition System using Docker.
|
||||
|
||||
## 📋 Prerequisites
|
||||
|
||||
Before you begin, ensure your system has:
|
||||
|
||||
- **Docker**: Version 20.10 or higher
|
||||
- **Docker Compose**: Version 2.0 or higher
|
||||
|
||||
### Installing Docker
|
||||
|
||||
#### macOS / Windows
|
||||
Download and install [Docker Desktop](https://www.docker.com/products/docker-desktop/)
|
||||
|
||||
#### Linux (Ubuntu/Debian)
|
||||
|
||||
> #### Docker Compose Version Notes
|
||||
>
|
||||
> **New User Recommendation:**
|
||||
> - **Use Docker Desktop**: Automatically includes latest Docker Compose, no separate installation needed
|
||||
> - Simple installation, one-click setup, provides GUI management
|
||||
> - Supports macOS, Windows, and some Linux distributions
|
||||
>
|
||||
> **Upgrading User Note:**
|
||||
> - **Deprecating standalone docker-compose**: No longer recommended to download the independent Docker Compose binary
|
||||
> - **Use built-in version**: Docker 20.10+ includes `docker compose` command (with space)
|
||||
> - If still using old `docker-compose`, please upgrade to new syntax
|
||||
|
||||
*Recommended: Use Docker Desktop (if available) or Docker CE with built-in Compose*
|
||||
|
||||
```bash
|
||||
# Install Docker (includes compose)
|
||||
curl -fsSL https://get.docker.com -o get-docker.sh
|
||||
sudo sh get-docker.sh
|
||||
|
||||
# Add user to docker group
|
||||
sudo usermod -aG docker $USER
|
||||
newgrp docker
|
||||
|
||||
# Verify installation (new command)
|
||||
docker --version
|
||||
docker compose --version # Docker 24+ includes this, no separate installation needed
|
||||
```
|
||||
|
||||
## 🚀 Quick Start (3 Steps)
|
||||
|
||||
### Step 1: Prepare Configuration File
|
||||
|
||||
```bash
|
||||
# Copy configuration template
|
||||
cp config.example.jsonc config.json
|
||||
|
||||
# Edit configuration file with your API keys
|
||||
nano config.json # or use any other editor
|
||||
|
||||
⚠️ **Note**: Basic config.json is still needed for some settings, but ~~trader configurations~~ are now done through the web interface.
|
||||
```
|
||||
|
||||
**Required fields:**
|
||||
```json
|
||||
{
|
||||
"traders": [
|
||||
{
|
||||
"id": "my_trader",
|
||||
"name": "My AI Trader",
|
||||
"ai_model": "deepseek",
|
||||
"binance_api_key": "YOUR_BINANCE_API_KEY", // ← Your Binance API Key
|
||||
"binance_secret_key": "YOUR_BINANCE_SECRET_KEY", // ← Your Binance Secret Key
|
||||
"deepseek_key": "YOUR_DEEPSEEK_API_KEY", // ← Your DeepSeek API Key
|
||||
"initial_balance": 1000.0,
|
||||
"scan_interval_minutes": 3
|
||||
}
|
||||
],
|
||||
"use_default_coins": true,
|
||||
"api_server_port": 8080
|
||||
}
|
||||
```
|
||||
|
||||
### Step 2: One-Click Start
|
||||
|
||||
```bash
|
||||
# Build and start all services (first run)
|
||||
docker compose up -d --build
|
||||
|
||||
# Subsequent starts (without rebuilding)
|
||||
docker compose up -d
|
||||
```
|
||||
|
||||
**Startup options:**
|
||||
- `--build`: Build Docker images (use on first run or after code updates)
|
||||
- `-d`: Run in detached mode (background)
|
||||
|
||||
### Step 3: Access the System
|
||||
|
||||
Once deployed, open your browser and visit:
|
||||
|
||||
- **Web Interface**: http://localhost:3000
|
||||
- **API Health Check**: http://localhost:8080/api/health
|
||||
|
||||
## 📊 Service Management
|
||||
|
||||
### View Running Status
|
||||
```bash
|
||||
# View all container status
|
||||
docker compose ps
|
||||
|
||||
# View service health status
|
||||
docker compose ps --format json | jq
|
||||
```
|
||||
|
||||
### View Logs
|
||||
```bash
|
||||
# View all service logs
|
||||
docker compose logs -f
|
||||
|
||||
# View backend logs only
|
||||
docker compose logs -f backend
|
||||
|
||||
# View frontend logs only
|
||||
docker compose logs -f frontend
|
||||
|
||||
# View last 100 lines
|
||||
docker compose logs --tail=100
|
||||
```
|
||||
|
||||
### Stop Services
|
||||
```bash
|
||||
# Stop all services (keep data)
|
||||
docker compose stop
|
||||
|
||||
# Stop and remove containers (keep data)
|
||||
docker compose down
|
||||
|
||||
# Stop and remove containers and volumes (clear all data)
|
||||
docker compose down -v
|
||||
```
|
||||
|
||||
### Restart Services
|
||||
```bash
|
||||
# Restart all services
|
||||
docker compose restart
|
||||
|
||||
# Restart backend only
|
||||
docker compose restart backend
|
||||
|
||||
# Restart frontend only
|
||||
docker compose restart frontend
|
||||
```
|
||||
|
||||
### Update Services
|
||||
```bash
|
||||
# Pull latest code
|
||||
git pull
|
||||
|
||||
# Rebuild and restart
|
||||
docker compose up -d --build
|
||||
```
|
||||
|
||||
## 🔧 Advanced Configuration
|
||||
|
||||
### Change Ports
|
||||
|
||||
Edit `docker-compose.yml` to modify port mappings:
|
||||
|
||||
```yaml
|
||||
services:
|
||||
backend:
|
||||
ports:
|
||||
- "8080:8080" # Change to "your_port:8080"
|
||||
|
||||
frontend:
|
||||
ports:
|
||||
- "3000:80" # Change to "your_port:80"
|
||||
```
|
||||
|
||||
### Resource Limits
|
||||
|
||||
Add resource limits in `docker-compose.yml`:
|
||||
|
||||
```yaml
|
||||
services:
|
||||
backend:
|
||||
deploy:
|
||||
resources:
|
||||
limits:
|
||||
cpus: '2'
|
||||
memory: 2G
|
||||
reservations:
|
||||
cpus: '1'
|
||||
memory: 1G
|
||||
```
|
||||
|
||||
### Environment Variables
|
||||
|
||||
Create `.env` file to manage environment variables:
|
||||
|
||||
```bash
|
||||
# .env
|
||||
TZ=Asia/Shanghai
|
||||
BACKEND_PORT=8080
|
||||
FRONTEND_PORT=3000
|
||||
```
|
||||
|
||||
Then use in `docker-compose.yml`:
|
||||
|
||||
```yaml
|
||||
services:
|
||||
backend:
|
||||
ports:
|
||||
- "${BACKEND_PORT}:8080"
|
||||
```
|
||||
|
||||
## 📁 Data Persistence
|
||||
|
||||
The system automatically persists data to local directories:
|
||||
|
||||
- `./decision_logs/`: AI decision logs
|
||||
- `./coin_pool_cache/`: Coin pool cache
|
||||
- ~~`./config.json`: Configuration file (mounted)~~ (Deprecated)
|
||||
|
||||
**Data locations:**
|
||||
```bash
|
||||
# View data directories
|
||||
ls -la decision_logs/
|
||||
ls -la coin_pool_cache/
|
||||
|
||||
# Backup data
|
||||
tar -czf backup_$(date +%Y%m%d).tar.gz decision_logs/ coin_pool_cache/ ~~config.json~~ trading.db
|
||||
|
||||
# Restore data
|
||||
tar -xzf backup_20241029.tar.gz
|
||||
```
|
||||
|
||||
## 🐛 Troubleshooting
|
||||
|
||||
### Container Won't Start
|
||||
|
||||
```bash
|
||||
# View detailed error messages
|
||||
docker compose logs backend
|
||||
docker compose logs frontend
|
||||
|
||||
# Check container status
|
||||
docker compose ps -a
|
||||
|
||||
# Rebuild (clear cache)
|
||||
docker compose build --no-cache
|
||||
```
|
||||
|
||||
### Port Already in Use
|
||||
|
||||
```bash
|
||||
# Find process using the port
|
||||
lsof -i :8080 # backend port
|
||||
lsof -i :3000 # frontend port
|
||||
|
||||
# Kill the process
|
||||
kill -9 <PID>
|
||||
```
|
||||
|
||||
### Configuration File Not Found
|
||||
|
||||
```bash
|
||||
# ~~Ensure config.json exists~~
|
||||
# ~~ls -la config.json~~
|
||||
|
||||
# ~~If not exists, copy template~~
|
||||
# ~~cp config.example.jsonc config.json~~
|
||||
|
||||
*Note: Now using SQLite database for configuration storage, no longer need config.json*
|
||||
```
|
||||
|
||||
### Health Check Failing
|
||||
|
||||
```bash
|
||||
# Check health status
|
||||
docker inspect nofx-backend | jq '.[0].State.Health'
|
||||
docker inspect nofx-frontend | jq '.[0].State.Health'
|
||||
|
||||
# Manually test health endpoints
|
||||
curl http://localhost:8080/api/health
|
||||
curl http://localhost:3000/health
|
||||
```
|
||||
|
||||
### Frontend Can't Connect to Backend
|
||||
|
||||
```bash
|
||||
# Check network connectivity
|
||||
docker compose exec frontend ping backend
|
||||
|
||||
# Check if backend service is running
|
||||
docker compose exec frontend wget -O- http://backend:8080/health
|
||||
```
|
||||
|
||||
### Clean Docker Resources
|
||||
|
||||
```bash
|
||||
# Clean unused images
|
||||
docker image prune -a
|
||||
|
||||
# Clean unused volumes
|
||||
docker volume prune
|
||||
|
||||
# Clean all unused resources (use with caution)
|
||||
docker system prune -a --volumes
|
||||
```
|
||||
|
||||
## 🔐 Security Recommendations
|
||||
|
||||
1. ~~**Don't commit config.json to Git**~~
|
||||
```bash
|
||||
# ~~Ensure config.json is in .gitignore~~
|
||||
# ~~echo "config.json" >> .gitignore~~
|
||||
```
|
||||
|
||||
*Note: Now using trading.db database, ensure not to commit sensitive data*
|
||||
|
||||
2. **Use environment variables for sensitive data**
|
||||
```yaml
|
||||
# docker-compose.yml
|
||||
services:
|
||||
backend:
|
||||
environment:
|
||||
- BINANCE_API_KEY=${BINANCE_API_KEY}
|
||||
- BINANCE_SECRET_KEY=${BINANCE_SECRET_KEY}
|
||||
```
|
||||
|
||||
3. **Restrict API access**
|
||||
```yaml
|
||||
# Only allow local access
|
||||
services:
|
||||
backend:
|
||||
ports:
|
||||
- "127.0.0.1:8080:8080"
|
||||
```
|
||||
|
||||
4. **Regularly update images**
|
||||
```bash
|
||||
docker compose pull
|
||||
docker compose up -d
|
||||
```
|
||||
|
||||
## 🌐 Production Deployment
|
||||
|
||||
### Using Nginx Reverse Proxy
|
||||
|
||||
```nginx
|
||||
# /etc/nginx/sites-available/nofx
|
||||
server {
|
||||
listen 80;
|
||||
server_name your-domain.com;
|
||||
|
||||
location / {
|
||||
proxy_pass http://localhost:3000;
|
||||
proxy_set_header Host $host;
|
||||
proxy_set_header X-Real-IP $remote_addr;
|
||||
}
|
||||
|
||||
location /api/ {
|
||||
proxy_pass http://localhost:8080/api/;
|
||||
proxy_set_header Host $host;
|
||||
proxy_set_header X-Real-IP $remote_addr;
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
### Configure HTTPS (Let's Encrypt)
|
||||
|
||||
```bash
|
||||
# Install Certbot
|
||||
sudo apt-get install certbot python3-certbot-nginx
|
||||
|
||||
# Get SSL certificate
|
||||
sudo certbot --nginx -d your-domain.com
|
||||
|
||||
# Auto-renewal
|
||||
sudo certbot renew --dry-run
|
||||
```
|
||||
|
||||
### Using Docker Swarm (Cluster Deployment)
|
||||
|
||||
```bash
|
||||
# Initialize Swarm
|
||||
docker swarm init
|
||||
|
||||
# Deploy stack
|
||||
docker stack deploy -c docker-compose.yml nofx
|
||||
|
||||
# View service status
|
||||
docker stack services nofx
|
||||
|
||||
# Scale services
|
||||
docker service scale nofx_backend=3
|
||||
```
|
||||
|
||||
## 📈 Monitoring & Logging
|
||||
|
||||
### Log Management
|
||||
|
||||
```bash
|
||||
# Configure log rotation (already configured in docker-compose.yml)
|
||||
logging:
|
||||
driver: "json-file"
|
||||
options:
|
||||
max-size: "10m"
|
||||
max-file: "3"
|
||||
|
||||
# View log statistics
|
||||
docker compose logs --timestamps | wc -l
|
||||
```
|
||||
|
||||
### Monitoring Tool Integration
|
||||
|
||||
Integrate Prometheus + Grafana for monitoring:
|
||||
|
||||
```yaml
|
||||
# docker-compose.yml (add monitoring services)
|
||||
services:
|
||||
prometheus:
|
||||
image: prom/prometheus
|
||||
ports:
|
||||
- "9090:9090"
|
||||
volumes:
|
||||
- ./prometheus.yml:/etc/prometheus/prometheus.yml
|
||||
|
||||
grafana:
|
||||
image: grafana/grafana
|
||||
ports:
|
||||
- "3001:3000"
|
||||
```
|
||||
|
||||
## 🆘 Get Help
|
||||
|
||||
- **GitHub Issues**: [Submit an issue](https://github.com/yourusername/open-nofx/issues)
|
||||
- **Documentation**: Check [README.md](README.md)
|
||||
- **Community**: Join our Discord/Telegram group
|
||||
|
||||
## 📝 Command Cheat Sheet
|
||||
|
||||
```bash
|
||||
# Start
|
||||
docker compose up -d --build # Build and start
|
||||
docker compose up -d # Start (without rebuilding)
|
||||
|
||||
# Stop
|
||||
docker compose stop # Stop services
|
||||
docker compose down # Stop and remove containers
|
||||
docker compose down -v # Stop and remove containers and data
|
||||
|
||||
# View
|
||||
docker compose ps # View status
|
||||
docker compose logs -f # View logs
|
||||
docker compose top # View processes
|
||||
|
||||
# Restart
|
||||
docker compose restart # Restart all services
|
||||
docker compose restart backend # Restart backend
|
||||
|
||||
# Update
|
||||
git pull && docker compose up -d --build
|
||||
|
||||
# Clean
|
||||
docker compose down -v # Clear all data
|
||||
docker system prune -a # Clean Docker resources
|
||||
```
|
||||
|
||||
---
|
||||
|
||||
🎉 Congratulations! You've successfully deployed the NOFX AI Trading Competition System!
|
||||
|
||||
If you encounter any issues, please check the [Troubleshooting](#-troubleshooting) section or submit an issue.
|
||||
489
docs/getting-started/docker-deploy.zh-CN.md
Normal file
489
docs/getting-started/docker-deploy.zh-CN.md
Normal file
@@ -0,0 +1,489 @@
|
||||
# 🐳 Docker 一键部署教程
|
||||
|
||||
本教程将指导你使用 Docker 快速部署 NOFX AI 交易竞赛系统。
|
||||
|
||||
## 📋 前置要求
|
||||
|
||||
在开始之前,请确保你的系统已安装:
|
||||
|
||||
- **Docker**: 版本 20.10 或更高
|
||||
- **Docker Compose**: 版本 2.0 或更高
|
||||
|
||||
### 安装 Docker
|
||||
|
||||
> #### 提示:Docker Compose 版本说明
|
||||
>
|
||||
> **新用户建议**:
|
||||
> - **推荐使用 Docker Desktop**:自动包含最新 Docker Compose,无需单独安装
|
||||
> - 安装简单,一键搞定,提供图形界面管理
|
||||
> - 支持 macOS、Windows、部分 Linux 发行版
|
||||
>
|
||||
> **旧用户提醒**:
|
||||
> - **弃用独立 docker-compose**:不再推荐下载独立的 Docker Compose 二进制文件
|
||||
> - **使用内置版**:Docker 20.10+ 自带 `docker compose` 命令(注意是空格)
|
||||
> - 如果还在使用旧的 `docker-compose`,请升级到新语法
|
||||
|
||||
#### macOS / Windows
|
||||
下载并安装 [Docker Desktop](https://www.docker.com/products/docker-desktop/)
|
||||
|
||||
**安装后验证:**
|
||||
```bash
|
||||
docker --version
|
||||
docker compose --version # 注意:使用空格,不再是连字符
|
||||
```
|
||||
|
||||
#### Linux (Ubuntu/Debian)
|
||||
**推荐方式:使用 Docker Desktop(如果可用)或 Docker CE**
|
||||
|
||||
```bash
|
||||
# 安装 Docker (自动包含 compose)
|
||||
curl -fsSL https://get.docker.com -o get-docker.sh
|
||||
sudo sh get-docker.sh
|
||||
|
||||
# 将当前用户加入 docker 组
|
||||
sudo usermod -aG docker $USER
|
||||
newgrp docker
|
||||
|
||||
# 验证安装(新命令)
|
||||
docker --version
|
||||
docker compose --version # Docker 24+ 自带,无需单独安装
|
||||
```
|
||||
|
||||
## 🚀 快速开始(3步完成部署)
|
||||
|
||||
### 第 1 步:准备配置文件
|
||||
|
||||
```bash
|
||||
# 复制配置文件模板
|
||||
cp config.example.jsonc config.json
|
||||
|
||||
# 编辑配置文件,填入你的 API 密钥
|
||||
nano config.json # 或使用其他编辑器
|
||||
```
|
||||
|
||||
**必须配置的字段:**
|
||||
```json
|
||||
{
|
||||
"use_default_coins": true,
|
||||
"api_server_port": 8081,
|
||||
"jwt_secret": "YOUR_JWT_SECRET_CHANGE_IN_PRODUCTION" // ← 填入一个长随机字符串作为JWT密钥
|
||||
}
|
||||
```
|
||||
|
||||
> **⚠️ 重要安全提醒**:
|
||||
> - `jwt_secret` 字段是用户认证系统的关键安全配置
|
||||
> - **必须设置一个长度至少32位的随机字符串**
|
||||
> - 在生产环境中,建议使用64位以上的随机字符串
|
||||
> - 可以使用命令生成:`openssl rand -base64 64`
|
||||
|
||||
**配置说明:**
|
||||
- 🔐 **用户认证**:系统现在支持用户注册登录,每个用户都有独立的AI模型和交易所配置
|
||||
- 🚫 **移除traders配置**:不再需要在config.json中预配置交易员,用户可以通过Web界面创建
|
||||
- 🔑 **JWT密钥**:用于保护用户会话安全,强烈建议在生产环境中设置复杂密钥
|
||||
|
||||
### 第 2 步:一键启动
|
||||
|
||||
```bash
|
||||
# 构建并启动所有服务(首次运行)
|
||||
docker compose up -d --build
|
||||
|
||||
# 后续启动(不重新构建)
|
||||
docker compose up -d
|
||||
```
|
||||
|
||||
**启动过程说明:**
|
||||
- `--build`: 构建 Docker 镜像(首次运行或代码更新后使用)
|
||||
- `-d`: 后台运行(detached mode)
|
||||
|
||||
### 第 3 步:访问系统
|
||||
|
||||
部署成功后,打开浏览器访问:
|
||||
|
||||
- **Web 界面**: http://localhost:3000
|
||||
- **API 文档**: http://localhost:8080/api/health
|
||||
|
||||
## 📊 服务管理
|
||||
|
||||
### 查看运行状态
|
||||
```bash
|
||||
# 查看所有容器状态
|
||||
docker compose ps
|
||||
|
||||
# 查看服务健康状态
|
||||
docker compose ps --format json | jq
|
||||
```
|
||||
|
||||
### 查看日志
|
||||
```bash
|
||||
# 查看所有服务日志
|
||||
docker compose logs -f
|
||||
|
||||
# 只查看后端日志
|
||||
docker compose logs -f backend
|
||||
|
||||
# 只查看前端日志
|
||||
docker compose logs -f frontend
|
||||
|
||||
# 查看最近 100 行日志
|
||||
docker compose logs --tail=100
|
||||
```
|
||||
|
||||
### 停止服务
|
||||
```bash
|
||||
# 停止所有服务(保留数据)
|
||||
docker compose stop
|
||||
|
||||
# 停止并删除容器(保留数据)
|
||||
docker compose down
|
||||
|
||||
# 停止并删除容器和卷(清除所有数据)
|
||||
docker compose down -v
|
||||
```
|
||||
|
||||
### 重启服务
|
||||
```bash
|
||||
# 重启所有服务
|
||||
docker compose restart
|
||||
|
||||
# 只重启后端
|
||||
docker compose restart backend
|
||||
|
||||
# 只重启前端
|
||||
docker compose restart frontend
|
||||
```
|
||||
|
||||
### 更新服务
|
||||
```bash
|
||||
# 拉取最新代码
|
||||
git pull
|
||||
|
||||
# 重新构建并重启
|
||||
docker compose up -d --build
|
||||
```
|
||||
|
||||
## 🔧 高级配置
|
||||
|
||||
### 修改端口
|
||||
|
||||
编辑 `docker-compose.yml`,修改端口映射:
|
||||
|
||||
```yaml
|
||||
services:
|
||||
backend:
|
||||
ports:
|
||||
- "8080:8080" # 改为 "你的端口:8080"
|
||||
|
||||
frontend:
|
||||
ports:
|
||||
- "3000:80" # 改为 "你的端口:80"
|
||||
```
|
||||
|
||||
### 资源限制
|
||||
|
||||
在 `docker-compose.yml` 中添加资源限制:
|
||||
|
||||
```yaml
|
||||
services:
|
||||
backend:
|
||||
deploy:
|
||||
resources:
|
||||
limits:
|
||||
cpus: '2'
|
||||
memory: 2G
|
||||
reservations:
|
||||
cpus: '1'
|
||||
memory: 1G
|
||||
```
|
||||
|
||||
### 环境变量
|
||||
|
||||
创建 `.env` 文件来管理环境变量:
|
||||
|
||||
```bash
|
||||
# .env
|
||||
TZ=Asia/Shanghai
|
||||
BACKEND_PORT=8080
|
||||
FRONTEND_PORT=3000
|
||||
```
|
||||
|
||||
然后在 `docker-compose.yml` 中使用:
|
||||
|
||||
```yaml
|
||||
services:
|
||||
backend:
|
||||
ports:
|
||||
- "${BACKEND_PORT}:8080"
|
||||
```
|
||||
|
||||
## 📁 数据持久化
|
||||
|
||||
系统会自动持久化以下数据到本地目录:
|
||||
|
||||
- `./decision_logs/`: AI 决策日志
|
||||
- `./coin_pool_cache/`: 币种池缓存
|
||||
- `./config.json`: 配置文件(挂载)
|
||||
|
||||
**数据位置:**
|
||||
```bash
|
||||
# 查看数据目录
|
||||
ls -la decision_logs/
|
||||
ls -la coin_pool_cache/
|
||||
|
||||
# 备份数据
|
||||
tar -czf backup_$(date +%Y%m%d).tar.gz decision_logs/ coin_pool_cache/ config.json
|
||||
|
||||
# 恢复数据
|
||||
tar -xzf backup_20241029.tar.gz
|
||||
```
|
||||
|
||||
## 🐛 故障排查
|
||||
|
||||
### 容器无法启动
|
||||
|
||||
```bash
|
||||
# 查看详细错误信息
|
||||
docker compose logs backend
|
||||
docker compose logs frontend
|
||||
|
||||
# 检查容器状态
|
||||
docker compose ps -a
|
||||
|
||||
# 重新构建(清除缓存)
|
||||
docker compose build --no-cache
|
||||
```
|
||||
|
||||
### 端口被占用
|
||||
|
||||
```bash
|
||||
# 查找占用端口的进程
|
||||
lsof -i :8080 # 后端端口
|
||||
lsof -i :3000 # 前端端口
|
||||
|
||||
# 杀死占用端口的进程
|
||||
kill -9 <PID>
|
||||
```
|
||||
|
||||
### 配置文件未找到
|
||||
|
||||
```bash
|
||||
# 确保 config.json 存在
|
||||
ls -la config.json
|
||||
|
||||
# 如果不存在,复制模板
|
||||
cp config.example.jsonc config.json
|
||||
```
|
||||
|
||||
### 健康检查失败
|
||||
|
||||
```bash
|
||||
# 检查健康状态
|
||||
docker inspect nofx-backend | jq '.[0].State.Health'
|
||||
docker inspect nofx-frontend | jq '.[0].State.Health'
|
||||
|
||||
# 手动测试健康端点
|
||||
curl http://localhost:8080/api/health
|
||||
curl http://localhost:3000/health
|
||||
```
|
||||
|
||||
### 前端无法连接后端
|
||||
|
||||
```bash
|
||||
# 检查网络连接
|
||||
docker compose exec frontend ping backend
|
||||
|
||||
# 检查后端服务是否正常
|
||||
docker compose exec frontend wget -O- http://backend:8080/health
|
||||
```
|
||||
|
||||
### 清理 Docker 资源
|
||||
|
||||
```bash
|
||||
# 清理未使用的镜像
|
||||
docker image prune -a
|
||||
|
||||
# 清理未使用的卷
|
||||
docker volume prune
|
||||
|
||||
# 清理所有未使用的资源(慎用)
|
||||
docker system prune -a --volumes
|
||||
```
|
||||
|
||||
## 🔐 安全建议
|
||||
|
||||
1. **JWT密钥安全配置**
|
||||
```bash
|
||||
# 生成强随机JWT密钥
|
||||
openssl rand -base64 64
|
||||
|
||||
# 或者使用其他工具生成
|
||||
head -c 64 /dev/urandom | base64
|
||||
```
|
||||
|
||||
**JWT密钥要求:**
|
||||
- 长度至少32位,推荐64位以上
|
||||
- 使用随机生成的字符串
|
||||
- 在生产环境中绝不使用默认值
|
||||
- 定期更换(会使现有用户需要重新登录)
|
||||
|
||||
2. ~~**不要将 config.json 提交到 Git**~~
|
||||
```bash
|
||||
# ~~确保 config.json 在 .gitignore 中~~
|
||||
# ~~echo "config.json" >> .gitignore~~
|
||||
```
|
||||
|
||||
*注意:现在使用trading.db数据库,请确保不提交敏感数据*
|
||||
|
||||
3. **使用环境变量存储敏感信息**
|
||||
```yaml
|
||||
# docker-compose.yml
|
||||
services:
|
||||
backend:
|
||||
environment:
|
||||
- JWT_SECRET=${JWT_SECRET}
|
||||
# 用户的API密钥现在通过Web界面配置,不再需要环境变量
|
||||
```
|
||||
|
||||
4. **限制 API 访问**
|
||||
```yaml
|
||||
# 只允许本地访问
|
||||
services:
|
||||
backend:
|
||||
ports:
|
||||
- "127.0.0.1:8080:8080"
|
||||
```
|
||||
|
||||
4. **定期更新镜像**
|
||||
```bash
|
||||
docker compose pull
|
||||
docker compose up -d
|
||||
```
|
||||
|
||||
## 🌐 生产环境部署
|
||||
|
||||
### 使用 Nginx 反向代理
|
||||
|
||||
```nginx
|
||||
# /etc/nginx/sites-available/nofx
|
||||
server {
|
||||
listen 80;
|
||||
server_name your-domain.com;
|
||||
|
||||
location / {
|
||||
proxy_pass http://localhost:3000;
|
||||
proxy_set_header Host $host;
|
||||
proxy_set_header X-Real-IP $remote_addr;
|
||||
}
|
||||
|
||||
location /api/ {
|
||||
proxy_pass http://localhost:8080/api/;
|
||||
proxy_set_header Host $host;
|
||||
proxy_set_header X-Real-IP $remote_addr;
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
### 配置 HTTPS (Let's Encrypt)
|
||||
|
||||
```bash
|
||||
# 安装 Certbot
|
||||
sudo apt-get install certbot python3-certbot-nginx
|
||||
|
||||
# 获取 SSL 证书
|
||||
sudo certbot --nginx -d your-domain.com
|
||||
|
||||
# 自动续期
|
||||
sudo certbot renew --dry-run
|
||||
```
|
||||
|
||||
### 使用 Docker Swarm (集群部署)
|
||||
|
||||
```bash
|
||||
# 初始化 Swarm
|
||||
docker swarm init
|
||||
|
||||
# 部署堆栈
|
||||
docker stack deploy -c docker-compose.yml nofx
|
||||
|
||||
# 查看服务状态
|
||||
docker stack services nofx
|
||||
|
||||
# 扩展服务
|
||||
docker service scale nofx_backend=3
|
||||
```
|
||||
|
||||
## 📈 监控与日志
|
||||
|
||||
### 日志管理
|
||||
|
||||
```bash
|
||||
# 配置日志轮转(已在 docker-compose.yml 中配置)
|
||||
logging:
|
||||
driver: "json-file"
|
||||
options:
|
||||
max-size: "10m"
|
||||
max-file: "3"
|
||||
|
||||
# 查看日志统计
|
||||
docker compose logs --timestamps | wc -l
|
||||
```
|
||||
|
||||
### 监控工具集成
|
||||
|
||||
可以集成 Prometheus + Grafana 进行监控:
|
||||
|
||||
```yaml
|
||||
# docker-compose.yml (添加监控服务)
|
||||
services:
|
||||
prometheus:
|
||||
image: prom/prometheus
|
||||
ports:
|
||||
- "9090:9090"
|
||||
volumes:
|
||||
- ./prometheus.yml:/etc/prometheus/prometheus.yml
|
||||
|
||||
grafana:
|
||||
image: grafana/grafana
|
||||
ports:
|
||||
- "3001:3000"
|
||||
```
|
||||
|
||||
## 🆘 获取帮助
|
||||
|
||||
- **GitHub Issues**: [提交问题](https://github.com/yourusername/open-nofx/issues)
|
||||
- **文档**: 查看 [README.md](README.md)
|
||||
- **社区**: 加入我们的 Discord/Telegram 群组
|
||||
|
||||
## 📝 常用命令速查表
|
||||
|
||||
```bash
|
||||
# 启动
|
||||
docker compose up -d --build # 构建并启动
|
||||
docker compose up -d # 启动(不重新构建)
|
||||
|
||||
# 停止
|
||||
docker compose stop # 停止服务
|
||||
docker compose down # 停止并删除容器
|
||||
docker compose down -v # 停止并删除容器和数据
|
||||
|
||||
# 查看
|
||||
docker compose ps # 查看状态
|
||||
docker compose logs -f # 查看日志
|
||||
docker compose top # 查看进程
|
||||
|
||||
# 重启
|
||||
docker compose restart # 重启所有服务
|
||||
docker compose restart backend # 重启后端
|
||||
|
||||
# 更新
|
||||
git pull && docker compose up -d --build
|
||||
|
||||
# 清理
|
||||
docker compose down -v # 清除所有数据
|
||||
docker system prune -a # 清理 Docker 资源
|
||||
```
|
||||
|
||||
---
|
||||
|
||||
🎉 恭喜!你已经成功部署了 NOFX AI 交易竞赛系统!
|
||||
|
||||
如有问题,请查看[故障排查](#-故障排查)部分或提交 Issue。
|
||||
303
docs/getting-started/pm2-deploy.en.md
Normal file
303
docs/getting-started/pm2-deploy.en.md
Normal file
@@ -0,0 +1,303 @@
|
||||
# NoFX Trading Bot - PM2 Deployment Guide
|
||||
|
||||
Complete guide for local development and production deployment using PM2.
|
||||
|
||||
## 🚀 Quick Start
|
||||
|
||||
### 1. Install PM2
|
||||
|
||||
```bash
|
||||
npm install -g pm2
|
||||
```
|
||||
|
||||
### 2. One-Command Launch
|
||||
|
||||
```bash
|
||||
./pm2.sh start
|
||||
```
|
||||
|
||||
That's it! Frontend and backend will start automatically.
|
||||
|
||||
---
|
||||
|
||||
## 📋 All Commands
|
||||
|
||||
### Service Management
|
||||
|
||||
```bash
|
||||
# Start services
|
||||
./pm2.sh start
|
||||
|
||||
# Stop services
|
||||
./pm2.sh stop
|
||||
|
||||
# Restart services
|
||||
./pm2.sh restart
|
||||
|
||||
# View status
|
||||
./pm2.sh status
|
||||
|
||||
# Delete services
|
||||
./pm2.sh delete
|
||||
```
|
||||
|
||||
### Log Viewing
|
||||
|
||||
```bash
|
||||
# View all logs (live)
|
||||
./pm2.sh logs
|
||||
|
||||
# Backend logs only
|
||||
./pm2.sh logs backend
|
||||
|
||||
# Frontend logs only
|
||||
./pm2.sh logs frontend
|
||||
```
|
||||
|
||||
### Build & Compile
|
||||
|
||||
```bash
|
||||
# Compile backend
|
||||
./pm2.sh build
|
||||
|
||||
# Recompile backend and restart
|
||||
./pm2.sh rebuild
|
||||
```
|
||||
|
||||
### Monitoring
|
||||
|
||||
```bash
|
||||
# Open PM2 monitoring dashboard (real-time CPU/Memory)
|
||||
./pm2.sh monitor
|
||||
```
|
||||
|
||||
---
|
||||
|
||||
## 📊 Access URLs
|
||||
|
||||
After successful startup:
|
||||
|
||||
- **Frontend Web Interface**: http://localhost:3000
|
||||
- **Backend API**: http://localhost:8080
|
||||
- **Health Check**: http://localhost:8080/api/health
|
||||
|
||||
---
|
||||
|
||||
## 🔧 Configuration Files
|
||||
|
||||
### pm2.config.js
|
||||
|
||||
PM2 configuration file, defines frontend and backend startup parameters:
|
||||
|
||||
```javascript
|
||||
const path = require('path');
|
||||
|
||||
module.exports = {
|
||||
apps: [
|
||||
{
|
||||
name: 'nofx-backend',
|
||||
script: './nofx', // Go binary
|
||||
cwd: __dirname, // Dynamically get current directory
|
||||
autorestart: true,
|
||||
max_memory_restart: '500M'
|
||||
},
|
||||
{
|
||||
name: 'nofx-frontend',
|
||||
script: 'npm',
|
||||
args: 'run dev', // Vite dev server
|
||||
cwd: path.join(__dirname, 'web'), // Dynamically join path
|
||||
autorestart: true,
|
||||
max_memory_restart: '300M'
|
||||
}
|
||||
]
|
||||
};
|
||||
```
|
||||
|
||||
**After modifying configuration, restart is required:**
|
||||
```bash
|
||||
./pm2.sh restart
|
||||
```
|
||||
|
||||
---
|
||||
|
||||
## 📝 Log File Locations
|
||||
|
||||
- **Backend Logs**: `./logs/backend-error.log` and `./logs/backend-out.log`
|
||||
- **Frontend Logs**: `./web/logs/frontend-error.log` and `./web/logs/frontend-out.log`
|
||||
|
||||
---
|
||||
|
||||
## 🔄 Startup on Boot
|
||||
|
||||
Set PM2 to start on boot:
|
||||
|
||||
```bash
|
||||
# 1. Start services
|
||||
./pm2.sh start
|
||||
|
||||
# 2. Save current process list
|
||||
pm2 save
|
||||
|
||||
# 3. Generate startup script
|
||||
pm2 startup
|
||||
|
||||
# 4. Follow the instructions to execute command (requires sudo)
|
||||
```
|
||||
|
||||
**Disable startup on boot:**
|
||||
```bash
|
||||
pm2 unstartup
|
||||
```
|
||||
|
||||
---
|
||||
|
||||
## 🛠️ Common Operations
|
||||
|
||||
### Restart After Code Changes
|
||||
|
||||
**Backend changes:**
|
||||
```bash
|
||||
./pm2.sh rebuild # Auto compile and restart
|
||||
```
|
||||
|
||||
**Frontend changes:**
|
||||
```bash
|
||||
./pm2.sh restart # Vite will auto hot-reload, no restart needed
|
||||
```
|
||||
|
||||
### View Real-time Resource Usage
|
||||
|
||||
```bash
|
||||
./pm2.sh monitor
|
||||
```
|
||||
|
||||
### View Detailed Information
|
||||
|
||||
```bash
|
||||
pm2 info nofx-backend # Backend details
|
||||
pm2 info nofx-frontend # Frontend details
|
||||
```
|
||||
|
||||
### Clear Logs
|
||||
|
||||
```bash
|
||||
pm2 flush
|
||||
```
|
||||
|
||||
---
|
||||
|
||||
## 🐛 Troubleshooting
|
||||
|
||||
### Service Startup Failed
|
||||
|
||||
```bash
|
||||
# 1. View detailed errors
|
||||
./pm2.sh logs
|
||||
|
||||
# 2. Check port usage
|
||||
lsof -i :8080 # Backend port
|
||||
lsof -i :3000 # Frontend port
|
||||
|
||||
# 3. Manual compile test
|
||||
go build -o nofx
|
||||
./nofx
|
||||
```
|
||||
|
||||
### Backend Won't Start
|
||||
|
||||
```bash
|
||||
# ~~Check if config.json exists~~
|
||||
# ~~ls -l config.json~~
|
||||
|
||||
# Check if database file exists
|
||||
ls -l trading.db
|
||||
|
||||
# Check permissions
|
||||
chmod +x nofx
|
||||
|
||||
# Run manually to see errors
|
||||
./nofx
|
||||
```
|
||||
|
||||
### Frontend Not Accessible
|
||||
|
||||
```bash
|
||||
# Check node_modules
|
||||
cd web && npm install
|
||||
|
||||
# Manual start test
|
||||
npm run dev
|
||||
```
|
||||
|
||||
---
|
||||
|
||||
## 🎯 Production Environment Recommendations
|
||||
|
||||
### 1. Use Production Mode
|
||||
|
||||
Modify `pm2.config.js`:
|
||||
|
||||
```javascript
|
||||
{
|
||||
name: 'nofx-frontend',
|
||||
script: 'npm',
|
||||
args: 'run preview', // Change to preview (requires npm run build first)
|
||||
env: {
|
||||
NODE_ENV: 'production'
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
### 2. Increase Instances (Load Balancing)
|
||||
|
||||
```javascript
|
||||
{
|
||||
name: 'nofx-backend',
|
||||
script: './nofx',
|
||||
instances: 2, // Start 2 instances
|
||||
exec_mode: 'cluster'
|
||||
}
|
||||
```
|
||||
|
||||
### 3. Auto Restart Strategy
|
||||
|
||||
```javascript
|
||||
{
|
||||
autorestart: true,
|
||||
max_restarts: 10,
|
||||
min_uptime: '10s',
|
||||
max_memory_restart: '500M'
|
||||
}
|
||||
```
|
||||
|
||||
---
|
||||
|
||||
## 📦 Comparison with Docker Deployment
|
||||
|
||||
| Feature | PM2 Deployment | Docker Deployment |
|
||||
|---------|---------------|-------------------|
|
||||
| Startup Speed | ⚡ Fast | 🐌 Slower |
|
||||
| Resource Usage | 💚 Low | 🟡 Medium |
|
||||
| Isolation | 🟡 Medium | 💚 High |
|
||||
| Use Case | Dev/Single-machine | Production/Cluster |
|
||||
| Configuration Complexity | 💚 Simple | 🟡 Medium |
|
||||
|
||||
**Recommendations:**
|
||||
- **Development Environment**: Use `./pm2.sh`
|
||||
- **Production Environment**: Use `./start.sh` (Docker)
|
||||
|
||||
---
|
||||
|
||||
## 🆘 Getting Help
|
||||
|
||||
```bash
|
||||
./pm2.sh help
|
||||
```
|
||||
|
||||
Or check PM2 official documentation: https://pm2.keymetrics.io/
|
||||
|
||||
---
|
||||
|
||||
## 📄 License
|
||||
|
||||
MIT
|
||||
303
docs/getting-started/pm2-deploy.md
Normal file
303
docs/getting-started/pm2-deploy.md
Normal file
@@ -0,0 +1,303 @@
|
||||
# NoFX Trading Bot - PM2 部署指南
|
||||
|
||||
使用 PM2 进行本地开发和生产部署的完整指南。
|
||||
|
||||
## 🚀 快速开始
|
||||
|
||||
### 1. 安装 PM2
|
||||
|
||||
```bash
|
||||
npm install -g pm2
|
||||
```
|
||||
|
||||
### 2. 一键启动
|
||||
|
||||
```bash
|
||||
./pm2.sh start
|
||||
```
|
||||
|
||||
就这么简单!前后端将自动启动。
|
||||
|
||||
---
|
||||
|
||||
## 📋 所有命令
|
||||
|
||||
### 服务管理
|
||||
|
||||
```bash
|
||||
# 启动服务
|
||||
./pm2.sh start
|
||||
|
||||
# 停止服务
|
||||
./pm2.sh stop
|
||||
|
||||
# 重启服务
|
||||
./pm2.sh restart
|
||||
|
||||
# 查看状态
|
||||
./pm2.sh status
|
||||
|
||||
# 删除服务
|
||||
./pm2.sh delete
|
||||
```
|
||||
|
||||
### 日志查看
|
||||
|
||||
```bash
|
||||
# 查看所有日志(实时)
|
||||
./pm2.sh logs
|
||||
|
||||
# 只看后端日志
|
||||
./pm2.sh logs backend
|
||||
|
||||
# 只看前端日志
|
||||
./pm2.sh logs frontend
|
||||
```
|
||||
|
||||
### 构建与编译
|
||||
|
||||
```bash
|
||||
# 编译后端
|
||||
./pm2.sh build
|
||||
|
||||
# 重新编译后端并重启
|
||||
./pm2.sh rebuild
|
||||
```
|
||||
|
||||
### 监控
|
||||
|
||||
```bash
|
||||
# 打开 PM2 监控面板(实时CPU/内存)
|
||||
./pm2.sh monitor
|
||||
```
|
||||
|
||||
---
|
||||
|
||||
## 📊 访问地址
|
||||
|
||||
启动成功后:
|
||||
|
||||
- **前端 Web 界面**: http://localhost:3000
|
||||
- **后端 API**: http://localhost:8080
|
||||
- **健康检查**: http://localhost:8080/api/health
|
||||
|
||||
---
|
||||
|
||||
## 🔧 配置文件
|
||||
|
||||
### pm2.config.js
|
||||
|
||||
PM2 配置文件,定义了前后端的启动参数:
|
||||
|
||||
```javascript
|
||||
const path = require('path');
|
||||
|
||||
module.exports = {
|
||||
apps: [
|
||||
{
|
||||
name: 'nofx-backend',
|
||||
script: './nofx', // Go 二进制文件
|
||||
cwd: __dirname, // 动态获取当前目录
|
||||
autorestart: true,
|
||||
max_memory_restart: '500M'
|
||||
},
|
||||
{
|
||||
name: 'nofx-frontend',
|
||||
script: 'npm',
|
||||
args: 'run dev', // Vite 开发服务器
|
||||
cwd: path.join(__dirname, 'web'), // 动态拼接路径
|
||||
autorestart: true,
|
||||
max_memory_restart: '300M'
|
||||
}
|
||||
]
|
||||
};
|
||||
```
|
||||
|
||||
**修改配置后需要重启:**
|
||||
```bash
|
||||
./pm2.sh restart
|
||||
```
|
||||
|
||||
---
|
||||
|
||||
## 📝 日志文件位置
|
||||
|
||||
- **后端日志**: `./logs/backend-error.log` 和 `./logs/backend-out.log`
|
||||
- **前端日志**: `./web/logs/frontend-error.log` 和 `./web/logs/frontend-out.log`
|
||||
|
||||
---
|
||||
|
||||
## 🔄 开机自启动
|
||||
|
||||
设置 PM2 开机自启动:
|
||||
|
||||
```bash
|
||||
# 1. 启动服务
|
||||
./pm2.sh start
|
||||
|
||||
# 2. 保存当前进程列表
|
||||
pm2 save
|
||||
|
||||
# 3. 生成启动脚本
|
||||
pm2 startup
|
||||
|
||||
# 4. 按照提示执行命令(需要 sudo)
|
||||
```
|
||||
|
||||
**取消开机自启动:**
|
||||
```bash
|
||||
pm2 unstartup
|
||||
```
|
||||
|
||||
---
|
||||
|
||||
## 🛠️ 常见操作
|
||||
|
||||
### 修改代码后重启
|
||||
|
||||
**后端修改:**
|
||||
```bash
|
||||
./pm2.sh rebuild # 自动编译并重启
|
||||
```
|
||||
|
||||
**前端修改:**
|
||||
```bash
|
||||
./pm2.sh restart # Vite 会自动热重载,无需重启
|
||||
```
|
||||
|
||||
### 查看实时资源占用
|
||||
|
||||
```bash
|
||||
./pm2.sh monitor
|
||||
```
|
||||
|
||||
### 查看详细信息
|
||||
|
||||
```bash
|
||||
pm2 info nofx-backend # 后端详情
|
||||
pm2 info nofx-frontend # 前端详情
|
||||
```
|
||||
|
||||
### 清空日志
|
||||
|
||||
```bash
|
||||
pm2 flush
|
||||
```
|
||||
|
||||
---
|
||||
|
||||
## 🐛 故障排查
|
||||
|
||||
### 服务启动失败
|
||||
|
||||
```bash
|
||||
# 1. 查看详细错误
|
||||
./pm2.sh logs
|
||||
|
||||
# 2. 检查端口占用
|
||||
lsof -i :8080 # 后端端口
|
||||
lsof -i :3000 # 前端端口
|
||||
|
||||
# 3. 手动编译测试
|
||||
go build -o nofx
|
||||
./nofx
|
||||
```
|
||||
|
||||
### 后端无法启动
|
||||
|
||||
```bash
|
||||
# ~~检查 config.json 是否存在~~
|
||||
# ~~ls -l config.json~~
|
||||
|
||||
# 检查数据库文件是否存在
|
||||
ls -l trading.db
|
||||
|
||||
# 检查权限
|
||||
chmod +x nofx
|
||||
|
||||
# 手动运行看报错
|
||||
./nofx
|
||||
```
|
||||
|
||||
### 前端无法访问
|
||||
|
||||
```bash
|
||||
# 检查 node_modules
|
||||
cd web && npm install
|
||||
|
||||
# 手动启动测试
|
||||
npm run dev
|
||||
```
|
||||
|
||||
---
|
||||
|
||||
## 🎯 生产环境建议
|
||||
|
||||
### 1. 使用生产模式
|
||||
|
||||
修改 `pm2.config.js`:
|
||||
|
||||
```javascript
|
||||
{
|
||||
name: 'nofx-frontend',
|
||||
script: 'npm',
|
||||
args: 'run preview', // 改为 preview(需先 npm run build)
|
||||
env: {
|
||||
NODE_ENV: 'production'
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
### 2. 增加实例数(负载均衡)
|
||||
|
||||
```javascript
|
||||
{
|
||||
name: 'nofx-backend',
|
||||
script: './nofx',
|
||||
instances: 2, // 启动 2 个实例
|
||||
exec_mode: 'cluster'
|
||||
}
|
||||
```
|
||||
|
||||
### 3. 自动重启策略
|
||||
|
||||
```javascript
|
||||
{
|
||||
autorestart: true,
|
||||
max_restarts: 10,
|
||||
min_uptime: '10s',
|
||||
max_memory_restart: '500M'
|
||||
}
|
||||
```
|
||||
|
||||
---
|
||||
|
||||
## 📦 与 Docker 部署的对比
|
||||
|
||||
| 特性 | PM2 部署 | Docker 部署 |
|
||||
|------|---------|------------|
|
||||
| 启动速度 | ⚡ 快 | 🐌 较慢 |
|
||||
| 资源占用 | 💚 低 | 🟡 中等 |
|
||||
| 隔离性 | 🟡 中等 | 💚 高 |
|
||||
| 适用场景 | 开发/单机 | 生产/集群 |
|
||||
| 配置复杂度 | 💚 简单 | 🟡 中等 |
|
||||
|
||||
**建议:**
|
||||
- **开发环境**: 使用 `./pm2.sh`
|
||||
- **生产环境**: 使用 `./start.sh` (Docker)
|
||||
|
||||
---
|
||||
|
||||
## 🆘 获取帮助
|
||||
|
||||
```bash
|
||||
./pm2.sh help
|
||||
```
|
||||
|
||||
或查看 PM2 官方文档:https://pm2.keymetrics.io/
|
||||
|
||||
---
|
||||
|
||||
## 📄 License
|
||||
|
||||
MIT
|
||||
Reference in New Issue
Block a user