2022-12-01 17:56:11 +01:00
|
|
|
package consul
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"net/http"
|
2022-12-01 19:05:59 +01:00
|
|
|
"strconv"
|
2022-12-01 17:56:11 +01:00
|
|
|
"time"
|
|
|
|
|
|
|
|
consul "github.com/hashicorp/consul/api"
|
|
|
|
)
|
|
|
|
|
|
|
|
type Service struct {
|
2022-12-05 19:23:32 +01:00
|
|
|
Name string
|
|
|
|
Address string
|
|
|
|
appID string
|
|
|
|
domain string
|
|
|
|
port int
|
|
|
|
ttl time.Duration
|
|
|
|
agent *consul.Agent
|
|
|
|
kv *consul.KV
|
2022-12-01 17:56:11 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
var ErrServiceUnavailable = fmt.Errorf("Service is unavailable")
|
|
|
|
|
2022-12-01 23:37:43 +01:00
|
|
|
func NewService(servAddr, id, name, hostname, domain string, appPort int) (*Service, error) {
|
2022-12-01 17:56:11 +01:00
|
|
|
s := new(Service)
|
2022-12-01 23:37:43 +01:00
|
|
|
s.Name = name
|
|
|
|
s.Address = hostname
|
2022-12-05 19:23:32 +01:00
|
|
|
s.appID = id
|
|
|
|
s.domain = domain
|
|
|
|
s.port = appPort
|
|
|
|
s.ttl = time.Second * 15
|
2022-12-01 17:56:11 +01:00
|
|
|
|
2022-12-01 23:24:25 +01:00
|
|
|
client, err := consul.NewClient(newClientConfig(servAddr))
|
2022-12-01 17:56:11 +01:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2022-12-05 19:23:32 +01:00
|
|
|
s.agent = client.Agent()
|
|
|
|
s.kv = client.KV()
|
2022-12-01 17:56:11 +01:00
|
|
|
|
|
|
|
return s, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func newClientConfig(serverAddr string) *consul.Config {
|
|
|
|
conf := consul.DefaultConfig()
|
|
|
|
conf.Address = serverAddr
|
|
|
|
|
|
|
|
return conf
|
|
|
|
}
|
|
|
|
|
2022-12-01 18:54:19 +01:00
|
|
|
func (s *Service) GetID() string {
|
2022-12-05 19:23:32 +01:00
|
|
|
return fmt.Sprintf("%s_%s", s.Name, s.appID)
|
2022-12-01 18:54:19 +01:00
|
|
|
}
|
|
|
|
|
2022-12-01 19:34:10 +01:00
|
|
|
func (s *Service) GetFullAddr() string {
|
2022-12-05 19:23:32 +01:00
|
|
|
return fmt.Sprintf("http://%s:%d/", s.Address, s.port)
|
2022-12-01 19:34:10 +01:00
|
|
|
}
|
|
|
|
|
2022-12-01 17:56:11 +01:00
|
|
|
func (s *Service) Register() error {
|
|
|
|
def := &consul.AgentServiceRegistration{
|
2022-12-01 18:54:19 +01:00
|
|
|
ID: s.GetID(),
|
2022-12-01 19:45:47 +01:00
|
|
|
Name: s.Name,
|
2022-12-01 23:37:43 +01:00
|
|
|
Address: s.Address,
|
2022-12-05 19:23:32 +01:00
|
|
|
Port: s.port,
|
2022-12-01 17:56:11 +01:00
|
|
|
Tags: s.getTags(),
|
|
|
|
Check: &consul.AgentServiceCheck{
|
2022-12-07 04:11:20 +01:00
|
|
|
TTL: s.ttl.String(),
|
|
|
|
DeregisterCriticalServiceAfter: "5s",
|
2022-12-01 17:56:11 +01:00
|
|
|
},
|
|
|
|
}
|
|
|
|
|
2022-12-05 19:23:32 +01:00
|
|
|
if err := s.agent.ServiceRegister(def); err != nil {
|
2022-12-01 17:56:11 +01:00
|
|
|
return err
|
|
|
|
}
|
2022-12-02 03:22:36 +01:00
|
|
|
|
2022-12-05 21:20:38 +01:00
|
|
|
go func(s *Service) { // startup register
|
2022-12-02 03:22:36 +01:00
|
|
|
ticker := time.NewTicker(time.Millisecond * 100)
|
|
|
|
for range ticker.C {
|
|
|
|
ok, _ := s.healthCheck()
|
|
|
|
if ok {
|
|
|
|
ticker.Stop()
|
|
|
|
}
|
|
|
|
}
|
2022-12-05 21:20:38 +01:00
|
|
|
}(s)
|
2022-12-02 03:22:36 +01:00
|
|
|
|
2022-12-05 21:20:38 +01:00
|
|
|
go func(s *Service) { // TTL
|
2022-12-05 19:23:32 +01:00
|
|
|
interval := s.ttl - time.Second*2
|
2022-12-02 18:48:12 +01:00
|
|
|
ticker := time.NewTicker(interval)
|
2022-12-02 03:22:36 +01:00
|
|
|
for range ticker.C {
|
2022-12-02 17:42:39 +01:00
|
|
|
_, err := s.healthCheck()
|
|
|
|
if err != nil {
|
|
|
|
fmt.Printf("TTL Error: %v\n", err)
|
|
|
|
}
|
2022-12-02 03:22:36 +01:00
|
|
|
}
|
2022-12-05 21:20:38 +01:00
|
|
|
}(s)
|
2022-12-01 17:56:11 +01:00
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
func (s *Service) Unregister() error {
|
2022-12-05 19:23:32 +01:00
|
|
|
return s.agent.ServiceDeregister(s.GetID())
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *Service) KV() *consul.KV {
|
|
|
|
return s.kv
|
2022-12-01 17:56:11 +01:00
|
|
|
}
|
|
|
|
|
2022-12-02 03:22:36 +01:00
|
|
|
func (s *Service) healthCheck() (bool, error) {
|
|
|
|
alive := func() bool {
|
|
|
|
client := &http.Client{}
|
|
|
|
healthUrl := s.GetFullAddr() + "health"
|
|
|
|
req, err := http.NewRequest(http.MethodGet, healthUrl, nil)
|
|
|
|
if err != nil {
|
|
|
|
return false
|
2022-12-01 17:56:11 +01:00
|
|
|
}
|
2022-12-02 03:22:36 +01:00
|
|
|
req.Header.Set("User-Agent", "Health Check")
|
2022-12-01 17:56:11 +01:00
|
|
|
|
2022-12-02 03:22:36 +01:00
|
|
|
resp, err := client.Do(req)
|
|
|
|
if err != nil {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
defer resp.Body.Close()
|
2022-12-01 17:56:11 +01:00
|
|
|
|
2022-12-02 03:22:36 +01:00
|
|
|
return resp.StatusCode == http.StatusOK
|
|
|
|
}()
|
2022-12-01 17:56:11 +01:00
|
|
|
|
2022-12-02 03:22:36 +01:00
|
|
|
if alive {
|
2022-12-05 19:23:32 +01:00
|
|
|
if err := s.agent.PassTTL("service:"+s.GetID(), "OK"); err != nil {
|
2022-12-02 14:21:33 +01:00
|
|
|
return false, err
|
2022-12-02 03:22:36 +01:00
|
|
|
}
|
2022-12-01 17:56:11 +01:00
|
|
|
return true, nil
|
|
|
|
}
|
|
|
|
|
2022-12-05 19:23:32 +01:00
|
|
|
if err := s.agent.FailTTL("service:"+s.GetID(), ErrServiceUnavailable.Error()); err != nil {
|
2022-12-02 14:21:33 +01:00
|
|
|
return false, err
|
2022-12-02 03:22:36 +01:00
|
|
|
}
|
2022-12-01 17:56:11 +01:00
|
|
|
return false, ErrServiceUnavailable
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *Service) getTags() []string {
|
|
|
|
tags := []string{
|
|
|
|
"traefik.enable=true",
|
2022-12-24 16:27:10 +01:00
|
|
|
// "traefik.http.routers." + s.Name + ".rule=Header(`X-API-SERVICE`, `" + s.Name + "`)",
|
|
|
|
"traefik.http.routers." + s.Name + ".rule=PathPrefix(`/catalog`)",
|
2022-12-01 17:56:11 +01:00
|
|
|
"traefik.http.routers." + s.Name + ".entryPoints=https",
|
2022-12-06 02:19:04 +01:00
|
|
|
"traefik.http.routers." + s.Name + ".tls=true",
|
2022-12-01 19:05:59 +01:00
|
|
|
"traefik.http.routers." + s.Name + ".service=" + s.Name,
|
2022-12-24 16:39:21 +01:00
|
|
|
"traefik.http.routers." + s.Name + ".middlewares=requestid,compress,stripPrefix",
|
2022-12-01 19:05:59 +01:00
|
|
|
"traefik.http.services." + s.Name + ".loadbalancer.server.scheme=http",
|
2022-12-05 19:23:32 +01:00
|
|
|
"traefik.http.services." + s.Name + ".loadbalancer.server.port=" + strconv.Itoa(s.port),
|
2022-12-24 15:09:01 +01:00
|
|
|
"traefik.http.services." + s.Name + ".loadbalancer.passhostheader=false",
|
2022-12-01 19:05:59 +01:00
|
|
|
"traefik.http.middlewares.requestid.plugin.requestid.headerName=X-Request-ID",
|
2022-12-24 16:43:19 +01:00
|
|
|
"traefik.http.middlewares.stripPrefix.stripprefix.prefixes=/catalog",
|
2022-12-24 16:39:21 +01:00
|
|
|
"traefik.http.middlewares.compress.compress=true",
|
2022-12-24 08:33:09 +01:00
|
|
|
// "traefik.http.middlewares.cors.headers.accesscontrolallowmethods=*",
|
|
|
|
// "traefik.http.middlewares.cors.headers.accesscontrolalloworiginlist=*",
|
2022-12-24 06:07:03 +01:00
|
|
|
// "traefik.http.middlewares.cors.headers.accessControlExposeHeaders=true",
|
|
|
|
// "traefik.http.middlewares.cors.headers.accesscontrolmaxage=100",
|
|
|
|
// "traefik.http.middlewares.cors.headers.addvaryheader=true",
|
2022-12-06 02:21:07 +01:00
|
|
|
"traefik.tls.certificates.certfile=/certs/client.cert",
|
|
|
|
"traefik.tls.certificates.keyfile=/certs/client.key",
|
2022-12-01 18:54:19 +01:00
|
|
|
// "traefik.http.services." + s.Name + ".loadbalancer.servers." + fullName + "=" + bFullAddr,
|
|
|
|
// "traefik.http.services." + s.Name + ".loadbalancer.servers." + fullName + ".url=" + bFullAddr,
|
2022-12-01 19:02:46 +01:00
|
|
|
// "traefik.http.services." + s.Name + ".loadbalancer.healthcheck.path=/health",
|
|
|
|
// "traefik.http.services." + s.Name + ".loadbalancer.healthcheck.interval=10s",
|
2022-12-01 17:56:11 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
return tags
|
|
|
|
}
|