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

在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,
    }
}

评论

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注

这个站点使用 Akismet 来减少垃圾评论。了解你的评论数据如何被处理