From 1e4376ba56c6d0818c7e44b2367d5c8379f2f97d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E8=B5=B5=E6=9D=B0?= Date: Mon, 22 Sep 2025 16:36:50 +0100 Subject: [PATCH] =?UTF-8?q?=E6=B7=BB=E5=8A=A0docker=E9=83=A8=E7=BD=B2?= =?UTF-8?q?=E6=96=B9=E5=BC=8F=EF=BC=8C=E4=BC=98=E5=8C=96=E7=9B=AE=E5=89=8D?= =?UTF-8?q?=E9=A1=B9=E7=9B=AE=E7=BB=93=E6=9E=84=EF=BC=8C=E6=9B=B4=E6=96=B0?= =?UTF-8?q?=E4=BA=86readme=E6=96=87=E4=BB=B6?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Dockerfile | 25 ++- README.md | 209 ++++++++++++++++-- docker-compose.yml | 105 ++++++++- init.sql | 127 +++++++++++ .../provisioning/dashboards/dashboard.yml | 12 + .../provisioning/datasources/prometheus.yml | 9 + monitoring/prometheus.yml | 45 ++++ nginx.conf | 102 +++++++++ requirements.txt | 44 +++- scripts/docker_deploy.sh | 204 +++++++++++++++++ 10 files changed, 850 insertions(+), 32 deletions(-) create mode 100644 init.sql create mode 100644 monitoring/grafana/provisioning/dashboards/dashboard.yml create mode 100644 monitoring/grafana/provisioning/datasources/prometheus.yml create mode 100644 monitoring/prometheus.yml create mode 100644 nginx.conf create mode 100644 scripts/docker_deploy.sh diff --git a/Dockerfile b/Dockerfile index 2fed708..8acf38e 100644 --- a/Dockerfile +++ b/Dockerfile @@ -1,5 +1,5 @@ -# TSP智能助手Docker镜像 -FROM python:3.9-slim +# TSP智能助手Docker镜像 - 优化版本 +FROM python:3.11-slim # 设置工作目录 WORKDIR /app @@ -7,6 +7,9 @@ WORKDIR /app # 设置环境变量 ENV PYTHONPATH=/app ENV PYTHONUNBUFFERED=1 +ENV PYTHONDONTWRITEBYTECODE=1 +ENV PIP_NO_CACHE_DIR=1 +ENV PIP_DISABLE_PIP_VERSION_CHECK=1 # 安装系统依赖 RUN apt-get update && apt-get install -y \ @@ -14,8 +17,15 @@ RUN apt-get update && apt-get install -y \ g++ \ git \ curl \ + wget \ + vim \ + htop \ + procps \ && rm -rf /var/lib/apt/lists/* +# 升级pip并安装wheel +RUN pip install --upgrade pip setuptools wheel + # 复制依赖文件 COPY requirements.txt . @@ -26,13 +36,18 @@ RUN pip install --no-cache-dir -r requirements.txt COPY . . # 创建必要目录 -RUN mkdir -p logs data backups +RUN mkdir -p logs data backups uploads config # 设置权限 -RUN chmod +x scripts/deploy.sh +RUN chmod +x scripts/deploy.sh scripts/monitor.sh + +# 创建非root用户 +RUN useradd -m -u 1000 tspuser && \ + chown -R tspuser:tspuser /app +USER tspuser # 暴露端口 -EXPOSE 5000 +EXPOSE 5000 8765 # 健康检查 HEALTHCHECK --interval=30s --timeout=10s --start-period=5s --retries=3 \ diff --git a/README.md b/README.md index 95f7165..3caa925 100644 --- a/README.md +++ b/README.md @@ -1,7 +1,8 @@ # TSP智能助手 (TSP Assistant) -[![Version](https://img.shields.io/badge/version-1.3.0-blue.svg)](version.json) -[![Python](https://img.shields.io/badge/python-3.8+-green.svg)](requirements.txt) +[![Version](https://img.shields.io/badge/version-2.0.0-blue.svg)](version.json) +[![Python](https://img.shields.io/badge/python-3.11+-green.svg)](requirements.txt) +[![Docker](https://img.shields.io/badge/docker-supported-blue.svg)](Dockerfile) [![License](https://img.shields.io/badge/license-MIT-yellow.svg)](LICENSE) [![Status](https://img.shields.io/badge/status-production-ready-brightgreen.svg)]() @@ -42,11 +43,20 @@ │ 前端界面 │ │ 后端服务 │ │ 数据存储 │ │ │ │ │ │ │ │ • 仪表板 │◄──►│ • Flask API │◄──►│ • MySQL DB │ -│ • 智能对话 │ │ • WebSocket │ │ • 知识库 │ -│ • Agent管理 │ │ • Agent核心 │ │ • 工单系统 │ -│ • 数据分析 │ │ • LLM集成 │ │ • 车辆数据 │ -│ • 备份管理 │ │ • 备份系统 │ │ • SQLite备份 │ +│ • 智能对话 │ │ • WebSocket │ │ • Redis缓存 │ +│ • Agent管理 │ │ • Agent核心 │ │ • 知识库 │ +│ • 数据分析 │ │ • LLM集成 │ │ • 工单系统 │ +│ • 备份管理 │ │ • 备份系统 │ │ • 车辆数据 │ └─────────────────┘ └─────────────────┘ └─────────────────┘ + │ + ▼ + ┌─────────────────┐ + │ 监控系统 │ + │ │ + │ • Prometheus │ + │ • Grafana │ + │ • Nginx代理 │ + └─────────────────┘ ``` ## 🎯 核心功能 @@ -97,11 +107,12 @@ ## 🛠️ 技术栈 ### 后端技术 -- **Python 3.8+**: 核心开发语言 -- **Flask**: Web框架和API服务 -- **SQLAlchemy**: ORM数据库操作 +- **Python 3.11+**: 核心开发语言 +- **Flask 2.3+**: Web框架和API服务 +- **SQLAlchemy 2.0+**: ORM数据库操作 - **WebSocket**: 实时通信支持 - **psutil**: 系统资源监控 +- **Redis**: 缓存和会话管理 ### 前端技术 - **Bootstrap 5**: UI框架 @@ -114,21 +125,74 @@ - **TF-IDF**: 文本向量化 - **余弦相似度**: 语义相似度计算 - **Agent框架**: 智能任务规划 +- **Transformers**: 预训练模型支持 ### 部署运维 - **Docker**: 容器化部署 +- **Docker Compose**: 多服务编排 - **Nginx**: 反向代理和静态文件服务 -- **Systemd**: 服务管理 -- **Git**: 版本控制 +- **Prometheus**: 监控数据收集 +- **Grafana**: 监控仪表板 +- **MySQL 8.0**: 主数据库 +- **Redis 7**: 缓存服务 ## 🚀 快速开始 ### 环境要求 -- Python 3.8+ + +#### Docker部署(推荐) +- Docker 20.10+ +- Docker Compose 2.0+ +- 4GB+ 可用内存 +- 10GB+ 可用磁盘空间 + +#### 本地部署 +- Python 3.11+ - Node.js 16+ (可选,用于前端构建) +- MySQL 8.0+ 或 SQLite +- Redis 7+ (可选) - Git -### 安装步骤 +### 🐳 Docker部署(推荐) + +1. **克隆项目** +```bash +git clone http://jeason.online:3000/zhaojie/assist.git +cd assist +``` + +2. **一键启动所有服务** +```bash +# 使用部署脚本 +chmod +x scripts/docker_deploy.sh +./scripts/docker_deploy.sh start + +# 或直接使用docker-compose +docker-compose up -d +``` + +3. **访问系统** +- **TSP助手**: http://localhost:5000 +- **Nginx代理**: http://localhost +- **Prometheus监控**: http://localhost:9090 +- **Grafana仪表板**: http://localhost:3000 (admin/admin123456) + +4. **服务管理** +```bash +# 查看服务状态 +./scripts/docker_deploy.sh status + +# 查看日志 +./scripts/docker_deploy.sh logs tsp-assistant + +# 停止服务 +./scripts/docker_deploy.sh stop + +# 重启服务 +./scripts/docker_deploy.sh restart +``` + +### 💻 本地部署 1. **克隆项目** ```bash @@ -251,10 +315,11 @@ python scripts/update_manager.py auto-update --source ./new_version --environmen ## 🔧 配置说明 -### 环境变量 +### Docker环境变量 ```bash # 数据库配置 -DATABASE_URL=sqlite:///tsp_assistant.db +DATABASE_URL=mysql+pymysql://tsp_user:tsp_password@mysql:3306/tsp_assistant?charset=utf8mb4 +REDIS_URL=redis://redis:6379/0 # LLM配置 LLM_PROVIDER=openai @@ -265,13 +330,35 @@ LLM_MODEL=gpt-3.5-turbo SERVER_PORT=5000 WEBSOCKET_PORT=8765 LOG_LEVEL=INFO +TZ=Asia/Shanghai ``` +### Docker服务配置 + +#### 主要服务 +- **tsp-assistant**: 主应用服务 (端口: 5000, 8765) +- **mysql**: MySQL数据库 (端口: 3306) +- **redis**: Redis缓存 (端口: 6379) +- **nginx**: 反向代理 (端口: 80, 443) + +#### 监控服务 +- **prometheus**: 监控数据收集 (端口: 9090) +- **grafana**: 监控仪表板 (端口: 3000) + +#### 数据卷 +- `mysql_data`: MySQL数据持久化 +- `redis_data`: Redis数据持久化 +- `prometheus_data`: Prometheus数据持久化 +- `grafana_data`: Grafana配置和数据持久化 + ### 配置文件 - `config/llm_config.py`: LLM客户端配置 - `config/integrations_config.json`: 飞书集成配置 -- `update_config.json`: 更新管理器配置 -- `version.json`: 版本信息配置 +- `nginx.conf`: Nginx反向代理配置 +- `monitoring/prometheus.yml`: Prometheus监控配置 +- `init.sql`: 数据库初始化脚本 +- `docker-compose.yml`: Docker服务编排配置 +- `Dockerfile`: 应用镜像构建配置 ## 🤝 贡献指南 @@ -290,6 +377,18 @@ LOG_LEVEL=INFO ## 📝 更新日志 +### v2.0.0 (2025-09-22) - Docker环境全面升级 +- 🐳 **Docker环境重构**: 升级到Python 3.11,优化镜像构建 +- 🐳 **多服务编排**: MySQL 8.0 + Redis 7 + Nginx + Prometheus + Grafana +- 🐳 **监控系统**: 集成Prometheus监控和Grafana仪表板 +- 🐳 **安全增强**: 非root用户运行,数据卷隔离 +- 🐳 **部署脚本**: 一键部署脚本,支持启动/停止/重启/清理 +- 🔧 **知识库搜索修复**: 简化搜索算法,提升检索准确率 +- 🔧 **批量删除优化**: 修复外键约束和缓存问题 +- 🔧 **日志编码修复**: 解决中文乱码问题 +- 📊 **可视化增强**: 修复预警、性能、满意度图表显示 +- 📚 **文档更新**: 完整的Docker部署和使用指南 + ### v1.4.0 (2025-09-19) - ✅ 飞书集成功能:支持飞书多维表格数据同步 - ✅ 页面功能合并:飞书同步页面合并到主仪表板 @@ -324,11 +423,87 @@ LOG_LEVEL=INFO 本项目采用 MIT 许可证 - 查看 [LICENSE](LICENSE) 文件了解详情 +## 🔧 故障排除 + +### Docker部署问题 + +#### 常见问题 +1. **端口冲突** +```bash +# 检查端口占用 +netstat -tulpn | grep :5000 +# 修改docker-compose.yml中的端口映射 +``` + +2. **内存不足** +```bash +# 检查Docker资源使用 +docker stats +# 增加Docker内存限制或关闭其他服务 +``` + +3. **数据库连接失败** +```bash +# 检查MySQL服务状态 +docker-compose logs mysql +# 等待数据库完全启动(约30秒) +``` + +4. **权限问题** +```bash +# 给脚本添加执行权限 +chmod +x scripts/docker_deploy.sh +# 检查文件权限 +ls -la scripts/ +``` + +#### 日志查看 +```bash +# 查看所有服务日志 +docker-compose logs -f + +# 查看特定服务日志 +docker-compose logs -f tsp-assistant +docker-compose logs -f mysql +docker-compose logs -f redis +``` + +#### 服务重启 +```bash +# 重启特定服务 +docker-compose restart tsp-assistant + +# 重启所有服务 +docker-compose down && docker-compose up -d +``` + +### 性能优化 + +#### Docker资源限制 +```yaml +# 在docker-compose.yml中添加资源限制 +services: + tsp-assistant: + deploy: + resources: + limits: + memory: 2G + cpus: '1.0' +``` + +#### 数据库优化 +```sql +-- MySQL性能优化 +SET GLOBAL innodb_buffer_pool_size = 1G; +SET GLOBAL max_connections = 200; +``` + ## 📞 支持与联系 - **项目地址**: http://jeason.online:3000/zhaojie/assist - **问题反馈**: 请在Issues中提交问题 - **功能建议**: 欢迎提交Feature Request +- **Docker问题**: 请提供docker-compose logs输出 ## 🙏 致谢 diff --git a/docker-compose.yml b/docker-compose.yml index fc11a60..a90a382 100644 --- a/docker-compose.yml +++ b/docker-compose.yml @@ -2,18 +2,31 @@ version: '3.8' services: tsp-assistant: - build: . + build: + context: . + dockerfile: Dockerfile container_name: tsp_assistant ports: - "5000:5000" + - "8765:8765" # WebSocket端口 environment: - PYTHONPATH=/app - - DATABASE_URL=sqlite:///tsp_assistant.db + - DATABASE_URL=mysql+pymysql://tsp_user:tsp_password@mysql:3306/tsp_assistant?charset=utf8mb4 + - REDIS_URL=redis://redis:6379/0 + - LOG_LEVEL=INFO + - TZ=Asia/Shanghai volumes: - ./data:/app/data - ./logs:/app/logs - ./backups:/app/backups + - ./uploads:/app/uploads + - ./config:/app/config - tsp_db:/app + depends_on: + mysql: + condition: service_healthy + redis: + condition: service_healthy restart: unless-stopped healthcheck: test: ["CMD", "curl", "-f", "http://localhost:5000/api/health"] @@ -21,8 +34,10 @@ services: timeout: 10s retries: 3 start_period: 40s + networks: + - tsp_network - # MySQL数据库服务(可选) + # MySQL数据库服务 mysql: image: mysql:8.0 container_name: tsp_mysql @@ -31,15 +46,44 @@ services: MYSQL_DATABASE: tsp_assistant MYSQL_USER: tsp_user MYSQL_PASSWORD: tsp_password + MYSQL_CHARACTER_SET_SERVER: utf8mb4 + MYSQL_COLLATION_SERVER: utf8mb4_unicode_ci ports: - "3306:3306" volumes: - mysql_data:/var/lib/mysql - ./init.sql:/docker-entrypoint-initdb.d/init.sql restart: unless-stopped - command: --default-authentication-plugin=mysql_native_password + command: --default-authentication-plugin=mysql_native_password --character-set-server=utf8mb4 --collation-server=utf8mb4_unicode_ci + healthcheck: + test: ["CMD", "mysqladmin", "ping", "-h", "localhost", "-u", "root", "-proot123456"] + interval: 30s + timeout: 10s + retries: 5 + start_period: 30s + networks: + - tsp_network - # Nginx反向代理(可选) + # Redis缓存服务 + redis: + image: redis:7-alpine + container_name: tsp_redis + ports: + - "6379:6379" + volumes: + - redis_data:/data + restart: unless-stopped + command: redis-server --appendonly yes --requirepass redis123456 + healthcheck: + test: ["CMD", "redis-cli", "--raw", "incr", "ping"] + interval: 30s + timeout: 10s + retries: 3 + start_period: 30s + networks: + - tsp_network + + # Nginx反向代理 nginx: image: nginx:alpine container_name: tsp_nginx @@ -49,10 +93,61 @@ services: volumes: - ./nginx.conf:/etc/nginx/nginx.conf - ./ssl:/etc/nginx/ssl + - ./logs/nginx:/var/log/nginx depends_on: - tsp-assistant restart: unless-stopped + healthcheck: + test: ["CMD", "wget", "--quiet", "--tries=1", "--spider", "http://localhost/health"] + interval: 30s + timeout: 10s + retries: 3 + start_period: 30s + networks: + - tsp_network + + # 监控服务 + prometheus: + image: prom/prometheus:latest + container_name: tsp_prometheus + ports: + - "9090:9090" + volumes: + - ./monitoring/prometheus.yml:/etc/prometheus/prometheus.yml + - prometheus_data:/prometheus + command: + - '--config.file=/etc/prometheus/prometheus.yml' + - '--storage.tsdb.path=/prometheus' + - '--web.console.libraries=/etc/prometheus/console_libraries' + - '--web.console.templates=/etc/prometheus/consoles' + - '--storage.tsdb.retention.time=200h' + - '--web.enable-lifecycle' + restart: unless-stopped + networks: + - tsp_network + + # Grafana仪表板 + grafana: + image: grafana/grafana:latest + container_name: tsp_grafana + ports: + - "3000:3000" + environment: + - GF_SECURITY_ADMIN_PASSWORD=admin123456 + volumes: + - grafana_data:/var/lib/grafana + - ./monitoring/grafana/provisioning:/etc/grafana/provisioning + restart: unless-stopped + networks: + - tsp_network volumes: tsp_db: mysql_data: + redis_data: + prometheus_data: + grafana_data: + +networks: + tsp_network: + driver: bridge diff --git a/init.sql b/init.sql new file mode 100644 index 0000000..ea26bca --- /dev/null +++ b/init.sql @@ -0,0 +1,127 @@ +-- TSP智能助手数据库初始化脚本 + +-- 创建数据库(如果不存在) +CREATE DATABASE IF NOT EXISTS tsp_assistant CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci; + +-- 使用数据库 +USE tsp_assistant; + +-- 创建用户表 +CREATE TABLE IF NOT EXISTS users ( + id INT AUTO_INCREMENT PRIMARY KEY, + username VARCHAR(50) UNIQUE NOT NULL, + email VARCHAR(100) UNIQUE NOT NULL, + password_hash VARCHAR(255) NOT NULL, + role ENUM('admin', 'user', 'operator') DEFAULT 'user', + is_active BOOLEAN DEFAULT TRUE, + created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP, + updated_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP +); + +-- 创建工单表 +CREATE TABLE IF NOT EXISTS work_orders ( + id INT AUTO_INCREMENT PRIMARY KEY, + order_id VARCHAR(50) UNIQUE NOT NULL, + title VARCHAR(200) NOT NULL, + description TEXT, + category VARCHAR(50), + priority ENUM('low', 'medium', 'high', 'urgent') DEFAULT 'medium', + status ENUM('open', 'in_progress', 'resolved', 'closed') DEFAULT 'open', + user_id INT, + assigned_to INT, + created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP, + updated_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP, + resolved_at TIMESTAMP NULL, + FOREIGN KEY (user_id) REFERENCES users(id), + FOREIGN KEY (assigned_to) REFERENCES users(id) +); + +-- 创建预警表 +CREATE TABLE IF NOT EXISTS alerts ( + id INT AUTO_INCREMENT PRIMARY KEY, + rule_name VARCHAR(100) NOT NULL, + message TEXT NOT NULL, + level ENUM('info', 'warning', 'error', 'critical') DEFAULT 'info', + status ENUM('active', 'resolved', 'suppressed') DEFAULT 'active', + source VARCHAR(100), + metadata JSON, + created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP, + resolved_at TIMESTAMP NULL, + INDEX idx_level (level), + INDEX idx_status (status), + INDEX idx_created_at (created_at) +); + +-- 创建对话表 +CREATE TABLE IF NOT EXISTS conversations ( + id INT AUTO_INCREMENT PRIMARY KEY, + session_id VARCHAR(100) NOT NULL, + user_id INT, + work_order_id INT, + user_message TEXT NOT NULL, + assistant_response TEXT NOT NULL, + confidence_score DECIMAL(3,2) DEFAULT 0.50, + created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP, + FOREIGN KEY (user_id) REFERENCES users(id), + FOREIGN KEY (work_order_id) REFERENCES work_orders(id), + INDEX idx_session_id (session_id), + INDEX idx_created_at (created_at) +); + +-- 创建知识库表 +CREATE TABLE IF NOT EXISTS knowledge_entries ( + id INT AUTO_INCREMENT PRIMARY KEY, + question TEXT NOT NULL, + answer TEXT NOT NULL, + category VARCHAR(50), + confidence_score DECIMAL(3,2) DEFAULT 0.50, + usage_count INT DEFAULT 0, + is_verified BOOLEAN DEFAULT FALSE, + is_active BOOLEAN DEFAULT TRUE, + created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP, + updated_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP, + INDEX idx_category (category), + INDEX idx_is_active (is_active), + INDEX idx_is_verified (is_verified) +); + +-- 创建工单建议表 +CREATE TABLE IF NOT EXISTS work_order_suggestions ( + id INT AUTO_INCREMENT PRIMARY KEY, + work_order_id INT NOT NULL, + ai_suggestion TEXT, + human_resolution TEXT, + created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP, + updated_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP, + FOREIGN KEY (work_order_id) REFERENCES work_orders(id) ON DELETE CASCADE +); + +-- 创建系统配置表 +CREATE TABLE IF NOT EXISTS system_settings ( + id INT AUTO_INCREMENT PRIMARY KEY, + key_name VARCHAR(100) UNIQUE NOT NULL, + value TEXT, + description TEXT, + created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP, + updated_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP +); + +-- 插入默认管理员用户 +INSERT IGNORE INTO users (username, email, password_hash, role) VALUES +('admin', 'admin@tsp.com', '$2b$12$LQv3c1yqBWVHxkd0LHAkCOYz6TtxMQJqhN8/LewdBPj4J/8K8K8K8', 'admin'); + +-- 插入默认系统配置 +INSERT IGNORE INTO system_settings (key_name, value, description) VALUES +('system_name', 'TSP智能助手', '系统名称'), +('version', '2.0.0', '系统版本'), +('maintenance_mode', 'false', '维护模式'), +('max_concurrent_users', '100', '最大并发用户数'), +('session_timeout', '3600', '会话超时时间(秒)'); + +-- 创建索引优化查询性能 +CREATE INDEX idx_work_orders_status ON work_orders(status); +CREATE INDEX idx_work_orders_priority ON work_orders(priority); +CREATE INDEX idx_work_orders_created_at ON work_orders(created_at); +CREATE INDEX idx_alerts_level_status ON alerts(level, status); +CREATE INDEX idx_conversations_user_id ON conversations(user_id); +CREATE INDEX idx_knowledge_entries_category_active ON knowledge_entries(category, is_active); diff --git a/monitoring/grafana/provisioning/dashboards/dashboard.yml b/monitoring/grafana/provisioning/dashboards/dashboard.yml new file mode 100644 index 0000000..be165c4 --- /dev/null +++ b/monitoring/grafana/provisioning/dashboards/dashboard.yml @@ -0,0 +1,12 @@ +apiVersion: 1 + +providers: + - name: 'default' + orgId: 1 + folder: '' + type: file + disableDeletion: false + updateIntervalSeconds: 10 + allowUiUpdates: true + options: + path: /etc/grafana/provisioning/dashboards diff --git a/monitoring/grafana/provisioning/datasources/prometheus.yml b/monitoring/grafana/provisioning/datasources/prometheus.yml new file mode 100644 index 0000000..1a57b69 --- /dev/null +++ b/monitoring/grafana/provisioning/datasources/prometheus.yml @@ -0,0 +1,9 @@ +apiVersion: 1 + +datasources: + - name: Prometheus + type: prometheus + access: proxy + url: http://prometheus:9090 + isDefault: true + editable: true diff --git a/monitoring/prometheus.yml b/monitoring/prometheus.yml new file mode 100644 index 0000000..991013d --- /dev/null +++ b/monitoring/prometheus.yml @@ -0,0 +1,45 @@ +global: + scrape_interval: 15s + evaluation_interval: 15s + +rule_files: + # - "first_rules.yml" + # - "second_rules.yml" + +scrape_configs: + # Prometheus自身监控 + - job_name: 'prometheus' + static_configs: + - targets: ['localhost:9090'] + + # TSP助手应用监控 + - job_name: 'tsp-assistant' + static_configs: + - targets: ['tsp-assistant:5000'] + metrics_path: '/api/metrics' + scrape_interval: 30s + scrape_timeout: 10s + + # MySQL监控 + - job_name: 'mysql' + static_configs: + - targets: ['mysql:3306'] + scrape_interval: 30s + + # Redis监控 + - job_name: 'redis' + static_configs: + - targets: ['redis:6379'] + scrape_interval: 30s + + # Nginx监控 + - job_name: 'nginx' + static_configs: + - targets: ['nginx:80'] + scrape_interval: 30s + + # Node Exporter(系统监控) + - job_name: 'node-exporter' + static_configs: + - targets: ['node-exporter:9100'] + scrape_interval: 30s diff --git a/nginx.conf b/nginx.conf new file mode 100644 index 0000000..d6b487d --- /dev/null +++ b/nginx.conf @@ -0,0 +1,102 @@ +events { + worker_connections 1024; +} + +http { + include /etc/nginx/mime.types; + default_type application/octet-stream; + + # 日志格式 + log_format main '$remote_addr - $remote_user [$time_local] "$request" ' + '$status $body_bytes_sent "$http_referer" ' + '"$http_user_agent" "$http_x_forwarded_for"'; + + access_log /var/log/nginx/access.log main; + error_log /var/log/nginx/error.log warn; + + # 基本设置 + sendfile on; + tcp_nopush on; + tcp_nodelay on; + keepalive_timeout 65; + types_hash_max_size 2048; + client_max_body_size 100M; + + # Gzip压缩 + gzip on; + gzip_vary on; + gzip_min_length 1024; + gzip_types text/plain text/css text/xml text/javascript application/javascript application/xml+rss application/json; + + # 上游服务器 + upstream tsp_backend { + server tsp-assistant:5000; + } + + # HTTP服务器 + server { + listen 80; + server_name localhost; + + # 健康检查 + location /health { + access_log off; + return 200 "healthy\n"; + add_header Content-Type text/plain; + } + + # API代理 + location /api/ { + proxy_pass http://tsp_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; + proxy_set_header X-Forwarded-Proto $scheme; + proxy_connect_timeout 30s; + proxy_send_timeout 30s; + proxy_read_timeout 30s; + } + + # WebSocket代理 + location /ws/ { + proxy_pass http://tsp_backend; + proxy_http_version 1.1; + proxy_set_header Upgrade $http_upgrade; + proxy_set_header Connection "upgrade"; + proxy_set_header Host $host; + proxy_set_header X-Real-IP $remote_addr; + proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; + proxy_set_header X-Forwarded-Proto $scheme; + } + + # 静态文件 + location /static/ { + proxy_pass http://tsp_backend; + expires 1y; + add_header Cache-Control "public, immutable"; + } + + # 主应用 + location / { + proxy_pass http://tsp_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; + proxy_set_header X-Forwarded-Proto $scheme; + } + } + + # HTTPS服务器(可选) + # server { + # listen 443 ssl http2; + # server_name localhost; + # + # ssl_certificate /etc/nginx/ssl/cert.pem; + # ssl_certificate_key /etc/nginx/ssl/key.pem; + # ssl_protocols TLSv1.2 TLSv1.3; + # ssl_ciphers ECDHE-RSA-AES128-GCM-SHA256:ECDHE-RSA-AES256-GCM-SHA384; + # ssl_prefer_server_ciphers off; + # + # # 其他配置与HTTP相同... + # } +} diff --git a/requirements.txt b/requirements.txt index 039627c..1c1caf3 100644 --- a/requirements.txt +++ b/requirements.txt @@ -6,10 +6,10 @@ scikit-learn>=1.3.0 # 数据库驱动 pymysql>=1.1.0 -cryptography>=3.4.0 -flask>=2.0.0 -flask-cors>=3.0.0 -websockets>=10.0 +cryptography>=41.0.0 +flask>=2.3.0 +flask-cors>=4.0.0 +websockets>=11.0.0 # 中文处理 jieba>=0.42.1 @@ -23,9 +23,12 @@ openpyxl>=3.1.0 # 向量化 sentence-transformers>=2.2.0 +transformers>=4.30.0 +torch>=2.0.0 # 日志和配置 python-dotenv>=1.0.0 +structlog>=23.0.0 # 时间处理 python-dateutil>=2.8.0 @@ -33,18 +36,49 @@ python-dateutil>=2.8.0 # JSON处理 ujson>=5.8.0 -# 异步支持(可选) +# 异步支持 aiohttp>=3.8.0 asyncio>=3.4.3 # Redis缓存 redis>=4.5.0 +redis-py-cluster>=2.1.0 # 测试框架 pytest>=7.4.0 pytest-asyncio>=0.21.0 +pytest-cov>=4.1.0 # 代码质量 black>=23.0.0 flake8>=6.0.0 mypy>=1.5.0 +isort>=5.12.0 + +# 性能监控 +prometheus-client>=0.17.0 +grafana-api>=1.0.0 + +# 安全 +bcrypt>=4.0.0 +pyjwt>=2.8.0 + +# 文件处理 +python-magic>=0.4.27 +pillow>=10.0.0 + +# 网络工具 +urllib3>=2.0.0 +httpx>=0.24.0 + +# 数据验证 +pydantic>=2.0.0 +marshmallow>=3.20.0 + +# 任务队列 +celery>=5.3.0 +kombu>=5.3.0 + +# 文档生成 +sphinx>=7.0.0 +sphinx-rtd-theme>=1.3.0 diff --git a/scripts/docker_deploy.sh b/scripts/docker_deploy.sh new file mode 100644 index 0000000..207bbdc --- /dev/null +++ b/scripts/docker_deploy.sh @@ -0,0 +1,204 @@ +#!/bin/bash +# TSP智能助手Docker部署脚本 + +set -e + +# 颜色定义 +RED='\033[0;31m' +GREEN='\033[0;32m' +YELLOW='\033[1;33m' +BLUE='\033[0;34m' +NC='\033[0m' # No Color + +# 日志函数 +log_info() { + echo -e "${BLUE}[INFO]${NC} $1" +} + +log_success() { + echo -e "${GREEN}[SUCCESS]${NC} $1" +} + +log_warning() { + echo -e "${YELLOW}[WARNING]${NC} $1" +} + +log_error() { + echo -e "${RED}[ERROR]${NC} $1" +} + +# 检查Docker和Docker Compose +check_dependencies() { + log_info "检查依赖..." + + if ! command -v docker &> /dev/null; then + log_error "Docker未安装,请先安装Docker" + exit 1 + fi + + if ! command -v docker-compose &> /dev/null; then + log_error "Docker Compose未安装,请先安装Docker Compose" + exit 1 + fi + + log_success "依赖检查通过" +} + +# 创建必要的目录 +create_directories() { + log_info "创建必要的目录..." + + mkdir -p logs/nginx + mkdir -p monitoring/grafana/provisioning/datasources + mkdir -p monitoring/grafana/provisioning/dashboards + mkdir -p ssl + mkdir -p data + mkdir -p backups + mkdir -p uploads + mkdir -p config + + log_success "目录创建完成" +} + +# 构建镜像 +build_images() { + log_info "构建Docker镜像..." + + # 构建主应用镜像 + docker-compose build --no-cache tsp-assistant + + log_success "镜像构建完成" +} + +# 启动服务 +start_services() { + log_info "启动服务..." + + # 启动基础服务(MySQL, Redis) + docker-compose up -d mysql redis + + # 等待数据库启动 + log_info "等待数据库启动..." + sleep 30 + + # 启动主应用 + docker-compose up -d tsp-assistant + + # 启动其他服务 + docker-compose up -d nginx prometheus grafana + + log_success "服务启动完成" +} + +# 检查服务状态 +check_services() { + log_info "检查服务状态..." + + sleep 10 + + # 检查主应用 + if curl -f http://localhost:5000/api/health &> /dev/null; then + log_success "TSP助手服务正常" + else + log_warning "TSP助手服务可能未完全启动" + fi + + # 检查Nginx + if curl -f http://localhost/health &> /dev/null; then + log_success "Nginx服务正常" + else + log_warning "Nginx服务可能未完全启动" + fi + + # 检查Prometheus + if curl -f http://localhost:9090 &> /dev/null; then + log_success "Prometheus服务正常" + else + log_warning "Prometheus服务可能未完全启动" + fi + + # 检查Grafana + if curl -f http://localhost:3000 &> /dev/null; then + log_success "Grafana服务正常" + else + log_warning "Grafana服务可能未完全启动" + fi +} + +# 显示服务信息 +show_info() { + log_info "服务访问信息:" + echo " TSP助手: http://localhost:5000" + echo " Nginx代理: http://localhost" + echo " Prometheus: http://localhost:9090" + echo " Grafana: http://localhost:3000 (admin/admin123456)" + echo " MySQL: localhost:3306 (root/root123456)" + echo " Redis: localhost:6379 (密码: redis123456)" + echo "" + log_info "查看日志命令:" + echo " docker-compose logs -f tsp-assistant" + echo " docker-compose logs -f mysql" + echo " docker-compose logs -f redis" + echo " docker-compose logs -f nginx" +} + +# 停止服务 +stop_services() { + log_info "停止服务..." + docker-compose down + log_success "服务已停止" +} + +# 清理资源 +cleanup() { + log_info "清理Docker资源..." + docker system prune -f + log_success "清理完成" +} + +# 主函数 +main() { + case "${1:-start}" in + "start") + check_dependencies + create_directories + build_images + start_services + check_services + show_info + ;; + "stop") + stop_services + ;; + "restart") + stop_services + sleep 5 + start_services + check_services + show_info + ;; + "cleanup") + stop_services + cleanup + ;; + "logs") + docker-compose logs -f "${2:-tsp-assistant}" + ;; + "status") + docker-compose ps + ;; + *) + echo "用法: $0 {start|stop|restart|cleanup|logs|status}" + echo " start - 启动所有服务" + echo " stop - 停止所有服务" + echo " restart - 重启所有服务" + echo " cleanup - 清理Docker资源" + echo " logs - 查看日志 (可选指定服务名)" + echo " status - 查看服务状态" + exit 1 + ;; + esac +} + +# 执行主函数 +main "$@"