|
|
|
@@ -21,7 +21,7 @@ type Service struct {
|
|
|
|
ttl time.Duration
|
|
|
|
ttl time.Duration
|
|
|
|
client *consul.Client
|
|
|
|
client *consul.Client
|
|
|
|
agent *consul.Agent
|
|
|
|
agent *consul.Agent
|
|
|
|
connect *consul.Connect
|
|
|
|
connect *connect.Service
|
|
|
|
kv *consul.KV
|
|
|
|
kv *consul.KV
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
@@ -61,7 +61,7 @@ func (s *Service) GetID() string {
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
func (s *Service) GetFullAddr() string {
|
|
|
|
func (s *Service) GetFullAddr() string {
|
|
|
|
return fmt.Sprintf("http://%s:%d/", s.Address, s.port)
|
|
|
|
return fmt.Sprintf("https://%s:%d/", s.domain, s.port)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
func (s *Service) Register() error {
|
|
|
|
func (s *Service) Register() error {
|
|
|
|
@@ -73,6 +73,7 @@ func (s *Service) Register() error {
|
|
|
|
Tags: s.getTags(),
|
|
|
|
Tags: s.getTags(),
|
|
|
|
Check: &consul.AgentServiceCheck{
|
|
|
|
Check: &consul.AgentServiceCheck{
|
|
|
|
TTL: s.ttl.String(),
|
|
|
|
TTL: s.ttl.String(),
|
|
|
|
|
|
|
|
Status: "passing",
|
|
|
|
DeregisterCriticalServiceAfter: "10s",
|
|
|
|
DeregisterCriticalServiceAfter: "10s",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
}
|
|
|
|
@@ -107,7 +108,15 @@ func (s *Service) Unregister() error {
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
func (s *Service) Connect() (*connect.Service, error) {
|
|
|
|
func (s *Service) Connect() (*connect.Service, error) {
|
|
|
|
return connect.NewService(s.Name, s.client)
|
|
|
|
fmt.Printf("CONNECT SERVER: %s\n", s.Name)
|
|
|
|
|
|
|
|
svc, err := connect.NewService(s.Name, s.client)
|
|
|
|
|
|
|
|
s.connect = svc
|
|
|
|
|
|
|
|
cnf := svc.ServerTLSConfig()
|
|
|
|
|
|
|
|
for k, c := range cnf.Certificates {
|
|
|
|
|
|
|
|
fmt.Printf("CONNECT CERT %d: %v", k, c)
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
return svc, err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
func (s *Service) KV() *consul.KV {
|
|
|
|
func (s *Service) KV() *consul.KV {
|
|
|
|
@@ -117,19 +126,24 @@ func (s *Service) KV() *consul.KV {
|
|
|
|
func (s *Service) healthCheck() (bool, error) {
|
|
|
|
func (s *Service) healthCheck() (bool, error) {
|
|
|
|
alive := func() bool {
|
|
|
|
alive := func() bool {
|
|
|
|
client := &http.Client{}
|
|
|
|
client := &http.Client{}
|
|
|
|
healthUrl := s.GetFullAddr() + "health"
|
|
|
|
healthUrl := fmt.Sprintf("%s%s?name=%s", s.GetFullAddr(), "health", s.Name)
|
|
|
|
req, err := http.NewRequest(http.MethodGet, healthUrl, nil)
|
|
|
|
req, err := http.NewRequest(http.MethodGet, healthUrl, nil)
|
|
|
|
if err != nil {
|
|
|
|
if err != nil {
|
|
|
|
return false
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
}
|
|
|
|
req.Header.Set("User-Agent", "service/internal")
|
|
|
|
req.Header.Set("User-Agent", "service/internal")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
fmt.Printf("Sending HEALTH CHECK request to: %s\n", healthUrl)
|
|
|
|
resp, err := client.Do(req)
|
|
|
|
resp, err := client.Do(req)
|
|
|
|
if err != nil {
|
|
|
|
if err != nil {
|
|
|
|
return false
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
}
|
|
|
|
defer resp.Body.Close()
|
|
|
|
defer resp.Body.Close()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
var body []byte
|
|
|
|
|
|
|
|
resp.Body.Read(body)
|
|
|
|
|
|
|
|
fmt.Printf("HEALTH CHECK response to: %v -- %v\n", resp, body)
|
|
|
|
|
|
|
|
|
|
|
|
return resp.StatusCode == http.StatusOK
|
|
|
|
return resp.StatusCode == http.StatusOK
|
|
|
|
}()
|
|
|
|
}()
|
|
|
|
|
|
|
|
|
|
|
|
|