-
Notifications
You must be signed in to change notification settings - Fork 1
/
instance.go
183 lines (148 loc) · 3.07 KB
/
instance.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
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
package goconsul
import (
"fmt"
"github.com/gogf/gf/text/gstr"
)
type ServiceInstance struct {
Check *ServiceCheck
KVCmd
ServiceName string
Host string
Port int
Metadata map[string]string
InstanceId string
Tags []string
LanIps []string
AnnouncedIps []string
registered bool
}
func NewInstance(
serviceName string,
host string,
port int,
metadata map[string]string,
tags []string,
instanceId string,
lanIps []string,
announcedIps []string,
client *Client,
dataRoot string) *ServiceInstance {
instance := &ServiceInstance{
registered: false,
}
instance.Constructor(
serviceName,
host,
port,
metadata,
tags,
instanceId,
lanIps,
announcedIps,
client,
dataRoot)
return instance
}
func (ins *ServiceInstance) Constructor(
serviceName string,
host string,
port int,
metadata map[string]string,
tags []string,
instanceId string,
lanIps []string,
announcedIps []string,
client *Client,
dataRoot string) {
ips := ""
for _, ip := range announcedIps {
ips += ip + ","
}
ips = gstr.TrimRight(ips, ",")
if metadata == nil {
metadata = make(map[string]string)
}
metadata["AnnouncedIp"] = ips
ips = ""
for _, ip := range lanIps {
ips += ip + ","
}
ips = gstr.TrimRight(ips, ",")
metadata["LanIp"] = ips
fullTags := []string{
fmt.Sprintf("serviceName=%s", serviceName),
fmt.Sprintf("instanceId=%s", instanceId),
}
fullTags = append(fullTags, tags...)
if client == nil {
client = DefaultClient()
}
ins.ServiceName = serviceName
ins.Host = host
ins.Port = port
ins.Metadata = metadata
ins.Tags = fullTags
ins.InstanceId = instanceId
ins.LanIps = lanIps
ins.AnnouncedIps = announcedIps
ins.KVCmd = KVCmd{
client: client,
DataRoot: dataRoot,
}
}
func (ins *ServiceInstance) GetId() string {
return ins.InstanceId
}
func (ins *ServiceInstance) GetName() string {
return ins.ServiceName
}
func (ins *ServiceInstance) GetHost() string {
return ins.Host
}
func (ins *ServiceInstance) GetPort() int {
return ins.Port
}
func (ins *ServiceInstance) GetMetadata() map[string]string {
return ins.Metadata
}
func (ins *ServiceInstance) GetInstanceId() string {
return ins.InstanceId
}
func (ins *ServiceInstance) GetTags() []string {
return ins.Tags
}
func (ins *ServiceInstance) GetLanIps() []string {
return ins.LanIps
}
func (ins *ServiceInstance) GetAnnouncedIps() []string {
return ins.AnnouncedIps
}
func (ins *ServiceInstance) GetMetadataValue(key string) (string, bool) {
val, found := ins.Metadata[key]
return val, found
}
func (ins *ServiceInstance) GetServiceCheck() *ServiceCheck {
return ins.Check
}
func (ins *ServiceInstance) Register() error {
if ins.client == nil {
ins.client = DefaultClient()
}
if err := ins.client.Register(ins); err != nil {
return err
}
ins.registered = true
return nil
}
func (ins *ServiceInstance) Deregister() {
if ins.client == nil {
ins.client = DefaultClient()
}
if ins.registered {
ins.registered = false
ins.client.Deregister(ins.InstanceId)
}
}
func (ins *ServiceInstance) IsRegistered() bool {
return ins.registered
}