OpenMix product:
https://openmix.org

Mix VarWatch

Monitor data changes in configuration structure variables and perform some tasks

Monitor the data changes of configuration structure variables and perform some tasks

The source address

Star doesn’t get lost immediately, and you’ll find it again next time you use it

  • http://github.com/mix-go/varwatch

Installation

go get github.com/mix-go/varwatch

Usage

SPF13 / VIPER JINZHU/CONFIGOR is used to dynamically update the configuration information

Any repository that uses the &config pointer to bind data will do

var Config struct {
    Logger struct {
        Level int `json:"level"`
    } `json:"logger" varwatch:"logger"`
    Database struct {
        User    string `json:"user"`
        Pwd     string `json:"pwd"`
        Db      string `json:"db"`
        MaxOpen int    `json:"max_open"`
        MaxIdle int    `json:"max_idle"`
    } `json:"database" varwatch:"database"`
}

err := viper.Unmarshal(&Config)

Take the example of dynamically changing the log Level: we need to execute some code to change the log Level when config.logger.level changes

  • First configure the Logger nodevarwatch:"logger"The tag information
  • The listening logic is then executed with the following code
w := varwatch.NewWatcher(&Config, 10 * time.Second) w.Watch("logger", Logrus.setLevel (logrus.level (uint32(lv)))}) logrus.setLevel (logrus.level (uint32(lv)))})

If you need to change the connection pool information dynamically, or the database account password, you can do this using the example above.

License

The Apache License Version 2.0, http://www.apache.org/licenses/