|
|
|
|
@@ -2,8 +2,8 @@ package consul
|
|
|
|
|
|
|
|
|
|
import (
|
|
|
|
|
"fmt"
|
|
|
|
|
"log"
|
|
|
|
|
"net/http"
|
|
|
|
|
"strconv"
|
|
|
|
|
"time"
|
|
|
|
|
|
|
|
|
|
consul "github.com/hashicorp/consul/api"
|
|
|
|
|
@@ -12,8 +12,8 @@ import (
|
|
|
|
|
type Service struct {
|
|
|
|
|
AppID string
|
|
|
|
|
Name string
|
|
|
|
|
Domain string
|
|
|
|
|
Address string
|
|
|
|
|
IP string
|
|
|
|
|
Port int
|
|
|
|
|
TTL time.Duration
|
|
|
|
|
ConsulAgent *consul.Agent
|
|
|
|
|
@@ -21,16 +21,16 @@ type Service struct {
|
|
|
|
|
|
|
|
|
|
var ErrServiceUnavailable = fmt.Errorf("Service is unavailable")
|
|
|
|
|
|
|
|
|
|
func NewService(serverAddr, appID, appName, ip, domain string, appPort int) (*Service, error) {
|
|
|
|
|
func NewService(servAddr, id, name, hostname, domain string, appPort int) (*Service, error) {
|
|
|
|
|
s := new(Service)
|
|
|
|
|
s.AppID = appID
|
|
|
|
|
s.Name = appName
|
|
|
|
|
s.Address = domain
|
|
|
|
|
s.IP = ip
|
|
|
|
|
s.AppID = id
|
|
|
|
|
s.Name = name
|
|
|
|
|
s.Address = hostname
|
|
|
|
|
s.Domain = domain
|
|
|
|
|
s.Port = appPort
|
|
|
|
|
s.TTL = time.Second * 15
|
|
|
|
|
|
|
|
|
|
client, err := consul.NewClient(newClientConfig(serverAddr))
|
|
|
|
|
client, err := consul.NewClient(newClientConfig(servAddr))
|
|
|
|
|
if err != nil {
|
|
|
|
|
return nil, err
|
|
|
|
|
}
|
|
|
|
|
@@ -46,11 +46,19 @@ func newClientConfig(serverAddr string) *consul.Config {
|
|
|
|
|
return conf
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func (s *Service) GetID() string {
|
|
|
|
|
return fmt.Sprintf("%s_%s", s.Name, s.AppID)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func (s *Service) GetFullAddr() string {
|
|
|
|
|
return fmt.Sprintf("http://%s:%d/", s.Address, s.Port)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func (s *Service) Register() error {
|
|
|
|
|
def := &consul.AgentServiceRegistration{
|
|
|
|
|
ID: s.Name + "_" + s.AppID,
|
|
|
|
|
ID: s.GetID(),
|
|
|
|
|
Name: s.Name,
|
|
|
|
|
Address: s.IP,
|
|
|
|
|
Address: s.Address,
|
|
|
|
|
Port: s.Port,
|
|
|
|
|
Tags: s.getTags(),
|
|
|
|
|
Check: &consul.AgentServiceCheck{
|
|
|
|
|
@@ -61,71 +69,84 @@ func (s *Service) Register() error {
|
|
|
|
|
if err := s.ConsulAgent.ServiceRegister(def); err != nil {
|
|
|
|
|
return err
|
|
|
|
|
}
|
|
|
|
|
go s.UpdateTTL(def)
|
|
|
|
|
|
|
|
|
|
go func() { // startup register
|
|
|
|
|
ticker := time.NewTicker(time.Millisecond * 100)
|
|
|
|
|
for range ticker.C {
|
|
|
|
|
ok, _ := s.healthCheck()
|
|
|
|
|
if ok {
|
|
|
|
|
ticker.Stop()
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}()
|
|
|
|
|
|
|
|
|
|
go func() { // TTL
|
|
|
|
|
interval := s.TTL - time.Second*2
|
|
|
|
|
ticker := time.NewTicker(interval)
|
|
|
|
|
for range ticker.C {
|
|
|
|
|
fmt.Println("HC call: ", time.Now().String())
|
|
|
|
|
_, err := s.healthCheck()
|
|
|
|
|
if err != nil {
|
|
|
|
|
fmt.Printf("TTL Error: %v\n", err)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}()
|
|
|
|
|
|
|
|
|
|
return nil
|
|
|
|
|
}
|
|
|
|
|
func (s *Service) Unregister() error {
|
|
|
|
|
return s.ConsulAgent.ServiceDeregister(s.Name + "_" + s.AppID)
|
|
|
|
|
return s.ConsulAgent.ServiceDeregister(s.GetID())
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func (s *Service) UpdateTTL(service *consul.AgentServiceRegistration) {
|
|
|
|
|
ticker := time.NewTicker(s.TTL / 2)
|
|
|
|
|
for range ticker.C {
|
|
|
|
|
ok, err := s.check()
|
|
|
|
|
if !ok {
|
|
|
|
|
if err := s.ConsulAgent.FailTTL("service:"+s.Name+"_"+s.AppID, err.Error()); err != nil {
|
|
|
|
|
log.Println(err)
|
|
|
|
|
}
|
|
|
|
|
} else {
|
|
|
|
|
if err := s.ConsulAgent.PassTTL("service:"+s.AppID, "OK"); err != nil {
|
|
|
|
|
log.Println(err)
|
|
|
|
|
}
|
|
|
|
|
func (s *Service) healthCheck() (bool, error) {
|
|
|
|
|
alive := func() bool {
|
|
|
|
|
client := &http.Client{}
|
|
|
|
|
healthUrl := s.GetFullAddr() + "health"
|
|
|
|
|
req, err := http.NewRequest(http.MethodGet, healthUrl, nil)
|
|
|
|
|
if err != nil {
|
|
|
|
|
return false
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
req.Header.Set("User-Agent", "Health Check")
|
|
|
|
|
|
|
|
|
|
func (s *Service) check() (bool, error) {
|
|
|
|
|
client := &http.Client{}
|
|
|
|
|
healthUrl := fmt.Sprintf("http://%s:%d/health", s.IP, s.Port)
|
|
|
|
|
req, err := http.NewRequest(http.MethodGet, healthUrl, nil)
|
|
|
|
|
if err != nil {
|
|
|
|
|
return false, ErrServiceUnavailable
|
|
|
|
|
}
|
|
|
|
|
req.Header.Set("User-Agent", "Health Check")
|
|
|
|
|
resp, err := client.Do(req)
|
|
|
|
|
if err != nil {
|
|
|
|
|
return false
|
|
|
|
|
}
|
|
|
|
|
defer resp.Body.Close()
|
|
|
|
|
|
|
|
|
|
resp, err := client.Do(req)
|
|
|
|
|
if err != nil {
|
|
|
|
|
return false, ErrServiceUnavailable
|
|
|
|
|
}
|
|
|
|
|
defer resp.Body.Close()
|
|
|
|
|
return resp.StatusCode == http.StatusOK
|
|
|
|
|
}()
|
|
|
|
|
|
|
|
|
|
if resp.StatusCode == http.StatusOK {
|
|
|
|
|
if alive {
|
|
|
|
|
if err := s.ConsulAgent.PassTTL("service:"+s.GetID(), "OK"); err != nil {
|
|
|
|
|
return false, err
|
|
|
|
|
}
|
|
|
|
|
return true, nil
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if err := s.ConsulAgent.FailTTL("service:"+s.GetID(), ErrServiceUnavailable.Error()); err != nil {
|
|
|
|
|
return false, err
|
|
|
|
|
}
|
|
|
|
|
return false, ErrServiceUnavailable
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func (s *Service) getTags() []string {
|
|
|
|
|
fullName := fmt.Sprintf("%s-%s", s.Name, s.AppID)
|
|
|
|
|
bFullAddr := fmt.Sprintf("http://%s:%d/", s.IP, s.Port) // FIXME: declare one once - dont need to refresh....
|
|
|
|
|
|
|
|
|
|
tags := []string{
|
|
|
|
|
"traefik.enable=true",
|
|
|
|
|
"traefik.http.routers." + s.Name + ".rule=Host(`" + s.Address + "`)",
|
|
|
|
|
"traefik.http.routers." + s.Name + ".rule=Host(`" + s.Domain + "`)",
|
|
|
|
|
"traefik.http.routers." + s.Name + ".entryPoints=https",
|
|
|
|
|
"traefik.http.routers." + s.Name + ".tls=true",
|
|
|
|
|
"traefik.http.routers." + s.Name + ".service=" + s.Name,
|
|
|
|
|
"traefik.http.routers." + s.Name + ".middlewares=compress,requestid",
|
|
|
|
|
"traefik.http.routers." + s.Name + ".tls=true",
|
|
|
|
|
// "traefik.http.services." + s.Name + ".loadbalancer.server.scheme=http",
|
|
|
|
|
// "traefik.http.services." + s.Name + ".loadbalancer.server.port=" + port,
|
|
|
|
|
"traefik.http.services." + s.Name + ".loadbalancer.passhostheader=false",
|
|
|
|
|
"traefik.http.services." + s.Name + ".loadbalancer.servers." + fullName + ".url=" + bFullAddr,
|
|
|
|
|
"traefik.http.services." + s.Name + ".loadbalancer.server.scheme=http",
|
|
|
|
|
"traefik.http.services." + s.Name + ".loadbalancer.server.port=" + strconv.Itoa(s.Port),
|
|
|
|
|
"traefik.http.middlewares.compress.compress=true",
|
|
|
|
|
"traefik.http.middlewares.requestid.plugin.requestid.headerName=X-Request-ID",
|
|
|
|
|
// "traefik.http.services." + fullName + ".loadbalancer.healthcheck.path=/health",
|
|
|
|
|
// "traefik.http.services." + fullName + ".loadbalancer.healthcheck.interval=10s",
|
|
|
|
|
// "traefik.http.services." + s.Name + ".loadbalancer.passhostheader=false",
|
|
|
|
|
// "traefik.http.services." + s.Name + ".loadbalancer.servers." + fullName + "=" + bFullAddr,
|
|
|
|
|
// "traefik.http.services." + s.Name + ".loadbalancer.servers." + fullName + ".url=" + bFullAddr,
|
|
|
|
|
// "traefik.http.services." + s.Name + ".loadbalancer.healthcheck.path=/health",
|
|
|
|
|
// "traefik.http.services." + s.Name + ".loadbalancer.healthcheck.interval=10s",
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return tags
|
|
|
|
|
|