From ff05219e284145c078646d7caa356a588203ac56 Mon Sep 17 00:00:00 2001 From: Vasiliy Tolstov Date: Fri, 9 Oct 2020 17:42:55 +0300 Subject: [PATCH] use helper Signed-off-by: Vasiliy Tolstov --- go.mod | 2 +- go.sum | 4 ++-- grpc.go | 50 +++++--------------------------------------------- 3 files changed, 8 insertions(+), 48 deletions(-) diff --git a/go.mod b/go.mod index b192646..4172cd9 100644 --- a/go.mod +++ b/go.mod @@ -6,7 +6,7 @@ require ( github.com/golang/protobuf v1.4.2 github.com/google/go-cmp v0.5.1 // indirect github.com/unistack-org/micro-codec-bytes v0.0.0-20200828083432-4e49e953d844 - github.com/unistack-org/micro/v3 v3.0.0-gamma.0.20201009132147-336868ed0d8d + github.com/unistack-org/micro/v3 v3.0.0-gamma.0.20201009135158-2fc47782cf47 golang.org/x/net v0.0.0-20200904194848-62affa334b73 golang.org/x/sys v0.0.0-20200803210538-64077c9b5642 // indirect golang.org/x/text v0.3.3 // indirect diff --git a/go.sum b/go.sum index a44beb2..53e29a9 100644 --- a/go.sum +++ b/go.sum @@ -265,8 +265,8 @@ github.com/unistack-org/micro-config-cmd v0.0.0-20200920140133-0853deb2e5dc/go.m github.com/unistack-org/micro/v3 v3.0.0-20200827083227-aa99378adc6e/go.mod h1:rPQbnry3nboAnMczj8B1Gzlcyv/HYoMZLgd3/3nttJ4= github.com/unistack-org/micro/v3 v3.0.0-gamma.0.20200909210629-caec730248b1/go.mod h1:mmqHR9WelHUXqg2mELjsQ+FJHcWs6mNmXg+wEYO2T3c= github.com/unistack-org/micro/v3 v3.0.0-gamma.0.20200920135754-1cbd1d2bad83/go.mod h1:HUzMG4Mcy97958VxWTg8zuazZgwQ/aoLZ8wtBVONwRE= -github.com/unistack-org/micro/v3 v3.0.0-gamma.0.20201009132147-336868ed0d8d h1:LcDlJMSPibasCxm8WNWM92bz+7NFX8StPfM+4u/fme8= -github.com/unistack-org/micro/v3 v3.0.0-gamma.0.20201009132147-336868ed0d8d/go.mod h1:aL+8VhSXpx0SuEeXPOWUo5BgS7kyvWYobeXFay90UUM= +github.com/unistack-org/micro/v3 v3.0.0-gamma.0.20201009135158-2fc47782cf47 h1:3d/HgT7Iq/UIw5OGyzfUeZPJwydhBohh9shyGJH14EA= +github.com/unistack-org/micro/v3 v3.0.0-gamma.0.20201009135158-2fc47782cf47/go.mod h1:aL+8VhSXpx0SuEeXPOWUo5BgS7kyvWYobeXFay90UUM= github.com/urfave/cli v1.22.1/go.mod h1:Gos4lmkARVdJ6EkW0WaNv/tZAAMe9V7XWyB60NtXRu0= github.com/vultr/govultr v0.1.4/go.mod h1:9H008Uxr/C4vFNGLqKx232C206GL0PBHzOP0809bGNA= github.com/xeipuuv/gojsonpointer v0.0.0-20180127040702-4e3ac2762d5f/go.mod h1:N2zxlSyiKSe5eX1tZViRH5QA0qijqEDrYZiPEAiq3wU= diff --git a/grpc.go b/grpc.go index fc59247..360fdd1 100644 --- a/grpc.go +++ b/grpc.go @@ -25,6 +25,7 @@ import ( "github.com/unistack-org/micro/v3/util/backoff" mgrpc "github.com/unistack-org/micro/v3/util/grpc" mnet "github.com/unistack-org/micro/v3/util/net" + regutil "github.com/unistack-org/micro/v3/util/registry" "golang.org/x/net/netutil" "google.golang.org/grpc" "google.golang.org/grpc/codes" @@ -718,50 +719,14 @@ func (g *grpcServer) Register() error { } var err error - var advt, host, port string + var service *registry.Service var cacheService bool - advt = config.Advertise - - if cnt := strings.Count(advt, ":"); cnt >= 1 { - // ipv6 address in format [host]:port or ipv4 host:port - host, port, err = net.SplitHostPort(advt) - if err != nil { - return err - } - } else { - host = advt - } - - if ip := net.ParseIP(host); ip != nil { - cacheService = true - } - - addr, err := addr.Extract(host) + service, err = regutil.NewService(g) if err != nil { return err } - // make copy of metadata - md := meta.Copy(config.Metadata) - - // register service - node := ®istry.Node{ - Id: config.Name + "-" + config.Id, - Address: mnet.HostPort(addr, port), - Metadata: md, - } - - if node.Metadata == nil { - node.Metadata = make(map[string]string, 5) - } - - node.Metadata["broker"] = config.Broker.String() - node.Metadata["registry"] = config.Registry.String() - node.Metadata["server"] = g.String() - node.Metadata["transport"] = g.String() - node.Metadata["protocol"] = "grpc" - g.RLock() // Maps are ordered randomly, sort the keys for consistency var handlerList []string @@ -794,12 +759,7 @@ func (g *grpcServer) Register() error { } g.RUnlock() - service := ®istry.Service{ - Name: config.Name, - Version: config.Version, - Nodes: []*registry.Node{node}, - Endpoints: endpoints, - } + service.Endpoints = endpoints g.RLock() registered := g.registered @@ -807,7 +767,7 @@ func (g *grpcServer) Register() error { if !registered { if logger.V(logger.InfoLevel) { - logger.Infof("Registry [%s] Registering node: %s", config.Registry.String(), node.Id) + logger.Infof("Registry [%s] Registering node: %s", config.Registry.String(), service.Nodes[0].Id) } }