gsvc

package
v2.7.7 Latest Latest
Warning

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

Go to latest
Published: Jun 28, 2024 License: MIT Imports: 13 Imported by: 6

Documentation

Overview

Package gsvc provides service registry and discovery definition.

Package gsvc provides service registry and discovery definition.

Package gsvc provides service registry and discovery definition.

Index

Constants

View Source
const (
	Schema                    = `service`            // Schema is the schema of service.
	DefaultHead               = `service`            // DefaultHead is the default head of service.
	DefaultDeployment         = `default`            // DefaultDeployment is the default deployment of service.
	DefaultNamespace          = `default`            // DefaultNamespace is the default namespace of service.
	DefaultVersion            = `latest`             // DefaultVersion is the default version of service.
	EnvPrefix                 = `GF_GSVC_PREFIX`     // EnvPrefix is the environment variable prefix.
	EnvDeployment             = `GF_GSVC_DEPLOYMENT` // EnvDeployment is the environment variable deployment.
	EnvNamespace              = `GF_GSVC_NAMESPACE`  // EnvNamespace is the environment variable namespace.
	EnvName                   = `GF_GSVC_Name`       // EnvName is the environment variable name.
	EnvVersion                = `GF_GSVC_VERSION`    // EnvVersion is the environment variable version.
	MDProtocol                = `protocol`           // MDProtocol is the metadata key for protocol.
	MDInsecure                = `insecure`           // MDInsecure is the metadata key for insecure.
	MDWeight                  = `weight`             // MDWeight is the metadata key for weight.
	DefaultProtocol           = `http`               // DefaultProtocol is the default protocol of service.
	DefaultSeparator          = "/"                  // DefaultSeparator is the default separator of service.
	EndpointHostPortDelimiter = ":"                  // EndpointHostPortDelimiter is the delimiter of host and port.

	EndpointsDelimiter = "," // EndpointsDelimiter is the delimiter of endpoints.
)

Variables

This section is empty.

Functions

func Deregister

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

Deregister removes `service` from 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) (result []Service, err error)

	// Watch watches specified condition changes.
	// The `key` is the prefix of service key.
	Watch(ctx context.Context, key string) (watcher Watcher, err error)
}

Discovery interface for service discovery.

type Endpoint

type Endpoint interface {
	// Host returns the IPv4/IPv6 address of a service.
	Host() string

	// Port returns the port of a service.
	Port() int

	// String formats and returns the Endpoint as a string.
	String() string
}

Endpoint interface for service.

func NewEndpoint

func NewEndpoint(address string) Endpoint

NewEndpoint creates and returns an Endpoint from address string of pattern "host:port", eg: "192.168.1.100:80".

type Endpoints

type Endpoints []Endpoint

Endpoints are composed by multiple Endpoint.

func NewEndpoints

func NewEndpoints(addresses string) Endpoints

NewEndpoints creates and returns Endpoints from multiple addresses like: "192.168.1.100:80,192.168.1.101:80".

func (Endpoints) String

func (es Endpoints) String() string

String formats and returns the Endpoints as a string like: "192.168.1.100:80,192.168.1.101:80"

type LocalEndpoint

type LocalEndpoint struct {
	// contains filtered or unexported fields
}

LocalEndpoint implements interface Endpoint.

func (*LocalEndpoint) Host

func (e *LocalEndpoint) Host() string

Host returns the IPv4/IPv6 address of a service.

func (*LocalEndpoint) Port

func (e *LocalEndpoint) Port() int

Port returns the port of a service.

func (*LocalEndpoint) String

func (e *LocalEndpoint) String() string

String formats and returns the Endpoint as a string, like: 192.168.1.100:80.

type LocalService

type LocalService struct {
	Head       string    // Service custom head string in service key.
	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  Endpoints // 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.
}

LocalService provides a default implements for interface Service.

func (*LocalService) GetEndpoints

func (s *LocalService) GetEndpoints() Endpoints

GetEndpoints returns the Endpoints of service. The Endpoints contain multiple host/port information of service.

func (*LocalService) GetKey

func (s *LocalService) GetKey() string

GetKey formats and returns a unique key string for service. The result key is commonly used for key-value registrar server.

