- Address">type Address
- Resolver">type Resolver
- ResolveNowOptions">type ResolveNowOptions
- BuildOptions">type BuildOptions
- Builder">type Builder
- ClientConn">type ClientConn
- State">type State
- Target">type Target
为了保持连接的活动性、健康性和可用性,gRPC使用了许多组件,其中最重要的是名称解析器和负载平衡器。解析程序将名称转换为地址,然后将这些地址交给负载平衡器。负载平衡器负责从这些地址创建连接,并在连接之间进行负载平衡。

func GetDefaultScheme() string 获取将使用的默认方案
func SetDefaultScheme(scheme string)
- SetDefaultScheme设置将使用的默认方案,一般不需要使用,在grpc拨号时,会根据scheme具体选择
func Register(b Builder) 注册解析器
type Address
type Address struct {// Addr是将在其上建立连接的服务器地址Addr string// ServerName is the name of this address.// If non-empty, the ServerName is used as the transport certification authority for// the address, instead of the hostname from the Dial target string. In most cases,// this should not be set.//// If Type is GRPCLB, ServerName should be the name of the remote load// balancer, not the name of the backend.//// WARNING: ServerName must only be populated with trusted values. It// is insecure to populate it with data from untrusted inputs since untrusted// values could be used to bypass the authority checks performed by TLS.ServerName string// Attributes contains arbitrary data about this address intended for// consumption by the load balancing policy.Attributes *attributes.Attributes// Type is the type of this address.//// Deprecated: use Attributes instead.Type AddressType// Metadata is the information associated with Addr, which may be used// to make load balancing decision.//// Deprecated: use Attributes instead.Metadata interface{}}
type Resolver
解析器监视指定目标上的更新。包括地址更新和服务配置更新
type Resolver interface {// ResolveNow将被gRPC调用,试图再次解析目标名称。这只是一个提示,// 如果没有必要,解析器可以忽略它。它可以被并发地调用多次。ResolveNow(ResolveNowOptions)// Close closes the resolver.Close()}
type ResolveNowOptions
type ResolveNowOptions struct{}
type BuildOptions
BuildOptions包含用于创建解析器的构建器的附加信息
type BuildOptions struct {// DisableServiceConfig indicates whether a resolver implementation should// fetch service config data.DisableServiceConfig bool// DialCreds is the transport credentials used by the ClientConn for// communicating with the target gRPC service (set via// WithTransportCredentials). In cases where a name resolution service// requires the same credentials, the resolver may use this field. In most// cases though, it is not appropriate, and this field may be ignored.DialCreds credentials.TransportCredentials// CredsBundle is the credentials bundle used by the ClientConn for// communicating with the target gRPC service (set via// WithCredentialsBundle). In cases where a name resolution service// requires the same credentials, the resolver may use this field. In most// cases though, it is not appropriate, and this field may be ignored.CredsBundle credentials.Bundle// Dialer is the custom dialer used by the ClientConn for dialling the// target gRPC service (set via WithDialer). In cases where a name// resolution service requires the same dialer, the resolver may use this// field. In most cases though, it is not appropriate, and this field may// be ignored.Dialer func(context.Context, string) (net.Conn, error)}
type Builder
type Builder interface {// Build为给定的目标创建一个新的解析器。gRPC拨号呼叫同步生成,如果返回的错误不是nil,则会失败。Build(target Target, cc ClientConn, opts BuildOptions) (Resolver, error)// Scheme返回此解析器支持的方案Scheme() string}
func Get(scheme string) Builder
- Get返回用给定方案注册的冲突解决程序生成器。如果没有生成器注册到方案中,则返回nil。
type ClientConn
type ClientConn interface {// 更新ClientConn的状态UpdateState(State)// ReportError通知ClientConn解析器遇到错误。ClientConn将通知负载均衡器,// 并开始使用指数级回退调用解析器上的ResolveNow。ReportError(error)// 解析器调用NewAddress来通知ClientConn一个已解析地址的新列表。// 地址列表应该是已解析地址的完整列表。弃用:改为使用UpdateState。NewAddress(addresses []Address)// 解析器调用NewServiceConfig来通知ClientConn一个新的服务配置。// 服务配置应该以json字符串的形式提供。弃用:改为使用UpdateState。NewServiceConfig(serviceConfig string)// ParseServiceConfig解析所提供的服务配置并返回对象,该对象提供已解析的配置ParseServiceConfig(serviceConfigJSON string) *serviceconfig.ParseResult}
ClientConn包含解析器的回调,用于通知gRPC ClientConn的任何更新。
该接口由gRPC实现。用户不应该需要这个接口的全新实现。对于测试之类的情况,新的实现应该嵌入这个接口。这允许gRPC向这个接口添加新方法。
type State
State包含与ClientConn相关的当前解析器状态
type State struct {// 地址是目标的最新解析地址集.Addresses []Address// ServiceConfig包含解析最新服务配置的结果。// 如果为nil,则表示不存在服务配置,或者解析器不提供服务配置。ServiceConfig *serviceconfig.ParseResult// 属性包含有关解析器的任意数据,供负载平衡策略使用。Attributes *attributes.Attributes}
type Target
type Target struct {Scheme stringAuthority stringEndpoint string}
Target表示gRPC的一个目标,具体如下:https://github.com/grpc/grpc/blob/master/doc/naming.md。
它是从目标字符串解析,由用户传递到Dial或DialContext。grpc将其传递给解析器和平衡器。
如果目标符合命名规范,并且解析后的方案已注册到grpc,我们将根据该规范解析目标字符串。“dns: / / some_authority / foo.bar”将被解析为&Target{Scheme: “dns”, Authority: “some_authority”, Endpoint: “foo.bar”}
如果目标不包含模式,我们将应用默认模式,并将目标设置为完整的目标字符串。如:“foo.bar”将被解析为&Target{Scheme: resolver.GetDefaultScheme(), Endpoint: “foo.bar”}。
如果解析后的模式没有注册(即没有相应的解析器可用来解析端点),我们将模式设置为默认模式,并将端点设置为完整的目标字符串。例如,目标字符串“unknown_scheme://authority/endpoint”将被解析为&Target{Scheme: resolv . getdefaultscheme(),Endpoint:“unknown_scheme://authority/endpoint”}。
