2024-06-16 14:16:09 +00:00
|
|
|
package gap
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"github.com/hashicorp/consul/api"
|
|
|
|
"github.com/spf13/viper"
|
2024-06-19 15:25:40 +00:00
|
|
|
"google.golang.org/grpc"
|
|
|
|
"google.golang.org/grpc/credentials/insecure"
|
2024-06-16 14:16:09 +00:00
|
|
|
"strconv"
|
|
|
|
"strings"
|
2024-06-19 15:25:40 +00:00
|
|
|
|
|
|
|
_ "github.com/mbobakov/grpc-consul-resolver"
|
2024-06-16 14:16:09 +00:00
|
|
|
)
|
|
|
|
|
2024-06-19 15:25:40 +00:00
|
|
|
var C *api.Client
|
|
|
|
|
2024-06-16 14:16:09 +00:00
|
|
|
func Register() error {
|
|
|
|
cfg := api.DefaultConfig()
|
|
|
|
cfg.Address = viper.GetString("consul.addr")
|
|
|
|
|
|
|
|
client, err := api.NewClient(cfg)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2024-06-17 14:21:34 +00:00
|
|
|
httpBind := strings.SplitN(viper.GetString("bind"), ":", 2)
|
|
|
|
grpcBind := strings.SplitN(viper.GetString("grpc_bind"), ":", 2)
|
2024-06-16 14:16:09 +00:00
|
|
|
|
2024-06-17 14:21:34 +00:00
|
|
|
outboundIp, _ := GetOutboundIP()
|
|
|
|
port, _ := strconv.Atoi(httpBind[1])
|
2024-06-16 14:16:09 +00:00
|
|
|
|
|
|
|
registration := new(api.AgentServiceRegistration)
|
|
|
|
registration.ID = viper.GetString("id")
|
|
|
|
registration.Name = "Hydrogen.Passport"
|
2024-06-17 14:21:34 +00:00
|
|
|
registration.Address = outboundIp.String()
|
2024-06-16 14:16:09 +00:00
|
|
|
registration.Port = port
|
|
|
|
registration.Check = &api.AgentServiceCheck{
|
2024-06-17 14:21:34 +00:00
|
|
|
GRPC: fmt.Sprintf("%s:%s", outboundIp, grpcBind[1]),
|
2024-06-16 14:16:09 +00:00
|
|
|
Timeout: "5s",
|
2024-06-17 14:21:34 +00:00
|
|
|
Interval: "1m",
|
|
|
|
DeregisterCriticalServiceAfter: "3m",
|
2024-06-16 14:16:09 +00:00
|
|
|
}
|
|
|
|
|
2024-06-19 15:25:40 +00:00
|
|
|
if err := client.Agent().ServiceRegister(registration); err != nil {
|
|
|
|
return err
|
|
|
|
} else {
|
|
|
|
C = client
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func DiscoverPaperclip() (*grpc.ClientConn, error) {
|
|
|
|
return grpc.NewClient(
|
|
|
|
"consul://127.0.0.1:8500/Hydrogen.Paperclip",
|
|
|
|
grpc.WithTransportCredentials(insecure.NewCredentials()),
|
|
|
|
grpc.WithDefaultServiceConfig(`{"loadBalancingPolicy": "round_robin"}`),
|
|
|
|
)
|
2024-06-16 14:16:09 +00:00
|
|
|
}
|