nacos_config.go 1.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061
  1. package c_nacos
  2. import (
  3. "github.com/nacos-group/nacos-sdk-go/clients"
  4. "github.com/nacos-group/nacos-sdk-go/common/constant"
  5. "github.com/nacos-group/nacos-sdk-go/vo"
  6. )
  7. func InitNacos() {
  8. sc := []constant.ServerConfig{
  9. {
  10. IpAddr: "36.110.106.156",
  11. Port: 8848,
  12. },
  13. }
  14. cc := constant.ClientConfig{
  15. NamespaceId: "7e6d8a49-995b-49a0-b43e-25c1de713a55", //namespace id
  16. TimeoutMs: 5000,
  17. NotLoadCacheAtStart: true,
  18. LogDir: "/tmp/nacos/log",
  19. CacheDir: "/tmp/nacos/cache",
  20. LogLevel: "debug",
  21. }
  22. //or a more graceful way to create ClientConfig
  23. _ = *constant.NewClientConfig(
  24. constant.WithNamespaceId("7e6d8a49-995b-49a0-b43e-25c1de713a55"),
  25. constant.WithTimeoutMs(5000),
  26. constant.WithNotLoadCacheAtStart(true),
  27. constant.WithLogDir("/tmp/nacos/log"),
  28. constant.WithCacheDir("/tmp/nacos/cache"),
  29. constant.WithLogLevel("debug"),
  30. constant.WithUsername("nacos"),
  31. constant.WithPassword("1qaz2wsx!"),
  32. )
  33. // a more graceful way to create naming client
  34. client, err := clients.NewNamingClient(
  35. vo.NacosClientParam{
  36. ClientConfig: &cc,
  37. ServerConfigs: sc,
  38. },
  39. )
  40. if err != nil {
  41. panic(err)
  42. }
  43. //Register with default cluster and group
  44. //ClusterName=DEFAULT,GroupName=DEFAULT_GROUP
  45. ExampleServiceClient_RegisterServiceInstance(client, vo.RegisterInstanceParam{
  46. Ip: "36.110.106.142",
  47. Port: 12340,
  48. ServiceName: "plugin-compile",
  49. Weight: 10,
  50. Enable: true,
  51. Healthy: true,
  52. Ephemeral: true,
  53. Metadata: map[string]string{"idc": "beijing", "开发者": "孟令鑫"},
  54. })
  55. }