From 822cc0e5dafb340cbf060d753a4292e21d64f927 Mon Sep 17 00:00:00 2001 From: Asim Date: Tue, 26 Jan 2016 20:30:05 +0000 Subject: [PATCH 1/2] Use ServiceID as node.ID rather than Node.... in time remove Node/Address completely --- registry/consul_registry.go | 68 +++++++++++++++++++++++++------------ 1 file changed, 46 insertions(+), 22 deletions(-) diff --git a/registry/consul_registry.go b/registry/consul_registry.go index 03918be5..6511db00 100644 --- a/registry/consul_registry.go +++ b/registry/consul_registry.go @@ -7,6 +7,7 @@ import ( "fmt" "net" "net/http" + "os" "runtime" "time" @@ -95,6 +96,20 @@ func decodeMetadata(tags []string) map[string]string { 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 { var opt Options for _, o := range opts { @@ -114,6 +129,7 @@ func newConsulRegistry(addrs []string, opts ...Option) Registry { addr, port, err := net.SplitHostPort(addrs[0]) if ae, ok := err.(*net.AddrError); ok && ae.Err == "missing port in address" { port = "8500" + addr = addrs[0] config.Address = fmt.Sprintf("%s:%s", addr, port) } else if err == nil { config.Address = fmt.Sprintf("%s:%s", addr, port) @@ -143,15 +159,7 @@ func (c *consulRegistry) Deregister(s *Service) error { if len(s.Nodes) == 0 { return errors.New("Require at least one node") } - - node := s.Nodes[0] - - _, err := c.Client.Catalog().Deregister(&consul.CatalogDeregistration{ - Node: node.Id, - Address: node.Address, - }, nil) - - return err + return c.Client.Agent().ServiceDeregister(s.Nodes[0].Id) } func (c *consulRegistry) Register(s *Service) error { @@ -163,19 +171,24 @@ func (c *consulRegistry) Register(s *Service) error { tags := encodeMetadata(node.Metadata) 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, Address: node.Address, Service: &consul.AgentService{ - ID: s.Version, + ID: node.Id, Service: s.Name, Port: node.Port, Tags: tags, + Address: node.Address, }, - }, nil) + }, nil); err != nil { + return err + } - return err + return nil } func (c *consulRegistry) GetService(name string) ([]*Service, error) { @@ -191,15 +204,26 @@ func (c *consulRegistry) GetService(name string) ([]*Service, error) { continue } - id := s.Node - key := s.ServiceID - version := s.ServiceID + // version is now a tag + version, found := decodeVersion(s.ServiceTags) + // 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 - // don't want to break backwards compatibility - if id == version { - key = "default" - version = "" + // if we can't get the new type of version + // use old the old ways + if !found { + // id was set as node + 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] @@ -214,7 +238,7 @@ func (c *consulRegistry) GetService(name string) ([]*Service, error) { svc.Nodes = append(svc.Nodes, &Node{ Id: id, - Address: s.Address, + Address: address, Port: s.ServicePort, Metadata: decodeMetadata(s.ServiceTags), }) From cd13f0389fb82962b672adfc20f7f5916a2a439f Mon Sep 17 00:00:00 2001 From: Asim Date: Tue, 26 Jan 2016 20:44:29 +0000 Subject: [PATCH 2/2] Fix this cruft --- registry/consul_registry.go | 11 +++++++++-- 1 file changed, 9 insertions(+), 2 deletions(-) diff --git a/registry/consul_registry.go b/registry/consul_registry.go index 6511db00..843f2281 100644 --- a/registry/consul_registry.go +++ b/registry/consul_registry.go @@ -7,7 +7,6 @@ import ( "fmt" "net" "net/http" - "os" "runtime" "time" @@ -159,7 +158,15 @@ func (c *consulRegistry) Deregister(s *Service) error { if len(s.Nodes) == 0 { 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, + ServiceID: node.Id, + }, nil) + return err } func (c *consulRegistry) Register(s *Service) error {