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.
)
SetRegistry sets the default Registry implements as your own implemented interface.
SetRegistry 将默认的 Registry 实现设置为您自己的实现接口。
类型
type Discovery
1
2
3
4
5
6
7
8
typeDiscoveryinterface{// Search searches and returns services with specified condition.
Search(ctxcontext.Context,inSearchInput)(result[]Service,errerror)// Watch watches specified condition changes.
// The `key` is the prefix of service key.
Watch(ctxcontext.Context,keystring)(watcherWatcher,errerror)}
Discovery interface for service discovery.
用于服务发现的发现接口。
type Endpoint <-2.1.0
1
2
3
4
5
6
7
8
9
10
typeEndpointinterface{// 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 <-2.1.0
1
funcNewEndpoint(addressstring)Endpoint
NewEndpoint creates and returns an Endpoint from address string of pattern “host:port”, eg: “192.168.1.100:80”.
typeLocalServicestruct{Headstring// Service custom head string in service key.
Deploymentstring// Service deployment name, eg: dev, qa, staging, prod, etc.
Namespacestring// Service Namespace, to indicate different services in the same environment with the same Name.
Namestring// Name for the service.
Versionstring// Service version, eg: v1.0.0, v2.1.1, etc.
EndpointsEndpoints// Service Endpoints, pattern: IP:port, eg: 192.168.1.2:8000.
MetadataMetadata// Custom data for this service, which can be set using JSON by environment or command-line.
}
LocalService provides a default implements for interface Service.
LocalService 为接口服务提供默认实现。
(*LocalService) GetEndpoints
1
func(s*LocalService)GetEndpoints()Endpoints
GetEndpoints returns the Endpoints of service. The Endpoints contain multiple host/port information of service.
GetEndpoints 返回服务的终结点。端点包含多个主机/端口服务信息。
(*LocalService) GetKey
1
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.
GetKey 格式化并返回服务的唯一键字符串。结果键通常用于键值注册器服务器。
(*LocalService) GetMetadata
1
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.
GetMetadata 返回服务的元数据映射。元数据是键值对映射,用于指定服务的额外属性。
(*LocalService) GetName
1
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.
GetName 返回服务的名称。该名称对于服务是必需的,并且在服务中应该是唯一的。
(*LocalService) GetPrefix
1
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
以 etcd server 为例,前缀字符串的使用方式如下: etcdctl get /services/prod/hello.svc --prefix
(*LocalService) GetValue
1
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.
GetValue 格式化并返回服务的值。结果值通常用于键值注册器服务器。
(*LocalService) GetVersion
1
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”.
GetVersion 返回服务的版本。建议使用 GNU 版本命名,例如:v1.0.0、v2.0.1、v2.1.0-rc。一个服务可以同时部署多个版本。如果未在服务中设置版本,则默认服务版本为“最新”。
type Metadata
1
typeMetadatamap[string]interface{}
Metadata stores custom key-value pairs.
元数据存储自定义键值对。
(Metadata) Get
1
func(mMetadata)Get(keystring)*gvar.Var
Get retrieves and returns value of specified key as gvar.
获取检索并返回指定键的值作为 gvar。
(Metadata) IsEmpty
1
func(mMetadata)IsEmpty()bool
IsEmpty checks and returns whether current Metadata is empty.
IsEmpty 检查并返回当前元数据是否为空。
(Metadata) Set
1
func(mMetadata)Set(keystring,valueinterface{})
Set sets key-value pair into metadata.
将键值对设置为元数据。
(Metadata) Sets
1
func(mMetadata)Sets(kvsmap[string]interface{})
Sets sets key-value pairs into metadata.
Sets 将键值对设置为元数据。
type Registrar
1
2
3
4
5
6
7
8
typeRegistrarinterface{// Register registers `service` to Registry.
// Note that it returns a new Service if it changes the input Service with custom one.
Register(ctxcontext.Context,serviceService)(registeredService,errerror)// Deregister off-lines and removes `service` from the Registry.
Deregister(ctxcontext.Context,serviceService)error}
Registrar interface for service registrar.
服务注册器的注册器接口。
type Registry
1
2
3
4
typeRegistryinterface{RegistrarDiscovery}
Registry interface for service.
服务的注册表接口。
func GetRegistry
1
funcGetRegistry()Registry
GetRegistry returns the default Registry that is previously set. It returns nil if no Registry is set.
GetRegistry 返回以前设置的默认注册表。如果未设置注册表,则返回 nil。
type SearchInput
1
2
3
4
5
6
typeSearchInputstruct{Prefixstring// Search by key prefix.
Namestring// Search by service name.
Versionstring// Search by service version.
MetadataMetadata// Filter by metadata if there are multiple result.
}
typeServiceinterface{// 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}
Search searches and returns services with specified condition.
搜索 搜索并返回具有指定条件的服务。
type ServiceWatch
1
typeServiceWatchfunc(serviceService)
ServiceWatch is used to watch the service status.
ServiceWatch 用于监视服务状态。
type Watcher
1
2
3
4
5
6
7
8
typeWatcherinterface{// Proceed proceeds watch in blocking way.
// It returns all complete services that watched by `key` if any change.
Proceed()(services[]Service,errerror)// Close closes the watcher.
Close()error}