Baike.dev
All toolsOnline toolsAI codingTrendingOpen sourceNewsSubmit
Log in
< Back to tools
N

nacos-sdk-go

> 编程语言
Open source

Nacos client in Golang

1.3K stars0 likes1 views
WebsiteGitHub

About

Nacos client in Golang

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:

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

Quick Examples

  • ClientConfig
…
  • ServerConfig
go

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

go

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
go

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
go

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
go

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
go

// 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
go
// 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
go

// 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
go

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
go

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

Dynamic configuration

  • publish config:PublishConfig
go

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

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

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

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
go

err := configClient.CancelListenConfig(vo.ConfigParam{
		DataId: "dataId",
		Group:  "group",
	})
  • Search config: SearchConfig
go
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 open

View all issuesOpen on GitHub

No open issues yet, or sync has not completed.

> Tags

Go

No comments yet. Be the first to share.

> Details

PublishedAug 1, 2026
UpdatedSep 17, 2026
Category编程语言
PricingOpen source

> Related tools

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