2016-12-14 17:41:48 +02:00
|
|
|
// Package registry is an interface for service discovery
|
2015-01-14 01:31:27 +02:00
|
|
|
package registry
|
|
|
|
|
2016-02-25 14:42:31 +02:00
|
|
|
import (
|
|
|
|
"errors"
|
|
|
|
)
|
|
|
|
|
2019-09-23 21:46:12 +02:00
|
|
|
var (
|
|
|
|
DefaultRegistry = NewRegistry()
|
|
|
|
|
|
|
|
// Not found error when GetService is called
|
|
|
|
ErrNotFound = errors.New("service not found")
|
|
|
|
// Watcher stopped error when watcher is stopped
|
|
|
|
ErrWatcherStopped = errors.New("watcher stopped")
|
|
|
|
)
|
|
|
|
|
2016-01-30 23:13:34 +02:00
|
|
|
// The registry provides an interface for service discovery
|
|
|
|
// and an abstraction over varying implementations
|
|
|
|
// {consul, etcd, zookeeper, ...}
|
2015-01-14 01:31:27 +02:00
|
|
|
type Registry interface {
|
2018-08-08 19:57:29 +02:00
|
|
|
Init(...Option) error
|
|
|
|
Options() Options
|
2016-01-27 01:32:27 +02:00
|
|
|
Register(*Service, ...RegisterOption) error
|
2020-04-14 13:32:59 +02:00
|
|
|
Deregister(*Service, ...DeregisterOption) error
|
|
|
|
GetService(string, ...GetOption) ([]*Service, error)
|
|
|
|
ListServices(...ListOption) ([]*Service, error)
|
2018-02-19 19:12:37 +02:00
|
|
|
Watch(...WatchOption) (Watcher, error)
|
2015-12-19 23:56:14 +02:00
|
|
|
String() string
|
2015-06-01 19:55:27 +02:00
|
|
|
}
|
|
|
|
|
2020-04-12 11:58:12 +02:00
|
|
|
type Service struct {
|
|
|
|
Name string `json:"name"`
|
|
|
|
Version string `json:"version"`
|
|
|
|
Metadata map[string]string `json:"metadata"`
|
|
|
|
Endpoints []*Endpoint `json:"endpoints"`
|
|
|
|
Nodes []*Node `json:"nodes"`
|
|
|
|
}
|
|
|
|
|
|
|
|
type Node struct {
|
|
|
|
Id string `json:"id"`
|
|
|
|
Address string `json:"address"`
|
|
|
|
Metadata map[string]string `json:"metadata"`
|
|
|
|
}
|
|
|
|
|
|
|
|
type Endpoint struct {
|
|
|
|
Name string `json:"name"`
|
|
|
|
Request *Value `json:"request"`
|
|
|
|
Response *Value `json:"response"`
|
|
|
|
Metadata map[string]string `json:"metadata"`
|
|
|
|
}
|
|
|
|
|
|
|
|
type Value struct {
|
|
|
|
Name string `json:"name"`
|
|
|
|
Type string `json:"type"`
|
|
|
|
Values []*Value `json:"values"`
|
|
|
|
}
|
|
|
|
|
2015-12-19 20:28:08 +02:00
|
|
|
type Option func(*Options)
|
2015-05-16 01:34:02 +02:00
|
|
|
|
2016-01-27 01:32:27 +02:00
|
|
|
type RegisterOption func(*RegisterOptions)
|
|
|
|
|
2018-02-19 19:12:37 +02:00
|
|
|
type WatchOption func(*WatchOptions)
|
|
|
|
|
2020-04-14 13:32:59 +02:00
|
|
|
type DeregisterOption func(*DeregisterOptions)
|
|
|
|
|
|
|
|
type GetOption func(*GetOptions)
|
|
|
|
|
|
|
|
type ListOption func(*ListOptions)
|
|
|
|
|
2016-01-30 23:13:34 +02:00
|
|
|
// Register a service node. Additionally supply options such as TTL.
|
2016-01-27 01:32:27 +02:00
|
|
|
func Register(s *Service, opts ...RegisterOption) error {
|
|
|
|
return DefaultRegistry.Register(s, opts...)
|
2015-01-14 01:31:27 +02:00
|
|
|
}
|
|
|
|
|
2016-01-30 23:13:34 +02:00
|
|
|
// Deregister a service node
|
2015-05-25 23:14:28 +02:00
|
|
|
func Deregister(s *Service) error {
|
2015-01-14 01:31:27 +02:00
|
|
|
return DefaultRegistry.Deregister(s)
|
|
|
|
}
|
|
|
|
|
2016-01-30 23:13:34 +02:00
|
|
|
// Retrieve a service. A slice is returned since we separate Name/Version.
|
2015-11-08 03:48:48 +02:00
|
|
|
func GetService(name string) ([]*Service, error) {
|
2015-01-14 01:31:27 +02:00
|
|
|
return DefaultRegistry.GetService(name)
|
|
|
|
}
|
2015-04-03 00:52:49 +02:00
|
|
|
|
2016-01-30 23:13:34 +02:00
|
|
|
// List the services. Only returns service names
|
2015-05-25 23:14:28 +02:00
|
|
|
func ListServices() ([]*Service, error) {
|
2015-04-03 00:52:49 +02:00
|
|
|
return DefaultRegistry.ListServices()
|
|
|
|
}
|
2015-12-05 03:12:29 +02:00
|
|
|
|
2016-01-30 23:13:34 +02:00
|
|
|
// Watch returns a watcher which allows you to track updates to the registry.
|
2018-02-19 19:12:37 +02:00
|
|
|
func Watch(opts ...WatchOption) (Watcher, error) {
|
|
|
|
return DefaultRegistry.Watch(opts...)
|
2015-12-05 03:12:29 +02:00
|
|
|
}
|
2015-12-19 23:56:14 +02:00
|
|
|
|
|
|
|
func String() string {
|
|
|
|
return DefaultRegistry.String()
|
|
|
|
}
|