Categorygithub.com/enpsl/conf-reload
modulepackage
1.0.0
Repository: https://github.com/enpsl/conf-reload.git
Documentation: pkg.go.dev

# README

conf-reload

Conf-Reload是一个Go库,用于常驻进程模式下配置文件信息获取,并支持配置热加载

工作流程概述:

  • 读取配置文件信息并基于Broker.unmarshaller进行解析

  • Broker会单独启动一个Goroutinefsnotify进行通信

  • fsnotify有文件创建或写事件进来时,Broker通知Engine重载配置文件

Features

  • 支持toml yaml json配置文件获取
  • 支持key多层级获取
  • 支持热更新,配置文件更新后,配置会重载
  • 基于mapstructure库实现,配置信息获取支持弱类型转化
  • 基于fsnotify库实现了IO多路复用的事件通知机制,性能较好
  • 配置结构获取提供多种类型转化api

Stability and Compatibility

Status: 目前处于功能拓展中,不过基础功能已经完善,不会在改变api结构

☝️ Important Note: v1.0.0之前为beta版本. v1.0.0版本为当前stable版本.

Quick Start

go get -u github.com/enpsl/conf-reload

_example

_example目录是测试用例,可copy到当前项目下测试运行

配置引擎加载,加载一次即可

f = "_example/example.toml"
conf_reload.LoadEngine(f, conf_relod.WithLevelSplit("."), conf_relod.WithLogLevel(0))

LoadEngine的一些option选项说明:

  • WithLevelSplit(string)配置信息分隔符设置,默认是.

  • WithWeaklyTypedInput(bool) 调用DecodeToStruct时,会启用弱类型转化

  • WithLogger(Logger) 外部日志接入,需实现Logger,不传入会默认使用项目自带终端输出方式记录日志

  • WithCapacity(int) LRU缓存容量设置,低于当前设置级别的日志记录不会在终端输出,可按照下面展示的级别进行设置

  • WithWatched(int) 是否开启Broker Watch检测,某些场景如命令行模式,不需要热加载,可关闭此选项即可停止文件监听

  • WithLogLevel(int)日志级别设置,低于当前设置级别的日志记录不会在终端输出,可按照下面展示的级别进行设置

配置信息读取,可更改文件内容观察文件变化情况

var http = &Http{}
for {
    err := conf_reload.DecodeToStruct("server.http", http)
    if err != nil {
        panic(err)
    }
    fmt.Println(http)
    time.Sleep(2 * time.Second)
}

输出示例:

conf-reload-test (main) ✗ go run _example/example.go
[email protected]: pid=16749 2023/02/19 12:37:42.571884 DEBUG: map[server:map[config:map[connection:false depends:[tcp ip] publish:2023-02-19 timeout:10s] http:map[host:0.0.0.0 port:8080]]]
[email protected]: pid=16749 2023/02/19 12:37:42.571917 DEBUG: map[config:map[connection:false depends:[tcp ip] publish:2023-02-19 timeout:10s] http:map[host:0.0.0.0 port:8080]]
&{0.0.0.0 8080}
&{0.0.0.0 8080}
&{0.0.0.0 8080}
[email protected]: pid=16749 2023/02/19 12:37:46.693127 DEBUG: modified file:/Users/kuailexingqiu/go/src/conf-reload-test/_example/example.toml, /Users/kuailexingqiu/go/src/conf-reload-test/_example/example.toml
[email protected]: pid=16749 2023/02/19 12:37:46.693437 DEBUG: map[server:map[config:map[connection:false depends:[tcp ip] publish:2023-02-19 timeout:10s] http:map[host:0.0.0.0 port:8081]]]
[email protected]: pid=16749 2023/02/19 12:37:48.572885 DEBUG: map[config:map[connection:false depends:[tcp ip] publish:2023-02-19 timeout:10s] http:map[host:0.0.0.0 port:8081]]
&{0.0.0.0 8081}
&{0.0.0.0 8081}

如果想了解更多api,See godoc

License

Copyright (c) 2023-present enpsl. conf-reload is free and open-source software licensed under the MIT License.

# Functions

DecodeToStruct The external exposure api is used for decoding, which can decode the value of the key map to the out variable.
Get external exposure api to get any type value.
GetBool external exposure api to get bool type value.
GetDuration external exposure api to get time.Duration type value.
GetFloat64 external exposure api to get float64 type value.
GetInt external exposure api to get int type value.
GetInt64 external exposure api to get int64 type value.
GetSlice external exposure api to get []interface{} type value.
GetString external exposure api to get string type value.
GetStringMap external exposure api to get map[string]interface{} type value.
GetStringMapString external exposure api to get map[string]string type value.
GetStringMapStringSlice external exposure api to get map[string][]string type value.
GetStringSlice external exposure api to get []string type value.
GetTime external exposure api to get time.Time type value.
No description provided by the author
NewEngine Engine init.
WithCapacity LRU cap options.
WithLevelSplit config file separator options.
WithLogger Logger options, The logger must be implement Logger.
WithLogLevel Log level options.
WithWatched Broker watch swich options.
WithWeaklyTypedInput options Whether to start weak type conversion See details https://github.com/mitchellh/mapstructure/blob/main/mapstructure_examples_test.go.

# Structs

conf-reload Engine,Used to coordinate and manage broker.

# Interfaces

No description provided by the author

# Type aliases

No description provided by the author