为了保持连接的活动性、健康性和可用性,gRPC使用了许多组件,其中最重要的是名称解析器和负载平衡器。解析程序将名称转换为地址,然后将这些地址交给负载平衡器。负载平衡器负责从这些地址创建连接,并在连接之间进行负载平衡。
grpc/resolver - 图1
grpc/resolver - 图2

func GetDefaultScheme() string 获取将使用的默认方案
func SetDefaultScheme(scheme string)

  • SetDefaultScheme设置将使用的默认方案,一般不需要使用,在grpc拨号时,会根据scheme具体选择

func Register(b Builder) 注册解析器

type Address

  1. type Address struct {
  2. // Addr是将在其上建立连接的服务器地址
  3. Addr string
  4. // ServerName is the name of this address.
  5. // If non-empty, the ServerName is used as the transport certification authority for
  6. // the address, instead of the hostname from the Dial target string. In most cases,
  7. // this should not be set.
  8. //
  9. // If Type is GRPCLB, ServerName should be the name of the remote load
  10. // balancer, not the name of the backend.
  11. //
  12. // WARNING: ServerName must only be populated with trusted values. It
  13. // is insecure to populate it with data from untrusted inputs since untrusted
  14. // values could be used to bypass the authority checks performed by TLS.
  15. ServerName string
  16. // Attributes contains arbitrary data about this address intended for
  17. // consumption by the load balancing policy.
  18. Attributes *attributes.Attributes
  19. // Type is the type of this address.
  20. //
  21. // Deprecated: use Attributes instead.
  22. Type AddressType
  23. // Metadata is the information associated with Addr, which may be used
  24. // to make load balancing decision.
  25. //
  26. // Deprecated: use Attributes instead.
  27. Metadata interface{}
  28. }

type Resolver

解析器监视指定目标上的更新。包括地址更新和服务配置更新

  1. type Resolver interface {
  2. // ResolveNow将被gRPC调用,试图再次解析目标名称。这只是一个提示,
  3. // 如果没有必要,解析器可以忽略它。它可以被并发地调用多次。
  4. ResolveNow(ResolveNowOptions)
  5. // Close closes the resolver.
  6. Close()
  7. }

type ResolveNowOptions

  1. type ResolveNowOptions struct{}

type BuildOptions

BuildOptions包含用于创建解析器的构建器的附加信息

  1. type BuildOptions struct {
  2. // DisableServiceConfig indicates whether a resolver implementation should
  3. // fetch service config data.
  4. DisableServiceConfig bool
  5. // DialCreds is the transport credentials used by the ClientConn for
  6. // communicating with the target gRPC service (set via
  7. // WithTransportCredentials). In cases where a name resolution service
  8. // requires the same credentials, the resolver may use this field. In most
  9. // cases though, it is not appropriate, and this field may be ignored.
  10. DialCreds credentials.TransportCredentials
  11. // CredsBundle is the credentials bundle used by the ClientConn for
  12. // communicating with the target gRPC service (set via
  13. // WithCredentialsBundle). In cases where a name resolution service
  14. // requires the same credentials, the resolver may use this field. In most
  15. // cases though, it is not appropriate, and this field may be ignored.
  16. CredsBundle credentials.Bundle
  17. // Dialer is the custom dialer used by the ClientConn for dialling the
  18. // target gRPC service (set via WithDialer). In cases where a name
  19. // resolution service requires the same dialer, the resolver may use this
  20. // field. In most cases though, it is not appropriate, and this field may
  21. // be ignored.
  22. Dialer func(context.Context, string) (net.Conn, error)
  23. }

type Builder

  1. type Builder interface {
  2. // Build为给定的目标创建一个新的解析器。gRPC拨号呼叫同步生成,如果返回的错误不是nil,则会失败。
  3. Build(target Target, cc ClientConn, opts BuildOptions) (Resolver, error)
  4. // Scheme返回此解析器支持的方案
  5. Scheme() string
  6. }

func Get(scheme string) Builder

  • Get返回用给定方案注册的冲突解决程序生成器。如果没有生成器注册到方案中,则返回nil。

type ClientConn

  1. type ClientConn interface {
  2. // 更新ClientConn的状态
  3. UpdateState(State)
  4. // ReportError通知ClientConn解析器遇到错误。ClientConn将通知负载均衡器,
  5. // 并开始使用指数级回退调用解析器上的ResolveNow。
  6. ReportError(error)
  7. // 解析器调用NewAddress来通知ClientConn一个已解析地址的新列表。
  8. // 地址列表应该是已解析地址的完整列表。弃用:改为使用UpdateState。
  9. NewAddress(addresses []Address)
  10. // 解析器调用NewServiceConfig来通知ClientConn一个新的服务配置。
  11. // 服务配置应该以json字符串的形式提供。弃用:改为使用UpdateState。
  12. NewServiceConfig(serviceConfig string)
  13. // ParseServiceConfig解析所提供的服务配置并返回对象,该对象提供已解析的配置
  14. ParseServiceConfig(serviceConfigJSON string) *serviceconfig.ParseResult
  15. }

ClientConn包含解析器的回调,用于通知gRPC ClientConn的任何更新。
该接口由gRPC实现。用户不应该需要这个接口的全新实现。对于测试之类的情况,新的实现应该嵌入这个接口。这允许gRPC向这个接口添加新方法。

type State

State包含与ClientConn相关的当前解析器状态

  1. type State struct {
  2. // 地址是目标的最新解析地址集.
  3. Addresses []Address
  4. // ServiceConfig包含解析最新服务配置的结果。
  5. // 如果为nil,则表示不存在服务配置,或者解析器不提供服务配置。
  6. ServiceConfig *serviceconfig.ParseResult
  7. // 属性包含有关解析器的任意数据,供负载平衡策略使用。
  8. Attributes *attributes.Attributes
  9. }

type Target

  1. type Target struct {
  2. Scheme string
  3. Authority string
  4. Endpoint string
  5. }

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”}。