GO-ZERO
01基础001快速入门
01基础002配置文件
01基础003web开发一
01基础004web开发二
本文档使用 MrDoc 发布
-
+
首页
01基础002配置文件
## 1. 格式 > 在生成的代码中,配置文件位于etc目录下,格式为yml,yml是最为常用的配置文件格式,go-zero支持多种配置文件格式: - yml或者yaml - toml - json ### 1.1 读取YML配置 配置文件: ``` Name: hello01-api Host: 0.0.0.0 Port: 8888 ``` 配置文件对应的结构定义: ```go type Config struct { Name string Host string Port int } ``` 读取配置文件: ```go //定义配置文件路径 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 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.3 小写 使用SpringBoot的java开发人员比较熟悉,SpringBoot使用yml配置文件,都是小写。 **在go-zero中如何实现小写呢?** 很简单,只需要将yml配置文件改为小写即可,go-zero在conf.MustLoad中已经支持。 如果想要将myProp这个配置读入Config中的prop字段中呢? `myProp: myValue` 我们可以加入如下的tag: ``` type Config struct { Name string Host string Port int Prop string `json:"myProp"` } ``` > 这时候,大家可能会奇怪,为什么加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.4 默认值 如果想给默认值,处理方式和json一样 ``` type Config struct { Name string Host string Port int Prop string `json:"myProp"` NoConfStr string `json:"noConfStr,default=默认值"` } ``` 由于json解析的特性,如果配置文件没有对应字段的配置就会报错,所以我们需要设置默认值或者设置为可选 type Config struct { Name string Host string Port int //配置文件可以没有myProp这个配置,不会报错,Prop的值为零值 Prop string `json:"myProp,optional"` NoConfStr string `json:"noConfStr,default=默认值"` } ### 1.5 读取JSON配置 > 读取方式和yml方式一样 hello01-api.json ```json { "name": "hello01-api", "host": "0.0.0.0", "port": 8888, "myProp": "myValue" } ``` ```go type Config struct { Name string Host string Port int Prop string `json:"myProp,optional"` NoConfStr string `json:"noConfStr,default=默认值"` } ``` ```go 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.6 读取toml配置 翻看源码: ```go func LoadFromTomlBytes(content []byte, v any) error { b, err := encoding.TomlToJson(content) if err != nil { return err } return LoadFromJsonBytes(b, v) } ``` > 处理方式和json,yml都一样 ```yaml name="hello01-api" host="0.0.0.0" port=8888 [database] url="postgres://postgres:postgres@localhost:5432/postgres" ``` ```go 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 } 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() } ``` ## 2. 自定义配置 > go-zero提供了默认的一些配置,方面我们使用,如果我们想要自己定义配置,只要在Config结构体中添加对应的字段即可 ```yaml name: hello01-api host: 0.0.0.0 port: 8888 customConfig: age: 18 address: "中国" ``` ```go type Config struct { Name string Host string Port int CustomConfig CustomConfig } type CustomConfig struct { Age int Address string } ``` ## 3. 指定配置文件 在部署时,配置文件并不会打包到二进制包中,我们需要指定配置文件,通过命令行参数的形式指定 ```shell # 执行文件 -f etc/hello01-api.json # go run main.go -f etc/hello01-api.json ```
admin
2025年1月9日 09:45
转发文档
收藏文档
上一篇
下一篇
手机扫码
复制链接
手机扫一扫转发分享
复制链接
Markdown文件
分享
链接
类型
密码
更新密码