小小程知识库 小小程知识库
首页
Golang
MySQL
归档
GitHub (opens new window)

xxcheng

记录美好生活~
首页
Golang
MySQL
归档
GitHub (opens new window)
  • 语言基础

  • GORM 框架

  • go-zero 微服务

    • 配置
      • 一、读取配置
        • 1.1 概述
        • 1.1.1 配置来源
        • 1.1.2 示例模板
        • 1.2 配置文件
        • 1.2.1 支持的文件后缀
        • 1.3 默认值
        • 1.4 环境变量
        • 方式一:使用 UseEnv
        • 方式二:使用 env Tag
        • 1.5 配置继承
  • 专题学习

  • 未整理的学习笔记

  • Golang
  • go-zero 微服务
xxcheng
2023-12-12
目录

配置

# 一、读取配置

# 1.1 概述

使用 MustLoad 或者 Load 读取配置文件,MustLoad 读取不成功直接 Fatal。

配置使用的结构体使用的 tag 均使用 json

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

# 1.1.1 配置来源

  • 配置文件;
  • 默认值 tag json:",default=defaultValue;
  • 环境变量,配置文件内 ${pathVar} 格式读取;
  • 环境变量,tag 形式读取,json:",env=pathVar";
  • 配置继承,tag 方式 inherit 关键字。

# 1.1.2 示例模板

package main

import (
	"flag"
	"fmt"

	"api/internal/config"
	"api/internal/handler"
	"api/internal/svc"

	"github.com/zeromicro/go-zero/core/conf"
	"github.com/zeromicro/go-zero/rest"
)

var configFile = flag.String("f", "etc/greet.yaml", "the config file")

func main() {
	flag.Parse()

	var c config.Config
	conf.MustLoad(*configFile, &c, conf.UseEnv())
	server := rest.MustNewServer(c.RestConf)
	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
25
26
27
28
29
30

# 1.2 配置文件

# 1.2.1 支持的文件后缀

  • yaml
  • toml
  • json
type Config struct {
	rest.RestConf
	Title string `json:"title"`
	Name  string `json:"customName"`
}
1
2
3
4
5
Name: ping
Host: 127.0.0.1
Port: 8888
Log:
  Encoding: plain
Title: Hello Go-Zero
CustomName:  CustomNameABC
1
2
3
4
5
6
7
  conf.MustLoad(*configFile, &c, conf.UseEnv())
+ fmt.Println("Title", c.Title)
+ fmt.Println("Name", c.Name)
1
2
3
Title Hello Go-Zero
Name CustomNameABC
1
2

# 1.3 默认值

json tag设置 default 来设置默认值

type Config struct {
	rest.RestConf
	DefaultTitle string `json:"defaultTitle,default=default_title"`
}
1
2
3
4
  conf.MustLoad(*configFile, &c, conf.UseEnv())
+ fmt.Println("DefaultTitle", c.DefaultTitle)
1
2
DefaultTitle default_title
1

# 1.4 环境变量

环境变量有两种应用方式,一种是使用 conf.UseEnv(),将 opt.env 设置为 true,然后将环境变量通过 ${pathVal} 的形式写到配置文件, conf 会自动替换。另外一种是通过 在 tag 配置env 指定环境变量名称来配置。

# 方式一:使用 UseEnv

配置环境变量

windows 下在 cmd 下载配置,powershell 这样子配置没有用

set title=this is path title
1
Name: ping
Host: 127.0.0.1
Port: 8888
Log:
  Encoding: plain
PathTitle: PathTitle_${title}
PathEmptyTitle: PathEmptyTitle_${title_empty}
1
2
3
4
5
6
7
type Config struct {
	rest.RestConf
	PathTitle      string `json:"pathTitle"`
	PathEmptyTitle string `json:"pathEmptyTitle"`
}
1
2
3
4
5
  conf.MustLoad(*configFile, &c, conf.UseEnv())
+ fmt.Println("PathTitle", c.PathTitle)
+ fmt.Println("PathEmptyTitle", c.PathEmptyTitle)
1
2
3
PathTitle PathTitle_this is path title
PathEmptyTitle PathEmptyTitle_
1
2

# 方式二:使用 env Tag

保持上面测试的窗口不变,维持环境变量,继续测试

type Config struct {
	rest.RestConf
	PathEnvTitle   string `json:"pathEnvTitle,env=title"`
}
1
2
3
4
  conf.MustLoad(*configFile, &c, conf.UseEnv())
+ fmt.Println("PathEnvTitle", c.PathEnvTitle)
1
2
PathEnvTitle this is path title
1

# 1.5 配置继承

一个配置下可能会多个子配置,这些子配置可能会依赖一个相同配置,比如数据库,重复配置麻烦而且不利于维护,可以添加一个tag inherit 开启配置继承。然后它就会向上查找相同的配置名称继承过来。

type MySQLConfig struct {
	Username string `json:"Username"`
	Password string `json:"Password"`
}
type UserServer struct {
	Name        string      `json:"Name"`
	MySQLConfig MySQLConfig `json:"MySQLConfig,inherit"`
}
type PayServer struct {
	Name        string      `json:"Name"`
	MySQLConfig MySQLConfig `json:"MySQLConfig,inherit"`
}
type Config struct {
	rest.RestConf
	MySQLConfig    MySQLConfig `json:"MySQLConfig"`
	UserServer     UserServer  `json:"UserServer"`
	PayServer      PayServer   `json:"PayServer"`
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
Name: ping
Host: 127.0.0.1
Port: 8888
Log:
  Encoding: plain
MySQLConfig:
  Username: uuuu
  Password: pppp
UserServer:
  Name: UserServer
PayServer:
  Name: PayServer
1
2
3
4
5
6
7
8
9
10
11
12
  conf.MustLoad(*configFile, &c, conf.UseEnv())
+ fmt.Println("MySQLConfig", c.MySQLConfig)
+ fmt.Println("UserServer", c.UserServer.MySQLConfig)
+ fmt.Println("PayServer", c.PayServer.MySQLConfig)
1
2
3
4
MySQLConfig {uuuu pppp}
UserServer {uuuu pppp}
PayServer {uuuu pppp}
1
2
3
#go-zero#微服务#配置
上次更新: 2023/12/12, 11:52:58
CRUD 接口
rpc学习:进阶到gRPC

← CRUD 接口 rpc学习:进阶到gRPC→

最近更新
01
Go:GMP模型深入理解
01-10
02
rpc学习:进阶到gRPC
01-04
03
子查询
08-05
更多文章>
Theme by Vdoing | Copyright © 2019-2024 xxcheng | 浙ICP备19024050号
  • 跟随系统
  • 浅色模式
  • 深色模式
  • 阅读模式