gsvc

package
v0.0.0-...-2910145 Latest Latest
Warning

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

Go to latest
Published: Feb 14, 2024 License: MIT Imports: 13 Imported by: 0

Documentation

Overview

Package gsvc 提供了服务注册与发现的定义。

Package gsvc 提供了服务注册与发现的定义。

Package gsvc 提供了服务注册与发现的定义。

Index

Constants

View Source
const (
	Schema                    = `service`            // Schema 是服务的架构。
	DefaultHead               = `service`            // DefaultHead 是服务的默认头部。
	DefaultDeployment         = `default`            // DefaultDeployment 是服务的默认部署。
	DefaultNamespace          = `default`            // DefaultNamespace 是服务的默认命名空间。
	DefaultVersion            = `latest`             // DefaultVersion 是服务的默认版本。
	EnvPrefix                 = `GF_GSVC_PREFIX`     // EnvPrefix 是环境变量前缀。
	EnvDeployment             = `GF_GSVC_DEPLOYMENT` // EnvDeployment 是环境变量部署。
	EnvNamespace              = `GF_GSVC_NAMESPACE`  // EnvNamespace 是环境变量的命名空间。
	EnvName                   = `GF_GSVC_Name`       // EnvName 是环境变量名称。
	EnvVersion                = `GF_GSVC_VERSION`    // EnvVersion 是环境变量版本。
	MDProtocol                = `protocol`           // MDProtocol 是用于协议的元数据键。
	MDInsecure                = `insecure`           // MDInsecure 是用于表示不安全的元数据键。
	MDWeight                  = `weight`             // MDWeight 是用于权重的元数据键。
	DefaultProtocol           = `http`               // DefaultProtocol 是服务的默认协议。
	DefaultSeparator          = "/"                  // DefaultSeparator 是服务的默认分隔符。
	EndpointHostPortDelimiter = ":"                  // EndpointHostPortDelimiter 是主机和端口之间的分隔符。

	EndpointsDelimiter = "," // EndpointsDelimiter 是 endpoints 的分隔符。
)

Variables

This section is empty.

Functions

func Deregister

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

Deregister 从默认注册表中移除`service`。

func SetRegistry

func SetRegistry(registry Registry)

SetRegistry 设置默认的注册中心为自定义实现的接口。

Types

type Discovery

type Discovery interface {
	// Search 按照指定条件搜索并返回服务。
	Search(ctx context.Context, in SearchInput) (result []Service, err error)

	// Watch 监视指定条件的变化。
	// `key` 是服务键的前缀。
	Watch(ctx context.Context, key string) (watcher Watcher, err error)
}

Discovery 接口用于服务发现。

type Endpoint

type Endpoint interface {
	// Host 返回一个服务的 IPv4/IPv6 地址。
	Host() string

	// Port 返回一个服务的端口号。
	Port() int

	// String 格式化并返回 Endpoint 作为字符串。
	String() string
}

Endpoint 是服务的接口。

func NewEndpoint

func NewEndpoint(address string) Endpoint

NewEndpoint 从形如 "host:port"(例如:"192.168.1.100:80")的地址字符串创建并返回一个 Endpoint 对象。

type Endpoints

type Endpoints []Endpoint

Endpoints是由多个Endpoint组成的。

func NewEndpoints

func NewEndpoints(addresses string) Endpoints

NewEndpoints 创建并返回 Endpoints,它可以从多个地址构建,如: "192.168.1.100:80,192.168.1.101:80"。

func (Endpoints) String

func (es Endpoints) String() string

String函数将Endpoints格式化并以如下形式返回字符串: "192.168.1.100:80,192.168.1.101:80"

type LocalEndpoint

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

LocalEndpoint 实现了接口 Endpoint。

func (*LocalEndpoint) Host

func (e *LocalEndpoint) Host() string

Host 返回一个服务的 IPv4/IPv6 地址。

func (*LocalEndpoint) Port

func (e *LocalEndpoint) Port() int

Port 返回一个服务的端口号。

func (*LocalEndpoint) String

func (e *LocalEndpoint) String() string

String 将Endpoint格式化并以字符串形式返回,例如:192.168.1.100:80。

type LocalService

type LocalService struct {
	Head       string    // 在服务键中自定义头部字符串。
	Deployment string    // 服务部署名称,例如:开发(dev)、测试(qa)、预发布(staging)、生产(prod)等。
	Namespace  string    // 服务命名空间,用于在相同环境中标识具有相同名称的不同服务。
	Name       string    // 服务名称。
	Version    string    // 服务版本,例如:v1.0.0,v2.1.1等。
	Endpoints  Endpoints // 服务端点,格式:IP:端口,例如:192.168.1.2:8000
	Metadata   Metadata  // 此处为本服务自定义数据,可以通过环境变量或命令行使用 JSON 设置。
}

LocalService 为接口 Service 提供了一个默认的实现。

func (*LocalService) GetEndpoints

func (s *LocalService) GetEndpoints() Endpoints

GetEndpoints 返回服务的 Endpoints。 这些 Endpoints 包含了服务的多个主机/端口信息。

func (*LocalService) GetKey

func (s *LocalService) GetKey() string

GetKey 格式化并返回服务的唯一密钥字符串。 生成的密钥通常用于键值注册服务器。

func (*LocalService) GetMetadata

func (s *LocalService) GetMetadata() Metadata

