|
|
|
@@ -4,15 +4,16 @@ import (
|
|
|
|
"fmt"
|
|
|
|
"fmt"
|
|
|
|
"log"
|
|
|
|
"log"
|
|
|
|
"net/http"
|
|
|
|
"net/http"
|
|
|
|
"strconv"
|
|
|
|
|
|
|
|
"time"
|
|
|
|
"time"
|
|
|
|
|
|
|
|
|
|
|
|
consul "github.com/hashicorp/consul/api"
|
|
|
|
consul "github.com/hashicorp/consul/api"
|
|
|
|
)
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
|
|
type Service struct {
|
|
|
|
type Service struct {
|
|
|
|
|
|
|
|
AppID string
|
|
|
|
Name string
|
|
|
|
Name string
|
|
|
|
Address string
|
|
|
|
Address string
|
|
|
|
|
|
|
|
IP string
|
|
|
|
Port int
|
|
|
|
Port int
|
|
|
|
TTL time.Duration
|
|
|
|
TTL time.Duration
|
|
|
|
ConsulAgent *consul.Agent
|
|
|
|
ConsulAgent *consul.Agent
|
|
|
|
@@ -20,10 +21,12 @@ type Service struct {
|
|
|
|
|
|
|
|
|
|
|
|
var ErrServiceUnavailable = fmt.Errorf("Service is unavailable")
|
|
|
|
var ErrServiceUnavailable = fmt.Errorf("Service is unavailable")
|
|
|
|
|
|
|
|
|
|
|
|
func NewService(serverAddr, appName, appDomain string, appPort int) (*Service, error) {
|
|
|
|
func NewService(serverAddr, appID, appName, ip, domain string, appPort int) (*Service, error) {
|
|
|
|
s := new(Service)
|
|
|
|
s := new(Service)
|
|
|
|
|
|
|
|
s.AppID = appID
|
|
|
|
s.Name = appName
|
|
|
|
s.Name = appName
|
|
|
|
s.Address = appDomain
|
|
|
|
s.Address = domain
|
|
|
|
|
|
|
|
s.IP = ip
|
|
|
|
s.Port = appPort
|
|
|
|
s.Port = appPort
|
|
|
|
s.TTL = time.Second * 15
|
|
|
|
s.TTL = time.Second * 15
|
|
|
|
|
|
|
|
|
|
|
|
@@ -45,9 +48,9 @@ func newClientConfig(serverAddr string) *consul.Config {
|
|
|
|
|
|
|
|
|
|
|
|
func (s *Service) Register() error {
|
|
|
|
func (s *Service) Register() error {
|
|
|
|
def := &consul.AgentServiceRegistration{
|
|
|
|
def := &consul.AgentServiceRegistration{
|
|
|
|
// ID: s.Name,
|
|
|
|
ID: s.Name + "_" + s.AppID,
|
|
|
|
Name: s.Name,
|
|
|
|
Name: s.Name,
|
|
|
|
Address: s.Address,
|
|
|
|
Address: s.IP,
|
|
|
|
Port: s.Port,
|
|
|
|
Port: s.Port,
|
|
|
|
Tags: s.getTags(),
|
|
|
|
Tags: s.getTags(),
|
|
|
|
Check: &consul.AgentServiceCheck{
|
|
|
|
Check: &consul.AgentServiceCheck{
|
|
|
|
@@ -63,7 +66,7 @@ func (s *Service) Register() error {
|
|
|
|
return nil
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
func (s *Service) Unregister() error {
|
|
|
|
func (s *Service) Unregister() error {
|
|
|
|
return s.ConsulAgent.ServiceDeregister(s.Name)
|
|
|
|
return s.ConsulAgent.ServiceDeregister(s.AppID)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
func (s *Service) UpdateTTL(service *consul.AgentServiceRegistration) {
|
|
|
|
func (s *Service) UpdateTTL(service *consul.AgentServiceRegistration) {
|
|
|
|
@@ -71,11 +74,11 @@ func (s *Service) UpdateTTL(service *consul.AgentServiceRegistration) {
|
|
|
|
for range ticker.C {
|
|
|
|
for range ticker.C {
|
|
|
|
ok, err := s.check()
|
|
|
|
ok, err := s.check()
|
|
|
|
if !ok {
|
|
|
|
if !ok {
|
|
|
|
if err := s.ConsulAgent.FailTTL("service:"+s.Name, err.Error()); err != nil {
|
|
|
|
if err := s.ConsulAgent.FailTTL("service:"+s.Name+"_"+s.AppID, err.Error()); err != nil {
|
|
|
|
log.Println(err)
|
|
|
|
log.Println(err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
} else {
|
|
|
|
if err := s.ConsulAgent.PassTTL("service:"+s.Name, "OK"); err != nil {
|
|
|
|
if err := s.ConsulAgent.PassTTL("service:"+s.AppID, "OK"); err != nil {
|
|
|
|
log.Println(err)
|
|
|
|
log.Println(err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
@@ -84,7 +87,7 @@ func (s *Service) UpdateTTL(service *consul.AgentServiceRegistration) {
|
|
|
|
|
|
|
|
|
|
|
|
func (s *Service) check() (bool, error) {
|
|
|
|
func (s *Service) check() (bool, error) {
|
|
|
|
client := &http.Client{}
|
|
|
|
client := &http.Client{}
|
|
|
|
healthUrl := fmt.Sprintf("http://%s:%d/health", s.Address, s.Port)
|
|
|
|
healthUrl := fmt.Sprintf("http://%s:%d/health", s.IP, s.Port)
|
|
|
|
req, err := http.NewRequest(http.MethodGet, healthUrl, nil)
|
|
|
|
req, err := http.NewRequest(http.MethodGet, healthUrl, nil)
|
|
|
|
if err != nil {
|
|
|
|
if err != nil {
|
|
|
|
return false, ErrServiceUnavailable
|
|
|
|
return false, ErrServiceUnavailable
|
|
|
|
@@ -105,24 +108,24 @@ func (s *Service) check() (bool, error) {
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
func (s *Service) getTags() []string {
|
|
|
|
func (s *Service) getTags() []string {
|
|
|
|
name, addr, port := s.Name, s.Address, strconv.Itoa(s.Port)
|
|
|
|
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." + name + ".rule=Host(`" + addr + "`)",
|
|
|
|
"traefik.http.routers." + s.Name + ".rule=Host(`" + s.Address + "`)",
|
|
|
|
"traefik.http.routers." + name + ".entryPoints=https",
|
|
|
|
"traefik.http.routers." + s.Name + ".entryPoints=https",
|
|
|
|
"traefik.http.routers." + name + ".service=" + name,
|
|
|
|
"traefik.http.routers." + s.Name + ".service=" + s.Name,
|
|
|
|
"traefik.http.routers." + name + ".middlewares=compress,requestid",
|
|
|
|
"traefik.http.routers." + s.Name + ".middlewares=compress,requestid",
|
|
|
|
"traefik.http.routers." + name + ".tls=true",
|
|
|
|
"traefik.http.routers." + s.Name + ".tls=true",
|
|
|
|
"traefik.http.services." + name + ".loadbalancer.server.scheme=http",
|
|
|
|
// "traefik.http.services." + s.Name + ".loadbalancer.server.scheme=http",
|
|
|
|
"traefik.http.services." + name + ".loadbalancer.server.port=" + port,
|
|
|
|
// "traefik.http.services." + s.Name + ".loadbalancer.server.port=" + port,
|
|
|
|
"traefik.http.services." + name + ".loadbalancer.passhostheader=false",
|
|
|
|
"traefik.http.services." + s.Name + ".loadbalancer.passhostheader=false",
|
|
|
|
|
|
|
|
"traefik.http.services." + s.Name + ".loadbalancer.servers." + fullName + ".url=" + bFullAddr,
|
|
|
|
"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.http.services." + name + ".loadbalancer.healthcheck.path=/health",
|
|
|
|
// "traefik.http.services." + fullName + ".loadbalancer.healthcheck.path=/health",
|
|
|
|
// "traefik.http.services." + name + ".loadbalancer.healthcheck.interval=10s",
|
|
|
|
// "traefik.http.services." + fullName + ".loadbalancer.healthcheck.interval=10s",
|
|
|
|
// "traefik.tls.certificates.certfile=/certs/client.cert",
|
|
|
|
|
|
|
|
// "traefik.tls.certificates.keyfile=/certs/client.key",
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
return tags
|
|
|
|
return tags
|
|
|
|
|