Add health check

This commit is contained in:
2024-06-22 12:18:54 +08:00
parent 9a8df85250
commit b951cd2696
18 changed files with 361 additions and 123 deletions

15
pkg/internal/gap/net.go Normal file
View File

@ -0,0 +1,15 @@
package gap
import "net"
func GetOutboundIP() (net.IP, error) {
conn, err := net.Dial("udp", "1.1.1.1:80")
if err != nil {
return nil, err
} else {
defer conn.Close()
}
localAddr := conn.LocalAddr().(*net.UDPAddr)
return localAddr.IP, nil
}

View File

@ -2,10 +2,13 @@ package gap
import (
"fmt"
"github.com/hashicorp/consul/api"
"github.com/spf13/viper"
"strconv"
"strings"
"github.com/hashicorp/consul/api"
"github.com/spf13/viper"
"google.golang.org/grpc"
"google.golang.org/grpc/credentials/insecure"
)
func Register() error {
@ -17,22 +20,32 @@ func Register() error {
return err
}
bind := strings.SplitN(viper.GetString("consul.srv_serve"), ":", 2)
baseAddr := viper.GetString("consul.srv_http")
httpBind := strings.SplitN(viper.GetString("bind"), ":", 2)
grpcBind := strings.SplitN(viper.GetString("grpc_bind"), ":", 2)
port, _ := strconv.Atoi(bind[1])
outboundIp, _ := GetOutboundIP()
port, _ := strconv.Atoi(httpBind[1])
registration := new(api.AgentServiceRegistration)
registration.ID = viper.GetString("id")
registration.Name = "Hydrogen.Paperclip"
registration.Address = bind[0]
registration.Address = outboundIp.String()
registration.Port = port
registration.Check = &api.AgentServiceCheck{
HTTP: fmt.Sprintf("%s/.well-known", baseAddr),
GRPC: fmt.Sprintf("%s:%s", outboundIp, grpcBind[1]),
Timeout: "5s",
Interval: "5s",
DeregisterCriticalServiceAfter: "10s",
Interval: "1m",
DeregisterCriticalServiceAfter: "3m",
}
return client.Agent().ServiceRegister(registration)
}
func DiscoverPassport() (*grpc.ClientConn, error) {
target := fmt.Sprintf("consul://%s/Hydrogen.Passport", viper.GetString("consul.addr"))
return grpc.NewClient(
target,
grpc.WithTransportCredentials(insecure.NewCredentials()),
grpc.WithDefaultServiceConfig(`{"loadBalancingPolicy": "round_robin"}`),
)
}