|
|
|
|
@@ -21,7 +21,7 @@ type Service struct {
|
|
|
|
|
ttl time.Duration
|
|
|
|
|
client *consul.Client
|
|
|
|
|
agent *consul.Agent
|
|
|
|
|
connect *consul.Connect
|
|
|
|
|
connect *connect.Service
|
|
|
|
|
kv *consul.KV
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@@ -61,18 +61,29 @@ func (s *Service) GetID() 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 {
|
|
|
|
|
def := &consul.AgentServiceRegistration{
|
|
|
|
|
ID: s.GetID(),
|
|
|
|
|
// Kind: consul.ServiceKindConnectProxy,
|
|
|
|
|
Name: s.Name,
|
|
|
|
|
Address: s.Address,
|
|
|
|
|
Port: s.port,
|
|
|
|
|
Tags: s.getTags(),
|
|
|
|
|
Connect: &consul.AgentServiceConnect{
|
|
|
|
|
Native: true,
|
|
|
|
|
// SidecarService: &consul.AgentServiceRegistration{
|
|
|
|
|
// Port: s.port,
|
|
|
|
|
// },
|
|
|
|
|
},
|
|
|
|
|
// Proxy: &consul.AgentServiceConnectProxyConfig{
|
|
|
|
|
// DestinationServiceName: s.Name,
|
|
|
|
|
// },
|
|
|
|
|
Check: &consul.AgentServiceCheck{
|
|
|
|
|
TTL: s.ttl.String(),
|
|
|
|
|
Status: "passing",
|
|
|
|
|
DeregisterCriticalServiceAfter: "10s",
|
|
|
|
|
},
|
|
|
|
|
}
|
|
|
|
|
@@ -81,16 +92,23 @@ func (s *Service) Register() error {
|
|
|
|
|
return err
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
go func(s *Service) { // startup register
|
|
|
|
|
return nil
|
|
|
|
|
}
|
|
|
|
|
func (s *Service) Unregister() error {
|
|
|
|
|
return s.agent.ServiceDeregister(s.GetID())
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func (s *Service) InitHealthChecks() {
|
|
|
|
|
go func() { // startup register
|
|
|
|
|
ticker := time.NewTicker(time.Second * 1)
|
|
|
|
|
for range ticker.C {
|
|
|
|
|
if ok, _ := s.healthCheck(); ok {
|
|
|
|
|
ticker.Stop()
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}(s)
|
|
|
|
|
}()
|
|
|
|
|
|
|
|
|
|
go func(s *Service) { // TTL
|
|
|
|
|
go func() { // TTL
|
|
|
|
|
interval := s.ttl - time.Second*2 // 2 seconds overhead
|
|
|
|
|
ticker := time.NewTicker(interval)
|
|
|
|
|
for range ticker.C {
|
|
|
|
|
@@ -98,16 +116,21 @@ func (s *Service) Register() error {
|
|
|
|
|
fmt.Printf("TTL Error #: %v\n", err)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}(s)
|
|
|
|
|
|
|
|
|
|
return nil
|
|
|
|
|
}
|
|
|
|
|
func (s *Service) Unregister() error {
|
|
|
|
|
return s.agent.ServiceDeregister(s.GetID())
|
|
|
|
|
}()
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func (s *Service) Connect() (*connect.Service, error) {
|
|
|
|
|
return connect.NewService(s.Name, s.client)
|
|
|
|
|
// srvName := s.Name
|
|
|
|
|
srvName := s.Name
|
|
|
|
|
svc, err := connect.NewService(srvName, s.client)
|
|
|
|
|
s.connect = svc
|
|
|
|
|
cnf := svc.ServerTLSConfig()
|
|
|
|
|
fmt.Printf("CONNECT SERVER:: %s CONFIG:: %v\n", srvName, cnf)
|
|
|
|
|
for k, c := range cnf.Certificates {
|
|
|
|
|
fmt.Printf("CONNECT CERT %d: %v", k, c)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return svc, err
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func (s *Service) KV() *consul.KV {
|
|
|
|
|
@@ -117,19 +140,25 @@ func (s *Service) KV() *consul.KV {
|
|
|
|
|
func (s *Service) healthCheck() (bool, error) {
|
|
|
|
|
alive := func() bool {
|
|
|
|
|
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)
|
|
|
|
|
if err != nil {
|
|
|
|
|
return false
|
|
|
|
|
}
|
|
|
|
|
req.Header.Set("User-Agent", "service/internal")
|
|
|
|
|
|
|
|
|
|
fmt.Printf("Sending HEALTH CHECK request to: %s\n", healthUrl)
|
|
|
|
|
resp, err := client.Do(req)
|
|
|
|
|
if err != nil {
|
|
|
|
|
fmt.Printf("Sending HEALTH CHECK request error: %v\n", err)
|
|
|
|
|
return false
|
|
|
|
|
}
|
|
|
|
|
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
|
|
|
|
|
}()
|
|
|
|
|
|
|
|
|
|
|