GetMetadata 返回服务的元数据映射。 元数据是一个键值对映射,用于指定服务的额外属性。

func (*LocalService) GetName

func (s *LocalService) GetName() string

GetName 返回服务的名称。 服务必须具有名称,并且在各个服务之间应该是唯一的。

func (*LocalService) GetPrefix

func (s *LocalService) GetPrefix() string

GetPrefix 格式化并返回键前缀字符串。 生成的前缀字符串通常用于键值注册服务器中进行服务搜索。

以 etcd 服务器为例,前缀字符串可以像这样使用: `etcdctl get /services/prod/hello.svc --prefix`

func (*LocalService) GetValue

func (s *LocalService) GetValue() string

GetValue 格式化并返回服务的值。 返回的结果值通常用于键值注册服务器。

func (*LocalService) GetVersion

func (s *LocalService) GetVersion() string

GetVersion 返回服务的版本。 建议采用类似 GNU 的版本命名方式,例如:v1.0.0、v2.0.1、v2.1.0-rc。 一项服务可以同时部署多个版本。 如果在服务中未设置版本,则服务的默认版本为 "latest"。

type Metadata

type Metadata map[string]interface{}

Metadata 用于存储自定义键值对。

func (Metadata) Get

func (m Metadata) Get(key string) *泛型类.Var

Get 函数用于获取并以 gvar 类型返回指定键的值。

func (Metadata) IsEmpty

func (m Metadata) IsEmpty() bool

IsEmpty 检查并返回当前 Metadata 是否为空。

func (Metadata) Sets

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

设置将键值对设置到元数据中。

func (Metadata) X设置值

func (m Metadata) X设置值(key string, value interface{})

Set 将键值对设置到元数据中。

type Registrar

type Registrar interface {
	// Register 将 `service` 注册到 Registry。
	// 注意,如果使用自定义服务更改了输入的服务,它将返回一个新的 Service。
	Register(ctx context.Context, service Service) (registered Service, err error)

	// 从注册中心注销并移除`service`的离线信息。
	Deregister(ctx context.Context, service Service) error
}

Registrar 是服务注册器的接口。

type Registry

type Registry interface {
	Registrar
	Discovery
}

Registry interface 用于服务。

func GetRegistry

func GetRegistry() Registry

GetRegistry 返回先前设置的默认 Registry。 如果未设置 Registry,则返回 nil。

type SearchInput

type SearchInput struct {
	Prefix   string   // 通过键前缀搜索。
	Name     string   // 通过服务名搜索
	Version  string   // 通过服务版本进行搜索。
	Metadata Metadata // 如果有多个结果,通过元数据进行过滤。
}

SearchInput 是用于服务搜索的输入参数。

type Service

type Service interface {
	// GetName 返回服务的名称。
	// 服务必须具有名称,并且在各个服务之间应该是唯一的。
	GetName() string

	// GetVersion 返回服务的版本信息。
	// 建议使用GNU版本命名规则,例如:v1.0.0、v2.0.1、v2.1.0-rc。
	// 一个服务可以同时部署多个版本。
	// 如果在服务中未设置版本,则服务的默认版本为 "latest"。
	GetVersion() string

	// GetKey 格式化并返回服务的唯一密钥字符串。
	// 生成的密钥通常用于键值注册服务器。
	GetKey() string

	// GetValue 格式化并返回服务的值。
	// 返回的结果值通常用于键值注册服务器。
	GetValue() string

	// GetPrefix 格式化并返回键前缀字符串。
	// 生成的前缀字符串通常用于键值注册服务器中的服务搜索。
	//
	// 以 etcd 服务器为例,前缀字符串可以像这样使用:
	// `etcdctl get /services/prod/hello.svc --prefix`
	GetPrefix() string

	// GetMetadata 返回服务的元数据映射。
	// 元数据是一个键值对映射,用于指定服务的额外属性。
	GetMetadata() Metadata

	// GetEndpoints 返回服务的 Endpoints。
	// 这些 Endpoints 包含了服务的多个主机/端口信息。
	GetEndpoints() Endpoints
}

Service 接口,用于定义服务。

func Get

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

Get通过服务名称获取并返回服务。

func GetAndWatch

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

GetAndWatch 用于获取服务,并使用自定义的 watch 回调函数进行监听。

func GetAndWatchWithDiscovery

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

GetAndWatchWithDiscovery 用于在`discovery`中使用自定义的观察回调函数获取服务。

func GetWithDiscovery

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

GetWithDiscovery 通过 `discovery` 获取并返回指定服务名的服务。

func NewServiceWithKV

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

NewServiceWithKV 通过键值对字符串创建并返回接口 Service 的一个默认实现。

func NewServiceWithName

func NewServiceWithName(name string) Service

NewServiceWithName 通过服务名创建并返回一个接口 Service 的默认实现。

func Register

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

Register 将`service`注册到默认注册中心。

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

Search 按照指定条件搜索并返回服务。

type ServiceWatch

type ServiceWatch func(service Service)

ServiceWatch 用于监视服务状态。

type Watcher

type Watcher interface {
	// Proceed 以阻塞方式继续观察。
	// 如果有任何变化,它将返回由`key`观察到的所有已完成服务。
	Proceed() (services []Service, err error)

	// Close 关闭监视器。
	Close() error
}

Watcher接口用于服务。

func Watch

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

Watch 监视指定条件的变化。

Jump to

Keyboard shortcuts

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