github.com/annwntech/go-micro/v2@v2.9.5/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 DefaultRegistry = NewRegistry() 10 11 // Not found error when GetService is called 12 ErrNotFound = errors.New("service not found") 13 // Watcher stopped error when watcher is stopped 14 ErrWatcherStopped = errors.New("watcher stopped") 15 ) 16 17 const ( 18 // WildcardDomain indicates any domain 19 WildcardDomain = "*" 20 // DefaultDomain to use if none was provided in options 21 DefaultDomain = "micro" 22 ) 23 24 // The registry provides an interface for service discovery 25 // and an abstraction over varying implementations 26 // {consul, etcd, zookeeper, ...} 27 type Registry interface { 28 Init(...Option) error 29 Options() Options 30 Register(*Service, ...RegisterOption) error 31 Deregister(*Service, ...DeregisterOption) error 32 GetService(string, ...GetOption) ([]*Service, error) 33 ListServices(...ListOption) ([]*Service, error) 34 Watch(...WatchOption) (Watcher, error) 35 String() string 36 } 37 38 type Service struct { 39 Name string `json:"name"` 40 Version string `json:"version"` 41 Metadata map[string]string `json:"metadata"` 42 Endpoints []*Endpoint `json:"endpoints"` 43 Nodes []*Node `json:"nodes"` 44 } 45 46 type Node struct { 47 Id string `json:"id"` 48 Address string `json:"address"` 49 Metadata map[string]string `json:"metadata"` 50 } 51 52 type Endpoint struct { 53 Name string `json:"name"` 54 Request *Value `json:"request"` 55 Response *Value `json:"response"` 56 Metadata map[string]string `json:"metadata"` 57 } 58 59 type Value struct { 60 Name string `json:"name"` 61 Type string `json:"type"` 62 Values []*Value `json:"values"` 63 } 64 65 // Register a service node. Additionally supply options such as TTL. 66 func Register(s *Service, opts ...RegisterOption) error { 67 return DefaultRegistry.Register(s, opts...) 68 } 69 70 // Deregister a service node 71 func Deregister(s *Service) error { 72 return DefaultRegistry.Deregister(s) 73 } 74 75 // Retrieve a service. A slice is returned since we separate Name/Version. 76 func GetService(name string) ([]*Service, error) { 77 return DefaultRegistry.GetService(name) 78 } 79 80 // List the services. Only returns service names 81 func ListServices() ([]*Service, error) { 82 return DefaultRegistry.ListServices() 83 } 84 85 // Watch returns a watcher which allows you to track updates to the registry. 86 func Watch(opts ...WatchOption) (Watcher, error) { 87 return DefaultRegistry.Watch(opts...) 88 } 89 90 func String() string { 91 return DefaultRegistry.String() 92 }