🔥码云GVP开源项目 12k star Uniapp+ElementUI 功能强大 支持多语言、二开方便! 广告
## 配置类型 ### golang原生配置 - 单一数据库配置 ```go // 单一数据库配置 type DbConfigSingle struct { Driver string // 驱动: mysql/sqlite/oracle/mssql/postgres EnableQueryLog bool // 是否开启sql日志 SetMaxOpenConns int // (连接池)最大打开的连接数,默认值为0表示不限制 SetMaxIdleConns int // (连接池)闲置的连接数 Prefix string // 表前缀 Dsn string // 数据库链接 } ``` - 数据库集群配置 ```go // 数据库集群配置 // 如果不启用集群, 则直接使用 DbConfig 即可 // 如果仍然使用此配置为非集群, 则 Slave 配置置空即可, 等同于使用 DbConfig type DbConfigCluster struct { Slave []*DbConfigSingle // 多台读服务器, 如果启用则需要放入对应的多台从服务器配置 Master *DbConfigSingle // 一台主服务器负责写数据 } ``` ### 配置文件示例 - json - 单一数据库配置 - json ```json { "Driver": "mysql", "EnableQueryLog": false, "SetMaxOpenConns": 0, "SetMaxIdleConns": 0, "Prefix": "", "Dsn": "root:root@tcp(localhost:3306)/test?charset=utf8" } ``` - 数据库集群配置 - json ```json { "Slave": [ { "Driver": "mysql", "EnableQueryLog": false, "SetMaxOpenConns": 0, "SetMaxIdleConns": 0, "Prefix": "", "Dsn": "root:root@tcp(localhost:3306)/test1?charset=utf8" }, { "Driver": "mysql", "EnableQueryLog": false, "SetMaxOpenConns": 0, "SetMaxIdleConns": 0, "Prefix": "", "Dsn": "root:root@tcp(localhost:3306)/test2?charset=utf8" } ], "Master": { "Driver": "mysql", "EnableQueryLog": false, "SetMaxOpenConns": 0, "SetMaxIdleConns": 0, "Prefix": "", "Dsn": "root:root@tcp(localhost:3306)/test?charset=utf8" } } ``` 理论上可以接收任何文件类型的配置, 只需要添加对应文件类型的解析器即可. 幸运的是, 我们可以轻松扩展文件解析器, 具体扩展方法, 我们可以查看第9节, 扩展开发