百科.dev
全部条目AI 编程趋势榜开源项目技术资讯提交条目
登录
< 返回工具列表
N

nacos-sdk-go

> 编程语言
开源

Golang 中的 Nacos 客户端

1.3K stars0 点赞0 次浏览
访问官网GitHub

工具介绍

Golang 中的 Nacos 客户端

Nacos-sdk-go 中文


Nacos-sdk-go

Nacos-sdk-go for Go client allows you to access Nacos service,it supports service discovery and dynamic configuration.

Requirements

Supported Go version over 1.15

Supported Nacos version over 2.x

Installation

Use go get to install SDK:

$ go get -u github.com/nacos-group/nacos-sdk-go/v2

Quick Examples

  • ClientConfig
…
  • ServerConfig

constant.ServerConfig{
    Scheme      string // the nacos server scheme,defaut=http,this is not required in 2.0 
    ContextPath string // the nacos server contextpath,defaut=/nacos,this is not required in 2.0 
    IpAddr      string // the nacos server address 
    Port        uint64 // nacos server port
    GrpcPort    uint64 // nacos server grpc port, default=server port + 1000, this is not required
}

Note:We can config multiple ServerConfig,the client will rotate request the servers

Create client

…

Create client for ACM

https://help.aliyun.com/document_detail/130146.html


cc := constant.ClientConfig{
		Endpoint:    "acm.aliyun.com:8080",
		NamespaceId: "e525eafa-f7d7-4029-83d9-008937f9d468",
		RegionId:    "cn-shanghai",
		AccessKey:   "LTAI4G8KxxxxxxxxxxxxxbwZLBr",
		SecretKey:   "n5jTL9YxxxxxxxxxxxxaxmPLZV9",
		OpenKMS:     true,
		TimeoutMs:   5000,
		LogLevel:    "debug",
	}

	// a more graceful way to create config client
	client, err := clients.NewConfigClient(
		vo.NacosClientParam{
			ClientConfig: &cc,
		},
	)
   

Service Discovery

  • Register instance:RegisterInstance

success, err := namingClient.RegisterInstance(vo.RegisterInstanceParam{
		Ip:          "10.0.0.11",
		Port:        8848,
		ServiceName: "demo.go",
		Weight:      10,
		Enable:      true,
		Healthy:     true,
		Ephemeral:   true,
		Metadata:    map[string]string{"idc":"shanghai"},
		ClusterName: "cluster-a", // default value is DEFAULT
		GroupName:   "group-a", // default value is DEFAULT_GROUP
	})
   
  • Deregister instance:DeregisterInstance

success, err := namingClient.DeregisterInstance(vo.DeregisterInstanceParam{
		Ip:          "10.0.0.11",
		Port:        8848,
		ServiceName: "demo.go",
		Ephemeral:   true,
		Cluster:     "cluster-a", // default value is DEFAULT
		GroupName:   "group-a", // default value is DEFAULT_GROUP
	})
  • Get service:GetService

services, err := namingClient.GetService(vo.GetServiceParam{
		ServiceName: "demo.go",
		Clusters:    []string{"cluster-a"}, // default value is DEFAULT
		GroupName:   "group-a", // default value is DEFAULT_GROUP
	})
  • Get all instances:SelectAllInstances

// SelectAllInstance return all instances,include healthy=false,enable=false,weight0
	instances, err := namingClient.SelectInstances(vo.SelectInstancesParam{
		ServiceName: "demo.go",
		GroupName:   "group-a", // default value is DEFAULT_GROUP
		Clusters:    []string{"cluster-a"}, // default value is DEFAULT
		HealthyOnly: true,
	})
  • Get one healthy instance(WRR):SelectOneHealthyInstance
// SelectOneHealthyInstance return one instance by WRR strategy for load balance
	// And the instance should be health=true,enable=true and weight>0
	instance, err := namingClient.SelectOneHealthyInstance(vo.SelectOneHealthInstanceParam{
		ServiceName: "demo.go",
		GroupName:   "group-a", // default value is DEFAULT_GROUP
		Clusters:    []string{"cluster-a"}, // default value is DEFAULT
	})
  • Listen service change event:Subscribe

