-
Notifications
You must be signed in to change notification settings - Fork 5
/
consul.go
48 lines (37 loc) · 889 Bytes
/
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
package pprofserver
import (
"context"
"sort"
consul "github.com/segmentio/consul-go"
)
type ConsulRegistry struct{}
func (r *ConsulRegistry) String() string {
return "consul"
}
func (r *ConsulRegistry) ListServices(ctx context.Context) ([]string, error) {
services, err := consul.ListServices(ctx)
if err != nil {
return nil, err
}
list := make([]string, 0, len(services))
for srv := range services {
list = append(list, srv)
}
sort.Strings(list)
return list, nil
}
func (r *ConsulRegistry) LookupService(ctx context.Context, name string) (Service, error) {
svc := Service{}
endpoints, err := consul.LookupService(ctx, name)
if err != nil {
return svc, err
}
svc.Hosts = make([]Host, 0, len(endpoints))
for _, endpoint := range endpoints {
svc.Hosts = append(svc.Hosts, Host{
Addr: endpoint.Addr,
Tags: endpoint.Tags,
})
}
return svc, nil
}