Go + PostgreSQL + Valkey backend (cmd/internal/migrations), uni-app mobile frontend, PC web (web/), admin console (admin/), Android WebView container (android_project/). Build artifacts, node_modules, local env and debug screenshots excluded.
179 lines
4.7 KiB
YAML
179 lines
4.7 KiB
YAML
# 本地/单机生产编排。
|
||
#
|
||
# 设计取舍:
|
||
# * API 起 2 副本,验证「无状态可水平扩展」这个前提是真的
|
||
# * PgBouncer 前置,transaction pooling;应用侧连接数不再随副本数线性膨胀
|
||
# * PG 与 Valkey 数据落 named volume,重建容器不丢
|
||
# * 所有服务挂了 healthcheck,depends_on 用 service_healthy 而非裸 depends_on
|
||
|
||
x-logging: &default-logging
|
||
driver: json-file
|
||
options:
|
||
max-size: "50m"
|
||
max-file: "5"
|
||
|
||
services:
|
||
postgres:
|
||
image: postgres:16-alpine
|
||
container_name: ap-pg
|
||
environment:
|
||
POSTGRES_USER: app
|
||
POSTGRES_PASSWORD: ${PG_PASSWORD:-change-me-in-prod}
|
||
POSTGRES_DB: article_platform
|
||
# 时区与排序规则显式固定,避免不同宿主机行为不一致
|
||
TZ: Asia/Shanghai
|
||
command:
|
||
- postgres
|
||
- -c
|
||
- max_connections=200
|
||
- -c
|
||
- shared_buffers=512MB
|
||
- -c
|
||
- effective_cache_size=1536MB
|
||
- -c
|
||
- work_mem=8MB
|
||
- -c
|
||
- maintenance_work_mem=128MB
|
||
- -c
|
||
- wal_level=replica
|
||
- -c
|
||
- max_wal_size=2GB
|
||
- -c
|
||
- checkpoint_completion_target=0.9
|
||
- -c
|
||
- random_page_cost=1.1
|
||
- -c
|
||
- log_min_duration_statement=500
|
||
volumes:
|
||
- pgdata:/var/lib/postgresql/data
|
||
- ./migrations:/docker-entrypoint-initdb.d:ro
|
||
ports:
|
||
- "127.0.0.1:55432:5432"
|
||
healthcheck:
|
||
test: ["CMD-SHELL", "pg_isready -U app -d article_platform"]
|
||
interval: 5s
|
||
timeout: 3s
|
||
retries: 10
|
||
logging: *default-logging
|
||
restart: unless-stopped
|
||
|
||
pgbouncer:
|
||
image: edoburu/pgbouncer:latest
|
||
container_name: ap-pgbouncer
|
||
environment:
|
||
DB_USER: app
|
||
DB_PASSWORD: ${PG_PASSWORD:-change-me-in-prod}
|
||
DB_HOST: postgres
|
||
DB_NAME: article_platform
|
||
POOL_MODE: transaction
|
||
MAX_CLIENT_CONN: 2000
|
||
DEFAULT_POOL_SIZE: 25
|
||
RESERVE_POOL_SIZE: 5
|
||
AUTH_TYPE: scram-sha-256
|
||
ports:
|
||
- "127.0.0.1:6432:5432"
|
||
depends_on:
|
||
postgres:
|
||
condition: service_healthy
|
||
healthcheck:
|
||
test: ["CMD-SHELL", "pg_isready -h 127.0.0.1 -p 5432 -U app"]
|
||
interval: 5s
|
||
timeout: 3s
|
||
retries: 10
|
||
logging: *default-logging
|
||
restart: unless-stopped
|
||
|
||
valkey:
|
||
# Valkey 而非 KeyDB:KeyDB 自 2024-05 起无提交、2023-10 后无发版。
|
||
# Valkey 是 Linux Foundation 接管的 BSD 分支,协议完全兼容 go-redis。
|
||
image: valkey/valkey:9-alpine
|
||
container_name: ap-valkey
|
||
command:
|
||
- valkey-server
|
||
- --port
|
||
- "6379"
|
||
- --appendonly
|
||
- "no"
|
||
- --maxmemory
|
||
- 1gb
|
||
# allkeys-lru:纯缓存用途,内存满时淘汰最久未用的,绝不返回写错误
|
||
- --maxmemory-policy
|
||
- allkeys-lru
|
||
- --save
|
||
- ""
|
||
- --io-threads
|
||
- "4"
|
||
ports:
|
||
- "127.0.0.1:56379:6379"
|
||
healthcheck:
|
||
test: ["CMD", "valkey-cli", "ping"]
|
||
interval: 5s
|
||
timeout: 3s
|
||
retries: 10
|
||
logging: *default-logging
|
||
restart: unless-stopped
|
||
|
||
api:
|
||
build:
|
||
context: .
|
||
dockerfile: Dockerfile
|
||
command: ["/app/api"]
|
||
environment:
|
||
APP_ENV: prod
|
||
HTTP_ADDR: ":8080"
|
||
# 连 PgBouncer 而不是 PG 直连:连接池前置后,副本扩容不会打爆数据库
|
||
PG_DSN: postgres://app:${PG_PASSWORD:-change-me-in-prod}@pgbouncer:5432/article_platform?sslmode=disable
|
||
PG_MAX_CONNS: "20"
|
||
VALKEY_ADDR: valkey:6379
|
||
JWT_SECRET: ${JWT_SECRET:?JWT_SECRET 必须设置,至少 32 字节}
|
||
JWT_TTL_HOURS: "720"
|
||
SESSION_RENEW_LAST_HOURS: "168"
|
||
RATE_ANON_PER_MIN: "120"
|
||
RATE_USER_PER_MIN: "600"
|
||
ports:
|
||
- "8080:8080"
|
||
depends_on:
|
||
pgbouncer:
|
||
condition: service_healthy
|
||
valkey:
|
||
condition: service_healthy
|
||
healthcheck:
|
||
test: ["CMD", "wget", "-qO-", "http://127.0.0.1:8080/readyz"]
|
||
interval: 10s
|
||
timeout: 3s
|
||
retries: 5
|
||
start_period: 10s
|
||
deploy:
|
||
replicas: 2
|
||
resources:
|
||
limits:
|
||
cpus: "2"
|
||
memory: 512M
|
||
reservations:
|
||
cpus: "0.5"
|
||
memory: 128M
|
||
logging: *default-logging
|
||
restart: unless-stopped
|
||
|
||
worker:
|
||
build:
|
||
context: .
|
||
dockerfile: Dockerfile
|
||
command: ["/app/worker"]
|
||
environment:
|
||
APP_ENV: prod
|
||
PG_DSN: postgres://app:${PG_PASSWORD:-change-me-in-prod}@pgbouncer:5432/article_platform?sslmode=disable
|
||
PG_MAX_CONNS: "5"
|
||
VALKEY_ADDR: valkey:6379
|
||
JWT_SECRET: ${JWT_SECRET:?JWT_SECRET 必须设置,至少 32 字节}
|
||
depends_on:
|
||
pgbouncer:
|
||
condition: service_healthy
|
||
valkey:
|
||
condition: service_healthy
|
||
logging: *default-logging
|
||
restart: unless-stopped
|
||
|
||
volumes:
|
||
pgdata:
|