configuration

package module
v0.0.0-...-6b77015 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Mar 7, 2020 License: MIT Imports: 7 Imported by: 0

README

configuration

这是一个通用的加载配置模块。

  • 支持Json格式的配置文件。
  • 支持主配置文件的加载。
  • 根据主配置文件中的组件设置,动态加载各个组件的配置。
设计思想

建议使用一个单独的目录编写配置模块的相关代码,例如接下来代码均位于src/lib/conf目录中。

首先,我们需要在模块的init()函数中,注册所有的组件的加载配置,包括主应用configuration.Application,以及各个自定义的组件。

然后,在调用configuration.Init方法时,传入options,里面包括配置文件所在目录。接下来按如下步骤初始化配置:

  • 初始化主应用配置。
  • 从主应用配置中,获取要加载的组件名称。
  • 对于每一个要加载的组件,生成完整的文件路径,然后加载。
主应用

首先我们要定义主应用的数据格式,例如:

package conf

// 主应用的配置信息格式
type ApplicationConfig struct {
    DeployEnv    string   `json:"DeployEnv"`    // 部署环境
    DeployTarget string   `json:"DeployTarget"` // 部署目标
    // 其他字段...
    Components   []string `json:"Components"`   // 包含的组件配置列表
}

func (c ApplicationConfig) GetComponents() []string {
    return c.Components
}

这里需要实现configuration.ApplicationConfig接口中的方法,即获取要加载的组件列表。

自定义组件

直接定义一个可以Json序列化的结构体,例如:

package conf

// MySQL的配置信息格式
type MySqlConfig struct {
    Host     string `json:"Host"`     // 服务器主机地址
    Port     int    `json:"Port"`     // 服务器端口号
    User     string `json:"User"`     // 服务器登陆用户
    Password string `json:"Password"` // 数据库密码
    // 其他参数...
}
初始化模块

在模块中,定义初始化接口,用于注册组件的目标对象:

package conf

var (
    Application ApplicationConfig // 主应用配置
    MySql       MySqlConfig       // MySQL配置
)

func init() {
    var err error
    if err = configuration.RegisterComponent(configuration.Application, &Application); err != nil {
        log.Fatal(err)
    }
    if err = configuration.RegisterComponent("mysql", &MySql); err != nil {
        log.Fatal(err)
    }
    // 其他组件的注册语句...
}
加载配置

最后,编写整个配置模块的加载方法:

package conf

// 加载配置
func Init() {
    options := configuration.Options{
        ConfigPath:     os.Getenv("CONFPATH"),
        MainFileName:   "application",
        PrefixFileName: "app",
    }
    err := configuration.Init(options)
    if err != nil {
        log.Fatal(err)
    }
    fmt.Printf("主配置 %+v\n", Application)
    fmt.Printf("MySQL配置 %+v\n", MySql)
}
配置文件

在项目中创建resource/conf目录,里面只有.json的文件,中括号中的变量都是configuration.Options的参数:

  • 主应用配置文件:[MainFileName].json,默认值为application.json
  • 组件配置文件:如果[PrefixFileName]为空,则文件名为[组件名].json,否则为[PrefixFileName]-[组件名].json

每一个Json文件中的字段,与相应组件的Go结构体对应上即可。

我们样例中使用环境变量作为配置文件路径的传参方式,那么运行时可以设置路径:

CONFPATH=`pwd`/resource/conf go run main.go

Documentation

Index

Constants

View Source
const Application = "application"

整个应用配置的组件名称

Variables

This section is empty.

Functions

func GetComponent

func GetComponent(name string) (target interface{}, err error)

获取组件存储目标

func Init

func Init(options Options) (err error)

配置初始化函数

func LoadJsonFile

func LoadJsonFile(filepath string, v interface{}) (err error)

加载Json文件

func RegisterComponent

func RegisterComponent(name string, target interface{}) (err error)

注册组件存储目标,target必须是struct的指针

Types

type ApplicationConfig

type ApplicationConfig interface {
	GetComponents() []string
}

应用组件必须实现的接口,即根据应用配置获取组件名称列表

type Options

type Options struct {
	// (必填)配置文件的所在目录
	ConfigPath string
	// 整个应用组件的文件名称,使用GetMainFileName获取完整的整个应用组件文件名
	MainFileName string
	// 组件的文件名称的前缀,使用GetComponentFileName获取完整的组件文件名
	PrefixFileName string
}

全局配置

func (Options) GetComponentFileName

func (opt Options) GetComponentFileName(name string) string

func (Options) GetMainFileName

func (opt Options) GetMainFileName() string

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL