gitee.com/liuxuezhan/go-micro-v1.18.0@v1.0.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 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 // The registry provides an interface for service discovery 18 // and an abstraction over varying implementations 19 // {consul, etcd, zookeeper, ...} 20 type Registry interface { 21 Init(...Option) error 22 Options() Options 23 Register(*Service, ...RegisterOption) error 24 Deregister(*Service) error 25 GetService(string) ([]*Service, error) 26 ListServices() ([]*Service, error) 27 Watch(...WatchOption) (Watcher, error) 28 String() string 29 } 30 31 type Option func(*Options) 32 33 type RegisterOption func(*RegisterOptions) 34 35 type WatchOption func(*WatchOptions) 36 37 // Register a service node. Additionally supply options such as TTL. 38 func Register(s *Service, opts ...RegisterOption) error { 39 return DefaultRegistry.Register(s, opts...) 40 } 41 42 // Deregister a service node 43 func Deregister(s *Service) error { 44 return DefaultRegistry.Deregister(s) 45 } 46 47 // Retrieve a service. A slice is returned since we separate Name/Version. 48 func GetService(name string) ([]*Service, error) { 49 return DefaultRegistry.GetService(name) 50 } 51 52 // List the services. Only returns service names 53 func ListServices() ([]*Service, error) { 54 return DefaultRegistry.ListServices() 55 } 56 57 // Watch returns a watcher which allows you to track updates to the registry. 58 func Watch(opts ...WatchOption) (Watcher, error) { 59 return DefaultRegistry.Watch(opts...) 60 } 61 62 func String() string { 63 return DefaultRegistry.String() 64 }