The ZRPC package is used to configure the RPC service in Go-Zero. Two structures (RpcServerConf and rpccclientconf) are defined in the ZRPC /config.go file.

1. RPCServerConf structure:

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]"`
}
  • Is the core service. ServiceConf: call/ServiceConf go configuration file, the file defined in ServiceConf structures, including Prometheus fields are defined in core/Prometheus/config. Go file
// 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 configuration type Config {Host string 'json:",optional"' Port int 'json:",default=9101"  Path string `json:",default=/metrics"` }
  • ListenOn: Listenon the path
  • ETCD: ETCD configuration. This calls the ETCDConf structure in the core/discov/config.go file. The structure has a Validate() method that determines if the Hosts and Key are empty.
// 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: Boolean value
  • Redis: Redis configuration, is called the core/stores/Redis/conf. Go RedisKeyConf structure in the file. This file defines two structures: RedisKeyConf and RedisConf, and RedisKeyConf is embedded with RedisConf.

    • RedisConf structure

      • Host: the Host
      • Type: Default is node, optional: node/cluster
      • Pass: optional
      • Tls: bool type, whether enabled, the default is not enabled

        // 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"` }
    • The redisKeyConf structure has only two fields: Key and the inline redisConf structure. This configuration is similar to the KV setting.
    • The RedisConf structure defines two methods, Validate and NewRedis. Validate determines if Type and Host are null. If so, an error is returned. Otherwise, nil is returned. If the Type is cluster, then add a cluster() to opts. If the Pass is greater than 0, then add WithPasss() to opts. If the Tls is true, then add a cluster() to opts. Then add WithTLS() to opts. Finally, a Redis configuration pointer consisting of Host and options is returned.
    // 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...) }
    • The redisKeyConf structure contains a Validate method that calls the redisConf’s Validate method and determines whether the keys are empty, returning err if one of them is wrong, and nil if the other is wrong.
  • StrictControl: Optional to turn on strict mode
  • Timeout: The default Timeout is 2000 milliseconds
  • CPUTHRESHOLD: Range 0-1000, default 900

The RpcServerConf method contains two methods: HasEtcd and Validate

  • The HasEtcd method determines whether the host and key are present, which determines whether etcd is used
  • The Validate method calls the Validate method in Redis

2. RPCCClientConf structure

// 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 field: ETCD configuration, same as RpcServerConf
  • Entpoints: An array of strings
  • App: string type
  • Token: string type
  • Timeout: Timeout defaults to 2000

RPCClientConf contains a HasCredential method that returns an Error, true if both App and Token exist, and false if both exist.

With that in mind, RPC’s client-side and server-side configuration fields and the methods used directly are basically covered, so let’s move on to a simple practice.

Three, practice

  • Create a YAML configuration file on the server side
Name: User.rpc Listenon: 127.0.0.1:8080 ETCD: Hosts: -127.0.0.1:2379 Key: User.rpc
  • In the server side, config.go defines a structure with fields defined inside the structure, and calls the RpcServerConf structure in ZRPC
package config

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

type Config struct {
  zrpc.RpcServerConf
}
  • To instantiate the configuration, the newServiceContext method returns *ServiceContext
package svc

import "user/rpc/internal/config"

type ServiceContext struct {
    Config config.Config
}

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