Added Consul KV base support
This commit is contained in:
parent
faace02cb4
commit
39a8327463
@ -10,31 +10,33 @@ import (
|
|||||||
)
|
)
|
||||||
|
|
||||||
type Service struct {
|
type Service struct {
|
||||||
AppID string
|
Name string
|
||||||
Name string
|
Address string
|
||||||
Domain string
|
appID string
|
||||||
Address string
|
domain string
|
||||||
Port int
|
port int
|
||||||
TTL time.Duration
|
ttl time.Duration
|
||||||
ConsulAgent *consul.Agent
|
agent *consul.Agent
|
||||||
|
kv *consul.KV
|
||||||
}
|
}
|
||||||
|
|
||||||
var ErrServiceUnavailable = fmt.Errorf("Service is unavailable")
|
var ErrServiceUnavailable = fmt.Errorf("Service is unavailable")
|
||||||
|
|
||||||
func NewService(servAddr, id, name, hostname, domain string, appPort int) (*Service, error) {
|
func NewService(servAddr, id, name, hostname, domain string, appPort int) (*Service, error) {
|
||||||
s := new(Service)
|
s := new(Service)
|
||||||
s.AppID = id
|
|
||||||
s.Name = name
|
s.Name = name
|
||||||
s.Address = hostname
|
s.Address = hostname
|
||||||
s.Domain = domain
|
s.appID = id
|
||||||
s.Port = appPort
|
s.domain = domain
|
||||||
s.TTL = time.Second * 15
|
s.port = appPort
|
||||||
|
s.ttl = time.Second * 15
|
||||||
|
|
||||||
client, err := consul.NewClient(newClientConfig(servAddr))
|
client, err := consul.NewClient(newClientConfig(servAddr))
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
s.ConsulAgent = client.Agent()
|
s.agent = client.Agent()
|
||||||
|
s.kv = client.KV()
|
||||||
|
|
||||||
return s, nil
|
return s, nil
|
||||||
}
|
}
|
||||||
@ -47,11 +49,11 @@ func newClientConfig(serverAddr string) *consul.Config {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (s *Service) GetID() string {
|
func (s *Service) GetID() string {
|
||||||
return fmt.Sprintf("%s_%s", s.Name, s.AppID)
|
return fmt.Sprintf("%s_%s", s.Name, s.appID)
|
||||||
}
|
}
|
||||||
|
|
||||||
func (s *Service) GetFullAddr() string {
|
func (s *Service) GetFullAddr() string {
|
||||||
return fmt.Sprintf("http://%s:%d/", s.Address, s.Port)
|
return fmt.Sprintf("http://%s:%d/", s.Address, s.port)
|
||||||
}
|
}
|
||||||
|
|
||||||
func (s *Service) Register() error {
|
func (s *Service) Register() error {
|
||||||
@ -59,14 +61,14 @@ func (s *Service) Register() error {
|
|||||||
ID: s.GetID(),
|
ID: s.GetID(),
|
||||||
Name: s.Name,
|
Name: s.Name,
|
||||||
Address: s.Address,
|
Address: s.Address,
|
||||||
Port: s.Port,
|
Port: s.port,
|
||||||
Tags: s.getTags(),
|
Tags: s.getTags(),
|
||||||
Check: &consul.AgentServiceCheck{
|
Check: &consul.AgentServiceCheck{
|
||||||
TTL: s.TTL.String(),
|
TTL: s.ttl.String(),
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
|
|
||||||
if err := s.ConsulAgent.ServiceRegister(def); err != nil {
|
if err := s.agent.ServiceRegister(def); err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -81,7 +83,7 @@ func (s *Service) Register() error {
|
|||||||
}()
|
}()
|
||||||
|
|
||||||
go func() { // TTL
|
go func() { // TTL
|
||||||
interval := s.TTL - time.Second*2
|
interval := s.ttl - time.Second*2
|
||||||
ticker := time.NewTicker(interval)
|
ticker := time.NewTicker(interval)
|
||||||
for range ticker.C {
|
for range ticker.C {
|
||||||
_, err := s.healthCheck()
|
_, err := s.healthCheck()
|
||||||
@ -94,7 +96,11 @@ func (s *Service) Register() error {
|
|||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
func (s *Service) Unregister() error {
|
func (s *Service) Unregister() error {
|
||||||
return s.ConsulAgent.ServiceDeregister(s.GetID())
|
return s.agent.ServiceDeregister(s.GetID())
|
||||||
|
}
|
||||||
|
|
||||||
|
func (s *Service) KV() *consul.KV {
|
||||||
|
return s.kv
|
||||||
}
|
}
|
||||||
|
|
||||||
func (s *Service) healthCheck() (bool, error) {
|
func (s *Service) healthCheck() (bool, error) {
|
||||||
@ -117,13 +123,13 @@ func (s *Service) healthCheck() (bool, error) {
|
|||||||
}()
|
}()
|
||||||
|
|
||||||
if alive {
|
if alive {
|
||||||
if err := s.ConsulAgent.PassTTL("service:"+s.GetID(), "OK"); err != nil {
|
if err := s.agent.PassTTL("service:"+s.GetID(), "OK"); err != nil {
|
||||||
return false, err
|
return false, err
|
||||||
}
|
}
|
||||||
return true, nil
|
return true, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
if err := s.ConsulAgent.FailTTL("service:"+s.GetID(), ErrServiceUnavailable.Error()); err != nil {
|
if err := s.agent.FailTTL("service:"+s.GetID(), ErrServiceUnavailable.Error()); err != nil {
|
||||||
return false, err
|
return false, err
|
||||||
}
|
}
|
||||||
return false, ErrServiceUnavailable
|
return false, ErrServiceUnavailable
|
||||||
@ -138,7 +144,7 @@ func (s *Service) getTags() []string {
|
|||||||
"traefik.http.routers." + s.Name + ".service=" + s.Name,
|
"traefik.http.routers." + s.Name + ".service=" + s.Name,
|
||||||
"traefik.http.routers." + s.Name + ".middlewares=compress,requestid",
|
"traefik.http.routers." + s.Name + ".middlewares=compress,requestid",
|
||||||
"traefik.http.services." + s.Name + ".loadbalancer.server.scheme=http",
|
"traefik.http.services." + s.Name + ".loadbalancer.server.scheme=http",
|
||||||
"traefik.http.services." + s.Name + ".loadbalancer.server.port=" + strconv.Itoa(s.Port),
|
"traefik.http.services." + s.Name + ".loadbalancer.server.port=" + strconv.Itoa(s.port),
|
||||||
"traefik.http.middlewares.compress.compress=true",
|
"traefik.http.middlewares.compress.compress=true",
|
||||||
"traefik.http.middlewares.requestid.plugin.requestid.headerName=X-Request-ID",
|
"traefik.http.middlewares.requestid.plugin.requestid.headerName=X-Request-ID",
|
||||||
"traefik.tls.certificates.certfile=/certs/client.cert",
|
"traefik.tls.certificates.certfile=/certs/client.cert",
|
||||||
|
Loading…
Reference in New Issue
Block a user