# go-MI-System **Repository Path**: wang-wei02/go-mi-system ## Basic Information - **Project Name**: go-MI-System - **Description**: No description available - **Primary Language**: Go - **License**: MulanPSL-2.0 - **Default Branch**: master - **Homepage**: None - **GVP Project**: No ## Statistics - **Stars**: 1 - **Forks**: 1 - **Created**: 2024-05-08 - **Last Updated**: 2024-09-02 ## Categories & Tags **Categories**: Uncategorized **Tags**: None ## README package main import ( "fmt" "gopkg.in/yaml.v3" "log" "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" ) func main() { ch := make(chan int) ch <- 1 } type AppConfig struct { Name string `yaml:"name"` Version string `yaml:"version"` } type Config struct { App AppConfig `yaml:"app"` } var GlobalConfig Config func init() { sc := []constant.ServerConfig{{ IpAddr: "192.168.40.180", Port: 8848, }} cc := constant.ClientConfig{ NamespaceId: "c0c576b7-1d60-48e9-9b2f-8d440d68c19b", // 如果需要支持多namespace,我们可以场景多个client,它们有不同的NamespaceId。当namespace是public时,此处填空字符串。 TimeoutMs: 5000, NotLoadCacheAtStart: true, LogDir: "log", CacheDir: "cache", LogLevel: "debug", Username: "nacos", Password: "nacos", } configClient, err := clients.CreateConfigClient(map[string]interface{}{ "serverConfigs": sc, "clientConfig": cc, }) if err != nil { fmt.Println(err.Error()) } fmt.Println("开始获取配置...") content, err := configClient.GetConfig(vo.ConfigParam{ DataId: "myv1.yaml", Group: "DEFAULT_GROUP", }) if err != nil { log.Fatal(err.Error()) } log.Println("获取的配置是:\n", content) err = yaml.Unmarshal([]byte(content), &GlobalConfig) if err != nil { log.Fatal(err.Error()) } fmt.Printf("解析后的配置结构体: %+v\n", GlobalConfig) fmt.Printf("解析后的name为: %+v\n", GlobalConfig.App.Name) fmt.Printf("解析后的version为: %+v\n", GlobalConfig.App.Version) err = configClient.ListenConfig(vo.ConfigParam{ DataId: "myv1.yaml", Group: "DEFAULT_GROUP", OnChange: func(namespace, group, dataId, data string) { fmt.Println("配置文件发生了变化...,新配置为:", data) err := yaml.Unmarshal([]byte(data), &GlobalConfig) if err != nil { log.Println("解析新配置失败:", err) } else { fmt.Printf("解析后的配置结构体: %+v\n", GlobalConfig) fmt.Printf("解析后的name为: %+v\n", GlobalConfig.App.Name) fmt.Printf("解析后的version为: %+v\n", GlobalConfig.App.Version) } }, }) }