func (*LocalService) GetMetadata

func (s *LocalService) GetMetadata() Metadata

GetMetadata returns the Metadata map of service. The Metadata is key-value pair map specifying extra attributes of a service.

func (*LocalService) GetName

func (s *LocalService) GetName() string

GetName returns the name of the service. The name is necessary for a service, and should be unique among services.

func (*LocalService) GetPrefix

func (s *LocalService) GetPrefix() string

GetPrefix formats and returns the key prefix string. The result prefix string is commonly used in key-value registrar server for service searching.

Take etcd server for example, the prefix string is used like: `etcdctl get /services/prod/hello.svc --prefix`

func (*LocalService) GetValue

func (s *LocalService) GetValue() string

GetValue formats and returns the value of the service. The result value is commonly used for key-value registrar server.

func (*LocalService) GetVersion

func (s *LocalService) GetVersion() string

GetVersion returns the version of the service. It is suggested using GNU version naming like: v1.0.0, v2.0.1, v2.1.0-rc. A service can have multiple versions deployed at once. If no version set in service, the default version of service is "latest".

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

func (m Metadata) IsEmpty() bool

IsEmpty checks and returns whether current Metadata is empty.

func (Metadata) Set

func (m Metadata) Set(key string, value interface{})

Set sets key-value pair into metadata.

func (Metadata) Sets

func (m Metadata) Sets(kvs map[string]interface{})

Sets sets key-value pairs into metadata.

type Registrar

type Registrar interface {
	// Register registers `service` to Registry.
	// Note that it returns a new Service if it changes the input Service with custom one.
	Register(ctx context.Context, service Service) (registered Service, err 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   // Search by key prefix.
	Name     string   // Search by service name.
	Version  string   // Search by service version.
	Metadata Metadata // Filter by metadata if there are multiple result.
}

SearchInput is the input for service searching.

type Service

type Service interface {
	// GetName returns the name of the service.
	// The name is necessary for a service, and should be unique among services.
	GetName() string

	// GetVersion returns the version of the service.
	// It is suggested using GNU version naming like: v1.0.0, v2.0.1, v2.1.0-rc.
	// A service can have multiple versions deployed at once.
	// If no version set in service, the default version of service is "latest".
	GetVersion() string

	// GetKey formats and returns a unique key string for service.
	// The result key is commonly used for key-value registrar server.
	GetKey() string

	// GetValue formats and returns the value of the service.
	// The result value is commonly used for key-value registrar server.
	GetValue() string

	// GetPrefix formats and returns the key prefix string.
	// The result prefix string is commonly used in key-value registrar server
	// for service searching.
	//
	// Take etcd server for example, the prefix string is used like:
	// `etcdctl get /services/prod/hello.svc --prefix`
	GetPrefix() string

	// GetMetadata returns the Metadata map of service.
	// The Metadata is key-value pair map specifying extra attributes of a service.
	GetMetadata() Metadata

	// GetEndpoints returns the Endpoints of service.
	// The Endpoints contain multiple host/port information of service.
	GetEndpoints() Endpoints
}

Service interface for service definition.

func Get

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

Get retrieves and returns the service by service name.

func GetAndWatch

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

GetAndWatch is used to getting the service with custom watch callback function.

func GetAndWatchWithDiscovery

func GetAndWatchWithDiscovery(ctx context.Context, discovery Discovery, name string, watch ServiceWatch) (service Service, err error)

GetAndWatchWithDiscovery is used to getting the service with custom watch callback function in `discovery`.

func GetWithDiscovery

func GetWithDiscovery(ctx context.Context, discovery Discovery, name string) (service Service, err error)

GetWithDiscovery retrieves and returns the service by service name in `discovery`.

func NewServiceWithKV

func NewServiceWithKV(key, value string) (Service, error)

NewServiceWithKV creates and returns a default implements for interface Service by key-value pair string.

func NewServiceWithName

func NewServiceWithName(name string) Service

NewServiceWithName creates and returns a default implements for interface Service by service name.

func Register

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

Register registers `service` to default registry..

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

Search searches and returns services with specified condition.

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.
	// It returns all complete services that watched by `key` if any change.
	Proceed() (services []Service, err 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