1
0
mirror of https://github.com/go-micro/go-micro.git synced 2025-08-10 21:52:01 +02:00

Remove Port from registry

This commit is contained in:
Asim Aslam
2019-07-08 08:01:42 +01:00
parent 5b7454e5a8
commit e0bf1c2283
35 changed files with 92 additions and 228 deletions

View File

@@ -7,6 +7,7 @@ import (
"net"
"net/http"
"runtime"
"strconv"
"sync"
"time"
@@ -220,7 +221,7 @@ func (c *consulRegistry) Register(s *registry.Service, opts ...registry.Register
deregTTL := getDeregisterTTL(regInterval)
check = &consul.AgentServiceCheck{
TCP: fmt.Sprintf("%s:%d", node.Address, node.Port),
TCP: node.Address,
Interval: fmt.Sprintf("%v", regInterval),
DeregisterCriticalServiceAfter: fmt.Sprintf("%v", deregTTL),
}
@@ -235,13 +236,16 @@ func (c *consulRegistry) Register(s *registry.Service, opts ...registry.Register
}
}
host, pt, _ := net.SplitHostPort(node.Address)
port, _ := strconv.Atoi(pt)
// register the service
asr := &consul.AgentServiceRegistration{
ID: node.Id,
Name: s.Name,
Tags: tags,
Port: node.Port,
Address: node.Address,
Port: port,
Address: host,
Check: check,
}
@@ -334,8 +338,7 @@ func (c *consulRegistry) GetService(name string) ([]*registry.Service, error) {
svc.Nodes = append(svc.Nodes, &registry.Node{
Id: id,
Address: address,
Port: s.Service.Port,
Address: fmt.Sprintf("%s:%d", address, s.Service.Port),
Metadata: decodeMetadata(s.Service.Tags),
})
}

View File

@@ -1,6 +1,7 @@
package consul
import (
"fmt"
"log"
"os"
"sync"
@@ -102,8 +103,7 @@ func (cw *consulWatcher) serviceHandler(idx uint64, data interface{}) {
svc.Nodes = append(svc.Nodes, &registry.Node{
Id: id,
Address: address,
Port: e.Service.Port,
Address: fmt.Sprintf("%s:%d", address, e.Service.Port),
Metadata: decodeMetadata(e.Service.Tags),
})
}

View File

@@ -3,7 +3,9 @@ package registry
import (
"context"
"fmt"
"net"
"strconv"
"strings"
"sync"
"time"
@@ -127,14 +129,22 @@ func (m *mdnsRegistry) Register(service *Service, opts ...RegisterOption) error
continue
}
//
host, pt, err := net.SplitHostPort(node.Address)
if err != nil {
gerr = err
continue
}
port, _ := strconv.Atoi(pt)
// we got here, new node
s, err := mdns.NewMDNSService(
node.Id,
service.Name,
"",
"",
node.Port,
[]net.IP{net.ParseIP(node.Address)},
port,
[]net.IP{net.ParseIP(host)},
txt,
)
if err != nil {
@@ -238,8 +248,7 @@ func (m *mdnsRegistry) GetService(service string) ([]*Service, error) {
s.Nodes = append(s.Nodes, &Node{
Id: strings.TrimSuffix(e.Name, "."+p.Service+"."+p.Domain+"."),
Address: e.AddrV4.String(),
Port: e.Port,
Address: fmt.Sprintf("%s:%d", e.AddrV4.String(), e.Port),
Metadata: txt.Metadata,
})

View File

@@ -13,8 +13,7 @@ func TestMDNS(t *testing.T) {
Nodes: []*Node{
&Node{
Id: "test1-1",
Address: "10.0.0.1",
Port: 10001,
Address: "10.0.0.1:10001",
Metadata: map[string]string{
"foo": "bar",
},
@@ -27,8 +26,7 @@ func TestMDNS(t *testing.T) {
Nodes: []*Node{
&Node{
Id: "test2-1",
Address: "10.0.0.2",
Port: 10002,
Address: "10.0.0.2:10002",
Metadata: map[string]string{
"foo2": "bar2",
},
@@ -41,8 +39,7 @@ func TestMDNS(t *testing.T) {
Nodes: []*Node{
&Node{
Id: "test3-1",
Address: "10.0.0.3",
Port: 10003,
Address: "10.0.0.3:10003",
Metadata: map[string]string{
"foo3": "bar3",
},
@@ -92,10 +89,6 @@ func TestMDNS(t *testing.T) {
if node.Address != service.Nodes[0].Address {
t.Fatalf("Expected node address %s got %s", service.Nodes[0].Address, node.Address)
}
if node.Port != service.Nodes[0].Port {
t.Fatalf("Expected node port %d got %d", service.Nodes[0].Port, node.Port)
}
}
services, err := r.ListServices()

View File

@@ -1,6 +1,7 @@
package registry
import (
"fmt"
"strings"
"github.com/micro/mdns"
@@ -52,8 +53,7 @@ func (m *mdnsWatcher) Next() (*Result, error) {
service.Nodes = append(service.Nodes, &Node{
Id: strings.TrimSuffix(e.Name, "."+service.Name+".local."),
Address: e.AddrV4.String(),
Port: e.Port,
Address: fmt.Sprintf("%s:%d", e.AddrV4.String(), e.Port),
Metadata: txt.Metadata,
})

View File

@@ -15,13 +15,11 @@ var (
Nodes: []*registry.Node{
{
Id: "foo-1.0.0-123",
Address: "localhost",
Port: 9999,
Address: "localhost:9999",
},
{
Id: "foo-1.0.0-321",
Address: "localhost",
Port: 9999,
Address: "localhost:9999",
},
},
},
@@ -31,8 +29,7 @@ var (
Nodes: []*registry.Node{
{
Id: "foo-1.0.1-321",
Address: "localhost",
Port: 6666,
Address: "localhost:6666",
},
},
},
@@ -42,8 +39,7 @@ var (
Nodes: []*registry.Node{
{
Id: "foo-1.0.3-345",
Address: "localhost",
Port: 8888,
Address: "localhost:8888",
},
},
},
@@ -55,13 +51,11 @@ var (
Nodes: []*registry.Node{
{
Id: "bar-1.0.0-123",
Address: "localhost",
Port: 9999,
Address: "localhost:9999",
},
{
Id: "bar-1.0.0-321",
Address: "localhost",
Port: 9999,
Address: "localhost:9999",
},
},
},
@@ -71,8 +65,7 @@ var (
Nodes: []*registry.Node{
{
Id: "bar-1.0.1-321",
Address: "localhost",
Port: 6666,
Address: "localhost:6666",
},
},
},

View File

@@ -11,7 +11,6 @@ type Service struct {
type Node struct {
Id string `json:"id"`
Address string `json:"address"`
Port int `json:"port"`
Metadata map[string]string `json:"metadata"`
}

View File

@@ -12,8 +12,7 @@ func TestRemove(t *testing.T) {
Nodes: []*Node{
{
Id: "foo-123",
Address: "localhost",
Port: 9999,
Address: "localhost:9999",
},
},
},
@@ -23,8 +22,7 @@ func TestRemove(t *testing.T) {
Nodes: []*Node{
{
Id: "foo-123",
Address: "localhost",
Port: 6666,
Address: "localhost:6666",
},
},
},
@@ -45,13 +43,11 @@ func TestRemoveNodes(t *testing.T) {
Nodes: []*Node{
{
Id: "foo-123",
Address: "localhost",
Port: 9999,
Address: "localhost:9999",
},
{
Id: "foo-321",
Address: "localhost",
Port: 6666,
Address: "localhost:6666",
},
},
},
@@ -61,8 +57,7 @@ func TestRemoveNodes(t *testing.T) {
Nodes: []*Node{
{
Id: "foo-123",
Address: "localhost",
Port: 6666,
Address: "localhost:6666",
},
},
},

View File

@@ -12,8 +12,7 @@ func TestWatcher(t *testing.T) {
Nodes: []*Node{
&Node{
Id: "test1-1",
Address: "10.0.0.1",
Port: 10001,
Address: "10.0.0.1:10001",
Metadata: map[string]string{
"foo": "bar",
},
@@ -26,8 +25,7 @@ func TestWatcher(t *testing.T) {
Nodes: []*Node{
&Node{
Id: "test2-1",
Address: "10.0.0.2",
Port: 10002,
Address: "10.0.0.2:10002",
Metadata: map[string]string{
"foo2": "bar2",
},
@@ -40,8 +38,7 @@ func TestWatcher(t *testing.T) {
Nodes: []*Node{
&Node{
Id: "test3-1",
Address: "10.0.0.3",
Port: 10003,
Address: "10.0.0.3:10003",
Metadata: map[string]string{
"foo3": "bar3",
},
@@ -77,10 +74,6 @@ func TestWatcher(t *testing.T) {
if node.Address != service.Nodes[0].Address {
t.Fatalf("Expected node address %s got %s", service.Nodes[0].Address, node.Address)
}
if node.Port != service.Nodes[0].Port {
t.Fatalf("Expected node port %d got %d", service.Nodes[0].Port, node.Port)
}
}
// new registry