# Redis 配置指南 ## 概述 本应用使用 Redis 作为 Celery 的消息代理和结果后端。在启动应用前,需要确保 Redis 服务正常运行。 ## Redis 安装 ### Windows 1. **下载 Redis for Windows** ```bash # 访问 https://github.com/microsoftarchive/redis/releases # 下载最新的 .msi 文件并安装 ``` 2. **使用 WSL (推荐)** ```bash # 在 WSL 中安装 sudo apt update sudo apt install redis-server sudo systemctl start redis-server sudo systemctl enable redis-server ``` ### Linux ```bash # Ubuntu/Debian sudo apt update sudo apt install redis-server sudo systemctl start redis-server sudo systemctl enable redis-server # CentOS/RHEL sudo yum install redis sudo systemctl start redis sudo systemctl enable redis ``` ### macOS ```bash # 使用 Homebrew brew install redis brew services start redis ``` ## Redis 配置 ### 基本配置 编辑 Redis 配置文件 `/etc/redis/redis.conf`: ```ini # 设置密码(可选) requirepass your_redis_password # 设置最大内存 maxmemory 256mb maxmemory-policy allkeys-lru # 持久化配置 save 900 1 save 300 10 save 60 10000 ``` ### 环境变量配置 在 `.env` 文件中配置: ```bash # Redis 配置 CELERY_BROKER_URL=redis://localhost:6379/0 CELERY_RESULT_BACKEND=redis://localhost:6379/0 # 如果设置了密码 # CELERY_BROKER_URL=redis://:your_password@localhost:6379/0 # CELERY_RESULT_BACKEND=redis://:your_password@localhost:6379/0 ``` ## Redis 服务管理 ### 启动 Redis ```bash # Windows redis-server # Linux (Systemd) sudo systemctl start redis-server # macOS (Homebrew) brew services start redis ``` ### 停止 Redis ```bash # Linux sudo systemctl stop redis-server # macOS brew services stop redis # 手动停止 redis-cli shutdown ``` ### 重启 Redis ```bash # Linux sudo systemctl restart redis-server # macOS brew services restart redis ``` ## 连接测试 ### 测试基本连接 ```bash # 使用 redis-cli redis-cli ping # 应该返回 PONG # 使用项目测试脚本 python test_redis.py ``` ### 检查 Redis 状态 ```bash # 查看连接信息 redis-cli info clients # 查看内存使用 redis-cli info memory # 查看数据库信息 redis-cli info keyspace ``` ## 应用中的 Redis 集成 ### 启动时检查 应用启动时会自动: 1. 验证 Redis 连接 2. 创建 Redis 客户端 3. 存储到应用状态中供其他组件使用 ### 健康检查 访问 `/health` 端点可以查看 Redis 连接状态: ```json { "status": "healthy", "redis": "connected" } ``` ### 故障排除 如果 Redis 连接失败,应用将无法启动。检查: 1. Redis 服务是否运行 2. 连接配置是否正确 3. 防火墙设置 4. 密码配置 ## 生产环境建议 ### 安全配置 1. **设置密码** ```ini requirepass your_strong_password ``` 2. **绑定特定 IP** ```ini bind 127.0.0.1 10.0.0.1 ``` 3. **禁用危险命令** ```ini rename-command FLUSHDB "" rename-command FLUSHALL "" rename-command KEYS "" rename-command CONFIG "" ``` ### 性能优化 1. **设置最大内存** ```ini maxmemory 1gb maxmemory-policy allkeys-lru ``` 2. **持久化配置** ```ini save 900 1 save 300 10 save 60 10000 ``` 3. **连接限制** ```ini maxclients 1000 ``` ### 监控 1. **使用 INFO 命令** ```bash redis-cli info server redis-cli info memory redis-cli info stats ``` 2. **监控工具** - RedisInsight - Redis Commander - 自定义监控脚本 ## 常见问题 ### Q: 应用启动失败,提示 Redis 连接错误 **A:** 检查以下几点: 1. Redis 服务是否启动:`redis-cli ping` 2. 配置文件中的 URL 是否正确 3. 防火墙是否阻止连接 4. Redis 密码配置是否匹配 ### Q: Redis 内存占用过高 **A:** 1. 检查键的数量:`redis-cli dbsize` 2. 设置过期策略:`CONFIG SET maxmemory-policy allkeys-lru` 3. 清理无用键:`redis-cli FLUSHDB` ### Q: Celery 任务不执行 **A:** 检查: 1. Redis 连接状态 2. Celery Worker 是否启动 3. 队列配置是否正确 ## 日志位置 - 应用日志:`logs/app.log` - Redis 日志:`/var/log/redis/redis.log` (Linux) - Celery 日志:控制台输出或配置的日志文件 ## 相关命令 ```bash # 查看所有键 redis-cli keys "*" # 查看特定键 redis-cli get key_name # 删除键 redis-cli del key_name # 清空数据库 redis-cli flushdb # 查看信息 redis-cli info # 监控命令 redis-cli monitor ```