神刀安全网

Redconf

RedConf

Sync config from redis or others storages while the key’s value changed

Usage

  • The following struct is the config what we want sync with storage while the values changed
type ServerConfig struct {     Host     string     Port     int     AllowIPs []string }  type LogConfig struct {     Path    string     Maxsize int }  type AppConfig struct {     Server ServerConfig     Log    LogConfig }
  • We need create storage for tell redconf where the config values stored, and create monitor to notify the redconf while the values changed
opts = redconf.Options{         "address":  "localhost:6379",         "password": "",         "db":       0,         "idle":     10,         "channel":  "ONCHANGED",     }      if monitor, err = redconf.CreateMonitor("redis", opts); err != nil {         fmt.Println(err)         return     }      if storage, err = redconf.CreateStorage("redis", opts); err != nil {         fmt.Println(err)         return     }
  • Create RedConf instance and watch the config
if redConf, err = redconf.New(namespace, storage, monitor); err != nil {         return     }      appConf := AppConfig{}      if err = redConf.Watch(&appConf); err != nil {         fmt.Println(err)         return     }
  • Initial redis key-value
$> redis-cli 127.0.0.1:6379> SET GOGAP:AppConfig:Server:AllowIPs 127.0.0.1,202.10.5.123 OK
  • Run example code
$> go run example/*.go
  • Open new terminal session and change the config in redis
$> redis-cli 127.0.0.1:6379>SET GOGAP:AppConfig:Server:AllowIPs 127.0.0.1,202.10.5.125 OK 127.0.0.1:6379> PUBLISH ONCHANGED GOGAP:AppConfig:Server:AllowIPs (integer) 1

Then you will see the change from your terminal

转载本站任何文章请注明:转载至神刀安全网,谢谢神刀安全网 » Redconf

分享到:更多 ()

评论 抢沙发

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址
分享按钮