...

Source file src/github.com/go-kit/kit/sd/consul/registrar.go

Documentation: github.com/go-kit/kit/sd/consul

     1  package consul
     2  
     3  import (
     4  	"fmt"
     5  
     6  	stdconsul "github.com/hashicorp/consul/api"
     7  
     8  	"github.com/go-kit/log"
     9  )
    10  
    11  // Registrar registers service instance liveness information to Consul.
    12  type Registrar struct {
    13  	client       Client
    14  	registration *stdconsul.AgentServiceRegistration
    15  	logger       log.Logger
    16  }
    17  
    18  // NewRegistrar returns a Consul Registrar acting on the provided catalog
    19  // registration.
    20  func NewRegistrar(client Client, r *stdconsul.AgentServiceRegistration, logger log.Logger) *Registrar {
    21  	return &Registrar{
    22  		client:       client,
    23  		registration: r,
    24  		logger:       log.With(logger, "service", r.Name, "tags", fmt.Sprint(r.Tags), "address", r.Address),
    25  	}
    26  }
    27  
    28  // Register implements sd.Registrar interface.
    29  func (p *Registrar) Register() {
    30  	if err := p.client.Register(p.registration); err != nil {
    31  		p.logger.Log("err", err)
    32  	} else {
    33  		p.logger.Log("action", "register")
    34  	}
    35  }
    36  
    37  // Deregister implements sd.Registrar interface.
    38  func (p *Registrar) Deregister() {
    39  	if err := p.client.Deregister(p.registration); err != nil {
    40  		p.logger.Log("err", err)
    41  	} else {
    42  		p.logger.Log("action", "deregister")
    43  	}
    44  }
    45  

View as plain text