go-micro.dev/v5@v5.12.0/registry/registry.go (about) 1 // Package registry is an interface for service discovery 2 package registry 3 4 import ( 5 "errors" 6 ) 7 8 var ( 9 // Not found error when GetService is called. 10 ErrNotFound = errors.New("service not found") 11 // Watcher stopped error when watcher is stopped. 12 ErrWatcherStopped = errors.New("watcher stopped") 13 ) 14 15 // The registry provides an interface for service discovery 16 // and an abstraction over varying implementations 17 // {consul, etcd, zookeeper, ...}. 18 type Registry interface { 19 Init(...Option) error 20 Options() Options 21 Register(*Service, ...RegisterOption) error 22 Deregister(*Service, ...DeregisterOption) error 23 GetService(string, ...GetOption) ([]*Service, error) 24 ListServices(...ListOption) ([]*Service, error) 25 Watch(...WatchOption) (Watcher, error) 26 String() string 27 } 28 29 type Service struct { 30 Name string `json:"name"` 31 Version string `json:"version"` 32 Metadata map[string]string `json:"metadata"` 33 Endpoints []*Endpoint `json:"endpoints"` 34 Nodes []*Node `json:"nodes"` 35 } 36 37 type Node struct { 38 Metadata map[string]string `json:"metadata"` 39 Id string `json:"id"` 40 Address string `json:"address"` 41 } 42 43 type Endpoint struct { 44 Request *Value `json:"request"` 45 Response *Value `json:"response"` 46 Metadata map[string]string `json:"metadata"` 47 Name string `json:"name"` 48 } 49 50 type Value struct { 51 Name string `json:"name"` 52 Type string `json:"type"` 53 Values []*Value `json:"values"` 54 } 55 56 type Option func(*Options) 57 58 type RegisterOption func(*RegisterOptions) 59 60 type WatchOption func(*WatchOptions) 61 62 type DeregisterOption func(*DeregisterOptions) 63 64 type GetOption func(*GetOptions) 65 66 type ListOption func(*ListOptions) 67 68 // Register a service node. Additionally supply options such as TTL. 69 func Register(s *Service, opts ...RegisterOption) error { 70 return DefaultRegistry.Register(s, opts...) 71 } 72 73 // Deregister a service node. 74 func Deregister(s *Service) error { 75 return DefaultRegistry.Deregister(s) 76 } 77 78 // Retrieve a service. A slice is returned since we separate Name/Version. 79 func GetService(name string) ([]*Service, error) { 80 return DefaultRegistry.GetService(name) 81 } 82 83 // List the services. Only returns service names. 84 func ListServices() ([]*Service, error) { 85 return DefaultRegistry.ListServices() 86 } 87 88 // Watch returns a watcher which allows you to track updates to the registry. 89 func Watch(opts ...WatchOption) (Watcher, error) { 90 return DefaultRegistry.Watch(opts...) 91 } 92 93 func String() string { 94 return DefaultRegistry.String() 95 } 96 97 var ( 98 DefaultRegistry = NewMDNSRegistry() 99 )