-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathconsul.go
159 lines (137 loc) · 3.67 KB
/
consul.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
package discover
import (
"context"
"encoding/json"
"fmt"
"net"
"strconv"
"strings"
consulapi "github.com/hashicorp/consul/api"
)
type consulDiscover struct {
ctx context.Context
cancel context.CancelFunc
prefix string
serviceName string
client *consulapi.Client
}
func newConsulDiscover(cfg *DiscoverConfig) (*consulDiscover, error) {
config := consulapi.DefaultConfig()
config.Address = strings.Join(cfg.BackendEndPoints, ",")
client, err := consulapi.NewClient(config)
if err != nil {
return nil, err
}
ctx, cancel := context.WithCancel(context.Background())
d := &consulDiscover{
ctx: ctx,
cancel: cancel,
prefix: cfg.DiscoverPrefix,
serviceName: cfg.ServiceName,
client: client,
}
return d, nil
}
func (d *consulDiscover) Start(callback EndpointCacher) {
d.discover(callback)
}
func (d *consulDiscover) Stop() {
d.cancel()
}
func (d *consulDiscover) discover(callback EndpointCacher) {
var lastIndex uint64
for {
select {
case <-d.ctx.Done():
return
default:
services, queryMeta, err := d.client.Health().Service(
d.serviceName, "", false, &consulapi.QueryOptions{
WaitIndex: lastIndex,
})
if err != nil {
callback.AddError(err)
}
lastIndex = queryMeta.LastIndex
for _, service := range services {
var attr []byte
endpoint := fmt.Sprintf("%s:%v", service.Service.Address, service.Service.Port)
switch service.Checks.AggregatedStatus() {
case consulapi.HealthPassing:
if service.Service.Meta != nil {
attr, _ = json.Marshal(service.Service.Meta)
}
callback.AddOrUpdate(endpoint, attr)
case consulapi.HealthCritical, consulapi.HealthWarning:
callback.Delete(endpoint)
}
}
}
}
}
func (d *consulDiscover) listService() {
d.client.Agent().Services()
}
type consulRegister struct {
prefix string
serviceName string
serviceId string
endpoint string
healthCheckEndpoint string
attr string
ttl int64
stopCh chan struct{}
client *consulapi.Client
}
func newConsulRegister(cfg *RegisterConfig) (*consulRegister, error) {
config := consulapi.DefaultConfig()
config.Address = strings.Join(cfg.BackendEndPoints, ",")
client, err := consulapi.NewClient(config)
if err != nil {
return nil, err
}
r := &consulRegister{
prefix: cfg.DiscoverPrefix,
serviceName: cfg.ServiceName,
endpoint: cfg.ServiceEndPoint,
healthCheckEndpoint: cfg.HealthCheckEndPoint,
attr: cfg.Attr,
ttl: cfg.HeartBeatPeriod,
stopCh: make(chan struct{}),
client: client,
}
return r, nil
}
func (s *consulRegister) Start() error {
return s.register()
}
func (s *consulRegister) Stop() error {
if s.serviceId != "" {
return s.client.Agent().ServiceDeregister(s.serviceId)
}
return nil
}
func (s *consulRegister) register() error {
registration := new(consulapi.AgentServiceRegistration)
address, port, err := net.SplitHostPort(s.endpoint)
if err != nil {
return err
}
registration.Address = address
portInt, err := strconv.Atoi(port)
if err != nil {
return err
}
registration.Port = portInt
serviceId := fmt.Sprintf("%s_%s", s.serviceName, address)
s.serviceId = serviceId
registration.Name = s.serviceName
registration.ID = serviceId
serviceCheck := new(consulapi.AgentServiceCheck)
serviceCheck.HTTP = fmt.Sprintf("http://%s", s.healthCheckEndpoint)
serviceCheck.Timeout = "2s"
serviceCheck.Interval = "2s"
serviceCheck.DeregisterCriticalServiceAfter = "30s"
registration.Check = serviceCheck
return s.client.Agent().ServiceRegister(registration)
}