go使用nacos作为注册中心
package main
import (
"github.com/nacos-group/nacos-sdk-go/clients"
"github.com/nacos-group/nacos-sdk-go/common/constant"
"github.com/nacos-group/nacos-sdk-go/example"
"github.com/nacos-group/nacos-sdk-go/vo"
)
func main() {
//nacosTest()
nacosTest2()
}
// 这个是sdk上的例子,虽然直接复制下来也可以直接使用,但是一堆的example很是影响
func nacosTest() {
client, err := clients.CreateNamingClient(map[string]interface{}{
"serverConfigs": []constant.ServerConfig{
{
IpAddr: "192.168.0.123",
Port: 8848,
},
},
"clientConfig": constant.ClientConfig{
TimeoutMs: 5000,
ListenInterval: 10000,
NotLoadCacheAtStart: true,
LogDir: "data\\nacos\\log",
//Username: "nacos",
//Password: "nacos",
},
})
if err != nil {
panic(err)
}
example.ExampleServiceClient_RegisterServiceInstance(client, vo.RegisterInstanceParam{
Ip: "192.168.0.123",
Port: 8848,
ServiceName: "demo.go",
Weight: 10,
ClusterName: "a",
Enable: true,
Healthy: true,
Ephemeral: true,
})
example.ExampleServiceClient_GetService(client)
}
// 我通过example的源码 创建一个真正的注册中心
func nacosTest2() {
client, err := clients.CreateNamingClient(map[string]interface{}{
"serverConfigs": []constant.ServerConfig{
{
IpAddr: "192.168.0.123",
Port: 8848,
},
},
"clientConfig": constant.ClientConfig{
TimeoutMs: 5000,
ListenInterval: 10000,
NotLoadCacheAtStart: true,
LogDir: "data\\nacos\\log",
NamespaceId: "a4495738-12c0-42b3-a036-82d3002bdd7a",
//Username: "nacos",
//Password: "nacos",
},
})
if err != nil {
panic(err)
}
success,err:=client.RegisterInstance(vo.RegisterInstanceParam{
Ip: "192.168.0.123",
Port: 8848,
ServiceName: "demo.go",
Weight: 10,
ClusterName: "zwt",
Enable: true,
Healthy: true,
Ephemeral: true,
})
if !success {
global.GVA_LOG.Debug("注册nacos失败,%v\n",err)
return
}
//service,_:=client.GetService(vo.GetServiceParam{
// Clusters: []string{
// "zwt",
// },
// ServiceName: "demo.go",
//})
//fmt.Println(service)
//return
}
详情请参见: 注册中心和配置中心的sdk地址:github.com/nacos-group… nacos官网:nacos.io/zh-cn/docs/…