diff --git a/backend/docker-entrypoint.sh b/backend/docker-entrypoint.sh index 280cd5b..ffe6a4f 100644 --- a/backend/docker-entrypoint.sh +++ b/backend/docker-entrypoint.sh @@ -48,6 +48,13 @@ echo "📦 执行数据库迁移..." echo "✅ 数据库迁移完成" -# 启动 uvicorn -echo "🌐 启动 API 服务..." -exec .venv/bin/uvicorn app.main:app --host 0.0.0.0 --port 8000 +# 启动 gunicorn (多工作进程模式) +# 默认开启 4 个 worker,显著提高并发处理能力 +echo "🌐 启动 API 服务 (Gunicorn + 4 Workers)..." +exec .venv/bin/gunicorn app.main:app \ + --workers 4 \ + --worker-class uvicorn.workers.UvicornWorker \ + --bind 0.0.0.0:8000 \ + --timeout 120 \ + --access-logfile - \ + --error-logfile - diff --git a/backend/pyproject.toml b/backend/pyproject.toml index e62303f..fd081bd 100644 --- a/backend/pyproject.toml +++ b/backend/pyproject.toml @@ -14,6 +14,7 @@ dependencies = [ # ============ Web Framework ============ "fastapi>=0.100.0", "uvicorn[standard]>=0.23.0", + "gunicorn>=21.2.0", "sse-starlette>=1.8.2", # ============ Database ============ diff --git a/backend/uv.lock b/backend/uv.lock index ba217cb..8a306f6 100644 --- a/backend/uv.lock +++ b/backend/uv.lock @@ -1023,6 +1023,7 @@ dependencies = [ { name = "esprima" }, { name = "fastapi" }, { name = "greenlet" }, + { name = "gunicorn" }, { name = "httpx" }, { name = "jinja2" }, { name = "jsbeautifier" }, @@ -1102,6 +1103,7 @@ requires-dist = [ { name = "esprima", specifier = ">=4.0.1" }, { name = "fastapi", specifier = ">=0.100.0" }, { name = "greenlet", specifier = ">=3.0.0" }, + { name = "gunicorn", specifier = ">=21.2.0" }, { name = "httpx", specifier = ">=0.25.0" }, { name = "jinja2", specifier = ">=3.1.6" }, { name = "jsbeautifier", specifier = ">=1.14.0" }, @@ -1748,6 +1750,18 @@ wheels = [ { url = "https://mirrors.ustc.edu.cn/pypi/packages/19/41/0b430b01a2eb38ee887f88c1f07644a1df8e289353b78e82b37ef988fb64/grpcio-1.76.0-cp314-cp314-win_amd64.whl", hash = "sha256:922fa70ba549fce362d2e2871ab542082d66e2aaf0c19480ea453905b01f384e", size = 4834462, upload-time = "2025-10-21T16:22:39.772Z" }, ] +[[package]] +name = "gunicorn" +version = "23.0.0" +source = { registry = "https://mirrors.ustc.edu.cn/pypi/simple" } +dependencies = [ + { name = "packaging" }, +] +sdist = { url = "https://mirrors.ustc.edu.cn/pypi/packages/34/72/9614c465dc206155d93eff0ca20d42e1e35afc533971379482de953521a4/gunicorn-23.0.0.tar.gz", hash = "sha256:f014447a0101dc57e294f6c18ca6b40227a4c90e9bdb586042628030cba004ec", size = 375031, upload-time = "2024-08-10T20:25:27.378Z" } +wheels = [ + { url = "https://mirrors.ustc.edu.cn/pypi/packages/cb/7d/6dac2a6e1eba33ee43f318edbed4ff29151a49b5d37f080aad1e6469bca4/gunicorn-23.0.0-py3-none-any.whl", hash = "sha256:ec400d38950de4dfd418cff8328b2c8faed0edb0d517d3394e457c317908ca4d", size = 85029, upload-time = "2024-08-10T20:25:24.996Z" }, +] + [[package]] name = "h11" version = "0.16.0" diff --git a/docker-compose.yml b/docker-compose.yml index a542b89..8c979a9 100644 --- a/docker-compose.yml +++ b/docker-compose.yml @@ -67,8 +67,8 @@ services: condition: service_healthy redis: condition: service_healthy - # 开发模式下,可以启用 --reload 热重载 - command: sh -c ".venv/bin/alembic upgrade head && .venv/bin/uvicorn app.main:app --host 0.0.0.0 --port 8000" + # 使用启动脚本自动执行数据库迁移并启动 Gunicorn + command: ["/app/docker-entrypoint.sh"] networks: - deepaudit-network