mirror of
https://gitee.com/xiangheng/x_admin.git
synced 2026-04-22 22:57:15 +08:00
优化基础配置
This commit is contained in:
@@ -9,7 +9,7 @@ APP:
|
||||
|
||||
# 数据库
|
||||
DB:
|
||||
DSN: 'root:root@tcp(localhost:3306)/x_admin?charset=utf8mb4&parseTime=True&loc=Local'
|
||||
DSN: 'root:123456789@tcp(localhost:3306)/x_admin_2?charset=utf8mb4&parseTime=True&loc=Local'
|
||||
|
||||
#Redis
|
||||
REDIS:
|
||||
|
||||
@@ -53,6 +53,7 @@ __debug_bin
|
||||
# custom
|
||||
# env
|
||||
.env
|
||||
.env.yaml
|
||||
# binary
|
||||
main
|
||||
# air
|
||||
|
||||
@@ -29,7 +29,7 @@ func loadConfig(config config) config {
|
||||
if envFilePath == "" {
|
||||
envFilePath = ".env"
|
||||
}
|
||||
viper.SetConfigType("yaml")
|
||||
// viper.SetConfigType("yaml")
|
||||
viper.AddConfigPath(".")
|
||||
viper.SetConfigFile(envFilePath)
|
||||
viper.AutomaticEnv()
|
||||
|
||||
+14
-15
@@ -1,26 +1,25 @@
|
||||
package config
|
||||
|
||||
import "time"
|
||||
|
||||
type dbConfig struct {
|
||||
Type string `mapstructure:"TYPE"` // 数据库类型:MySQL, PostgreSQL, GaussDB, SQLite, SQLServer, TiDB
|
||||
Dsn string `mapstructure:"DSN"` // 数据库
|
||||
MaxOpenConns int `mapstructure:"MAX_OPEN_CONNS"` // 数据库连接池最大值
|
||||
MaxIdleConns int `mapstructure:"MAX_IDLE_CONNS"` // 数据库空闲连接池最大值
|
||||
ConnMaxLifetimeSeconds int `mapstructure:"CONN_MAX_LIFETIME_SECONDS"` // 连接可复用的最大时间(秒:默认28800秒)
|
||||
TablePrefix string `mapstructure:"TABLE_PREFIX"` // 数据库表前缀
|
||||
SlowThreshold time.Duration `mapstructure:"SLOW_THRESHOLD"` // 数据库慢查询阈值(秒:默认1秒)
|
||||
LogLevel string `mapstructure:"LOG_LEVEL"` // 数据库日志级别
|
||||
DefaultStringSize uint `mapstructure:"DEFAULT_STRING_SIZE"` // 数据库string类型字段的默认长度:256
|
||||
Type string `mapstructure:"TYPE"` // 数据库类型:MySQL, PostgreSQL, GaussDB, SQLite, SQLServer, TiDB
|
||||
Dsn string `mapstructure:"DSN"` // 数据库
|
||||
MaxOpenConns int `mapstructure:"MAX_OPEN_CONNS"` // 数据库连接池最大值
|
||||
MaxIdleConns int `mapstructure:"MAX_IDLE_CONNS"` // 数据库空闲连接池最大值
|
||||
ConnMaxLifetimeSeconds int `mapstructure:"CONN_MAX_LIFETIME_SECONDS"` // 连接可复用的最大时间(秒:默认28800秒)
|
||||
TablePrefix string `mapstructure:"TABLE_PREFIX"` // 数据库表前缀
|
||||
SlowThreshold int `mapstructure:"SLOW_THRESHOLD"` // 数据库慢查询阈值(秒:默认1秒)
|
||||
LogLevel string `mapstructure:"LOG_LEVEL"` // 数据库日志级别
|
||||
DefaultStringSize uint `mapstructure:"DEFAULT_STRING_SIZE"` // 数据库string类型字段的默认长度:256
|
||||
}
|
||||
|
||||
var DBConfig = dbConfig{
|
||||
Type: "mysql",
|
||||
Type: "MySQL",
|
||||
Dsn: "", //root:123456@tcp(127.0.0.1:3306)/x_admin?charset=utf8mb4&parseTime=True&loc=Local
|
||||
MaxOpenConns: 100,
|
||||
MaxIdleConns: 10,
|
||||
ConnMaxLifetimeSeconds: 60 * 60 * 24,
|
||||
MaxIdleConns: 50,
|
||||
ConnMaxLifetimeSeconds: 6,
|
||||
TablePrefix: "x_",
|
||||
SlowThreshold: 1 * time.Second,
|
||||
SlowThreshold: 1,
|
||||
LogLevel: "info",
|
||||
DefaultStringSize: 256,
|
||||
}
|
||||
|
||||
@@ -1,20 +1,18 @@
|
||||
package config
|
||||
|
||||
import "time"
|
||||
|
||||
type redisConfig struct {
|
||||
Url string `mapstructure:"URL"` // Redis源配置: redis://:@127.0.0.1:6379/0
|
||||
PoolSize int `mapstructure:"POOL_SIZE"` // Redis连接池大小
|
||||
MaxIdleConns int `mapstructure:"MAX_IDLE_CONNS"` // Redis空闲连接池最大值
|
||||
ConnMaxLifetime time.Duration `mapstructure:"CONN_MAX_LIFETIME"` // Redis连接可复用的最大时间(秒:默认60秒)
|
||||
RedisPrefix string `mapstructure:"REDIS_PREFIX"` // Redis键前缀: x:
|
||||
Url string `mapstructure:"URL"` // Redis源配置: redis://:@127.0.0.1:6379/0
|
||||
PoolSize int `mapstructure:"POOL_SIZE"` // Redis连接池大小
|
||||
MaxIdleConns int `mapstructure:"MAX_IDLE_CONNS"` // Redis空闲连接池最大值
|
||||
ConnMaxLifetime int `mapstructure:"CONN_MAX_LIFETIME"` // Redis连接可复用的最大时间(秒:默认60秒)
|
||||
RedisPrefix string `mapstructure:"REDIS_PREFIX"` // Redis键前缀: x:
|
||||
}
|
||||
|
||||
var RedisConfig = redisConfig{
|
||||
Url: "redis://:@127.0.0.1:6379/0",
|
||||
PoolSize: 100,
|
||||
MaxIdleConns: 10,
|
||||
ConnMaxLifetime: 60 * time.Second,
|
||||
ConnMaxLifetime: 60,
|
||||
// 默认x:
|
||||
RedisPrefix: "x:",
|
||||
}
|
||||
|
||||
+12
-2
@@ -20,6 +20,7 @@ func GetDB() *gorm.DB {
|
||||
|
||||
// initMysql 初始化mysql会话
|
||||
func initMysql() *gorm.DB {
|
||||
// fmt.Printf("%#v\n", config.DBConfig)
|
||||
// 日志配置
|
||||
slowThreshold := time.Second
|
||||
ignoreRecordNotFoundError := true
|
||||
@@ -29,7 +30,7 @@ func initMysql() *gorm.DB {
|
||||
ignoreRecordNotFoundError = false
|
||||
}
|
||||
if config.DBConfig.SlowThreshold > 0 {
|
||||
slowThreshold = config.DBConfig.SlowThreshold
|
||||
slowThreshold = time.Duration(config.DBConfig.SlowThreshold) * time.Second
|
||||
}
|
||||
|
||||
logger := logger.New(
|
||||
@@ -59,7 +60,7 @@ func initMysql() *gorm.DB {
|
||||
log.Fatal("initMysql gorm.Open err:", err)
|
||||
}
|
||||
db.InstanceSet("gorm:table_options", "ENGINE=InnoDB")
|
||||
sqlDB, err := db.DB()
|
||||
sqlDB, err := db.DB() //通用的数据库接口 *sql.DB
|
||||
if err != nil {
|
||||
log.Fatal("initMysql db.DB err:", err)
|
||||
}
|
||||
@@ -69,6 +70,15 @@ func initMysql() *gorm.DB {
|
||||
sqlDB.SetMaxOpenConns(config.DBConfig.MaxOpenConns)
|
||||
// 连接可复用的最大时间
|
||||
sqlDB.SetConnMaxLifetime(time.Duration(config.DBConfig.ConnMaxLifetimeSeconds) * time.Second)
|
||||
// 定时打印DBStats
|
||||
// go func() {
|
||||
// for {
|
||||
// time.Sleep(time.Second * 1)
|
||||
// stats := sqlDB.Stats()
|
||||
// log.Printf("DBStats: OpenConnections =%d, 正在使用InUse=%d, 空闲连接数Idle=%d, 等待的连接总数WaitCount=%d, 阻塞等待新连接的总时间WaitDuration=%s,MaxIdleTimeClosed=%d,MaxLifetimeClosed=%d \n",
|
||||
// stats.OpenConnections, stats.InUse, stats.Idle, stats.WaitCount, stats.WaitDuration, stats.MaxIdleTimeClosed, stats.MaxLifetimeClosed)
|
||||
// }
|
||||
// }()
|
||||
return db
|
||||
}
|
||||
|
||||
|
||||
@@ -19,7 +19,7 @@ func initRedis() *redis.Client {
|
||||
}
|
||||
// opt.PoolSize = config.Config.RedisPoolSize
|
||||
opt.MaxIdleConns = config.RedisConfig.MaxIdleConns
|
||||
opt.ConnMaxLifetime = config.RedisConfig.ConnMaxLifetime
|
||||
opt.ConnMaxLifetime = time.Duration(config.RedisConfig.ConnMaxLifetime) * time.Second
|
||||
|
||||
client := redis.NewClient(opt)
|
||||
ctx, cancel := context.WithTimeout(context.Background(), 3*time.Second)
|
||||
|
||||
+1
-1
@@ -77,7 +77,7 @@ func initIpUtil() *ipUtil {
|
||||
fmt.Printf("failed to create searcher: %s\n", err.Error())
|
||||
return &ip_util
|
||||
}
|
||||
fmt.Printf("创建完全基于内存的查询对象。")
|
||||
// fmt.Printf("创建完全基于内存的查询对象。")
|
||||
ip_util.Searcher = searcher
|
||||
return &ip_util
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user