github.com/tickoalcantara12/micro/v3@v3.0.0-20221007104245-9d75b9bcbab9/service/registry/registry.go (about)

     1  // Licensed under the Apache License, Version 2.0 (the "License");
     2  // you may not use this file except in compliance with the License.
     3  // You may obtain a copy of the License at
     4  //
     5  //     https://www.apache.org/licenses/LICENSE-2.0
     6  //
     7  // Unless required by applicable law or agreed to in writing, software
     8  // distributed under the License is distributed on an "AS IS" BASIS,
     9  // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    10  // See the License for the specific language governing permissions and
    11  // limitations under the License.
    12  //
    13  // Original source: github.com/micro/go-micro/v3/registry/registry.go
    14  
    15  // Package registry is the micro registry
    16  package registry
    17  
    18  import (
    19  	"errors"
    20  )
    21  
    22  var (
    23  	// DefaultRegistry implementation
    24  	DefaultRegistry Registry
    25  	// ErrNotFound error when GetService is called
    26  	ErrNotFound = errors.New("service not found")
    27  	// ErrWatcherStopped error when watcher is stopped
    28  	ErrWatcherStopped = errors.New("watcher stopped")
    29  )
    30  
    31  const (
    32  	// WildcardDomain indicates any domain
    33  	WildcardDomain = "*"
    34  	// DefaultDomain to use if none was provided in options
    35  	DefaultDomain = "micro"
    36  )
    37  
    38  // Registry provides an interface for service discovery
    39  // and an abstraction over varying implementations
    40  // {consul, etcd, zookeeper, ...}
    41  type Registry interface {
    42  	Init(...Option) error
    43  	Options() Options
    44  	Register(*Service, ...RegisterOption) error
    45  	Deregister(*Service, ...DeregisterOption) error
    46  	GetService(string, ...GetOption) ([]*Service, error)
    47  	ListServices(...ListOption) ([]*Service, error)
    48  	Watch(...WatchOption) (Watcher, error)
    49  	String() string
    50  }
    51  
    52  type Service struct {
    53  	Name      string            `json:"name"`
    54  	Version   string            `json:"version"`
    55  	Metadata  map[string]string `json:"metadata"`
    56  	Endpoints []*Endpoint       `json:"endpoints"`
    57  	Nodes     []*Node           `json:"nodes"`
    58  }
    59  
    60  type Node struct {
    61  	Id       string            `json:"id"`
    62  	Address  string            `json:"address"`
    63  	Metadata map[string]string `json:"metadata"`
    64  }
    65  
    66  type Endpoint struct {
    67  	Name     string            `json:"name"`
    68  	Request  *Value            `json:"request"`
    69  	Response *Value            `json:"response"`
    70  	Metadata map[string]string `json:"metadata"`
    71  }
    72  
    73  type Value struct {
    74  	Name   string   `json:"name"`
    75  	Type   string   `json:"type"`
    76  	Values []*Value `json:"values"`
    77  }
    78  
    79  // GetService from the registry
    80  func GetService(service string) ([]*Service, error) {
    81  	return DefaultRegistry.GetService(service)
    82  }
    83  
    84  // ListServices in the registry
    85  func ListServices() ([]*Service, error) {
    86  	return DefaultRegistry.ListServices()
    87  }
    88  
    89  // Watch the registry for updates
    90  func Watch() (Watcher, error) {
    91  	return DefaultRegistry.Watch()
    92  }