2017-03-31 19:01:58 +03:00
|
|
|
package consul
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
|
|
|
|
stdconsul "github.com/hashicorp/consul/api"
|
|
|
|
|
|
|
|
"github.com/go-kit/kit/log"
|
|
|
|
)
|
|
|
|
|
|
|
|
// Registrar registers service instance liveness information to Consul.
|
|
|
|
type Registrar struct {
|
|
|
|
client Client
|
|
|
|
registration *stdconsul.AgentServiceRegistration
|
|
|
|
logger log.Logger
|
|
|
|
}
|
|
|
|
|
|
|
|
// NewRegistrar returns a Consul Registrar acting on the provided catalog
|
|
|
|
// registration.
|
|
|
|
func NewRegistrar(client Client, r *stdconsul.AgentServiceRegistration, logger log.Logger) *Registrar {
|
|
|
|
return &Registrar{
|
|
|
|
client: client,
|
|
|
|
registration: r,
|
2017-05-18 19:54:23 +03:00
|
|
|
logger: log.NewContext(logger).With("service", r.Name, "tags", fmt.Sprint(r.Tags), "address", r.Address),
|
2017-03-31 19:01:58 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Register implements sd.Registrar interface.
|
|
|
|
func (p *Registrar) Register() {
|
|
|
|
if err := p.client.Register(p.registration); err != nil {
|
|
|
|
p.logger.Log("err", err)
|
|
|
|
} else {
|
|
|
|
p.logger.Log("action", "register")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Deregister implements sd.Registrar interface.
|
|
|
|
func (p *Registrar) Deregister() {
|
|
|
|
if err := p.client.Deregister(p.registration); err != nil {
|
|
|
|
p.logger.Log("err", err)
|
|
|
|
} else {
|
|
|
|
p.logger.Log("action", "deregister")
|
|
|
|
}
|
|
|
|
}
|