sync config from redis or others storage while the config values changed
Switch branches/tags
Nothing to show
Clone or download
Fetching latest commit…
Cannot retrieve the latest commit at this time.
Permalink
Failed to load latest commit information.
cmd/json2redis
example
.gitignore
LICENSE
README.md
conv.go
field.go
monitor.go
options.go
options_test.go
redconf.go
redconf_test.go
redis_monitor.go
redis_storage.go
storage.go
watching_config.go

README.md

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

  • if you want subscribe the value change event, you could do as following:
func onValueChangedSubscriber(event redconf.OnValueChangedEvent) {
	var err error
	var data []byte
	if data, err = json.MarshalIndent(&event, "", "    "); err != nil {
		fmt.Println(err)
		return
	}
	fmt.Println(string(data))
}


redConf.Subscribe(onValueChangedSubscriber)