// Subscribe key = serviceName+groupName+cluster
	// Note: We call add multiple SubscribeCallback with the same key.
	err := namingClient.Subscribe(vo.SubscribeParam{
		ServiceName: "demo.go",
		GroupName:   "group-a", // default value is DEFAULT_GROUP
		Clusters:    []string{"cluster-a"}, // default value is DEFAULT
		SubscribeCallback: func (services []model.Instance, err error) {
			log.Printf("\n\n callback return services:%s \n\n", utils.ToJsonString(services))
		},
	})
  • Cancel listen of service change event:Unsubscribe

err := namingClient.Unsubscribe(vo.SubscribeParam{
		ServiceName: "demo.go",
		GroupName:   "group-a", // default value is DEFAULT_GROUP
		Clusters:    []string{"cluster-a"}, // default value is DEFAULT
		SubscribeCallback: func (services []model.Instance, err error) {
			log.Printf("\n\n callback return services:%s \n\n", utils.ToJsonString(services))
		},
	})
  • Get all services name:GetAllServicesInfo

serviceInfos, err := namingClient.GetAllServicesInfo(vo.GetAllServiceInfoParam{
		NameSpace: "0e83cc81-9d8c-4bb8-a28a-ff703187543f",
		PageNo:   1,
		PageSize: 10,
	}),

Dynamic configuration

  • publish config:PublishConfig

success, err := configClient.PublishConfig(vo.ConfigParam{
		DataId:  "dataId",
		Group:   "group",
		Content: "hello world!222222"})
  • delete config:DeleteConfig

success, err = configClient.DeleteConfig(vo.ConfigParam{
		DataId: "dataId",
		Group:  "group"})
  • get config info:GetConfig

content, err := configClient.GetConfig(vo.ConfigParam{
		DataId: "dataId",
		Group:  "group"})
  • Listen config change event:ListenConfig

err := configClient.ListenConfig(vo.ConfigParam{
		DataId: "dataId",
		Group:  "group",
		OnChange: func (namespace, group, dataId, data string) {
			fmt.Println("group:" + group + ", dataId:" + dataId + ", data:" + data)
		},
	})
  • Cancel the listening of config change event:CancelListenConfig

err := configClient.CancelListenConfig(vo.ConfigParam{
		DataId: "dataId",
		Group:  "group",
	})
  • Search config: SearchConfig
configPage, err := configClient.SearchConfig(vo.SearchConfigParam{
		Search:   "blur",
		DataId:   "",
		Group:    "",
		PageNo:   1,
		PageSize: 10,
	})

Example

We can run example to learn how to use nacos go client.

  • Config Example
  • Naming Example

Documentation

You can view the open-api documentation from the Nacos open-api wepsite.

You can view the full documentation from the Nacos website.

Contributing

Contributors are welcomed to join Nacos-sdk-go project. Please check CONTRIBUTING.md about how to contribute to this project.

Contact

  • Join us from DingDing Group(23191211).
  • Gitter: Nacos's IM tool for community messaging, collaboration and discovery.
  • Twitter: Follow along for latest nacos news on Twitter.
  • Weibo: Follow along for latest nacos news on Weibo (Twitter of China version).
  • Nacos SegmentFault: Get the latest notice and prompt help from SegmentFault.
  • Email Group:
    • [email protected]: Nacos usage general discussion.
    • [email protected]: Nacos developer discussion (APIs, feature design, etc).
    • [email protected]: Commits notice, very high frequency.

Issues· 0 开放

查看全部 Issues在 GitHub 打开

暂无开放 Issues,或尚未同步最近议题。

> 标签

Go

暂无评论,来聊聊你的看法吧

> 工具信息

发布日期2026年8月1日
最后更新2026年9月17日
分类编程语言
定价开源

> 相关工具

T
TypeScript
JavaScript 的超集,为前端与全栈提供静态类型
P
Python
通用编程语言,广泛用于 Web、数据与 AI
G
Go
Google 推出的简洁高效系统语言