Compare commits
8 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
| b8cfbf6b99 | |||
| 2204f6116b | |||
| 65dbbc16df | |||
| e5c95b0476 | |||
| 34ace207a7 | |||
| ad4368cdf8 | |||
| a4f4b6e059 | |||
| 5bf58aad16 |
@@ -5,7 +5,6 @@ import (
|
|||||||
"log"
|
"log"
|
||||||
"net/http"
|
"net/http"
|
||||||
"strconv"
|
"strconv"
|
||||||
"strings"
|
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
consul "github.com/hashicorp/consul/api"
|
consul "github.com/hashicorp/consul/api"
|
||||||
@@ -14,8 +13,8 @@ import (
|
|||||||
type Service struct {
|
type Service struct {
|
||||||
AppID string
|
AppID string
|
||||||
Name string
|
Name string
|
||||||
|
Domain string
|
||||||
Address string
|
Address string
|
||||||
IP string
|
|
||||||
Port int
|
Port int
|
||||||
TTL time.Duration
|
TTL time.Duration
|
||||||
ConsulAgent *consul.Agent
|
ConsulAgent *consul.Agent
|
||||||
@@ -23,16 +22,16 @@ type Service struct {
|
|||||||
|
|
||||||
var ErrServiceUnavailable = fmt.Errorf("Service is unavailable")
|
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 := new(Service)
|
||||||
s.AppID = appID
|
s.AppID = id
|
||||||
s.Name = strings.Replace(appName, "-", "", -1)
|
s.Name = name
|
||||||
s.Address = domain
|
s.Address = hostname
|
||||||
s.IP = ip
|
s.Domain = domain
|
||||||
s.Port = appPort
|
s.Port = appPort
|
||||||
s.TTL = time.Second * 15
|
s.TTL = time.Second * 15
|
||||||
|
|
||||||
client, err := consul.NewClient(newClientConfig(serverAddr))
|
client, err := consul.NewClient(newClientConfig(servAddr))
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
@@ -53,17 +52,16 @@ func (s *Service) GetID() string {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (s *Service) GetFullAddr() string {
|
func (s *Service) GetFullAddr() string {
|
||||||
return fmt.Sprintf("http://%s:%d/", s.IP, s.Port)
|
return fmt.Sprintf("http://%s:%d/", s.Address, s.Port)
|
||||||
}
|
}
|
||||||
|
|
||||||
func (s *Service) Register() error {
|
func (s *Service) Register() error {
|
||||||
def := &consul.AgentServiceRegistration{
|
def := &consul.AgentServiceRegistration{
|
||||||
ID: s.GetID(),
|
ID: s.GetID(),
|
||||||
Name: s.Name,
|
Name: s.Name,
|
||||||
Address: s.IP,
|
Address: s.Address,
|
||||||
Port: s.Port,
|
Port: s.Port,
|
||||||
Tags: s.getTags(),
|
Tags: s.getTags(),
|
||||||
Meta: s.getMetadata(),
|
|
||||||
Check: &consul.AgentServiceCheck{
|
Check: &consul.AgentServiceCheck{
|
||||||
TTL: s.TTL.String(),
|
TTL: s.TTL.String(),
|
||||||
},
|
},
|
||||||
@@ -72,7 +70,23 @@ func (s *Service) Register() error {
|
|||||||
if err := s.ConsulAgent.ServiceRegister(def); err != nil {
|
if err := s.ConsulAgent.ServiceRegister(def); err != nil {
|
||||||
return err
|
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
|
||||||
|
ticker := time.NewTicker(s.TTL)
|
||||||
|
for range ticker.C {
|
||||||
|
s.healthCheck()
|
||||||
|
}
|
||||||
|
}()
|
||||||
|
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
@@ -80,60 +94,42 @@ func (s *Service) Unregister() error {
|
|||||||
return s.ConsulAgent.ServiceDeregister(s.GetID())
|
return s.ConsulAgent.ServiceDeregister(s.GetID())
|
||||||
}
|
}
|
||||||
|
|
||||||
func (s *Service) UpdateTTL(service *consul.AgentServiceRegistration) {
|
func (s *Service) healthCheck() (bool, error) {
|
||||||
ticker := time.NewTicker(s.TTL / 2)
|
alive := func() bool {
|
||||||
for range ticker.C {
|
client := &http.Client{}
|
||||||
ok, err := s.check()
|
healthUrl := s.GetFullAddr() + "health"
|
||||||
if !ok {
|
req, err := http.NewRequest(http.MethodGet, healthUrl, nil)
|
||||||
if err := s.ConsulAgent.FailTTL("service:"+s.GetID(), err.Error()); err != nil {
|
if err != nil {
|
||||||
log.Println(err)
|
return false
|
||||||
}
|
|
||||||
} else {
|
|
||||||
if err := s.ConsulAgent.PassTTL("service:"+s.GetID(), "OK"); err != nil {
|
|
||||||
log.Println(err)
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
req.Header.Set("User-Agent", "Health Check")
|
||||||
}
|
|
||||||
|
|
||||||
func (s *Service) check() (bool, error) {
|
resp, err := client.Do(req)
|
||||||
client := &http.Client{}
|
if err != nil {
|
||||||
healthUrl := fmt.Sprintf("http://%s/health", s.GetFullAddr())
|
return false
|
||||||
req, err := http.NewRequest(http.MethodGet, healthUrl, nil)
|
}
|
||||||
if err != nil {
|
defer resp.Body.Close()
|
||||||
return false, ErrServiceUnavailable
|
|
||||||
}
|
|
||||||
req.Header.Set("User-Agent", "Health Check")
|
|
||||||
|
|
||||||
resp, err := client.Do(req)
|
return resp.StatusCode == http.StatusOK
|
||||||
if err != nil {
|
}()
|
||||||
return false, ErrServiceUnavailable
|
|
||||||
}
|
|
||||||
defer resp.Body.Close()
|
|
||||||
|
|
||||||
if resp.StatusCode == http.StatusOK {
|
if alive {
|
||||||
|
if err := s.ConsulAgent.PassTTL("service:"+s.GetID(), "OK"); err != nil {
|
||||||
|
log.Println("health-check pass err:" + err.Error())
|
||||||
|
}
|
||||||
return true, nil
|
return true, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if err := s.ConsulAgent.FailTTL("service:"+s.GetID(), ErrServiceUnavailable.Error()); err != nil {
|
||||||
|
log.Println("health-check fail err:" + err.Error())
|
||||||
|
}
|
||||||
return false, ErrServiceUnavailable
|
return false, ErrServiceUnavailable
|
||||||
}
|
}
|
||||||
|
|
||||||
func (s *Service) getMetadata() map[string]string {
|
|
||||||
m := map[string]string{}
|
|
||||||
m["traefik/http/services/"+s.Name+"/loadBalancer/servers/0/url"] = s.GetFullAddr()
|
|
||||||
|
|
||||||
fmt.Printf("netadata: %v", m)
|
|
||||||
|
|
||||||
return m
|
|
||||||
}
|
|
||||||
|
|
||||||
func (s *Service) getTags() []string {
|
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{
|
tags := []string{
|
||||||
"traefik.enable=true",
|
"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 + ".entryPoints=https",
|
||||||
"traefik.http.routers." + s.Name + ".tls=true",
|
"traefik.http.routers." + s.Name + ".tls=true",
|
||||||
"traefik.http.routers." + s.Name + ".service=" + s.Name,
|
"traefik.http.routers." + s.Name + ".service=" + s.Name,
|
||||||
|
|||||||
Reference in New Issue
Block a user