Drop direct connection and uses consul

This commit is contained in:
2024-06-19 23:25:40 +08:00
parent 5a3c6fcada
commit d9aa478d10
9 changed files with 78 additions and 59 deletions

View File

@ -4,10 +4,16 @@ import (
"fmt"
"github.com/hashicorp/consul/api"
"github.com/spf13/viper"
"google.golang.org/grpc"
"google.golang.org/grpc/credentials/insecure"
"strconv"
"strings"
_ "github.com/mbobakov/grpc-consul-resolver"
)
var C *api.Client
func Register() error {
cfg := api.DefaultConfig()
cfg.Address = viper.GetString("consul.addr")
@ -35,5 +41,18 @@ func Register() error {
DeregisterCriticalServiceAfter: "3m",
}
return client.Agent().ServiceRegister(registration)
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"}`),
)
}