From bf0e46dc0d9ca0e08ec9efca6cb4c84041374513 Mon Sep 17 00:00:00 2001 From: Shu Xian Date: Wed, 14 Aug 2019 21:32:28 +0800 Subject: [PATCH] fix registry addr error for mq-rpc --- registry/consul/consul.go | 6 +++++- server/rpc_server.go | 7 ++++++- util/net/net.go | 11 ++++++----- 3 files changed, 17 insertions(+), 7 deletions(-) diff --git a/registry/consul/consul.go b/registry/consul/consul.go index 4b6fa770..007d7369 100644 --- a/registry/consul/consul.go +++ b/registry/consul/consul.go @@ -13,6 +13,7 @@ import ( consul "github.com/hashicorp/consul/api" "github.com/micro/go-micro/registry" + mnet "github.com/micro/go-micro/util/net" hash "github.com/mitchellh/hashstructure" ) @@ -250,6 +251,9 @@ func (c *consulRegistry) Register(s *registry.Service, opts ...registry.Register } host, pt, _ := net.SplitHostPort(node.Address) + if host == "" { + host = node.Address + } port, _ := strconv.Atoi(pt) // register the service @@ -351,7 +355,7 @@ func (c *consulRegistry) GetService(name string) ([]*registry.Service, error) { svc.Nodes = append(svc.Nodes, ®istry.Node{ Id: id, - Address: fmt.Sprintf("%s:%d", address, s.Service.Port), + Address: mnet.HostPort(address, s.Service.Port), Metadata: decodeMetadata(s.Service.Tags), }) } diff --git a/server/rpc_server.go b/server/rpc_server.go index 90b00862..c6576a71 100644 --- a/server/rpc_server.go +++ b/server/rpc_server.go @@ -316,10 +316,15 @@ func (s *rpcServer) Register() error { md[k] = v } + // mq-rpc(eg. nats) doesn't need the port. its addr is queue name. + if port != "" { + addr = mnet.HostPort(addr, port) + } + // register service node := ®istry.Node{ Id: config.Name + "-" + config.Id, - Address: mnet.HostPort(addr, port), + Address: addr, Metadata: md, } diff --git a/util/net/net.go b/util/net/net.go index 678e2568..0ce674fa 100644 --- a/util/net/net.go +++ b/util/net/net.go @@ -14,12 +14,13 @@ func HostPort(addr string, port interface{}) string { if strings.Count(addr, ":") > 0 { host = fmt.Sprintf("[%s]", addr) } - // TODO check for NATS case - if v, ok := port.(string); ok { - if v == "" { - return fmt.Sprintf("%s", host) - } + // when port is blank or 0, host is a queue name + if v, ok := port.(string); ok && v == "" { + return host + } else if v, ok := port.(int); ok && v == 0 && net.ParseIP(host) == nil { + return host } + return fmt.Sprintf("%s:%v", host, port) }