diff --git a/.gitignore b/.gitignore index 812c84e..4872903 100644 --- a/.gitignore +++ b/.gitignore @@ -8,6 +8,4 @@ wheels/ # Virtual environments .venv - -# Database backups data-bak/ diff --git a/docker-compose.dev.yml b/docker-compose.dev.yml index 9e2d409..c7ba48e 100644 --- a/docker-compose.dev.yml +++ b/docker-compose.dev.yml @@ -12,58 +12,6 @@ services: - "5432:5432" volumes: - db_data:/var/lib/postgresql/data - healthcheck: - test: ["CMD-SHELL", "pg_isready -U postgres"] - interval: 5s - timeout: 3s - retries: 10 - start_period: 10s - restart: unless-stopped - - pgbouncer: - image: pgbouncer/pgbouncer:latest - container_name: pgbouncer - # 使用环境变量配置,让镜像自动生成配置文件 - environment: - # 数据库连接配置 - 使用 DATABASES 环境变量(格式:数据库名=host=主机 port=端口 dbname=数据库名 user=用户 password=密码) - # 确保密码与 PostgreSQL 容器的 POSTGRES_PASSWORD 完全一致 - DATABASES: flower=host=postgres port=5432 dbname=flower user=postgres password=postgres - # PgBouncer 基本配置 - PGBOUNCER_LISTEN_ADDR: 0.0.0.0 - PGBOUNCER_LISTEN_PORT: 6432 - PGBOUNCER_AUTH_TYPE: trust - # 使用 trust 认证时,需要指定 auth_user,所有客户端连接都使用此用户 - PGBOUNCER_AUTH_USER: postgres - # 连接池配置 - PGBOUNCER_POOL_MODE: transaction - PGBOUNCER_MAX_CLIENT_CONN: "100" - PGBOUNCER_DEFAULT_POOL_SIZE: "25" - PGBOUNCER_MIN_POOL_SIZE: "5" - PGBOUNCER_RESERVE_POOL_SIZE: "5" - PGBOUNCER_RESERVE_POOL_TIMEOUT: "3" - # 连接超时设置 - PGBOUNCER_SERVER_CONNECT_TIMEOUT: "15" - PGBOUNCER_SERVER_IDLE_TIMEOUT: "600" - PGBOUNCER_QUERY_TIMEOUT: "0" - PGBOUNCER_QUERY_WAIT_TIMEOUT: "120" - # 日志配置 - PGBOUNCER_LOG_CONNECTIONS: "1" - PGBOUNCER_LOG_DISCONNECTIONS: "1" - PGBOUNCER_LOG_POOLER_ERRORS: "1" - # 其他设置 - PGBOUNCER_IGNORE_STARTUP_PARAMETERS: extra_float_digits - PGBOUNCER_APPLICATION_NAME_ADD_HOST: "1" - ports: - - "6432:6432" - # 确保 postgres 完全启动后再启动 pgbouncer - depends_on: - postgres: - condition: service_healthy - # DNS 配置:使用 Docker 内置 DNS,并添加 DNS 搜索域 - dns: - - 127.0.0.11 # Docker 内置 DNS - dns_search: - - . # 当前网络域 restart: unless-stopped pgadmin: @@ -118,13 +66,12 @@ services: PYTHONPATH: /app DEBUG: "1" ALLOWED_HOSTS: "localhost,127.0.0.1,0.0.0.0,8.148.215.233,yuwenerp.yuwen.cloud" - DB_HOST: pgbouncer - DB_PORT: "6432" + DB_HOST: postgres + DB_PORT: "5432" DB_NAME: flower DB_USER: postgres DB_PASSWORD: postgres depends_on: - - pgbouncer - postgres - redis - rabbitmq @@ -147,7 +94,6 @@ services: volumes: - .:/app depends_on: - - pgbouncer - postgres - redis - rabbitmq @@ -155,8 +101,8 @@ services: CELERY_BROKER_URL: amqp://guest:guest@rabbitmq:5672// CELERY_RESULT_BACKEND: redis://redis:6379/0 PYTHONPATH: /app - DB_HOST: pgbouncer - DB_PORT: "6432" + DB_HOST: postgres + DB_PORT: "5432" DB_NAME: flower DB_USER: postgres DB_PASSWORD: postgres @@ -179,21 +125,20 @@ services: volumes: - .:/app - celery_beat_data:/var/lib/celery + depends_on: + - postgres + - redis + - rabbitmq environment: DJANGO_SETTINGS_MODULE: flower.settings PYTHONPATH: /app - DB_HOST: pgbouncer - DB_PORT: "6432" + DB_HOST: postgres + DB_PORT: "5432" DB_NAME: flower DB_USER: postgres DB_PASSWORD: postgres CELERY_BROKER_URL: amqp://guest:guest@rabbitmq:5672// CELERY_RESULT_BACKEND: redis://redis:6379/0 - depends_on: - - pgbouncer - - postgres - - redis - - rabbitmq restart: unless-stopped volumes: diff --git a/docker-compose.prod.yml b/docker-compose.prod.yml index 2db3c51..e5ec511 100644 --- a/docker-compose.prod.yml +++ b/docker-compose.prod.yml @@ -8,65 +8,11 @@ services: POSTGRES_USER: ${POSTGRES_USER:-postgres} POSTGRES_PASSWORD: ${POSTGRES_PASSWORD:-postgres} POSTGRES_DB: ${POSTGRES_DB:-flower} + # 仅绑定到本机,避免把数据库暴露到公网 ports: - - "5432:5432" + - "127.0.0.1:${POSTGRES_PORT:-5432}:5432" volumes: - db_data:/var/lib/postgresql/data - dns: - - 8.8.8.8 - - 8.8.4.4 - healthcheck: - test: ["CMD-SHELL", "pg_isready -U ${POSTGRES_USER:-postgres}"] - interval: 5s - timeout: 3s - retries: 10 - start_period: 10s - restart: unless-stopped - - pgbouncer: - image: pgbouncer/pgbouncer:latest - container_name: pgbouncer - # 使用环境变量配置,让镜像自动生成配置文件 - environment: - # 数据库连接配置 - 使用 DATABASES 环境变量(格式:数据库名=host=主机 port=端口 dbname=数据库名 user=用户 password=密码) - # 确保密码与 PostgreSQL 容器的 POSTGRES_PASSWORD 完全一致 - # 注意:如果密码包含特殊字符,可能需要转义或使用引号 - DATABASES: flower=host=postgres port=${POSTGRES_PORT:-5432} dbname=${POSTGRES_DB:-flower} user=${POSTGRES_USER:-postgres} password=${POSTGRES_PASSWORD:-postgres} - # PgBouncer 基本配置 - PGBOUNCER_LISTEN_ADDR: 0.0.0.0 - PGBOUNCER_LISTEN_PORT: 6432 - PGBOUNCER_AUTH_TYPE: trust - # 使用 trust 认证时,需要指定 auth_user,所有客户端连接都使用此用户 - PGBOUNCER_AUTH_USER: postgres - # 连接池配置 - PGBOUNCER_POOL_MODE: transaction - PGBOUNCER_MAX_CLIENT_CONN: "100" - PGBOUNCER_DEFAULT_POOL_SIZE: "25" - PGBOUNCER_MIN_POOL_SIZE: "5" - PGBOUNCER_RESERVE_POOL_SIZE: "5" - PGBOUNCER_RESERVE_POOL_TIMEOUT: "3" - # 连接超时设置 - PGBOUNCER_SERVER_CONNECT_TIMEOUT: "15" - PGBOUNCER_SERVER_IDLE_TIMEOUT: "600" - PGBOUNCER_QUERY_TIMEOUT: "0" - PGBOUNCER_QUERY_WAIT_TIMEOUT: "120" - # 日志配置 - PGBOUNCER_LOG_CONNECTIONS: "1" - PGBOUNCER_LOG_DISCONNECTIONS: "1" - PGBOUNCER_LOG_POOLER_ERRORS: "1" - # 其他设置 - PGBOUNCER_IGNORE_STARTUP_PARAMETERS: extra_float_digits - PGBOUNCER_APPLICATION_NAME_ADD_HOST: "1" - ports: - - "${PGBOUNCER_PORT:-6432}:6432" - # 确保 postgres 完全启动后再启动 pgbouncer - depends_on: - postgres: - condition: service_healthy - # DNS 配置:使用 Docker 内置 DNS,并添加 DNS 搜索域 - dns: - - 8.8.8.8 - - 8.8.4.4 restart: unless-stopped redis: @@ -105,15 +51,14 @@ services: PYTHONPATH: /app DEBUG: "${DEBUG:-0}" ALLOWED_HOSTS: "${ALLOWED_HOSTS:-yuwenerp.yuwen.cloud}" - DB_HOST: ${DB_HOST:-pgbouncer} - DB_PORT: "${DB_PORT:-6432}" + DB_HOST: ${DB_HOST:-postgres} + DB_PORT: "${DB_PORT:-5432}" DB_NAME: "${DB_NAME:-flower}" DB_USER: "${DB_USER:-postgres}" DB_PASSWORD: "${DB_PASSWORD:-postgres}" CELERY_BROKER_URL: "${CELERY_BROKER_URL:-amqp://guest:guest@rabbitmq:5672//}" CELERY_RESULT_BACKEND: "${CELERY_RESULT_BACKEND:-redis://redis:6379/0}" depends_on: - - pgbouncer - postgres - redis - rabbitmq @@ -133,8 +78,8 @@ services: - --pool=solo environment: PYTHONPATH: /app - DB_HOST: ${DB_HOST:-pgbouncer} - DB_PORT: "${DB_PORT:-6432}" + DB_HOST: ${DB_HOST:-postgres} + DB_PORT: "${DB_PORT:-5432}" DB_NAME: "${DB_NAME:-flower}" DB_USER: "${DB_USER:-postgres}" DB_PASSWORD: "${DB_PASSWORD:-postgres}" @@ -146,7 +91,6 @@ services: # 也可通过 BACKUP_DIR 指定绝对路径,如 /srv/flower/data-bak - ${BACKUP_DIR:-./data-bak}:/app/data-bak depends_on: - - pgbouncer - postgres - redis - rabbitmq @@ -167,8 +111,8 @@ services: environment: DJANGO_SETTINGS_MODULE: flower.settings PYTHONPATH: /app - DB_HOST: ${DB_HOST:-pgbouncer} - DB_PORT: "${DB_PORT:-6432}" + DB_HOST: ${DB_HOST:-postgres} + DB_PORT: "${DB_PORT:-5432}" DB_NAME: "${DB_NAME:-flower}" DB_USER: "${DB_USER:-postgres}" DB_PASSWORD: "${DB_PASSWORD:-postgres}" @@ -178,7 +122,6 @@ services: volumes: - celery_beat_data:/var/lib/celery depends_on: - - pgbouncer - postgres - redis - rabbitmq diff --git a/docker-compose.yml b/docker-compose.yml index aa6657f..29b2815 100644 --- a/docker-compose.yml +++ b/docker-compose.yml @@ -12,58 +12,6 @@ services: - "5432:5432" volumes: - db_data:/var/lib/postgresql/data - healthcheck: - test: ["CMD-SHELL", "pg_isready -U postgres"] - interval: 5s - timeout: 3s - retries: 10 - start_period: 10s - restart: unless-stopped - - pgbouncer: - image: pgbouncer/pgbouncer:latest - container_name: pgbouncer - # 使用环境变量配置,让镜像自动生成配置文件 - environment: - # 数据库连接配置 - 使用 DATABASES 环境变量(格式:数据库名=host=主机 port=端口 dbname=数据库名 user=用户 password=密码) - # 确保密码与 PostgreSQL 容器的 POSTGRES_PASSWORD 完全一致 - DATABASES: flower=host=postgres port=5432 dbname=flower user=postgres password=postgres - # PgBouncer 基本配置 - PGBOUNCER_LISTEN_ADDR: 0.0.0.0 - PGBOUNCER_LISTEN_PORT: 6432 - PGBOUNCER_AUTH_TYPE: trust - # 使用 trust 认证时,需要指定 auth_user,所有客户端连接都使用此用户 - PGBOUNCER_AUTH_USER: postgres - # 连接池配置 - PGBOUNCER_POOL_MODE: transaction - PGBOUNCER_MAX_CLIENT_CONN: "100" - PGBOUNCER_DEFAULT_POOL_SIZE: "25" - PGBOUNCER_MIN_POOL_SIZE: "5" - PGBOUNCER_RESERVE_POOL_SIZE: "5" - PGBOUNCER_RESERVE_POOL_TIMEOUT: "3" - # 连接超时设置 - PGBOUNCER_SERVER_CONNECT_TIMEOUT: "15" - PGBOUNCER_SERVER_IDLE_TIMEOUT: "600" - PGBOUNCER_QUERY_TIMEOUT: "0" - PGBOUNCER_QUERY_WAIT_TIMEOUT: "120" - # 日志配置 - PGBOUNCER_LOG_CONNECTIONS: "1" - PGBOUNCER_LOG_DISCONNECTIONS: "1" - PGBOUNCER_LOG_POOLER_ERRORS: "1" - # 其他设置 - PGBOUNCER_IGNORE_STARTUP_PARAMETERS: extra_float_digits - PGBOUNCER_APPLICATION_NAME_ADD_HOST: "1" - ports: - - "6432:6432" - # 确保 postgres 完全启动后再启动 pgbouncer - depends_on: - postgres: - condition: service_healthy - # DNS 配置:使用 Docker 内置 DNS,并添加 DNS 搜索域 - dns: - - 127.0.0.11 # Docker 内置 DNS - dns_search: - - . # 当前网络域 restart: unless-stopped pgadmin: @@ -120,13 +68,12 @@ services: PYTHONPATH: /app DEBUG: "1" ALLOWED_HOSTS: "localhost,127.0.0.1,0.0.0.0,8.148.215.233,yuwenerp.yuwen.cloud" - DB_HOST: pgbouncer - DB_PORT: "6432" + DB_HOST: postgres + DB_PORT: "5432" DB_NAME: flower DB_USER: postgres DB_PASSWORD: postgres depends_on: - - pgbouncer - postgres - redis - rabbitmq @@ -148,20 +95,19 @@ services: - --pool=solo volumes: - .:/app + depends_on: + - postgres + - redis + - rabbitmq environment: CELERY_BROKER_URL: amqp://guest:guest@rabbitmq:5672// CELERY_RESULT_BACKEND: redis://redis:6379/0 PYTHONPATH: /app - DB_HOST: pgbouncer - DB_PORT: "6432" + DB_HOST: postgres + DB_PORT: "5432" DB_NAME: flower DB_USER: postgres DB_PASSWORD: postgres - depends_on: - - pgbouncer - - postgres - - redis - - rabbitmq restart: unless-stopped celery_beat: @@ -178,21 +124,20 @@ services: - info volumes: - .:/app + depends_on: + - postgres + - redis + - rabbitmq environment: DJANGO_SETTINGS_MODULE: flower.settings PYTHONPATH: /app - DB_HOST: pgbouncer - DB_PORT: "6432" + DB_HOST: postgres + DB_PORT: "5432" DB_NAME: flower DB_USER: postgres DB_PASSWORD: postgres CELERY_BROKER_URL: amqp://guest:guest@rabbitmq:5672// CELERY_RESULT_BACKEND: redis://redis:6379/0 - depends_on: - - pgbouncer - - postgres - - redis - - rabbitmq restart: unless-stopped volumes: diff --git a/flower/settings.py b/flower/settings.py index c64fe77..23b6163 100644 --- a/flower/settings.py +++ b/flower/settings.py @@ -196,7 +196,7 @@ DATABASES = { 'HOST': env('DB_HOST', default='localhost'), 'PORT': env('DB_PORT', default='5432'), 'CONN_HEALTH_CHECK': True, - 'CONN_MAX_AGE': 0, # 使用 PgBouncer 连接池时,必须设置为 0 + 'CONN_MAX_AGE': 60, } } diff --git a/pgbouncer/pgbouncer.ini b/pgbouncer/pgbouncer.ini deleted file mode 100644 index f61df01..0000000 --- a/pgbouncer/pgbouncer.ini +++ /dev/null @@ -1,38 +0,0 @@ -[databases] -flower = host=postgres port=5432 dbname=flower user=postgres password=postgres - -[pgbouncer] -; 监听地址和端口 -listen_addr = 0.0.0.0 -listen_port = 6432 - -; 认证方式:trust 表示信任所有连接(适用于容器内部网络) -; 这样客户端连接时不需要密码,但 PgBouncer 到 PostgreSQL 的连接仍需要密码 -auth_type = trust - -; 连接池配置 -; pool_mode: session, transaction, statement -; transaction 模式最适合 Django,每个事务结束后释放连接 -pool_mode = transaction -max_client_conn = 100 -default_pool_size = 25 -min_pool_size = 5 -reserve_pool_size = 5 -reserve_pool_timeout = 3 - -; 连接超时设置 -server_connect_timeout = 15 -server_idle_timeout = 600 -query_timeout = 0 -query_wait_timeout = 120 - -; 日志配置 -log_connections = 1 -log_disconnections = 1 -log_pooler_errors = 1 - -; 其他设置 -; 忽略 PostgreSQL 16 的额外启动参数 -ignore_startup_parameters = extra_float_digits -application_name_add_host = 1 -