diff --git a/app/auth/conf/conf.go b/app/auth/conf/conf.go index a80ec4e..1f929e8 100644 --- a/app/auth/conf/conf.go +++ b/app/auth/conf/conf.go @@ -8,8 +8,6 @@ import ( "github.com/cloudwego/kitex/pkg/klog" "github.com/kr/pretty" - _ "github.com/spf13/viper" - _ "github.com/spf13/viper/remote" "gopkg.in/validator.v2" "gopkg.in/yaml.v2" ) diff --git a/app/user/biz/dal/mysql/init.go b/app/user/biz/dal/mysql/init.go index 3b05789..26ae516 100644 --- a/app/user/biz/dal/mysql/init.go +++ b/app/user/biz/dal/mysql/init.go @@ -1,11 +1,8 @@ package mysql import ( - "fmt" "github.com/joho/godotenv" "github.com/suyiiyii/hertz101/app/user/conf" - "os" - "gorm.io/driver/mysql" "gorm.io/gorm" ) @@ -20,8 +17,9 @@ func Init() { if err != nil { panic(err) } - dsn := fmt.Sprintf(conf.GetConf().MySQL.DSN, os.Getenv("MYSQL_USER"), os.Getenv("MYSQL_PASSWORD"), os.Getenv("MYSQL_HOST"), os.Getenv("MYSQL_PORT")) - DB, err = gorm.Open(mysql.Open(dsn), + //dsn := fmt.Sprintf(conf.GetConf().MySQL.DSN, os.Getenv("MYSQL_USER"), os.Getenv("MYSQL_PASSWORD"), os.Getenv("MYSQL_HOST"), os.Getenv("MYSQL_PORT")) + //DB, err = gorm.Open(mysql.Open(dsn), + DB, err = gorm.Open(mysql.Open(conf.GetConf().MySQL.DSN), &gorm.Config{ PrepareStmt: true, SkipDefaultTransaction: true, diff --git a/app/user/conf/conf.go b/app/user/conf/conf.go index 1f929e8..5640ca8 100644 --- a/app/user/conf/conf.go +++ b/app/user/conf/conf.go @@ -1,6 +1,7 @@ package conf import ( + "github.com/spf13/viper" "io/ioutil" "os" "path/filepath" @@ -8,6 +9,8 @@ import ( "github.com/cloudwego/kitex/pkg/klog" "github.com/kr/pretty" + _ "github.com/spf13/viper" + _ "github.com/spf13/viper/remote" "gopkg.in/validator.v2" "gopkg.in/yaml.v2" ) @@ -67,6 +70,20 @@ func initConf() { } conf = new(Config) err = yaml.Unmarshal(content, conf) + + // viper 获取远程配置测试 + err = viper.AddRemoteProvider("consul", conf.Registry.RegistryAddress[0], "USER") + if err != nil { + return + } + viper.SetConfigType("yaml") + err = viper.ReadRemoteConfig() + if err != nil { + return + } + + conf.MySQL.DSN = viper.GetString("MYSQL_DSN") + if err != nil { klog.Error("parse yaml error - %v", err) panic(err)