# Redis 设置指南 ## 概述 AWS Resource Scanner 使用 Redis 作为 Celery 的消息队列和结果后端。Redis 是必需的组件,用于处理异步的 AWS 资源扫描任务。 ## 安装 Redis ### Windows #### 方式1: 使用 Chocolatey (推荐) ```bash choco install redis-64 redis-server --service-install redis-server --service-start ``` #### 方式2: 下载预编译版本 从 https://github.com/microsoftarchive/redis/releases 下载并安装 ### Linux #### Ubuntu/Debian ```bash sudo apt-get install redis-server sudo systemctl start redis-server sudo systemctl enable redis-server ``` #### CentOS/RHEL ```bash sudo yum install redis sudo systemctl start redis sudo systemctl enable redis ``` ### macOS ```bash brew install redis brew services start redis ``` ### Docker (跨平台) ```bash docker run -d --name redis -p 6379:6379 redis:alpine ``` ## 验证 Redis 运行 ```bash redis-cli ping # 应该返回: PONG ``` ## 配置 ### 环境变量 (.env) ```env CELERY_BROKER_URL=redis://localhost:6379/0 CELERY_RESULT_BACKEND=redis://localhost:6379/1 # 如果 Redis 有密码 # CELERY_BROKER_URL=redis://:password@localhost:6379/0 # CELERY_RESULT_BACKEND=redis://:password@localhost:6379/1 ``` ## 启动 Celery Worker Redis 运行后,需要启动 Celery Worker 来处理任务: ```bash cd backend # 激活虚拟环境 activate_venv.bat # Windows # 或 source activate_venv.sh # Unix/Linux # 启动 Celery Worker celery -A celery_worker.celery_app worker --loglevel=info --pool=solo ``` ## 故障排除 ### Redis 连接失败 1. 检查 Redis 是否运行: ```bash redis-cli ping ``` 2. 检查端口占用: ```bash netstat -an | grep 6379 ``` 3. 重启 Redis 服务: ```bash # Windows redis-server --service-stop redis-server --service-start # Linux sudo systemctl restart redis-server ``` ### Celery Worker 问题 1. 确保使用正确的启动命令: ```bash celery -A celery_worker.celery_app worker --loglevel=info --pool=solo ``` 2. 检查 Worker 状态: ```bash celery -A celery_worker.celery_app inspect active ``` ## 生产环境建议 1. 配置 Redis 持久化 2. 设置 Redis 内存限制 3. 启用 Redis 密码认证 4. 配置 Celery Worker 监控 5. 设置任务结果过期时间