github.com/micro/go-micro/v2@v2.9.1/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, ...DeregisterOption) error
    25  	GetService(string, ...GetOption) ([]*Service, error)
    26  	ListServices(...ListOption) ([]*Service, error)
    27  	Watch(...WatchOption) (Watcher, error)
    28  	String() string
    29  }
    30  
    31  type Service struct {
    32  	Name      string            `json:"name"`
    33  	Version   string            `json:"version"`
    34  	Metadata  map[string]string `json:"metadata"`
    35  	Endpoints []*Endpoint       `json:"endpoints"`
    36  	Nodes     []*Node           `json:"nodes"`
    37  }
    38  
    39  type Node struct {
    40  	Id       string            `json:"id"`
    41  	Address  string            `json:"address"`
    42  	Metadata map[string]string `json:"metadata"`
    43  }
    44  
    45  type Endpoint struct {
    46  	Name     string            `json:"name"`
    47  	Request  *Value            `json:"request"`
    48  	Response *Value            `json:"response"`
    49  	Metadata map[string]string `json:"metadata"`
    50  }
    51  
    52  type Value struct {
    53  	Name   string   `json:"name"`
    54  	Type   string   `json:"type"`
    55  	Values []*Value `json:"values"`
    56  }
    57  
    58  type Option func(*Options)
    59  
    60  type RegisterOption func(*RegisterOptions)
    61  
    62  type WatchOption func(*WatchOptions)
    63  
    64  type DeregisterOption func(*DeregisterOptions)
    65  
    66  type GetOption func(*GetOptions)
    67  
    68  type ListOption func(*ListOptions)
    69  
    70  // Register a service node. Additionally supply options such as TTL.
    71  func Register(s *Service, opts ...RegisterOption) error {
    72  	return DefaultRegistry.Register(s, opts...)
    73  }
    74  
    75  // Deregister a service node
    76  func Deregister(s *Service) error {
    77  	return DefaultRegistry.Deregister(s)
    78  }
    79  
    80  // Retrieve a service. A slice is returned since we separate Name/Version.
    81  func GetService(name string) ([]*Service, error) {
    82  	return DefaultRegistry.GetService(name)
    83  }
    84  
    85  // List the services. Only returns service names
    86  func ListServices() ([]*Service, error) {
    87  	return DefaultRegistry.ListServices()
    88  }
    89  
    90  // Watch returns a watcher which allows you to track updates to the registry.
    91  func Watch(opts ...WatchOption) (Watcher, error) {
    92  	return DefaultRegistry.Watch(opts...)
    93  }
    94  
    95  func String() string {
    96  	return DefaultRegistry.String()
    97  }