nacos-sdk-go地址

1. go操作nacos

  1. package main
  2. import (
  3. "fmt"
  4. "time"
  5. "github.com/nacos-group/nacos-sdk-go/clients"
  6. "github.com/nacos-group/nacos-sdk-go/common/constant"
  7. "github.com/nacos-group/nacos-sdk-go/vo"
  8. )
  9. func main(){
  10. sc := []constant.ServerConfig{
  11. {
  12. IpAddr: "192.168.1.103",
  13. Port: 8848,
  14. },
  15. }
  16. cc := constant.ClientConfig {
  17. NamespaceId: "c1872978-d51c-4188-a497-4e0cd20b97d5", // 如果需要支持多namespace,我们可以场景多个client,它们有不同的NamespaceId
  18. TimeoutMs: 5000,
  19. NotLoadCacheAtStart: true,
  20. LogDir: "tmp/nacos/log",
  21. CacheDir: "tmp/nacos/cache",
  22. RotateTime: "1h",
  23. MaxAge: 3,
  24. LogLevel: "debug",
  25. }
  26. configClient, err := clients.CreateConfigClient(map[string]interface{}{
  27. "serverConfigs": sc,
  28. "clientConfig": cc,
  29. })
  30. if err != nil {
  31. panic(err)
  32. }
  33. content, err := configClient.GetConfig(vo.ConfigParam{
  34. DataId: "user-web.yaml",
  35. Group: "dev"})
  36. if err != nil {
  37. panic(err)
  38. }
  39. fmt.Println(content)
  40. err = configClient.ListenConfig(vo.ConfigParam{
  41. DataId: "user-web.yaml",
  42. Group: "dev",
  43. OnChange: func(namespace, group, dataId, data string) {
  44. fmt.Println("配置文件变化")
  45. fmt.Println("group:" + group + ", dataId:" + dataId + ", data:" + data)
  46. },
  47. })
  48. time.Sleep(3000 * time.Second)
  49. }

2. 如何将nacos中的配置映射成go的struct

转换地址: https://www.json2yaml.com/convert-yaml-to-json

  1. //go语言本身支持将json的字符串反射成struct