go-zero configuration overview

Overview

go-Zero provides a powerful conf package to load configurations.We currently support yaml, json, toml 3 format configuration files, go-zero will load their own file format by suffix.

How to use

We use package github.com/zeromicro/go-zero/core/conf conf to load it.

As a first step, we will define our configuration structure, which defines all our needs dependency.

The second step goes on to prepare a configuration file based on the configuration.

Third party loading configuration via conf.MustLoad

Specific Usage:

  • main.go
  • config.yaml
  1. package main
  2. import (
  3. "flag"
  4. "github.com/zeromicro/go-zero/core/conf"
  5. )
  6. type Config struct {
  7. Host string `json:",default=0.0.0.0"`
  8. Port int
  9. }
  10. var f = flag.String("f", "config.yaml", "config file")
  11. func main() {
  12. flag.Parse()
  13. var c Config
  14. conf.MustLoad(*f, &c)
  15. println(c.Host)
  16. }
  1. Host: 127.0.0.1
  2. Port: 8888

We typically load the configuration when the program is started, and we generally need to define the structure we need for the configuration, In go-zero we recommend that all service dependencies be defined in the config so that the configuration can find all dependencies later based on the config configuration.

We use func MostLoad(path string, v interface{}, opts ..Option) to load configuration, path to configuration, v to structure. This method will complete the configuration load. If the configuration load fails, the whole app will stop dropping.

Of course we also offer other means of loading, eg::

  1. func Load(file string, v interface{}, opts ...Option) error

Other formatting profiles

We currently support the configuration format below:

  • json
  • yaml | yml
  • toml

Our program will automatically load the corresponding format with a file suffix.

We also provide a way to load binary data in conf package:

  1. func LoadFromJsonBytes(content []byte, v interface{}) error
  2. func LoadFromTomlBytes(content []byte, v interface{}) error
  3. func LoadFromYamlBytes(content []byte, v interface{}) error

Simple Example:

  1. text := []byte(`a: foo
  2. B: bar`)
  3. var val struct {
  4. A string
  5. B string
  6. }
  7. _ = LoadFromYamlBytes(text, &val)
go-zero configuration overview - 图1note

For some of those who need custom tag, for ease of harmonization, we currently all tag are json tag.

Case insensitive

conf now automatically supports key case insensitivity by default, for example, we can parse out the following configurations:

  1. Host: "127.0.0.1"
  2. host: "127.0.0.1"

Environment Variables

Current conf configuration supports environment variable injections, we have 2 ways to implement

1. conf.UseEnv()

  1. var c struct {
  2. Name string
  3. }
  4. conf.MustLoad("config.yaml", &c, conf.UseEnv())
  1. Name: ${SERVER_NAME}

As above, we pass in UseEnv during Load, and conf will automatically replace the ${var} or $var current environment variable in the string according to the value.

2. env Tag

Note that env requires go-zero v1.4.3 to be supported.

  1. var c struct {
  2. Name string `json:",env=SERVER_NAME"`
  3. }
  4. conf.MustLoad("config.yaml", &c)

We can add env=SERVER_NAME , after json tag and conf will automatically load the corresponding environment variable.

::note
We configure the order of loading priority env > definition in configuration > definition of default in json tag :::

tag checksum rule

We can state the rules for the reception of parameters in a tag in a tag, in addition to supporting the verification of parameters, which are written in tag value, as simple as:

  1. type Config struct {
  2. Name string // No tag, indicating configuration required
  3. Port int64 `json:",default=8080"` // If not configured in the configuration, it will be 8080
  4. Path string `json:",optional"`
  5. }

If we are loading in conf the verification is not passed, the corresponding error will be reported.

The currently supported verification rules for go-zero are as follows:

Receive rulesNoteSample
optionalThe current field is an optional parameter, allowing zero value (zero value)json:"foo,optional"
optionsCurrent parameter can only receive an enumeration valueProtestant 1:portrait line\
defaultCurrent Argument Defaultjson:"gender,default=male"
rangeThe valid range of the current parameter value, only valid for the value. Details of the writing rule are given belowjson:"age,range=[0:120]"
envCurrent parameters are taken from environmental variablesjson:"mode,env=MODE"

::note range expressed value rule

  1. Left close interval:(min:max], meaning that min is less than or equal to max, when min is default, min represents value 0, max is unlimited when max is default, min and max are not allowed to default at the same time
  2. Left right interval:[min:max), which indicates that it is less than min max, when max is default, max represents a value of 0, min is large when min is missing
  3. Shutdown interval:[min:max], denotes less than min less than equals max, when min is default, min represents value 0, max is infinite when max is default, min and max are not allowed to coalesce
  4. Open interval:(min:max), indicates that min is less than max, when min is default, min represents a value of 0, max is large when max is default,min and max cannot be used simultaneous :::

More reference unmarshaler_test.go

inherit 配置继承

In our daily configuration, there are many duplicate configurations, such as rpcClientConf where each rpc has a etcd configuration, but in most of our cases the etcd configuration is the same and we hope it can be configured only once. Examples below

  1. type Config struct {
  2. Etcd discov.EtcdConf
  3. UserRpc zrpc.RpcClientConf
  4. PortRpc zrpc.RpcClientConf
  5. OtherRpc zrpc.RpcClientConf
  6. }
  7. const str = `
  8. Etcd:
  9. Key: rpcServer"
  10. Hosts:
  11. - "127.0.0.1:6379"
  12. - "127.0.0.1:6377"
  13. - "127.0.0.1:6376"
  14. UserRpc:
  15. Etcd:
  16. Key: UserRpc
  17. Hosts:
  18. - "127.0.0.1:6379"
  19. - "127.0.0.1:6377"
  20. - "127.0.0.1:6376"
  21. PortRpc:
  22. Etcd:
  23. Key: PortRpc
  24. Hosts:
  25. - "127.0.0.1:6379"
  26. - "127.0.0.1:6377"
  27. - "127.0.0.1:6376"
  28. OtherRpc:
  29. Etcd:
  30. Key: OtherRpc
  31. Hosts:
  32. - "127.0.0.1:6379"
  33. - "127.0.0.1:6377"
  34. - "127.0.0.1:6376"
  35. `

We must add Hosts to every Etcd and other base configurations.

But if we use the tag definition inherit, the method used is to add to the tag.The following is:

  1. // A RpcClientConf is a rpc client config.
  2. RpcClientConf struct {
  3. Etcd discov.EtcdConf `json:",optional,inherit"`
  4. ....
  5. }

This will allow us to simplify the Etcd configuration, which he will automatically look to the top level.

  1. const str = `
  2. Etcd:
  3. Key: rpcServer"
  4. Hosts:
  5. - "127.0.0.1:6379"
  6. - "127.0.0.1:6377"
  7. - "127.0.0.1:6376"
  8. UserRpc:
  9. Etcd:
  10. Key: UserRpc
  11. PortRpc:
  12. Etcd:
  13. Key: PortRpc
  14. OtherRpc:
  15. Etcd:
  16. Key: OtherRpc
  17. `