package main import ( "fmt" "github.com/nacos-group/nacos-sdk-go/clients" "github.com/nacos-group/nacos-sdk-go/common/constant" "github.com/nacos-group/nacos-sdk-go/vo" "github.com/spf13/viper" "log" "strings" ) func main() { //创建 ServerConfig serverConfigs := []constant.ServerConfig{ { IpAddr: "127.0.0.1", Port: 8848, }, } //创建 ClientConfig clientConfig := constant.ClientConfig{ NamespaceId: "36a76206-1dec-4fb7-aa7f-dc49314e7963", //we can create multiple clients with different namespaceId to support multiple namespace.When namespace is public, fill in the blank string here. TimeoutMs: 5000, NotLoadCacheAtStart: true, LogLevel: "debug", } //创建 Client nacosClient, err := clients.NewConfigClient( vo.NacosClientParam{ ClientConfig: &clientConfig, ServerConfigs: serverConfigs, }, ) if err != nil { log.Fatal("nacos初始化错误:", err) } //读取配置 content, err := nacosClient.GetConfig(vo.ConfigParam{DataId: "dev001", Group: "DEFAULT_GROUP"}) if err != nil { log.Fatalln("nacos读取配置错误:" + content) } //viper解析配置文件 var defaultConfig *viper.Viper defaultConfig = viper.New() defaultConfig.SetConfigType("toml") err = defaultConfig.ReadConfig(strings.NewReader(content)) if err != nil { log.Fatalln("viper解析配置失败:", err) } //获取解析出的配置文件的内容 port := defaultConfig.GetString("server.port") fmt.Println("the config port:", port) }