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" "fmt"
"log" "log"
"net/http" "net/http"
"strconv"
"time" "time"
consul "github.com/hashicorp/consul/api" consul "github.com/hashicorp/consul/api"
@ -46,9 +47,13 @@ func newClientConfig(serverAddr string) *consul.Config {
return conf return conf
} }
func (s *Service) GetID() string {
return fmt.Sprintf("%s_%s", s.Name, s.AppID)
}
func (s *Service) Register() error { func (s *Service) Register() error {
def := &consul.AgentServiceRegistration{ def := &consul.AgentServiceRegistration{
ID: s.Name + "_" + s.AppID, ID: s.GetID(),
Name: s.Name, Name: s.Name,
Address: s.IP, Address: s.IP,
Port: s.Port, Port: s.Port,
@ -66,7 +71,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 + "_" + s.AppID) return s.ConsulAgent.ServiceDeregister(s.GetID())
} }
func (s *Service) UpdateTTL(service *consul.AgentServiceRegistration) { func (s *Service) UpdateTTL(service *consul.AgentServiceRegistration) {
@ -74,11 +79,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+"_"+s.AppID, err.Error()); err != nil { if err := s.ConsulAgent.FailTTL("service:"+s.GetID(), err.Error()); err != nil {
log.Println(err) log.Println(err)
} }
} else { } 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) log.Println(err)
} }
} }
@ -108,8 +113,8 @@ func (s *Service) check() (bool, error) {
} }
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....
tags := []string{ tags := []string{
"traefik.enable=true", "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 + ".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.routers." + s.Name + ".tls=true",
// "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=" + 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.passhostheader=false",
"traefik.http.services." + s.Name + ".loadbalancer.servers." + fullName + "=" + bFullAddr, // "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 + ".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." + fullName + ".loadbalancer.healthcheck.path=/health",