配置文件
1. 格式
在生成的代码中,配置文件位于
etc
目录下,格式为yml
,yml
是最为常用的配置文件格式,go-zero支持多种配置文件格式:
yml
或者yaml
toml
json
1.1 读取YML配置
配置文件:
Name: hello01-api
Host: 0.0.0.0
Port: 8888
1
2
3
2
3
配置文件对应的结构定义:
type Config struct {
Name string
Host string
Port int
}
1
2
3
4
5
2
3
4
5
读取配置文件:
//定义配置文件路径
var configFile = flag.String("f", "etc/hello01-api.yaml", "the config file")
func main() {
flag.Parse()
//加载配置
var c config.Config
conf.MustLoad(*configFile, &c)
server := rest.MustNewServer(rest.RestConf{
Host: c.Host,
Port: c.Port,
})
defer server.Stop()
ctx := svc.NewServiceContext(c)
handler.RegisterHandlers(server, ctx)
fmt.Printf("Starting server at %s:%d...\n", c.Host, c.Port)
server.Start()
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
1.2 RestConf
rest.RestConf
是go-zero提供的配置映射实体,提供了一些默认的配置,方便我们使用。
RestConf struct {
service.ServiceConf
Host string `json:",default=0.0.0.0"`
Port int
CertFile string `json:",optional"`
KeyFile string `json:",optional"`
Verbose bool `json:",optional"`
MaxConns int `json:",default=10000"`
MaxBytes int64 `json:",default=1048576"`
// milliseconds
Timeout int64 `json:",default=3000"`
CpuThreshold int64 `json:",default=900,range=[0:1000)"`
Signature SignatureConf `json:",optional"`
// There are default values for all the items in Middlewares.
Middlewares MiddlewaresConf
// TraceIgnorePaths is paths blacklist for trace middleware.
TraceIgnorePaths []string `json:",optional"`
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
1.3 小写
使用SpringBoot的java开发人员比较熟悉,SpringBoot使用yml配置文件,都是小写。
在go-zero中如何实现小写呢?
很简单,只需要将yml
配置文件改为小写即可,go-zero在conf.MustLoad
中已经支持。
如果想要将myProp
这个配置读入Config中的prop
字段中呢?
myProp: myValue
1
我们可以加入如下的tag:
type Config struct {
Name string
Host string
Port int
Prop string `json:"myProp"`
}
1
2
3
4
5
6
2
3
4
5
6
这时候,大家可能会奇怪,为什么加
json
的tag可以实现?
我们翻看源码可以知道:
//在处理yml配置文件时,是将其转为json进行处理
func LoadFromYamlBytes(content []byte, v any) error {
b, err := encoding.YamlToJson(content)
if err != nil {
return err
}
return LoadFromJsonBytes(b, v)
}
1
2
3
4
5
6
7
8
9
2
3
4
5
6
7
8
9
1.4 默认值
如果想给默认值,处理方式和json
一样
type Config struct {
Name string
Host string
Port int
Prop string `json:"myProp"`
NoConfStr string `json:"noConfStr,default=默认值"`
}
1
2
3
4
5
6
7
2
3
4
5
6
7
由于json解析的特性,如果配置文件没有对应字段的配置就会报错,所以我们需要设置
默认值
或者设置为可选
type Config struct {
Name string
Host string
Port int
//配置文件可以没有myProp这个配置,不会报错,Prop的值为零值
Prop string `json:"myProp,optional"`
NoConfStr string `json:"noConfStr,default=默认值"`
}
1
2
3
4
5
6
7
8
2
3
4
5
6
7
8
1.5 读取JSON配置
读取方式和yml方式一样
hello01-api.json
{
"name": "hello01-api",
"host": "0.0.0.0",
"port": 8888,
"myProp": "myValue"
}
1
2
3
4
5
6
7
2
3
4
5
6
7
type Config struct {
Name string
Host string
Port int
Prop string `json:"myProp,optional"`
NoConfStr string `json:"noConfStr,default=默认值"`
}
1
2
3
4
5
6
7
8
2
3
4
5
6
7
8
var configFile = flag.String("f", "etc/hello01-api.json", "the config file")
func main() {
flag.Parse()
var c config.Config
conf.MustLoad(*configFile, &c)
server := rest.MustNewServer(rest.RestConf{
Host: c.Host,
Port: c.Port,
})
defer server.Stop()
ctx := svc.NewServiceContext(c)
handler.RegisterHandlers(server, ctx)
fmt.Printf("Starting server at %s:%d...\n", c.Host, c.Port)
server.Start()
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
1.6 读取toml配置
翻看源码:
func LoadFromTomlBytes(content []byte, v any) error {
b, err := encoding.TomlToJson(content)
if err != nil {
return err
}
return LoadFromJsonBytes(b, v)
}
1
2
3
4
5
6
7
8
2
3
4
5
6
7
8
处理方式和json,yml都一样
name="hello01-api"
host="0.0.0.0"
port=8888
[database]
url="postgres://postgres:postgres@localhost:5432/postgres"
1
2
3
4
5
2
3
4
5
type Config struct {
Name string
Host string
Port int
Prop string `json:"myProp,optional"`
NoConfStr string `json:"noConfStr,default=默认值"`
Database Database
}
type Database struct {
Url string
}
1
2
3
4
5
6
7
8
9
10
11
2
3
4
5
6
7
8
9
10
11
package main
var configFile = flag.String("f", "etc/hello01-api.toml", "the config file")
func main() {
flag.Parse()
var c config.Config
conf.MustLoad(*configFile, &c)
server := rest.MustNewServer(rest.RestConf{
Host: c.Host,
Port: c.Port,
})
defer server.Stop()
ctx := svc.NewServiceContext(c)
handler.RegisterHandlers(server, ctx)
fmt.Printf("Starting server at %s:%d...\n", c.Host, c.Port)
server.Start()
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
2. 自定义配置
go-zero提供了默认的一些配置,方面我们使用,如果我们想要自己定义配置,只要在Config结构体中添加对应的字段即可
name: hello01-api
host: 0.0.0.0
port: 8888
customConfig:
age: 18
address: "中国"
1
2
3
4
5
6
7
2
3
4
5
6
7
type Config struct {
Name string
Host string
Port int
CustomConfig CustomConfig
}
type CustomConfig struct {
Age int
Address string
}
1
2
3
4
5
6
7
8
9
10
11
2
3
4
5
6
7
8
9
10
11
3. 指定配置文件
在部署时,配置文件并不会打包到二进制包中
,我们需要指定配置文件,通过命令行参数的形式指定
# 执行文件 -f etc/hello01-api.json
# go run main.go -f etc/hello01-api.json
1
2
2