consul fix
This commit is contained in:
parent
97afd0d966
commit
4050a3a13d
@ -4,15 +4,17 @@ import (
|
||||
"fmt"
|
||||
"log"
|
||||
"net/http"
|
||||
"strconv"
|
||||
"os"
|
||||
"time"
|
||||
|
||||
consul "github.com/hashicorp/consul/api"
|
||||
)
|
||||
|
||||
type Service struct {
|
||||
AppID string
|
||||
Name string
|
||||
Address string
|
||||
IP string
|
||||
Port int
|
||||
TTL time.Duration
|
||||
ConsulAgent *consul.Agent
|
||||
@ -20,10 +22,14 @@ type Service struct {
|
||||
|
||||
var ErrServiceUnavailable = fmt.Errorf("Service is unavailable")
|
||||
|
||||
func NewService(serverAddr, appName, appDomain string, appPort int) (*Service, error) {
|
||||
func NewService(serverAddr, appID, appName, ip, domain string, appPort int) (*Service, error) {
|
||||
hostname, _ := os.Hostname()
|
||||
fmt.Println("hostname:" + hostname)
|
||||
s := new(Service)
|
||||
s.AppID = appID
|
||||
s.Name = appName
|
||||
s.Address = appDomain
|
||||
s.Address = domain
|
||||
s.IP = ip
|
||||
s.Port = appPort
|
||||
s.TTL = time.Second * 15
|
||||
|
||||
@ -45,9 +51,9 @@ func newClientConfig(serverAddr string) *consul.Config {
|
||||
|
||||
func (s *Service) Register() error {
|
||||
def := &consul.AgentServiceRegistration{
|
||||
// ID: s.Name,
|
||||
ID: s.AppID,
|
||||
Name: s.Name,
|
||||
Address: s.Address,
|
||||
Address: s.IP,
|
||||
Port: s.Port,
|
||||
Tags: s.getTags(),
|
||||
Check: &consul.AgentServiceCheck{
|
||||
@ -63,7 +69,7 @@ func (s *Service) Register() error {
|
||||
return nil
|
||||
}
|
||||
func (s *Service) Unregister() error {
|
||||
return s.ConsulAgent.ServiceDeregister(s.Name)
|
||||
return s.ConsulAgent.ServiceDeregister(s.AppID)
|
||||
}
|
||||
|
||||
func (s *Service) UpdateTTL(service *consul.AgentServiceRegistration) {
|
||||
@ -84,7 +90,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.Address, s.Port)
|
||||
healthUrl := fmt.Sprintf("http://%s:%d/health", s.IP, s.Port)
|
||||
req, err := http.NewRequest(http.MethodGet, healthUrl, nil)
|
||||
if err != nil {
|
||||
return false, ErrServiceUnavailable
|
||||
@ -105,24 +111,23 @@ func (s *Service) check() (bool, error) {
|
||||
}
|
||||
|
||||
func (s *Service) getTags() []string {
|
||||
name, addr, port := s.Name, s.Address, strconv.Itoa(s.Port)
|
||||
fullName := fmt.Sprintf("%s_%s", s.Name, s.AppID)
|
||||
|
||||
tags := []string{
|
||||
"traefik.enable=true",
|
||||
"traefik.http.routers." + name + ".rule=Host(`" + addr + "`)",
|
||||
"traefik.http.routers." + name + ".entryPoints=https",
|
||||
"traefik.http.routers." + name + ".service=" + name,
|
||||
"traefik.http.routers." + name + ".middlewares=compress,requestid",
|
||||
"traefik.http.routers." + name + ".tls=true",
|
||||
"traefik.http.services." + name + ".loadbalancer.server.scheme=http",
|
||||
"traefik.http.services." + name + ".loadbalancer.server.port=" + port,
|
||||
"traefik.http.services." + name + ".loadbalancer.passhostheader=false",
|
||||
"traefik.http.routers." + s.Name + ".rule=Host(`" + s.Address + "`)",
|
||||
"traefik.http.routers." + s.Name + ".entryPoints=https",
|
||||
"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.servers." + fullName + "=" + s.Address,
|
||||
"traefik.http.services." + s.Name + ".loadbalancer.passhostheader=false",
|
||||
"traefik.http.middlewares.compress.compress=true",
|
||||
"traefik.http.middlewares.requestid.plugin.requestid.headerName=X-Request-ID",
|
||||
// "traefik.http.services." + name + ".loadbalancer.healthcheck.path=/health",
|
||||
// "traefik.http.services." + name + ".loadbalancer.healthcheck.interval=10s",
|
||||
// "traefik.tls.certificates.certfile=/certs/client.cert",
|
||||
// "traefik.tls.certificates.keyfile=/certs/client.key",
|
||||
// "traefik.http.services." + fullName + ".loadbalancer.healthcheck.path=/health",
|
||||
// "traefik.http.services." + fullName + ".loadbalancer.healthcheck.interval=10s",
|
||||
}
|
||||
|
||||
return tags
|
||||
|
Loading…
Reference in New Issue
Block a user