Golang 中的 Nacos 客户端
Go 客户端的 Nacos-sdk-go 允许您访问 Nacos 服务,支持服务发现和动态配置。
支持 1.15 以上的 Go 版本 支持 2.x 版本以上的 Nacos
使用 go get 安装 SDK:
$ go get -u github.com/nacos-group/nacos-sdk-go/v2…
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
}
注意:我们可以配置多个 ServerConfig,客户端将轮流请求服务器
…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,
},
)
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
})
成功, err := namingClient.DeregisterInstance(vo.DeregisterInstanceParam{
[](https://goreportcard.com/report/github.com/nacos-group/nacos-sdk-go)暂无开放 Issues,或尚未同步最近议题。