diff --git a/env.yaml b/env.yaml new file mode 100644 index 00000000..b61412d6 --- /dev/null +++ b/env.yaml @@ -0,0 +1,7 @@ +nacos: + - server.fusen.3718.cn:8848 + - server.fusen.3718.cn:8849 +username: ... +password: ... +namespace: fs_server_api_dev +group: FS-SERVER-API diff --git a/goctl_template/api/main.tpl b/goctl_template/api/main.tpl index f4a688cb..057d55c7 100644 --- a/goctl_template/api/main.tpl +++ b/goctl_template/api/main.tpl @@ -7,6 +7,7 @@ import ( "time" "fusenapi/utils/auth" + "fusenapi/utils/fsconfig" {{.importPackages}} ) @@ -16,8 +17,10 @@ var configFile = flag.String("f", "etc/{{.serviceName}}.yaml", "the config file" func main() { flag.Parse() + cfgContent := fsconfig.StartNacosConfig(configFile,nil) var c config.Config - conf.MustLoad(*configFile, &c) + conf.LoadConfigFromYamlBytes([]byte(cfgContent),&c) + c.Timeout = int64(time.Second * 15) server := rest.MustNewServer(c.RestConf, rest.WithCustomCors(auth.FsCors, func(w http.ResponseWriter) { })) diff --git a/goctl_template/gateway/main.tpl b/goctl_template/gateway/main.tpl index dafdd982..33c303b8 100644 --- a/goctl_template/gateway/main.tpl +++ b/goctl_template/gateway/main.tpl @@ -13,7 +13,7 @@ func main() { flag.Parse() var c gateway.GatewayConf - conf.MustLoad(*configFile, &c) + c.Timeout = int64(time.Second * 15) gw := gateway.MustNewServer(c) defer gw.Stop() diff --git a/goctl_template/rpc/main.tpl b/goctl_template/rpc/main.tpl index f095b259..13fbae92 100644 --- a/goctl_template/rpc/main.tpl +++ b/goctl_template/rpc/main.tpl @@ -18,8 +18,10 @@ var configFile = flag.String("f", "etc/{{.serviceName}}.yaml", "the config file" func main() { flag.Parse() + cfgContent := fsconfig.StartNacosConfig(configFile,nil) var c config.Config - conf.MustLoad(*configFile, &c) + conf.LoadConfigFromYamlBytes([]byte(cfgContent),&c) + c.Timeout = int64(time.Second * 15) ctx := svc.NewServiceContext(c) diff --git a/goctl_template_backend/api/main.tpl b/goctl_template_backend/api/main.tpl index 71e4fb99..6cc7a21c 100644 --- a/goctl_template_backend/api/main.tpl +++ b/goctl_template_backend/api/main.tpl @@ -7,6 +7,7 @@ import ( "time" "fusenapi/utils/auth" + "fusenapi/utils/fsconfig" {{.importPackages}} ) @@ -16,8 +17,10 @@ var configFile = flag.String("f", "etc/{{.serviceName}}.yaml", "the config file" func main() { flag.Parse() + cfgContent := fsconfig.StartNacosConfig(configFile,nil) var c config.Config - conf.MustLoad(*configFile, &c) + conf.LoadConfigFromYamlBytes([]byte(cfgContent),&c) + c.Timeout = int64(time.Second * 15) server := rest.MustNewServer(c.RestConf, rest.WithCustomCors(auth.FsCors, func(w http.ResponseWriter) { diff --git a/goctl_template_backend/gateway/main.tpl b/goctl_template_backend/gateway/main.tpl index dafdd982..33c303b8 100644 --- a/goctl_template_backend/gateway/main.tpl +++ b/goctl_template_backend/gateway/main.tpl @@ -13,7 +13,7 @@ func main() { flag.Parse() var c gateway.GatewayConf - conf.MustLoad(*configFile, &c) + c.Timeout = int64(time.Second * 15) gw := gateway.MustNewServer(c) defer gw.Stop() diff --git a/goctl_template_backend/rpc/main.tpl b/goctl_template_backend/rpc/main.tpl index f095b259..13fbae92 100644 --- a/goctl_template_backend/rpc/main.tpl +++ b/goctl_template_backend/rpc/main.tpl @@ -18,8 +18,10 @@ var configFile = flag.String("f", "etc/{{.serviceName}}.yaml", "the config file" func main() { flag.Parse() + cfgContent := fsconfig.StartNacosConfig(configFile,nil) var c config.Config - conf.MustLoad(*configFile, &c) + conf.LoadConfigFromYamlBytes([]byte(cfgContent),&c) + c.Timeout = int64(time.Second * 15) ctx := svc.NewServiceContext(c) diff --git a/server/auth/auth.go b/server/auth/auth.go index 34623035..1c1278f1 100644 --- a/server/auth/auth.go +++ b/server/auth/auth.go @@ -6,6 +6,7 @@ import ( "net/http" "fusenapi/utils/auth" + "fusenapi/utils/fsconfig" "fusenapi/server/auth/internal/config" "fusenapi/server/auth/internal/handler" @@ -20,8 +21,10 @@ var configFile = flag.String("f", "etc/auth.yaml", "the config file") func main() { flag.Parse() + cfgContent := fsconfig.StartNacosConfig(*configFile, nil) var c config.Config - conf.MustLoad(*configFile, &c) + conf.LoadConfigFromYamlBytes([]byte(cfgContent), &c) + server := rest.MustNewServer(c.RestConf, rest.WithCustomCors(auth.FsCors, func(w http.ResponseWriter) { })) defer server.Stop() diff --git a/server/base/base.go b/server/base/base.go index 1d3f1af1..7db23d4a 100644 --- a/server/base/base.go +++ b/server/base/base.go @@ -6,6 +6,7 @@ import ( "net/http" "fusenapi/utils/auth" + "fusenapi/utils/fsconfig" "fusenapi/server/base/internal/config" "fusenapi/server/base/internal/handler" @@ -20,8 +21,9 @@ var configFile = flag.String("f", "etc/base.yaml", "the config file") func main() { flag.Parse() + cfgContent := fsconfig.StartNacosConfig(*configFile, nil) var c config.Config - conf.MustLoad(*configFile, &c) + conf.LoadConfigFromYamlBytes([]byte(cfgContent), &c) server := rest.MustNewServer(c.RestConf, rest.WithCustomCors(auth.FsCors, func(w http.ResponseWriter) { })) diff --git a/server/canteen/canteen.go b/server/canteen/canteen.go index eb2e1f27..2261a4ad 100644 --- a/server/canteen/canteen.go +++ b/server/canteen/canteen.go @@ -9,6 +9,7 @@ import ( "fusenapi/server/canteen/internal/handler" "fusenapi/server/canteen/internal/svc" "fusenapi/utils/auth" + "fusenapi/utils/fsconfig" "github.com/zeromicro/go-zero/core/conf" "github.com/zeromicro/go-zero/rest" @@ -19,9 +20,9 @@ var configFile = flag.String("f", "etc/canteen.yaml", "the config file") func main() { flag.Parse() + cfgContent := fsconfig.StartNacosConfig(*configFile, nil) var c config.Config - - conf.MustLoad(*configFile, &c) + conf.LoadConfigFromYamlBytes([]byte(cfgContent), &c) server := rest.MustNewServer(c.RestConf, rest.WithCustomCors(auth.FsCors, func(w http.ResponseWriter) { diff --git a/server/data-transfer/data-transfer.go b/server/data-transfer/data-transfer.go index 8ad3fa67..db2a4230 100644 --- a/server/data-transfer/data-transfer.go +++ b/server/data-transfer/data-transfer.go @@ -9,7 +9,6 @@ import ( "fusenapi/utils/auth" "net/http" - "github.com/zeromicro/go-zero/core/conf" "github.com/zeromicro/go-zero/rest" ) @@ -19,7 +18,6 @@ func main() { flag.Parse() var c config2.Config - conf.MustLoad(*configFile, &c) server := rest.MustNewServer(c.RestConf, rest.WithCustomCors(auth.FsCors, func(w http.ResponseWriter) { diff --git a/server/home-user-auth/home-user-auth.go b/server/home-user-auth/home-user-auth.go index 911ae5de..16c3a315 100644 --- a/server/home-user-auth/home-user-auth.go +++ b/server/home-user-auth/home-user-auth.go @@ -9,6 +9,7 @@ import ( "fusenapi/server/home-user-auth/internal/handler" "fusenapi/server/home-user-auth/internal/svc" "fusenapi/utils/auth" + "fusenapi/utils/fsconfig" "github.com/zeromicro/go-zero/core/conf" "github.com/zeromicro/go-zero/rest" @@ -19,9 +20,9 @@ var configFile = flag.String("f", "etc/home-user-auth.yaml", "the config file") func main() { flag.Parse() + cfgContent := fsconfig.StartNacosConfig(*configFile, nil) var c config.Config - - conf.MustLoad(*configFile, &c) + conf.LoadConfigFromYamlBytes([]byte(cfgContent), &c) server := rest.MustNewServer(c.RestConf, rest.WithCustomCors(auth.FsCors, func(w http.ResponseWriter) { diff --git a/server/info/info.go b/server/info/info.go index 4571550d..613b74a1 100644 --- a/server/info/info.go +++ b/server/info/info.go @@ -7,6 +7,7 @@ import ( "time" "fusenapi/utils/auth" + "fusenapi/utils/fsconfig" "fusenapi/server/info/internal/config" "fusenapi/server/info/internal/handler" @@ -21,8 +22,10 @@ var configFile = flag.String("f", "etc/info.yaml", "the config file") func main() { flag.Parse() + cfgContent := fsconfig.StartNacosConfig(*configFile, nil) var c config.Config - conf.MustLoad(*configFile, &c) + conf.LoadConfigFromYamlBytes([]byte(cfgContent), &c) + c.Timeout = int64(time.Second * 15) server := rest.MustNewServer(c.RestConf, rest.WithCustomCors(auth.FsCors, func(w http.ResponseWriter) { })) diff --git a/server/map-library/map-library.go b/server/map-library/map-library.go index b8f7e38b..a7ac3fd3 100644 --- a/server/map-library/map-library.go +++ b/server/map-library/map-library.go @@ -9,6 +9,7 @@ import ( "fusenapi/server/map-library/internal/handler" "fusenapi/server/map-library/internal/svc" "fusenapi/utils/auth" + "fusenapi/utils/fsconfig" "github.com/zeromicro/go-zero/core/conf" "github.com/zeromicro/go-zero/rest" @@ -19,8 +20,9 @@ var configFile = flag.String("f", "etc/map-library.yaml", "the config file") func main() { flag.Parse() + cfgContent := fsconfig.StartNacosConfig(*configFile, nil) var c config.Config - conf.MustLoad(*configFile, &c) + conf.LoadConfigFromYamlBytes([]byte(cfgContent), &c) server := rest.MustNewServer(c.RestConf, rest.WithCustomCors(auth.FsCors, func(w http.ResponseWriter) { diff --git a/server/pay/pay.go b/server/pay/pay.go index ecfedfef..5b57352c 100644 --- a/server/pay/pay.go +++ b/server/pay/pay.go @@ -6,6 +6,7 @@ import ( "net/http" "fusenapi/utils/auth" + "fusenapi/utils/fsconfig" "fusenapi/server/pay/internal/config" "fusenapi/server/pay/internal/handler" @@ -20,8 +21,10 @@ var configFile = flag.String("f", "etc/pay.yaml", "the config file") func main() { flag.Parse() + cfgContent := fsconfig.StartNacosConfig(*configFile, nil) var c config.Config - conf.MustLoad(*configFile, &c) + conf.LoadConfigFromYamlBytes([]byte(cfgContent), &c) + server := rest.MustNewServer(c.RestConf, rest.WithCustomCors(auth.FsCors, func(w http.ResponseWriter) { })) defer server.Stop() diff --git a/server/product-model/product-model.go b/server/product-model/product-model.go index a6374de1..0d71e15a 100644 --- a/server/product-model/product-model.go +++ b/server/product-model/product-model.go @@ -9,6 +9,7 @@ import ( "fusenapi/server/product-model/internal/handler" "fusenapi/server/product-model/internal/svc" "fusenapi/utils/auth" + "fusenapi/utils/fsconfig" "github.com/zeromicro/go-zero/core/conf" "github.com/zeromicro/go-zero/rest" @@ -19,8 +20,9 @@ var configFile = flag.String("f", "etc/product-model.yaml", "the config file") func main() { flag.Parse() + cfgContent := fsconfig.StartNacosConfig(*configFile, nil) var c config.Config - conf.MustLoad(*configFile, &c) + conf.LoadConfigFromYamlBytes([]byte(cfgContent), &c) server := rest.MustNewServer(c.RestConf, rest.WithCustomCors(auth.FsCors, func(w http.ResponseWriter) { diff --git a/server/product-template-tag/product-template-tag.go b/server/product-template-tag/product-template-tag.go index 5f1bd6cd..fa892576 100644 --- a/server/product-template-tag/product-template-tag.go +++ b/server/product-template-tag/product-template-tag.go @@ -6,6 +6,7 @@ import ( "net/http" "fusenapi/utils/auth" + "fusenapi/utils/fsconfig" "fusenapi/server/product-template-tag/internal/config" "fusenapi/server/product-template-tag/internal/handler" @@ -20,8 +21,10 @@ var configFile = flag.String("f", "etc/product-template-tag.yaml", "the config f func main() { flag.Parse() + cfgContent := fsconfig.StartNacosConfig(*configFile, nil) var c config.Config - conf.MustLoad(*configFile, &c) + conf.LoadConfigFromYamlBytes([]byte(cfgContent), &c) + server := rest.MustNewServer(c.RestConf, rest.WithCustomCors(auth.FsCors, func(w http.ResponseWriter) { })) defer server.Stop() diff --git a/server/product-template/product-template.go b/server/product-template/product-template.go index 3819752a..260b7c56 100644 --- a/server/product-template/product-template.go +++ b/server/product-template/product-template.go @@ -9,6 +9,7 @@ import ( "fusenapi/server/product-template/internal/handler" "fusenapi/server/product-template/internal/svc" "fusenapi/utils/auth" + "fusenapi/utils/fsconfig" "github.com/zeromicro/go-zero/core/conf" "github.com/zeromicro/go-zero/rest" @@ -19,8 +20,9 @@ var configFile = flag.String("f", "etc/product-template.yaml", "the config file" func main() { flag.Parse() + cfgContent := fsconfig.StartNacosConfig(*configFile, nil) var c config.Config - conf.MustLoad(*configFile, &c) + conf.LoadConfigFromYamlBytes([]byte(cfgContent), &c) server := rest.MustNewServer(c.RestConf, rest.WithCustomCors(auth.FsCors, func(w http.ResponseWriter) { diff --git a/server/product/product.go b/server/product/product.go index 13fba403..adc4c782 100644 --- a/server/product/product.go +++ b/server/product/product.go @@ -9,6 +9,7 @@ import ( "fusenapi/server/product/internal/handler" "fusenapi/server/product/internal/svc" "fusenapi/utils/auth" + "fusenapi/utils/fsconfig" "github.com/zeromicro/go-zero/core/conf" "github.com/zeromicro/go-zero/rest" @@ -19,8 +20,9 @@ var configFile = flag.String("f", "etc/product.yaml", "the config file") func main() { flag.Parse() + cfgContent := fsconfig.StartNacosConfig(*configFile, nil) var c config.Config - conf.MustLoad(*configFile, &c) + conf.LoadConfigFromYamlBytes([]byte(cfgContent), &c) server := rest.MustNewServer(c.RestConf, rest.WithCustomCors(auth.FsCors, func(w http.ResponseWriter) { diff --git a/server/resource/resource.go b/server/resource/resource.go index 29887332..e40a9db7 100644 --- a/server/resource/resource.go +++ b/server/resource/resource.go @@ -6,6 +6,7 @@ import ( "net/http" "fusenapi/utils/auth" + "fusenapi/utils/fsconfig" "fusenapi/server/resource/internal/config" "fusenapi/server/resource/internal/handler" @@ -20,8 +21,10 @@ var configFile = flag.String("f", "etc/resource.yaml", "the config file") func main() { flag.Parse() + cfgContent := fsconfig.StartNacosConfig(*configFile, nil) var c config.Config - conf.MustLoad(*configFile, &c) + conf.LoadConfigFromYamlBytes([]byte(cfgContent), &c) + server := rest.MustNewServer(c.RestConf, rest.WithCustomCors(auth.FsCors, func(w http.ResponseWriter) { })) defer server.Stop() diff --git a/server/shopping-cart/shopping-cart.go b/server/shopping-cart/shopping-cart.go index f7a8cb3f..bf9dabc1 100644 --- a/server/shopping-cart/shopping-cart.go +++ b/server/shopping-cart/shopping-cart.go @@ -7,6 +7,7 @@ import ( "time" "fusenapi/utils/auth" + "fusenapi/utils/fsconfig" "fusenapi/server/shopping-cart/internal/config" "fusenapi/server/shopping-cart/internal/handler" @@ -21,8 +22,10 @@ var configFile = flag.String("f", "etc/shopping-cart.yaml", "the config file") func main() { flag.Parse() + cfgContent := fsconfig.StartNacosConfig(*configFile, nil) var c config.Config - conf.MustLoad(*configFile, &c) + conf.LoadConfigFromYamlBytes([]byte(cfgContent), &c) + c.Timeout = int64(time.Second * 15) server := rest.MustNewServer(c.RestConf, rest.WithCustomCors(auth.FsCors, func(w http.ResponseWriter) { })) diff --git a/server/upload/upload.go b/server/upload/upload.go index bea4af45..42a589d6 100644 --- a/server/upload/upload.go +++ b/server/upload/upload.go @@ -9,6 +9,7 @@ import ( "fusenapi/server/upload/internal/handler" "fusenapi/server/upload/internal/svc" "fusenapi/utils/auth" + "fusenapi/utils/fsconfig" "github.com/zeromicro/go-zero/core/conf" "github.com/zeromicro/go-zero/rest" @@ -19,8 +20,9 @@ var configFile = flag.String("f", "etc/upload.yaml", "the config file") func main() { flag.Parse() + cfgContent := fsconfig.StartNacosConfig(*configFile, nil) var c config.Config - conf.MustLoad(*configFile, &c) + conf.LoadConfigFromYamlBytes([]byte(cfgContent), &c) server := rest.MustNewServer(c.RestConf, rest.WithCustomCors(auth.FsCors, func(w http.ResponseWriter) { diff --git a/server/webset/webset.go b/server/webset/webset.go index ab154b3b..be0cfcd0 100644 --- a/server/webset/webset.go +++ b/server/webset/webset.go @@ -9,6 +9,7 @@ import ( "fusenapi/server/webset/internal/handler" "fusenapi/server/webset/internal/svc" "fusenapi/utils/auth" + "fusenapi/utils/fsconfig" "github.com/zeromicro/go-zero/core/conf" "github.com/zeromicro/go-zero/rest" @@ -19,8 +20,9 @@ var configFile = flag.String("f", "etc/webset.yaml", "the config file") func main() { flag.Parse() + cfgContent := fsconfig.StartNacosConfig(*configFile, nil) var c config.Config - conf.MustLoad(*configFile, &c) + conf.LoadConfigFromYamlBytes([]byte(cfgContent), &c) server := rest.MustNewServer(c.RestConf, rest.WithCustomCors(auth.FsCors, func(w http.ResponseWriter) { diff --git a/server/websocket/websocket.go b/server/websocket/websocket.go index d77e9242..6dbc99a2 100644 --- a/server/websocket/websocket.go +++ b/server/websocket/websocket.go @@ -8,6 +8,7 @@ import ( "net/http" "fusenapi/utils/auth" + "fusenapi/utils/fsconfig" "fusenapi/server/websocket/internal/config" "fusenapi/server/websocket/internal/handler" @@ -22,8 +23,10 @@ var configFile = flag.String("f", "etc/websocket.yaml", "the config file") func main() { flag.Parse() + cfgContent := fsconfig.StartNacosConfig(*configFile, nil) var c config.Config - conf.MustLoad(*configFile, &c) + conf.LoadConfigFromYamlBytes([]byte(cfgContent), &c) + server := rest.MustNewServer(c.RestConf, rest.WithCustomCors(auth.FsCors, func(w http.ResponseWriter) { })) defer server.Stop() diff --git a/utils/autoconfig/autoconfig.go b/utils/autoconfig/autoconfig.go index 72442729..97fc2c05 100644 --- a/utils/autoconfig/autoconfig.go +++ b/utils/autoconfig/autoconfig.go @@ -74,7 +74,7 @@ func AutoGetEtcYaml() *string { dirs = dirs[0 : len(dirs)-1] // 列出所有 curPath 下的文件夹 - files, err := ioutil.ReadDir(curPath) + files, err := os.ReadDir(curPath) if err != nil { log.Println(err) continue diff --git a/utils/autoconfig/autoconfig_test.go b/utils/autoconfig/autoconfig_test.go index 7f118a8a..0a72aab0 100644 --- a/utils/autoconfig/autoconfig_test.go +++ b/utils/autoconfig/autoconfig_test.go @@ -1,7 +1,10 @@ package autoconfig -import "testing" +import ( + "log" + "testing" +) func TestAutoConfig(t *testing.T) { - AutoGetEtcYaml() + log.Println(*AutoGetEtcYaml()) } diff --git a/utils/collect/collect.go b/utils/collect/collect.go index b0502d61..c3630e5c 100644 --- a/utils/collect/collect.go +++ b/utils/collect/collect.go @@ -119,7 +119,7 @@ func Array2MapByKey[KEY comparable, VALUE any](arrSrc []VALUE, fieldName string) } fv := srcv.FieldByName(fieldName) k := fv.Interface().(KEY) - result[k] = srcv.Interface().(VALUE) + result[k] = arr.Index(i).Interface().(VALUE) } return result @@ -166,7 +166,7 @@ func Array2MapByKeyTag[KEY comparable, VALUE any](arrSrc []VALUE, tag string) (r fv = fv.Elem() } k := fv.Interface().(KEY) - result[k] = srcv.Interface().(VALUE) + result[k] = arr.Index(i).Interface().(VALUE) } return diff --git a/utils/fsconfig/config.go b/utils/fsconfig/config.go index 116043dc..66f5841e 100644 --- a/utils/fsconfig/config.go +++ b/utils/fsconfig/config.go @@ -1,53 +1,142 @@ package fsconfig import ( - "fmt" "log" "os" + "path/filepath" + "strconv" + "strings" + "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" "gopkg.in/yaml.v2" ) type EnvConfig struct { - Host string `yaml:"host"` - Port uint64 `yaml:"port"` - - UserName string `yaml:"username"` - Password string `yaml:"password"` - NamespaceId string `yaml:"namespace"` - DataId string `yaml:"dataid"` - Group string `yaml:"group"` + NacosServers []string `yaml:"nacos"` + UserName string `yaml:"username"` + Password string `yaml:"password"` + NamespaceId string `yaml:"namespace"` + DataId string `yaml:"dataid"` + Group string `yaml:"group"` } -var OptPathDir = "/opt" +var optPathDirs = []string{"/opt", "./", "../", "../../"} var nacosConfig *EnvConfig func GetEnvCofing() *EnvConfig { - return nacosConfig + + if nacosConfig != nil { + return nacosConfig + } + + for _, optDir := range optPathDirs { + if optDir[len(optDir)-1] != '/' { + optDir = optDir + "/" + } + + for _, yname := range []string{"env.yaml", "env.yml"} { + f, err := os.Open(optDir + yname) + if err != nil { + // log.Println(err) + continue + } + cfg := &EnvConfig{} + err = yaml.NewDecoder(f).Decode(&cfg) + if err != nil { + // log.Println(err) + continue + } + + nacosConfig = cfg + return nacosConfig + } + } + + panic("Can't find env.yaml or env.yml in the specified directories") + } func init() { - if OptPathDir[len(OptPathDir)-1] != '/' { - OptPathDir = OptPathDir + "/" - } - for _, yname := range []string{"env.yaml", "env.yml"} { - f, err := os.Open(OptPathDir + "/" + yname) - if err != nil { - log.Println(err) - continue - } - cfg := &EnvConfig{} - err = yaml.NewDecoder(f).Decode(&cfg) - if err != nil { - log.Println(err) - continue - } - - nacosConfig = cfg - return - } - - panic(fmt.Sprintf("can't find %s(env.yaml|env.yml) ", OptPathDir)) +} + +func StartNacosConfig(configFile string, OnChange func(namespace, group, dataId, data string)) string { + env := GetEnvCofing() + + // 创建serverConfig + // 支持多个;至少一个ServerConfig + var serverConfig []constant.ServerConfig + + for _, s := range env.NacosServers { + + sp := strings.Split(s, ":") + host := sp[0] + port, err := strconv.ParseUint(sp[1], 10, 64) + if err != nil { + panic(err) + } + + serverConfig = append(serverConfig, constant.ServerConfig{ + IpAddr: host, + Port: port, + }) + } + + // 创建clientConfig + clientConfig := constant.ClientConfig{ + NamespaceId: env.NamespaceId, // 如果需要支持多namespace,我们可以场景多个client,它们有不同的NamespaceId。当namespace是public时,此处填空字符串。 + TimeoutMs: 50000, + NotLoadCacheAtStart: true, + LogLevel: "debug", + LogDir: "/tmp/nacos/log", + CacheDir: "/tmp/nacos/cache", + Username: env.UserName, + Password: env.Password, + } + + // 创建服务发现客户端的另一种方式 (推荐) + // namingClient, err := clients.NewNamingClient( + // vo.NacosClientParam{ + // ClientConfig: &clientConfig, + // ServerConfigs: serverConfig, + // }, + // ) + // if err != nil { + // log.Fatalf("初始化nacos失败: %s", err.Error()) + // } + + // log.Println(namingClient) + + // 创建 Nacos 配置客户端 + configClient, err := clients.CreateConfigClient(map[string]interface{}{ + "clientConfig": clientConfig, + "serverConfigs": serverConfig, + }) + if err != nil { + log.Fatalf("Failed to create Nacos config client: %v", err) + } + + cfgYamls := strings.Split(configFile, "/") + cfgYaml := cfgYamls[len(cfgYamls)-1] + yamlExt := filepath.Ext(cfgYaml) + if !(yamlExt == ".yaml" || yamlExt == ".yml") { + log.Panic(configFile) + } + + // 获取配置 + content, err := configClient.GetConfig(vo.ConfigParam{ + DataId: cfgYaml, + Group: env.Group, + OnChange: OnChange, + }) + if err != nil { + log.Fatalf("Failed to get config from Nacos: %v", err) + } + + return content + + // log.Println(content) } diff --git a/utils/fsconfig/config_test.go b/utils/fsconfig/config_test.go index f285c6bb..8ffaaffb 100644 --- a/utils/fsconfig/config_test.go +++ b/utils/fsconfig/config_test.go @@ -2,69 +2,9 @@ package fsconfig_test import ( "fusenapi/utils/fsconfig" - "log" "testing" - - "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" ) func TestCase1(t *testing.T) { - - env := fsconfig.GetEnvCofing() - - // 创建serverConfig - // 支持多个;至少一个ServerConfig - serverConfig := []constant.ServerConfig{ - { - IpAddr: env.Host, - Port: uint64(env.Port), - }, - } - - // 创建clientConfig - clientConfig := constant.ClientConfig{ - NamespaceId: env.NamespaceId, // 如果需要支持多namespace,我们可以场景多个client,它们有不同的NamespaceId。当namespace是public时,此处填空字符串。 - TimeoutMs: 50000, - NotLoadCacheAtStart: true, - LogLevel: "debug", - LogDir: "/tmp/nacos", - CacheDir: "/tmp/nacos", - Username: env.UserName, - Password: env.Password, - } - - // 创建服务发现客户端的另一种方式 (推荐) - // namingClient, err := clients.NewNamingClient( - // vo.NacosClientParam{ - // ClientConfig: &clientConfig, - // ServerConfigs: serverConfig, - // }, - // ) - // if err != nil { - // log.Fatalf("初始化nacos失败: %s", err.Error()) - // } - - // log.Println(namingClient) - - // 创建 Nacos 配置客户端 - configClient, err := clients.CreateConfigClient(map[string]interface{}{ - "clientConfig": clientConfig, - "serverConfigs": serverConfig, - }) - if err != nil { - log.Fatalf("Failed to create Nacos config client: %v", err) - } - - // 获取配置 - content, err := configClient.GetConfig(vo.ConfigParam{ - DataId: "auth.yaml", - Group: env.Group, - }) - if err != nil { - log.Fatalf("Failed to get config from Nacos: %v", err) - } - log.Println(content) - + fsconfig.StartNacosConfig("auth.yaml", nil) }