Compare commits
7 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
| ba6bd4137c | |||
| 48a7e968d1 | |||
| 6c46a0e6a4 | |||
| ceaeecf1b4 | |||
| 0b035dfcc9 | |||
| 73d302d2c3 | |||
| 1566f31f42 |
@@ -5,6 +5,7 @@ import (
|
|||||||
"log"
|
"log"
|
||||||
"net/http"
|
"net/http"
|
||||||
"strconv"
|
"strconv"
|
||||||
|
"strings"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
consul "github.com/hashicorp/consul/api"
|
consul "github.com/hashicorp/consul/api"
|
||||||
@@ -25,7 +26,7 @@ var ErrServiceUnavailable = fmt.Errorf("Service is unavailable")
|
|||||||
func NewService(serverAddr, appID, appName, ip, domain 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.AppID = appID
|
||||||
s.Name = appName
|
s.Name = strings.Replace(appName, "-", "", -1)
|
||||||
s.Address = domain
|
s.Address = domain
|
||||||
s.IP = ip
|
s.IP = ip
|
||||||
s.Port = appPort
|
s.Port = appPort
|
||||||
@@ -51,6 +52,10 @@ 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 {
|
||||||
|
return fmt.Sprintf("http://%s:%d/", s.IP, s.Port)
|
||||||
|
}
|
||||||
|
|
||||||
func (s *Service) Register() error {
|
func (s *Service) Register() error {
|
||||||
def := &consul.AgentServiceRegistration{
|
def := &consul.AgentServiceRegistration{
|
||||||
ID: s.GetID(),
|
ID: s.GetID(),
|
||||||
@@ -58,6 +63,7 @@ func (s *Service) Register() error {
|
|||||||
Address: s.IP,
|
Address: s.IP,
|
||||||
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(),
|
||||||
},
|
},
|
||||||
@@ -92,7 +98,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.IP, s.Port)
|
healthUrl := fmt.Sprintf("http://%s/health", s.GetFullAddr())
|
||||||
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
|
||||||
@@ -112,6 +118,15 @@ func (s *Service) check() (bool, 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)
|
// 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....
|
// bFullAddr := fmt.Sprintf("http://%s:%d/", s.IP, s.Port) // FIXME: declare one once - dont need to refresh....
|
||||||
@@ -120,18 +135,18 @@ func (s *Service) getTags() []string {
|
|||||||
"traefik.enable=true",
|
"traefik.enable=true",
|
||||||
"traefik.http.routers." + s.Name + ".rule=Host(`" + s.Address + "`)",
|
"traefik.http.routers." + s.Name + ".rule=Host(`" + s.Address + "`)",
|
||||||
"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 + ".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.routers." + s.Name + ".tls=true",
|
"traefik.http.services." + s.Name + ".loadbalancer.passhostheader=false",
|
||||||
"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.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.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." + fullName + ".loadbalancer.healthcheck.path=/health",
|
// "traefik.http.services." + s.Name + ".loadbalancer.servers." + fullName + "=" + bFullAddr,
|
||||||
// "traefik.http.services." + fullName + ".loadbalancer.healthcheck.interval=10s",
|
// "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
|
return tags
|
||||||
|
|||||||
Reference in New Issue
Block a user