nacos config

This commit is contained in:
eson
2023-09-19 17:34:10 +08:00
parent 7fd65d9ac7
commit 4f37274438
75 changed files with 82 additions and 1141 deletions

View File

@@ -10,6 +10,7 @@ import (
"github.com/nacos-group/nacos-sdk-go/v2/clients"
"github.com/nacos-group/nacos-sdk-go/v2/common/constant"
"github.com/nacos-group/nacos-sdk-go/v2/vo"
"github.com/zeromicro/go-zero/core/conf"
"gopkg.in/yaml.v2"
)
@@ -63,7 +64,7 @@ func init() {
}
func StartNacosConfig(configFile string, OnChange func(namespace, group, dataId, data string)) string {
func StartNacosConfig(configFile string, cfg any, OnChange func(namespace, group, dataId, data string)) {
env := GetEnvCofing()
// 创建serverConfig
@@ -136,9 +137,40 @@ func StartNacosConfig(configFile string, OnChange func(namespace, group, dataId,
log.Fatalf("Failed to get config from Nacos: %v", err)
}
log.Println("加载成功:", cfgYaml)
var selfConfig map[string]interface{} = make(map[string]interface{})
err = yaml.Unmarshal([]byte(content), &selfConfig)
if err != nil {
log.Fatalf("Failed to get config from Nacos: %v", err)
}
return content
content, err = configClient.GetConfig(vo.ConfigParam{
DataId: "common",
Group: env.Group,
OnChange: nil,
})
if err != nil {
log.Fatalf("Failed to get config from Nacos: %v", err)
}
var commonConfig map[string]interface{} = make(map[string]interface{})
err = yaml.Unmarshal([]byte(content), &commonConfig)
if err != nil {
log.Fatalf("Failed to get config from Nacos: %v", err)
}
for k, v := range commonConfig {
selfConfig[k] = v
}
data, err := yaml.Marshal(selfConfig)
if err != nil {
log.Fatalf("Failed to get config from Nacos: %v", err)
}
log.Print("加载成功: ", cfgYaml, "\n环境: ", env.Group, "\n", string(data))
err = conf.LoadFromYamlBytes(data, cfg)
if err != nil {
log.Fatalf("Failed to get config content from Nacos: %v", err)
}
// log.Println(content)
}

View File

@@ -3,8 +3,33 @@ package fsconfig_test
import (
"fusenapi/utils/fsconfig"
"testing"
"github.com/zeromicro/go-zero/rest"
)
func TestCase1(t *testing.T) {
fsconfig.StartNacosConfig("auth.yaml", nil)
type Config struct {
rest.RestConf
SourceMysql string
ReplicaId uint64
MainAddress string
WebsocketAddr string
OAuth struct {
Google struct {
Appid string
Secret string
}
Facebook struct {
Appid string
Secret string
}
}
}
func TestCase1(t *testing.T) {
var c Config
fsconfig.StartNacosConfig("auth.yaml", &c, nil)
}