目录:

  • 思路
  • 实现
    • 编写配置文件
    • 编写结构体
    • 读取文件
    • 使用

Go系列:

  1. Go(一)基础入门
  2. Go(二)结构体
  3. Go(三)Go配置文件
  4. Go(四)Redis操作
SpringBootSpringBoot
GoSpringBoot

思路

json、xml、yml、toml

虽然说配置文件各种各样,但是总体处理步骤都大致相同

  1. 编写配置文件
  2. 编写与配置文件相对应的结构体
  3. 读取配置文件并且加载到相对应的结构体当中

实现

SpringBootymlyml

编写配置文件

yml
server:
  port: 8000
  runMode: debug # release debug
  logLevel: debug # debug info warn error

database:
  type: mysql
  host: localhost
  port: 32306
  username: root
  password: 123456
  dbname: test
  max_idle_conn: 10
  max_open_conn: 30
  conn_max_lifetime: 300

redis:
  host: localhost
  port: 32307
  password:
  db: 0
SpringBootmysql、redis

编写结构体

ymlyaml:"server"
var Server *server
var Database *database
var Redis *myRedis

type conf struct {
 Svc         server   `yaml:"server"`
 DB          database `yaml:"database"`
 RedisConfig myRedis  `yaml:"redis"`
}

type server struct {
 Port     int    `yaml:"port"`
 RunMode  string `yaml:"runMode"`
 LogLevel string `yaml:"logLevel"`
}

type database struct {
 Type            string `yaml:"type"`
 Host            string `yaml:"host"`
 Port            string `yaml:"port"`
 UserName        string `yaml:"username"`
 Password        string `yaml:"password"`
 DbName          string `yaml:"dbname"`
 MaxIdleConn     int    `yaml:"max_idle_conn"`
 MaxOpenConn     int    `yaml:"max_open_conn"`
 ConnMaxLifetime int    `yaml:"conn_max_lifetime"`
}

type myRedis struct {
 Host     string `yaml:"host"`
 Port     string `yaml:"port"`
 Password string `yaml:"password"`
 DB       int    `yaml:"db"`
}
String
func (c conf) String() string {
 return fmt.Sprintf("%vn%vn%v", c.Svc, c.DB, c.RedisConfig)
}

func (s server) String() string {
 return fmt.Sprintf("server : n"+
  "tport : %v n"+
  "tRunMode : %v", s.Port, s.RunMode)
}

func (m database) String() string {
 return fmt.Sprintf("database : n"+
  "ttype : %v n"+
  "thost : %v n"+
  "tport : %v n"+
  "tusername : %v n"+
  "tpassword : %v n"+
  "tdbname : %v n"+
  "tmax_idle_conn : %v n"+
  "tmax_open_conn : %v n"+
  "tconn_max_lifetime : %v",
  m.Type, m.Host, m.Port, m.UserName, m.Password, m.DbName, m.MaxOpenConn, m.MaxIdleConn, m.ConnMaxLifetime)
}
func (r myRedis) String() string {
 return fmt.Sprintf("redis : n"+
  "thost : %v n"+
  "tport : %v n"+
  "tPassword : %v n"+
  "tdb : %v",
  r.Host, r.Port, r.Password, r.DB)
}

读取文件

重头戏来咯!!!

前提:引入依赖

go get gopkg.in/yaml.v2

需要做的就是将配置文件中的信息读取出来,并且绑定到相应的结构体当中。

func InitConf(dataFile string) {
 // 解决相对路经下获取不了配置文件问题
 _, filename, _, _ := runtime.Caller(0)
 filePath := path.Join(path.Dir(filename), dataFile)
 _, err := os.Stat(filePath)
 if err != nil {
  log.Printf("config file path %s not exist", filePath)
 }
 yamlFile, err := ioutil.ReadFile(filePath)
 if err != nil {
  log.Printf("yamlFile.Get err   #%v ", err)
 }
 c := new(conf)
 err = yaml.Unmarshal(yamlFile, &c)
 if err != nil {
  log.Printf("Unmarshal: %v", err)
 }
 log.Printf("load conf successn %v", c)
 // 绑定到外部可以访问的变量中
 Server = &c.Svc
 Database = &c.DB
 Redis = &c.RedisConfig
}

如果使用相对路经读取不到文件的,可以加入一下代码

_, filename, _, _ := runtime.Caller(0)
 filePath := path.Join(path.Dir(filename), dataFile)

使用

init
func init() {
 // 加载配置文件
 config.InitConf("../../config/app-dev.yaml")
}
init

├─config
│   └─ app-dev.yaml
└─util
│   └─config
│       └─ ConfigUtil.go
└─main.go    

使用的时候直接调用结构体即可

ginServer.RunMode
func InitRouters() *gin.Engine {
 r := gin.Default()
 gin.SetMode(config.Server.RunMode)
 return r
}

References

[1][2][3][4]