gsvc

package
v2.1.0-rc2 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: May 19, 2022 License: MIT Imports: 12 Imported by: 49

Documentation

Overview

Package gsvc provides service registry and discovery definition.

Index

Constants

View Source
const (
	Schema            = `services`
	DefaultPrefix     = `services`
	DefaultDeployment = `default`
	DefaultNamespace  = `default`
	DefaultVersion    = `latest`
	EnvPrefix         = `GF_GSVC_PREFIX`
	EnvDeployment     = `GF_GSVC_DEPLOYMENT`
	EnvNamespace      = `GF_GSVC_NAMESPACE`
	EnvName           = `GF_GSVC_Name`
	EnvVersion        = `GF_GSVC_VERSION`
	MDProtocol        = `protocol`
	MDInsecure        = `insecure`
	MDWeight          = `weight`

	DefaultProtocol = `http`
)
View Source
const (
	// DefaultSeparator is the default separator for the service name and method name.
	DefaultSeparator = "/"
)

Variables

This section is empty.

Functions

func Deregister

func Deregister(ctx context.Context, service *Service) error

Deregister removes `service` from default registry.

func Register

func Register(ctx context.Context, service *Service) error

Register registers `service` to default registry..

func SetRegistry

func SetRegistry(registry Registry)

SetRegistry sets the default Registry implements as your own implemented interface.

Types

type Discovery

type Discovery interface {
	// Search searches and returns services with specified condition.
	Search(ctx context.Context, in SearchInput) ([]*Service, error)

	// Watch watches specified condition changes.
	Watch(ctx context.Context, key string) (Watcher, error)
}

Discovery interface for service discovery.

type Metadata

type Metadata map[string]interface{}

Metadata stores custom key-value pairs.

func (Metadata) Get

func (m Metadata) Get(key string) *gvar.Var

Get retrieves and returns value of specified key as gvar.

func (Metadata) Set

func (m Metadata) Set(key string, value string)

Set sets key-value pair into metadata.

type Registrar

type Registrar interface {
	// Register registers `service` to Registry.
	Register(ctx context.Context, service *Service) error

	// Deregister off-lines and removes `service` from the Registry.
	Deregister(ctx context.Context, service *Service) error
}

Registrar interface for service registrar.

type Registry

type Registry interface {
	Registrar
	Discovery
}

Registry interface for service.

func GetRegistry

func GetRegistry() Registry

GetRegistry returns the default Registry that is previously set. It returns nil if no Registry is set.

type SearchInput

type SearchInput struct {
	Prefix     string   // Service prefix.
	Deployment string   // Service deployment name, eg: dev, qa, staging, prod, etc.
	Namespace  string   // Service Namespace, to indicate different services in the same environment with the same Name.
	Name       string   // Name for the service.
	Version    string   // Service version, eg: v1.0.0, v2.1.1, etc.}
	Metadata   Metadata // Custom data for this service, which can be set using JSON by environment or command-line.
	Separator  string   // Separator for service name and version, eg: _, -, etc.
}

SearchInput is the input for service searching.

func (*SearchInput) Key

func (s *SearchInput) Key() string

Key formats and returns a string for prefix searching purpose.

type Service

type Service struct {
	ID         string   // ID is the unique instance ID as registered.
	Prefix     string   // Service prefix.
	Deployment string   // Service deployment name, eg: dev, qa, staging, prod, etc.
	Namespace  string   // Service Namespace, to indicate different services in the same environment with the same Name.
	Name       string   // Name for the service.
	Version    string   // Service version, eg: v1.0.0, v2.1.1, etc.
	Endpoints  []string // Service Endpoints, pattern: IP:port, eg: 192.168.1.2:8000.
	Metadata   Metadata // Custom data for this service, which can be set using JSON by environment or command-line.
	Separator  string   // Separator for service name and version, eg: _, -, etc.
}

Service definition.

func Get

func Get(ctx context.Context, name string) (service *Service, err error)

Get the watched service map.

func GetWithWatch

func GetWithWatch(ctx context.Context, name string, watch ServiceWatch) (service *Service, err error)

GetWithWatch is used to getting the service with watch.

func NewServiceWithKV

func NewServiceWithKV(key, value []byte) (s *Service, err error)

NewServiceWithKV creates and returns service from `key` and `value`.

func NewServiceWithName

func NewServiceWithName(name string) (s *Service)

NewServiceWithName creates and returns service from `name`.

func Search(ctx context.Context, in SearchInput) ([]*Service, error)

Search searches and returns services with specified condition.

func (*Service) Address

func (s *Service) Address() string

Address returns the first endpoint of Service. Eg: 192.168.1.12:9000.

func (*Service) Key

func (s *Service) Key() string

Key formats the service information and returns the Service as registering key.

func (*Service) KeyWithSchema

func (s *Service) KeyWithSchema() string

KeyWithSchema formats the service information and returns the Service as dialing target key.

func (*Service) KeyWithoutEndpoints

func (s *Service) KeyWithoutEndpoints() string

KeyWithoutEndpoints formats the service information and returns a string as a unique name of service.

func (*Service) Value

func (s *Service) Value() string

Value formats the service information and returns the Service as registering value.

type ServiceWatch

type ServiceWatch func(service *Service)

ServiceWatch is used to watch the service status.

type Watcher

type Watcher interface {
	// Proceed proceeds watch in blocking way.
	Proceed() ([]*Service, error)

	// Close closes the watcher.
	Close() error
}

Watcher interface for service.

func Watch

func Watch(ctx context.Context, key string) (Watcher, error)

Watch watches specified condition changes.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL