-
Notifications
You must be signed in to change notification settings - Fork 5
/
nacos_discovery.go
86 lines (76 loc) · 2.17 KB
/
nacos_discovery.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
package gfnacos
import (
"github.com/nacos-group/nacos-sdk-go/model"
"github.com/nacos-group/nacos-sdk-go/vo"
)
var ip string
var port uint64
func initDiscoveryService(){
ip = getServerIp()
if nacosCfg.AppPort <= 0 {
port = 8080
}else{
port = nacosCfg.AppPort
}
Register()
}
func getServerIp()string{
if nacosCfg.AppIp == "" || nacosCfg.AppIp == "0.0.0.0" {
ip,_:= getExternalIP()
return ip
}
return nacosCfg.AppIp
}
func Register() (bool, error) {
return nacosClients.namingClient.RegisterInstance(vo.RegisterInstanceParam{
Ip: ip,
Port: port,
ServiceName: nacosCfg.AppName,
Weight: 10,
Enable: true,
Healthy: true,
Ephemeral: true,
Metadata: nacosCfg.Meta,
//ClusterName: "cluster-a", // 默认值DEFAULT
GroupName: nacosCfg.DiscoveryGroup,
})
}
func UnRegister() (bool, error) {
return nacosClients.namingClient.DeregisterInstance(vo.DeregisterInstanceParam{
Ip: ip,
Port: port,
ServiceName: nacosCfg.AppName,
Ephemeral: true,
//Cluster: "cluster-a", // 默认值DEFAULT
GroupName: nacosCfg.DiscoveryGroup,
})
}
func GetService(name string) (model.Service, error) {
return nacosClients.namingClient.GetService(vo.GetServiceParam{
ServiceName: name,
//Clusters: []string{"cluster-a"}, // 默认值DEFAULT
GroupName: nacosCfg.DiscoveryGroup,
})
}
func SelectAllInstances(name string) ([]model.Instance, error) {
return nacosClients.namingClient.SelectAllInstances(vo.SelectAllInstancesParam{
ServiceName: name,
GroupName: nacosCfg.DiscoveryGroup,
//Clusters: []string{"cluster-a"}, // 默认值DEFAULT
})
}
func SelectInstances(name string) ([]model.Instance, error) {
return nacosClients.namingClient.SelectInstances(vo.SelectInstancesParam{
ServiceName: name,
GroupName: nacosCfg.DiscoveryGroup,
//Clusters: []string{"cluster-a"}, // 默认值DEFAULT
HealthyOnly: true,
})
}
func SelectOneHealthyInstance(name string) (*model.Instance, error) {
return nacosClients.namingClient.SelectOneHealthyInstance(vo.SelectOneHealthInstanceParam{
ServiceName: name,
GroupName: nacosCfg.DiscoveryGroup,
//Clusters: []string{"cluster-a"}, // 默认值DEFAULT
})
}