go-zero 基础 -- 配置
创始人
2025-06-01 20:57:08
0

1、api配置

api配置控制着api服务中的各种功能,包含但不限于服务监听地址,端口,环境配置,日志配置等,下面我们从一个简单的配置来看一下api中常用配置分别有什么作用。

1.1 配置说明

通过yaml配置我们会发现,有很多参数我们并没有与config对齐,这是因为config定义中,有很多都是带optional或者default 标签的,对于optional可选项,你可以根据自己需求判断是否需要设置,对于default标签,如果你觉得默认值就已经够了,可以不用设置, 一般default中的值基本不用修改,可以认为是最佳实践值。

Config

type Config struct{rest.RestConf // rest api配置Auth struct { // jwt鉴权配置AccessSecret string // jwt密钥AccessExpire int64 // 有效期,单位:秒}Mysql struct { // 数据库配置,除mysql外,可能还有mongo等其他数据库DataSource string // mysql链接地址,满足 $user:$password@tcp($ip:$port)/$db?$queries 格式即可}CacheRedis cache.CacheConf // redis缓存UserRpc    zrpc.RpcClientConf // rpc client配置
}    

1.2 rest.RestConf

api服务基础配置,包含监听地址监听端口证书配置限流熔断参数超时参数等控制,对其展开我们可以看到:

	// A RestConf is a http service config.// Why not name it as Conf, because we need to consider usage like://  type Config struct {//     zrpc.RpcConf//     rest.RestConf//  }// if with the name Conf, there will be two Conf inside Config.RestConf struct {service.ServiceConfHost     string `json:",default=0.0.0.0"`Port     intCertFile string `json:",optional"`KeyFile  string `json:",optional"`Verbose  bool   `json:",optional"`MaxConns int    `json:",default=10000"`MaxBytes int64  `json:",default=1048576"`// millisecondsTimeout      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.1 service.ServiceConf

// A ServiceConf is a service config.
type ServiceConf struct {Name       stringLog        logx.LogConfMode       string `json:",default=pro,options=dev|test|rt|pre|pro"`MetricsUrl string `json:",optional"`// Deprecated: please use DevServerPrometheus prometheus.Config `json:",optional"`Telemetry  trace.Config      `json:",optional"`DevServer  devserver.Config  `json:",optional"`
}

logx.LogConf

// A LogConf is a logging config.
type LogConf struct {ServiceName string `json:",optional"` // 服务名称// Mode represents the logging mode, default is `console`.// console: log to console.// file: log to file.// volume: used in k8s, prepend the hostname to the log file name.Mode string `json:",default=console,options=[console,file,volume]"`// Encoding represents the encoding type, default is `json`.// json: json encoding.// plain: plain text encoding, typically used in development.Encoding string `json:",default=json,options=[json,plain]"`// TimeFormat represents the time format, default is `2006-01-02T15:04:05.000Z07:00`.TimeFormat string `json:",optional"`// Path represents the log file path, default is `logs`.Path string `json:",default=logs"`// Level represents the log level, default is `info`.Level string `json:",default=info,options=[debug,info,error,severe]"`// MaxContentLength represents the max content bytes, default is no limit.MaxContentLength uint32 `json:",optional"`// Compress represents whether to compress the log file, default is `false`.// 是否开启gzip压缩Compress bool `json:",optional"`// Stdout represents whether to log statistics, default is `true`.Stat bool `json:",default=true"`// KeepDays represents how many days the log files will be kept. Default to keep all files.// Only take effect when Mode is `file` or `volume`, both work when Rotation is `daily` or `size`.KeepDays int `json:",optional"`// StackCooldownMillis represents the cooldown time for stack logging, default is 100ms.// 日志write间隔StackCooldownMillis int `json:",default=100"`// MaxBackups represents how many backup log files will be kept. 0 means all files will be kept forever.// Only take effect when RotationRuleType is `size`.// Even thougth `MaxBackups` sets 0, log files will still be removed// if the `KeepDays` limitation is reached.MaxBackups int `json:",default=0"`// MaxSize represents how much space the writing log file takes up. 0 means no limit. The unit is `MB`.// Only take effect when RotationRuleType is `size`MaxSize int `json:",default=0"`// RotationRuleType represents the type of log rotation rule. Default is `daily`.// daily: daily rotation.// size: size limited rotation.Rotation string `json:",default=daily,options=[daily,size]"`
}

prometheus.Config

type Config struct {Host string `json:",optional"` // prometheus 监听hostPort int    `json:",default=9101"` // prometheus 监听端口Path string `json:",default=/metrics"` // 上报地址
}

1.2.2 SignatureConf

type SignatureConf struct {Strict      bool          `json:",default=false"` // 是否Strict模式,如果是则PrivateKeys必填Expiry      time.Duration `json:",default=1h"` // 有效期,默认1小时PrivateKeys []PrivateKeyConf // 签名密钥相关配置
}

PrivateKeyConf

type PrivateKeyConf struct {Fingerprint string // 指纹配置KeyFile     string // 密钥配置
}

1.3 cache.CacheConf

