feat: 微服务架构拆分和前后端优化

后端:
- 拆分出 agent_service, runtime_service, trading_service, news_service
- Gateway 模块化拆分 (gateway_*.py)
- 添加 domains/ 领域层
- 新增 control_client, runtime_client
- 更新 start-dev.sh 支持 split 服务模式

前端:
- 完善 API 服务层 (newsApi, tradingApi)
- 更新 vite.config.js
- Explain 组件优化

测试:
- 添加多个服务 app 测试

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
This commit is contained in:
2026-03-23 17:45:39 +08:00
parent 0f1bc2bb39
commit 3448667b79
54 changed files with 5440 additions and 2947 deletions

View File

@@ -1,75 +1,73 @@
# EvoTraders Services Architecture
This document describes the modular service architecture for EvoTraders.
This repo is currently in a **migration state** between a modular monolith and
fully split services. Service boundaries now exist as dedicated FastAPI app
surfaces, and local development now runs those split services directly.
## Architecture
## Current App Surfaces
EvoTraders uses a **modular single-process architecture** with services as Python modules:
| App surface | Default port | Responsibility |
| --- | --- | --- |
| `backend.apps.agent_service` | 8000 | Control-plane only: workspaces, agents, guard. |
| `backend.apps.runtime_service` | 8003 | Runtime lifecycle only: `/api/runtime/*`. |
| `backend.apps.trading_service` | 8001 | Read-only trading data: prices, financials, insider trades, market status, market cap. |
| `backend.apps.news_service` | 8002 | Read-only explain/news data: enriched news, categories, story, similar days, range explain. |
```
backend/
├── app.py # FastAPI entry point (port 8000)
├── main.py # CLI trading system entry point
├── api/ # REST API routes
│ ├── agents.py # Agent management
│ ├── guard.py # Tool guard
│ ├── runtime.py # Runtime management
│ └── workspaces.py # Workspace management
├── agents/ # Multi-agent system
│ ├── base/ # Base agent classes
│ ├── team/ # Team coordination
│ └── skills/ # Agent skills
├── core/ # Pipeline & scheduler
├── services/ # Core services
│ ├── gateway.py # WebSocket gateway
│ ├── market.py # Market data service
│ └── storage.py # Storage service
└── services/ # Modular services (optional)
├── trading/ # Trading module
├── news/ # News module
└── agents/ # Agents module
```
## Local Development Modes
## Entry Points
### 1. Split-service mode
| Entry Point | Port | Purpose |
|------------|------|---------|
| `backend/app.py` | 8000 | FastAPI REST API |
| `backend/main.py` | CLI | Trading system (live/backtest) |
## Running
This is now the default development mode.
```bash
# Development mode (FastAPI only)
./start-dev.sh
# Or manually
python -m uvicorn backend.app:app --port 8000 --reload
# Trading system (CLI)
evotraders live --mock
evotraders backtest --start 2025-11-01 --end 2025-12-01
# explicit
./start-dev.sh split
```
## Service Modules
| Module | Description |
|--------|-------------|
| `gateway.py` | WebSocket gateway for frontend communication |
| `market.py` | Market data fetching (prices, news, financials) |
| `storage.py` | Dashboard state and trade history persistence |
## Module Dependencies
Run dedicated service surfaces explicitly:
```bash
python -m uvicorn backend.apps.agent_service:app --port 8000 --reload
python -m uvicorn backend.apps.runtime_service:app --port 8003 --reload
python -m uvicorn backend.apps.trading_service:app --port 8001 --reload
python -m uvicorn backend.apps.news_service:app --port 8002 --reload
```
app.py (FastAPI)
├── runtime_router
│ └── backend/main.py (when task starts)
│ └── Gateway
│ ├── MarketService
│ ├── StorageService
│ └── TradingPipeline
│ ├── Analysts (4x)
│ ├── RiskManager
│ └── PortfolioManager
## Migration Variables
These env vars control whether the app still uses local-module fallbacks or
prefers service boundaries:
| Variable | Used by | Purpose |
| --- | --- | --- |
| `NEWS_SERVICE_URL` | backend Gateway | Prefer `news-service` for explain/news read paths |
| `TRADING_SERVICE_URL` | backend Gateway | Prefer `trading-service` for trading read paths |
| `RUNTIME_SERVICE_URL` | reserved | Future runtime/control-plane split follow-up |
| `VITE_NEWS_SERVICE_URL` | frontend | Direct browser calls to `news-service` for selected explain paths |
| `VITE_TRADING_SERVICE_URL` | frontend | Reserved for future direct trading reads |
If these are empty, the repo keeps using local module fallbacks where they still exist.
## Current Internal Direction
The repository is now organized around split service surfaces:
```text
frontend
├─ runtime/control/news/trading split endpoints
└─ selective per-request fallbacks where still retained
backend.apps.agent_service
└─ control-plane routes
backend.apps.runtime_service
└─ runtime lifecycle + gateway discovery
backend.apps.trading_service
└─ read-only trading contract
backend.apps.news_service
└─ read-only explain/news contract
```