关于golang:gozero框架之zrpcRpcServerConf配置源码分析

10次阅读

共计 3478 个字符,预计需要花费 9 分钟才能阅读完成。

在 go-zero 中应用 zrpc 包来配置 rpc 服务,zrpc/config.go 文件中定义了两个构造体 (RpcServerConf 和 RpcClientConf)。

一、RpcServerConf 构造体:

type RpcServerConf struct {
    service.ServiceConf
    ListenOn      string
    Etcd          discov.EtcdConf    `json:",optional"`
    Auth          bool               `json:",optional"`
    Redis         redis.RedisKeyConf `json:",optional"`
    StrictControl bool               `json:",optional"`
    // setting 0 means no timeout
    Timeout      int64 `json:",default=2000"`
    CpuThreshold int64 `json:",default=900,range=[0:1000]"`
}
  • service.ServiceConf:调用的是 core/serviceConf.go 配置文件,文件中定义 ServiceConf 构造体,其中 Prometheus 字段被定义在 core/prometheus/config.go 文件
// 服务端的根本配置信息
type ServiceConf struct {
    Name       string
    Log        logx.LogConf
    Mode       string            `json:",default=pro,options=dev|test|rt|pre|pro"`
    MetricsUrl string            `json:",optional"`
    Prometheus prometheus.Config `json:",optional"`
}

// prometheus 配置
type Config struct {
    Host string `json:",optional"`
    Port int    `json:",default=9101"`
    Path string `json:",default=/metrics"`
}
  • ListenOn:监听门路
  • Etcd:etcd 配置,该项调用的是 core/discov/config.go 文件中的 EtcdConf 构造体,构造体有一个 Validate() 办法,该办法用于判断 Hosts 和 Key 是否为空,如果为空返回谬误,反之返回 nil。
// EtcdConf is the config item with the given key on etcd.
type EtcdConf struct {Hosts []string
    Key   string
}

// Validate validates c.
func (c EtcdConf) Validate() error {if len(c.Hosts) == 0 {return errors.New("empty etcd hosts")
    } else if len(c.Key) == 0 {return errors.New("empty etcd key")
    } else {return nil}
}
  • Auth:bool 值
  • Redis:redis 配置,调用的是 core/stores/redis/conf.go 文件中的 RedisKeyConf 构造体。该文件定义两个构造体:RedisKeyConf 和 RedisConf,RedisKeyConf 内嵌 RedisConf。

    • RedisConf 构造体

      • Host: 主机
      • Type: 默认为 node,可选项:node/cluster
      • Pass: 可选
      • Tls: bool 类型,是否开启,默认不开启

        // A RedisConf is a redis config.
        RedisConf struct {
        Host string
        Type string `json:",default=node,options=node|cluster"`
        Pass string `json:",optional"`
        Tls  bool   `json:",default=false,options=true|false"`
        }
    • RedisKeyConf 构造体只有两个字段:key 和内嵌的 RedisConf 构造体,这种配置相似于 kv 设置。
    • RedisConf 构造体定义了两个办法,Validate 和 NewRedis。Validate 判断的 Type 和 Host 是否为空,如果为空返回谬误,反之返回为 nil。NewRedis 中定义 Option 组成的数组 opts,如果 Type 为 cluster,则向 opts 中增加一个 cluster(),如果 Pass 大于 0,则向 opts 中增加 WithPasss(),如果 Tls 为 true,则向 opts 增加 WithTLS()。最初返回一个 host 和可选项组成的 Redis 配置指针。
    // Validate validates the RedisConf.
    func (rc RedisConf) Validate() error {if len(rc.Host) == 0 {return ErrEmptyHost}
    
      if len(rc.Type) == 0 {return ErrEmptyType}
    
      return nil
    }
    
    // NewRedis returns a Redis.
    func (rc RedisConf) NewRedis() *Redis {var opts []Option
      if rc.Type == ClusterType {opts = append(opts, Cluster())
      }
      if len(rc.Pass) > 0 {opts = append(opts, WithPass(rc.Pass))
      }
      if rc.Tls {opts = append(opts, WithTLS())
      }
    
      return New(rc.Host, opts...)
    }
    • RedisKeyConf 构造体蕴含一个 Validate 办法,该办法调用 RedisConf 的 Validate 办法,并且判断 key 是否为空,如果其中一个有错返回 err,反之返回 nil。
  • StrictControl:是否开启严格模式,可选
  • Timeout:超时工夫默认为 2000 毫秒
  • CpuThreshold:范畴在 0 -1000,默认 900

RpcServerConf 办法蕴含两个办法:HasEtcd 和 Validate

  • HasEtcd 办法判断 host 和 key 是否存在,从判断是否应用 etcd
  • Validate 办法调用 redis 中的 Validate 办法

二、RpcClientConf 构造体

// A RpcClientConf is a rpc client config.
RpcClientConf struct {
    Etcd      discov.EtcdConf `json:",optional"`
    Endpoints []string        `json:",optional=!Etcd"`
    App       string          `json:",optional"`
    Token     string          `json:",optional"`
    Timeout   int64           `json:",default=2000"`
}
  • Etcd 字段:etcd 配置,同 RpcServerConf
  • Entpoints:由 string 类型组成的数组
  • App:string 类型
  • Token:string 类型
  • Timeout:超时默认 2000

RpcClientConf 蕴含一个 HasCredential 办法,该办法返回一个 error,如果 App 和 Token 同时存在则返回 true,反之返回 false。

到这里,rpc 的客户端和服务端配置字段和间接用到的办法根本就介绍完了,下一步进行一个简略的实际。

三、实际

  • 在服务端创立 yaml 配置文件
Name: user.rpc
ListenOn: 127.0.0.1:8080
Etcd:
  Hosts:
  - 127.0.0.1:2379
  Key: user.rpc
  • 在服务端中 config.go 定义一个构造体,构造体外部定义字段,调用 zrpc 中的 RpcServerConf 构造体
package config

import "github.com/tal-tech/go-zero/zrpc"

type Config struct {zrpc.RpcServerConf}
  • 实例化配置,NewServiceContext 办法返回 *ServiceContext
package svc

import "user/rpc/internal/config"

type ServiceContext struct {Config config.Config}

func NewServiceContext(c config.Config) *ServiceContext {
    return &ServiceContext{Config: c,}
}

正文完
 0