gsvc

package
v2.0.6 Latest Latest
Warning

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

Go to latest
Published: Jun 10, 2022 License: MIT Imports: 12 Imported by: 0

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`
)

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 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 service 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.}
}

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 {
	Prefix     string   // Service prefix.
	Deployment string   // Service deployment name, eg: dev, qa, staging, prod, etc.
	Namespace  string   // Service Namespace, to indicate different service 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.
}

Service definition.

func Get

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

func GetWithWatch

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

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 unique name of service.

func (*Service) Value

func (s *Service) Value() string

type ServiceWatch

type ServiceWatch func(service *Service)

type WatchInput

type WatchInput struct {
	Prefix     string // Service prefix.
	Deployment string // Service deployment name, eg: dev, qa, staging, prod, etc.
	Namespace  string // Service Namespace, to indicate different service 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.}
}

WatchInput is the input for service watching.

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