完成nacos和数据库等配置文件

This commit is contained in:
2022-05-25 18:43:49 +08:00
parent 9bb43f4484
commit af58c03c7c
9 changed files with 128 additions and 0 deletions

23
client/mysql.go Normal file
View File

@@ -0,0 +1,23 @@
package client
import (
"git.echol.cn/loser/logger/log"
"online_code/config"
"git.echol.cn/loser/logger"
"gorm.io/driver/mysql"
"gorm.io/gorm"
)
var MySQL *gorm.DB
func InitMySQLClient() {
// 创建连接对象
conn, err := gorm.Open(mysql.Open(config.Scd.MySQL.GetDSN()), &gorm.Config{Logger: logger.DefaultGormLogger()})
if err != nil {
log.Panic("初始化MySQL连接失败, 错误信息: %v", err)
} else {
log.Debug("MySQL连接成功")
}
MySQL = conn
}

26
client/redis.go Normal file
View File

@@ -0,0 +1,26 @@
package client
import (
"context"
"git.echol.cn/loser/logger/log"
"github.com/go-redis/redis/v8"
"online_code/config"
)
var Redis *redis.Client
func InitRedisClient() {
conf := config.Scd.Redis
// 初始化连接
conn := redis.NewClient(&redis.Options{
Addr: conf.GetDSN(),
Password: conf.Password,
DB: conf.Db,
})
if err := conn.Ping(context.Background()).Err(); err != nil {
log.Panicf("Redis连接初始化失败: %v", err)
} else {
log.Debug("Redis连接初始化成功")
}
Redis = conn
}