From 0b035dfcc90f3ef7d00f7d8aa6bb91de4db32d62 Mon Sep 17 00:00:00 2001 From: Piotr Biernat Date: Thu, 1 Dec 2022 19:34:10 +0100 Subject: [PATCH] register fix --- consul/discovery.go | 14 +++++++++++++- 1 file changed, 13 insertions(+), 1 deletion(-) diff --git a/consul/discovery.go b/consul/discovery.go index e5cb559..8097b92 100644 --- a/consul/discovery.go +++ b/consul/discovery.go @@ -51,6 +51,10 @@ func (s *Service) GetID() string { 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 { def := &consul.AgentServiceRegistration{ ID: s.GetID(), @@ -58,6 +62,7 @@ func (s *Service) Register() error { Address: s.IP, Port: s.Port, Tags: s.getTags(), + Meta: s.getMetadata(), Check: &consul.AgentServiceCheck{ TTL: s.TTL.String(), }, @@ -92,7 +97,7 @@ func (s *Service) UpdateTTL(service *consul.AgentServiceRegistration) { func (s *Service) check() (bool, error) { 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) if err != nil { return false, ErrServiceUnavailable @@ -112,6 +117,13 @@ func (s *Service) check() (bool, error) { 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() + + return m +} + 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....