register fix

This commit is contained in:
Piotr Biernat 2022-12-01 18:54:19 +01:00
parent 0326e985cd
commit c85d881954

View File

@ -4,6 +4,7 @@ import (
"fmt"
"log"
"net/http"
"strconv"
"time"
consul "github.com/hashicorp/consul/api"
@ -46,9 +47,13 @@ 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) Register() error {
def := &consul.AgentServiceRegistration{
ID: s.Name + "_" + s.AppID,
ID: s.GetID(),
Name: s.Name,
Address: s.IP,
Port: s.Port,
@ -66,7 +71,7 @@ func (s *Service) Register() error {
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) {
@ -74,11 +79,11 @@ func (s *Service) UpdateTTL(service *consul.AgentServiceRegistration) {
for range ticker.C {
ok, err := s.check()
if !ok {
if err := s.ConsulAgent.FailTTL("service:"+s.Name+"_"+s.AppID, err.Error()); err != nil {
if err := s.ConsulAgent.FailTTL("service:"+s.GetID(), err.Error()); err != nil {
log.Println(err)
}
} else {
if err := s.ConsulAgent.PassTTL("service:"+s.Name+"_"+s.AppID, "OK"); err != nil {
if err := s.ConsulAgent.PassTTL("service:"+s.GetID(), "OK"); err != nil {
log.Println(err)
}
}
@ -108,8 +113,8 @@ func (s *Service) check() (bool, error) {
}
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....
// 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",
@ -118,11 +123,11 @@ func (s *Service) getTags() []string {
"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.server.scheme=http",
"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.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.requestid.plugin.requestid.headerName=X-Request-ID",
// "traefik.http.services." + fullName + ".loadbalancer.healthcheck.path=/health",