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
- func Deregister(ctx context.Context, service Service) error
- func SetRegistry(registry Registry)
- type Discovery
- type Endpoint
- type Endpoints
- type LocalEndpoint
- type LocalService
- type Metadata
- type Registrar
- type Registry
- type SearchInput
- type Service
- func Get(ctx context.Context, name string) (service Service, err error)
- func GetAndWatch(ctx context.Context, name string, watch ServiceWatch) (service Service, err error)
- func NewServiceWithKV(key, value string) (Service, error)
- func NewServiceWithName(name string) Service
- func Register(ctx context.Context, service Service) (Service, error)
- func Search(ctx context.Context, in SearchInput) ([]Service, error)
- type ServiceWatch
- type Watcher
Constants ¶
const ( Schema = `services` DefaultHead = `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` DefaultSeparator = "/" )
Variables ¶
This section is empty.
Functions ¶
func Deregister ¶
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) ([]Service, error) // Watch watches specified condition changes. Watch(ctx context.Context, key string) (Watcher, error) }
Discovery interface for service discovery.
type Endpoint ¶ added in v2.1.0
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 ¶ added in v2.1.0
NewEndpoint creates and returns an Endpoint from address string of pattern "host:port", eg: "192.168.1.100:80".
type Endpoints ¶ added in v2.1.0
type Endpoints []Endpoint
Endpoints are composed by multiple Endpoint.
func NewEndpoints ¶ added in v2.1.0
NewEndpoints creates and returns Endpoints from multiple addresses like: "192.168.1.100:80,192.168.1.101:80".
type LocalEndpoint ¶ added in v2.1.0
type LocalEndpoint struct {
// contains filtered or unexported fields
}
LocalEndpoint implements interface Endpoint.
func (*LocalEndpoint) Host ¶ added in v2.1.0
func (e *LocalEndpoint) Host() string
Host returns the IPv4/IPv6 address of a service.
func (*LocalEndpoint) Port ¶ added in v2.1.0
func (e *LocalEndpoint) Port() int
Port returns the port of a service.
func (*LocalEndpoint) String ¶ added in v2.1.0
func (e *LocalEndpoint) String() string
String formats and returns the Endpoint as a string, like: 192.168.1.100:80.
type LocalService ¶ added in v2.1.0
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 ¶ added in v2.1.0
func (s *LocalService) GetEndpoints() Endpoints
GetEndpoints returns the Endpoints of service. The Endpoints contain multiple host/port information of service.
func (*LocalService) GetKey ¶ added in v2.1.0
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 ¶ added in v2.1.0
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 ¶ added in v2.1.0
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 ¶ added in v2.1.0
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 ¶ added in v2.1.0
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 ¶ added in v2.1.0
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) IsEmpty ¶ added in v2.1.0
IsEmpty checks and returns whether current Metadata is empty.
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) (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 ¶
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 GetAndWatch ¶ added in v2.1.0
GetAndWatch is used to getting the service with custom watch callback function.
func NewServiceWithKV ¶
NewServiceWithKV creates and returns a default implements for interface Service by key-value pair string.
func NewServiceWithName ¶
NewServiceWithName creates and returns a default implements for interface Service by service name.
type ServiceWatch ¶
type ServiceWatch func(service Service)
ServiceWatch is used to watch the service status.