Compare commits

...

9 Commits

Author SHA1 Message Date
6c46a0e6a4 register fix 2022-12-01 19:45:47 +01:00
ceaeecf1b4 register fix 2022-12-01 19:43:35 +01:00
0b035dfcc9 register fix 2022-12-01 19:34:10 +01:00
73d302d2c3 register fix 2022-12-01 19:05:59 +01:00
1566f31f42 register fix 2022-12-01 19:02:46 +01:00
c85d881954 register fix 2022-12-01 18:54:19 +01:00
0326e985cd fix 2022-12-01 18:30:18 +01:00
5888636310 fix 2022-12-01 18:10:16 +01:00
e76fe0fead fix 2022-12-01 17:57:17 +01:00
2 changed files with 59 additions and 14 deletions

View File

@@ -4,6 +4,8 @@ import (
"fmt"
"log"
"net/http"
"strconv"
"strings"
"time"
consul "github.com/hashicorp/consul/api"
@@ -24,7 +26,7 @@ var ErrServiceUnavailable = fmt.Errorf("Service is unavailable")
func NewService(serverAddr, appID, appName, ip, domain string, appPort int) (*Service, error) {
s := new(Service)
s.AppID = appID
s.Name = appName
s.Name = strings.Replace(appName, "-", "_", -1)
s.Address = domain
s.IP = ip
s.Port = appPort
@@ -46,13 +48,22 @@ 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) GetFullAddr() string {
return fmt.Sprintf("http://%s:%d/", s.IP, s.Port)
}
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,
Tags: s.getTags(),
Meta: s.getMetadata(),
Check: &consul.AgentServiceCheck{
TTL: s.TTL.String(),
},
@@ -66,7 +77,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 +85,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.AppID, "OK"); err != nil {
if err := s.ConsulAgent.PassTTL("service:"+s.GetID(), "OK"); err != nil {
log.Println(err)
}
}
@@ -87,7 +98,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
@@ -107,25 +118,33 @@ 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....
// 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",
"traefik.http.routers." + s.Name + ".rule=Host(`" + s.Address + "`)",
"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 + ".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.passhostheader=false",
"traefik.http.services." + s.Name + ".loadbalancer.servers." + fullName + ".url=" + bFullAddr,
"traefik.http.services." + s.Name + ".loadbalancer.server.scheme=http",
"traefik.http.services." + s.Name + ".loadbalancer.server.port=" + strconv.Itoa(s.Port),
"traefik.http.middlewares.compress.compress=true",
"traefik.http.middlewares.requestid.plugin.requestid.headerName=X-Request-ID",
// "traefik.http.services." + fullName + ".loadbalancer.healthcheck.path=/health",
// "traefik.http.services." + fullName + ".loadbalancer.healthcheck.interval=10s",
// "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.healthcheck.path=/health",
// "traefik.http.services." + s.Name + ".loadbalancer.healthcheck.interval=10s",
}
return tags

View File

@@ -0,0 +1,26 @@
package rabbitmq
import (
"log"
"github.com/streadway/amqp"
)
func Open(url string) (*amqp.Connection, *amqp.Channel, error) {
conn, err := amqp.Dial(url)
if err != nil {
return nil, nil, err
}
ch, err := conn.Channel()
if err != nil {
log.Printf("Failed to open a channel: %v\n", err)
return nil, nil, err
}
return conn, ch, nil
}
func Close(conn *amqp.Connection) {
conn.Close()
}