feat(db): implement read-write splitting for postgres
Add support for database read replicas using the GORM dbresolver plugin. This allows for scaling read operations by distributing them across one or multiple replica nodes. - Update `DatabaseConfig` to support single and multiple replica configurations. - Add configuration options for replica connection pooling and selection policy. - Integrate `dbresolver` in the database initialization process for PostgreSQL. - Add helper methods to aggregate replica configurations.
This commit is contained in:
@@ -21,6 +21,28 @@ database:
|
||||
password: postgres
|
||||
dbname: with_you
|
||||
sslmode: disable
|
||||
# 读写分离配置(仅 postgres 模式生效,不配置则所有请求走主库)
|
||||
# 单副本配置(支持环境变量):
|
||||
# APP_DATABASE_REPLICA_HOST, APP_DATABASE_REPLICA_PORT, APP_DATABASE_REPLICA_USER,
|
||||
# APP_DATABASE_REPLICA_PASSWORD, APP_DATABASE_REPLICA_DBNAME, APP_DATABASE_REPLICA_SSLMODE
|
||||
# replica:
|
||||
# host: replica1.internal
|
||||
# port: 5432
|
||||
# user: readonly
|
||||
# password: readonly_pass
|
||||
# dbname: with_you
|
||||
# sslmode: disable
|
||||
# 多副本配置(仅 YAML,环境变量不支持切片结构体):
|
||||
# replicas:
|
||||
# - host: replica1.internal
|
||||
# port: 5432
|
||||
# user: readonly
|
||||
# password: readonly_pass
|
||||
# dbname: with_you
|
||||
# sslmode: disable
|
||||
# replica_policy: random # random
|
||||
# replica_max_idle_conns: 10
|
||||
# replica_max_open_conns: 100
|
||||
max_idle_conns: 10
|
||||
max_open_conns: 100
|
||||
log_level: warn
|
||||
|
||||
Reference in New Issue
Block a user