discov

package module
v1.0.0 Latest Latest
Warning

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

Go to latest
Published: Feb 7, 2021 License: MIT Imports: 11 Imported by: 0

README

Discov

中文文档

Discov is a generic gRPC service registration and discovery / load balancing component.

Supported Scenarios

  • The scenario of etcd as the service registry.
  • The scenario of using headless service as DNS name to discover services in Kubernetes cluster.
  • The scenario of DNS as a service registry.

Architecture

architecture

Examples

Documentation

pkg.go.dev

License

MIT License

Documentation

Overview

Package discov provides the APIs for service registration and discovery of gRPC.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Builder

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

Builder implements the gRPC resolver.Builder interface. It Builds a discov Resolver that implements the gRPC resolver.Resolver interface.

func NewBuilder

func NewBuilder(opts ...BuilderOption) *Builder

NewBuilder returns a Builder that contain the passed options.

func (*Builder) Build

func (b *Builder) Build(target resolver.Target, cc resolver.ClientConn, opts resolver.BuildOptions) (r resolver.Resolver, err error)

Build creates a new resolver for the given target.

If the authority of target that passed to grpc dial method is etcd, Build returns an internal etcdResolver, if the authority is dns, returns an internal dnsResolver.

The options passed to NewBuilder will be used in this method.

func (*Builder) Scheme

func (b *Builder) Scheme() string

Scheme returns the scheme "discov" correspond to the Builder.

type BuilderOption

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

BuilderOption is the option that passed to NewBuilder function.

func WithDNSPollingInterval

func WithDNSPollingInterval(d time.Duration) BuilderOption

WithDNSPollingInterval customizes the polling frequency of the internal dnsResolver.

func WithEtcdClient

func WithEtcdClient(cli *clientv3.Client) BuilderOption

WithEtcdClient injects an etcd client. The option is required when the authority of discov is etcd.

func WithEtcdKvResolver

func WithEtcdKvResolver(r EtcdKvResolver) BuilderOption

WithEtcdKvResolver injects a custom EtcdKvResolver implementation to determine how the internal etcdResolver watches keys and retrieves addrs.

type DefaultEtcdKvBuilder

type DefaultEtcdKvBuilder struct{}

DefaultEtcdKvBuilder will be used if the user does not RegisterKvBuilder.

func (*DefaultEtcdKvBuilder) BuildKey

func (*DefaultEtcdKvBuilder) BuildKey(srvName, srvAddr string) string

BuildKey returns the key of "/srv/${srvName}/${srvAddr}".

func (*DefaultEtcdKvBuilder) BuildValue

func (*DefaultEtcdKvBuilder) BuildValue(_, srvAddr string) string

BuildValue returns the value as the same as srvAddr.

type DefaultEtcdKvResolver

type DefaultEtcdKvResolver struct{}

DefaultEtcdKvResolver will be used if the user doesn't set the EtcdKvResolver. DefaultEtcdKvResolver watches keys with prefix of "/srv/${srvName}", and treats the value of the key directly as an address.

func (*DefaultEtcdKvResolver) GetKeyPrefixForSrv

func (*DefaultEtcdKvResolver) GetKeyPrefixForSrv(srvName string) string

GetKeyPrefixForSrv returns the prefix of "/srv/${srvName}".

func (*DefaultEtcdKvResolver) ResolveSrvAddr

func (r *DefaultEtcdKvResolver) ResolveSrvAddr(value []byte) string

ResolveSrvAddr returns the string content of value.

type EtcdKvBuilder

type EtcdKvBuilder interface {
	// BuildKey builds the etcd key according to the name and
	// the address of the service.
	BuildKey(srvName, srvAddr string) (key string)
	// BuildValue builds the etcd value according to the name
	// and the address of the service.
	BuildValue(srvName, srvAddr string) (value string)
}

EtcdKvBuilder builds the key and the value to store to etcd.

type EtcdKvResolver

type EtcdKvResolver interface {
	// GetKeyPrefixForSrv generates the key prefix for etcdResolver to watch.
	// srvName is the endpoint in the target that passed to the grpc Dial method.
	GetKeyPrefixForSrv(srvName string) (prefix string)
	// ResolveSrvAddr resolves the service address from the value corresponding
	// to the etcd key that be watched.
	ResolveSrvAddr(value []byte) (srvAddr string)
}

EtcdKvResolver determines how the internal etcdResolver watches keys and retrieves addrs.

type EtcdSrvRecord

type EtcdSrvRecord interface {
	// Register registers the record to etcd.
	// This method should be called when the service starts.
	Register()
	// Unregister unregisters the record from etcd.
	// This method should be called when the service is closed.
	Unregister(context.Context) error
	// CatchRuntimeErrors catches runtime errors.
	// Since the EtcdSrvRecord spawns goroutines to keep heartbeat
	// to etcd after calling Register, some errors may occur at runtime.
	CatchRuntimeErrors() <-chan error
	// RegisterKvBuilder injects an EtcdKvBuilder to custom the
	// format of key and value to store.
	RegisterKvBuilder(EtcdKvBuilder)
}

EtcdSrvRecord is a service record to be registered to or unregistered from etcd.

func NewEtcdSrvRecord

func NewEtcdSrvRecord(cli *clientv3.Client, srvName, srvAddr string) (EtcdSrvRecord, error)

NewEtcdSrvRecord creates a service record.

Directories

Path Synopsis
examples

Jump to

Keyboard shortcuts

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