- 支持三套独立环境:实盘(production)、回测(backtest)、模拟(simulation) - 环境完全隔离:独立数据库、Redis频道前缀、端口范围 - 配置系统改造: * 新增环境配置类(EnvironmentConfig)和频道配置类(RedisChannels) * 数据库名、Redis频道自动根据环境切换 * 支持从环境变量(CONFIG_FILE)加载配置 - Redis频道动态化: * RedisChannels类改为使用动态前缀 * 信号/订单频道使用统一配置 - 数据库初始化: * 创建三个独立数据库(QTrade_RealTrade/BackTestTrade/SimuTrade) * 提供Python初始化脚本 - 清理冗余代码: * 删除废弃的services配置目录 * 删除旧的启动脚本和初始化脚本 * 删除未使用的backtest目录 - 新增工具脚本: * start_services.py - 多环境服务启动 * init_databases.py - 数据库初始化 * test_config.py - 配置测试 环境映射: - production: DB=QTrade_RealTrade, Redis=real:, Port=9000+ - backtest: DB=QTrade_BackTestTrade, Redis=bt:, Port=10000+ - simulation: DB=QTrade_SimuTrade, Redis=simu:, Port=11000+
166 lines
4.5 KiB
Python
166 lines
4.5 KiB
Python
"""
|
|
QTrade 多环境服务启动脚本
|
|
|
|
支持启动三套独立环境:
|
|
- production: 实盘环境 (端口9000+, DB: QTrade_RealTrade, Redis: real:)
|
|
- backtest: 回测环境 (端口10000+, DB: QTrade_BackTestTrade, Redis: bt:)
|
|
- simulation: 模拟环境 (端口11000+, DB: QTrade_SimuTrade, Redis: simu:)
|
|
|
|
用法:
|
|
# 启动实盘环境的数据服务和策略引擎
|
|
python scripts/start_services.py production data_service strategy_engine
|
|
|
|
# 启动回测环境的所有服务
|
|
python scripts/start_services.py backtest data_service strategy_engine backtest_service
|
|
|
|
# 启动模拟环境
|
|
python scripts/start_services.py simulation data_service strategy_engine trade_service
|
|
"""
|
|
|
|
import os
|
|
import subprocess
|
|
import sys
|
|
import time
|
|
from pathlib import Path
|
|
|
|
|
|
# 项目根目录
|
|
PROJECT_ROOT = Path(__file__).parent.parent
|
|
|
|
# 环境配置
|
|
ENV_CONFIGS = {
|
|
"production": {
|
|
"env_file": ".env.production",
|
|
"config_file": "configs/production.yaml",
|
|
"description": "实盘环境",
|
|
},
|
|
"backtest": {
|
|
"env_file": ".env.backtest",
|
|
"config_file": "configs/backtest.yaml",
|
|
"description": "回测环境",
|
|
},
|
|
"simulation": {
|
|
"env_file": ".env.simulation",
|
|
"config_file": "configs/simulation.yaml",
|
|
"description": "模拟环境",
|
|
},
|
|
}
|
|
|
|
# 服务配置
|
|
SERVICE_APPS = {
|
|
"data_service": "src.data_service.app:app",
|
|
"strategy_engine": "src.strategy_engine.app:app",
|
|
"trade_service": "src.trade_service.app:app",
|
|
"backtest_service": "src.backtest_service.app:app",
|
|
}
|
|
|
|
|
|
def start_service(service_name: str, env: str, detach: bool = True):
|
|
"""
|
|
启动指定环境的服务
|
|
|
|
Args:
|
|
service_name: 服务名称
|
|
env: 环境名称 (production/backtest/simulation)
|
|
detach: 是否后台运行
|
|
"""
|
|
if env not in ENV_CONFIGS:
|
|
print(f"❌ 未知环境: {env}")
|
|
print(f"支持的环境: {', '.join(ENV_CONFIGS.keys())}")
|
|
sys.exit(1)
|
|
|
|
if service_name not in SERVICE_APPS:
|
|
print(f"❌ 未知服务: {service_name}")
|
|
print(f"支持的服务: {', '.join(SERVICE_APPS.keys())}")
|
|
sys.exit(1)
|
|
|
|
env_config = ENV_CONFIGS[env]
|
|
app_path = SERVICE_APPS[service_name]
|
|
env_file = PROJECT_ROOT / env_config["env_file"]
|
|
|
|
if not env_file.exists():
|
|
print(f"❌ 环境配置文件不存在: {env_file}")
|
|
sys.exit(1)
|
|
|
|
print(f"🚀 启动 {env_config['description']} - {service_name}")
|
|
|
|
# 构建命令
|
|
cmd = [
|
|
"uvicorn",
|
|
app_path,
|
|
"--env-file",
|
|
str(env_file),
|
|
"--reload",
|
|
]
|
|
|
|
# 设置环境变量
|
|
env_vars = os.environ.copy()
|
|
env_vars["ENV"] = env
|
|
env_vars["CONFIG_FILE"] = env_config["config_file"]
|
|
|
|
if detach:
|
|
# Windows 后台运行
|
|
if sys.platform == "win32":
|
|
# 创建新的控制台窗口
|
|
subprocess.Popen(
|
|
cmd,
|
|
env=env_vars,
|
|
creationflags=subprocess.CREATE_NEW_CONSOLE,
|
|
cwd=PROJECT_ROOT,
|
|
)
|
|
else:
|
|
# Linux/Mac 后台运行
|
|
subprocess.Popen(
|
|
cmd,
|
|
env=env_vars,
|
|
stdout=subprocess.DEVNULL,
|
|
stderr=subprocess.DEVNULL,
|
|
cwd=PROJECT_ROOT,
|
|
)
|
|
print(f"✓ {service_name} 已在后台启动")
|
|
else:
|
|
# 前台运行
|
|
subprocess.run(cmd, env=env_vars, cwd=PROJECT_ROOT)
|
|
|
|
|
|
def main():
|
|
if len(sys.argv) < 3:
|
|
print("用法: python scripts/start_services.py <env> <service1> [service2] ...")
|
|
print()
|
|
print("环境:")
|
|
for env, config in ENV_CONFIGS.items():
|
|
print(f" {env:12} - {config['description']}")
|
|
print()
|
|
print("服务:")
|
|
for service in SERVICE_APPS.keys():
|
|
print(f" {service}")
|
|
print()
|
|
print("示例:")
|
|
print(
|
|
" python scripts/start_services.py production data_service strategy_engine"
|
|
)
|
|
print(
|
|
" python scripts/start_services.py backtest data_service strategy_engine backtest_service"
|
|
)
|
|
sys.exit(1)
|
|
|
|
env = sys.argv[1]
|
|
services = sys.argv[2:]
|
|
|
|
print("=" * 60)
|
|
print(f"环境: {ENV_CONFIGS[env]['description']}")
|
|
print(f"服务: {', '.join(services)}")
|
|
print("=" * 60)
|
|
print()
|
|
|
|
for service in services:
|
|
start_service(service, env)
|
|
time.sleep(1) # 间隔启动
|
|
|
|
print()
|
|
print("✅ 所有服务已启动")
|
|
|
|
|
|
if __name__ == "__main__":
|
|
main()
|