Use ServiceID as node.ID rather than Node.... in time remove Node/Address completely
This commit is contained in:
parent
1cbe06413e
commit
822cc0e5da
@ -7,6 +7,7 @@ import (
|
|||||||
"fmt"
|
"fmt"
|
||||||
"net"
|
"net"
|
||||||
"net/http"
|
"net/http"
|
||||||
|
"os"
|
||||||
"runtime"
|
"runtime"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
@ -95,6 +96,20 @@ func decodeMetadata(tags []string) map[string]string {
|
|||||||
return md
|
return md
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func encodeVersion(v string) string {
|
||||||
|
return "v=" + v
|
||||||
|
}
|
||||||
|
|
||||||
|
func decodeVersion(tags []string) (string, bool) {
|
||||||
|
for _, tag := range tags {
|
||||||
|
if len(tag) == 0 || tag[0] != 'v' {
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
return tag[2:], true
|
||||||
|
}
|
||||||
|
return "", false
|
||||||
|
}
|
||||||
|
|
||||||
func newConsulRegistry(addrs []string, opts ...Option) Registry {
|
func newConsulRegistry(addrs []string, opts ...Option) Registry {
|
||||||
var opt Options
|
var opt Options
|
||||||
for _, o := range opts {
|
for _, o := range opts {
|
||||||
@ -114,6 +129,7 @@ func newConsulRegistry(addrs []string, opts ...Option) Registry {
|
|||||||
addr, port, err := net.SplitHostPort(addrs[0])
|
addr, port, err := net.SplitHostPort(addrs[0])
|
||||||
if ae, ok := err.(*net.AddrError); ok && ae.Err == "missing port in address" {
|
if ae, ok := err.(*net.AddrError); ok && ae.Err == "missing port in address" {
|
||||||
port = "8500"
|
port = "8500"
|
||||||
|
addr = addrs[0]
|
||||||
config.Address = fmt.Sprintf("%s:%s", addr, port)
|
config.Address = fmt.Sprintf("%s:%s", addr, port)
|
||||||
} else if err == nil {
|
} else if err == nil {
|
||||||
config.Address = fmt.Sprintf("%s:%s", addr, port)
|
config.Address = fmt.Sprintf("%s:%s", addr, port)
|
||||||
@ -143,15 +159,7 @@ func (c *consulRegistry) Deregister(s *Service) error {
|
|||||||
if len(s.Nodes) == 0 {
|
if len(s.Nodes) == 0 {
|
||||||
return errors.New("Require at least one node")
|
return errors.New("Require at least one node")
|
||||||
}
|
}
|
||||||
|
return c.Client.Agent().ServiceDeregister(s.Nodes[0].Id)
|
||||||
node := s.Nodes[0]
|
|
||||||
|
|
||||||
_, err := c.Client.Catalog().Deregister(&consul.CatalogDeregistration{
|
|
||||||
Node: node.Id,
|
|
||||||
Address: node.Address,
|
|
||||||
}, nil)
|
|
||||||
|
|
||||||
return err
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func (c *consulRegistry) Register(s *Service) error {
|
func (c *consulRegistry) Register(s *Service) error {
|
||||||
@ -163,19 +171,24 @@ func (c *consulRegistry) Register(s *Service) error {
|
|||||||
|
|
||||||
tags := encodeMetadata(node.Metadata)
|
tags := encodeMetadata(node.Metadata)
|
||||||
tags = append(tags, encodeEndpoints(s.Endpoints)...)
|
tags = append(tags, encodeEndpoints(s.Endpoints)...)
|
||||||
|
tags = append(tags, encodeVersion(s.Version))
|
||||||
|
|
||||||
_, err := c.Client.Catalog().Register(&consul.CatalogRegistration{
|
if _, err := c.Client.Catalog().Register(&consul.CatalogRegistration{
|
||||||
|
// TODO: remove setting node and address
|
||||||
Node: node.Id,
|
Node: node.Id,
|
||||||
Address: node.Address,
|
Address: node.Address,
|
||||||
Service: &consul.AgentService{
|
Service: &consul.AgentService{
|
||||||
ID: s.Version,
|
ID: node.Id,
|
||||||
Service: s.Name,
|
Service: s.Name,
|
||||||
Port: node.Port,
|
Port: node.Port,
|
||||||
Tags: tags,
|
Tags: tags,
|
||||||
|
Address: node.Address,
|
||||||
},
|
},
|
||||||
}, nil)
|
}, nil); err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
return err
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (c *consulRegistry) GetService(name string) ([]*Service, error) {
|
func (c *consulRegistry) GetService(name string) ([]*Service, error) {
|
||||||
@ -191,15 +204,26 @@ func (c *consulRegistry) GetService(name string) ([]*Service, error) {
|
|||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
|
|
||||||
id := s.Node
|
// version is now a tag
|
||||||
key := s.ServiceID
|
version, found := decodeVersion(s.ServiceTags)
|
||||||
version := s.ServiceID
|
// service ID is now the node id
|
||||||
|
id := s.ServiceID
|
||||||
|
// key is always the version
|
||||||
|
key := version
|
||||||
|
// address is service address
|
||||||
|
address := s.ServiceAddress
|
||||||
|
|
||||||
// We're adding service version but
|
// if we can't get the new type of version
|
||||||
// don't want to break backwards compatibility
|
// use old the old ways
|
||||||
if id == version {
|
if !found {
|
||||||
key = "default"
|
// id was set as node
|
||||||
version = ""
|
id = s.Node
|
||||||
|
// key was service id
|
||||||
|
key = s.ServiceID
|
||||||
|
// version was service id
|
||||||
|
version = s.ServiceID
|
||||||
|
// address was address
|
||||||
|
address = s.Address
|
||||||
}
|
}
|
||||||
|
|
||||||
svc, ok := serviceMap[key]
|
svc, ok := serviceMap[key]
|
||||||
@ -214,7 +238,7 @@ func (c *consulRegistry) GetService(name string) ([]*Service, error) {
|
|||||||
|
|
||||||
svc.Nodes = append(svc.Nodes, &Node{
|
svc.Nodes = append(svc.Nodes, &Node{
|
||||||
Id: id,
|
Id: id,
|
||||||
Address: s.Address,
|
Address: address,
|
||||||
Port: s.ServicePort,
|
Port: s.ServicePort,
|
||||||
Metadata: decodeMetadata(s.ServiceTags),
|
Metadata: decodeMetadata(s.ServiceTags),
|
||||||
})
|
})
|
||||||
|
Loading…
Reference in New Issue
Block a user