-
Notifications
You must be signed in to change notification settings - Fork 18
/
consul.go
36 lines (30 loc) · 841 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
package main
import (
"github.com/hashicorp/consul/api"
"github.com/pkg/errors"
)
type Consul struct {
client *api.Client
queryOpts *api.QueryOptions
}
type ConsulService struct {
Name string
Address string
Port int
}
func (c *Consul) GetService(serviceName string) ([]ConsulService, error) {
serviceAddressesPorts := []ConsulService{}
// get consul service addresses and ports
addresses, _, err := c.client.Health().Service(serviceName, "", true, c.queryOpts)
if err != nil {
return nil, errors.Wrap(err, "get consul service")
}
for _, addr := range addresses {
// append service addresses and ports
serviceAddressesPorts = append(serviceAddressesPorts, ConsulService{
Name: addr.Service.Service,
Address: addr.Node.Address,
Port: addr.Service.Port})
}
return serviceAddressesPorts, nil
}