type CacheConf struct{ClusterConf []NodeConfNodeConf struct {redis.RedisConfWeight int `json:",default=100"` // 权重}
}

redis.RedisConf

type RedisConf struct {Host string // redis地址Type string `json:",default=node,options=node|cluster"` // redis类型Pass string `json:",optional"` // redis密码
}

2、rpc配置

rpc配置控制着一个rpc服务的各种功能,包含但不限于监听地址etcd配置超时熔断配置等,下面我们以一个常见的rpc服务配置来进行说明。

2.1 配置说明

type Config struct {zrpc.RpcServerConfCacheRedis         cache.CacheConf // redis缓存配置,详情见api配置说明,这里不赘述Mysql struct { // mysql数据库访问配置,详情见api配置说明,这里不赘述DataSource string}
}

2.1 zrpc.RpcServerConf

type RpcServerConf struct {service.ServiceConf // 服务配置,详情见api配置说明,这里不赘述ListenOn      string // rpc监听地址和端口,如:127.0.0.1:8888Etcd          discov.EtcdConf    `json:",optional"` // etcd相关配置Auth          bool               `json:",optional"` // 是否开启Auth,如果是则Redis为必填Redis         redis.RedisKeyConf `json:",optional"` // Auth验证StrictControl bool               `json:",optional"` // 是否Strict模式,如果是则遇到错误是Auth失败,否则可以认为成功// pending forever is not allowed// never set it to 0, if zero, the underlying will set to 2s automatically// 不允许永久挂起// 超时控制,单位:毫秒Timeout      int64 `json:",default=2000"` // cpu降载阈值,默认900,可允许设置范围0到1000CpuThreshold int64 `json:",default=900,range=[0:1000]"` 
}

discov.EtcdConf

type EtcdConf struct {Hosts []string // etcd host数组Key   string // rpc注册key
}

redis.RedisKeyConf

type RedisConf struct {Host string // redis 主机Type string `json:",default=node,options=node|cluster"` // redis类型Pass string `json:",optional"` // redis密码
}type RedisKeyConf struct {RedisConfKey string `json:",optional"` // 验证key
}

相关内容

热门资讯

走进乌兰布统 感受雄浑壮阔的塞... 本文转自:人民网-内蒙古频道6月2日,航拍克什克腾旗乌兰布统草原美景。李富摄在内蒙古自治区赤峰市克什...
重庆五天四晚旅游攻略,两个人去... 重庆五天四晚旅游攻略,两个人去重庆玩五天要消费多少? 最近,我和朋友一直计划着一场说走就走的旅行,而...
原创 黄... 斑马消费 杨伟 2025年A股酒水板块“冰火两重天”,白酒承压,啤酒失速,黄酒却异军突起! Wind...
原创 4... 在城市的喧嚣中,我们常常忽略了那些默默付出的劳动者。今天,我要讲述的是一位来自农村的大姐,她的故事让...
原创 鱼... 标题:鱼肉的最新吃法,不蒸不煮不烧,鲜香爽滑,可以吃下两大碗米饭! 在探索美食的旅途中,我们总在寻...
小小面团“潮”出圈 当古老技艺遇上年轻力量,会碰撞出怎样的火花?他是郎佳子彧,国家级非物质文化遗产代表性项目面人(北京面...
夏天没食欲,学做这几道家常菜|... hi,我是草莓 01 杏鲍菇炒牛肉 【材料】牛肉、杏鲍菇、泡椒、泡姜、蒜【做法】1、牛肉用生抽、蚝油...
原创 白... 作者|睿研消费 编辑|EMMA 来源|蓝筹企业评论 以旧换新的国补是一点没有,消费不足的挑战是只多不...
这几种“奇葩果” 买了就后悔,... 来源:央视三农“拇指西瓜”,长得像西瓜的微缩板,但压根不是西瓜。吃起来也远没有西瓜的甘甜多汁。刺角瓜...
原创 新... 古茗,要开始走蜜雪的路线了吗? 近日,古茗售价1元的冰水和售价2.5元的柠檬水。这是古茗在售的商...
原创 西... 标题:西红柿焖牛腩:酸爽滋味,牛肉的醇香与汤汁的余韵 在美食的世界里,有一种味道能够瞬间唤醒味蕾的...
粤菜辉煌20年,未来只能走融合... 过去二十年,粤菜从八大菜系领头羊,高端餐饮的代表菜系,到近年来被川、湘菜快速追赶,再到如今开始走融合...
原创 鲜... 标题:鲜香滑嫩的虾仁炒蛋,味道好,营养足,学生族最爱! 在繁忙的学习生活中,我们总是在寻找那些既简...
原创 1... 标题:1个芒果,1碗糯米粉,蒸一蒸做出的美食,香甜软糯,上桌就被抢光 在这个快节奏的时代,我们总是...
皖北麦浪起笔“时代答卷”:古井... 5月27日至28日,2025四季村歌特色展演暨古井贡酒•年份原浆第五届原粮丰收季在古井酒神广场盛大开...
黄山四天旅游路线推荐,黄山旅游... 黄山,这座镶嵌在安徽省南部的自然瑰宝,自古以来便以其奇松、怪石、云海、温泉四绝吸引着无数文人墨客前来...
去潮汕旅行5天4晚自由行多少钱... 去潮汕旅行5天4晚自由行多少钱,亲子出游看这篇就足够了! 潮汕,一个充满魅力的地区,以其悠久的历史、...
四川旅游攻略自由行攻略参团6日... 四川:一场视觉与味觉的盛宴 四川旅游推荐!当地导游-乐乐:185 8335 5758(加他微信-立减...
2025旅游攻略,黄山纯玩5天... 黄山,这座被誉为“天下第一奇山”的地方,以其奇松、怪石、云海、温泉、冬雪“五绝”闻名遐迩。我一直梦想...