1. go操作nacos
package main
import (
"fmt"
"time"
"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(){
sc := []constant.ServerConfig{
{
IpAddr: "192.168.1.103",
Port: 8848,
},
}
cc := constant.ClientConfig {
NamespaceId: "c1872978-d51c-4188-a497-4e0cd20b97d5", // 如果需要支持多namespace,我们可以场景多个client,它们有不同的NamespaceId
TimeoutMs: 5000,
NotLoadCacheAtStart: true,
LogDir: "tmp/nacos/log",
CacheDir: "tmp/nacos/cache",
RotateTime: "1h",
MaxAge: 3,
LogLevel: "debug",
}
configClient, err := clients.CreateConfigClient(map[string]interface{}{
"serverConfigs": sc,
"clientConfig": cc,
})
if err != nil {
panic(err)
}
content, err := configClient.GetConfig(vo.ConfigParam{
DataId: "user-web.yaml",
Group: "dev"})
if err != nil {
panic(err)
}
fmt.Println(content)
err = configClient.ListenConfig(vo.ConfigParam{
DataId: "user-web.yaml",
Group: "dev",
OnChange: func(namespace, group, dataId, data string) {
fmt.Println("配置文件变化")
fmt.Println("group:" + group + ", dataId:" + dataId + ", data:" + data)
},
})
time.Sleep(3000 * time.Second)
}
2. 如何将nacos中的配置映射成go的struct
转换地址: https://www.json2yaml.com/convert-yaml-to-json
//go语言本身支持将json的字符串反射成struct