Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/gogap/redconf
sync config from redis or others storage while the config values changed
https://github.com/gogap/redconf
config configuration-management dynamic-configuration
Last synced: 3 months ago
JSON representation
sync config from redis or others storage while the config values changed
- Host: GitHub
- URL: https://github.com/gogap/redconf
- Owner: gogap
- License: apache-2.0
- Created: 2016-03-30T07:21:57.000Z (over 8 years ago)
- Default Branch: master
- Last Pushed: 2016-08-02T06:50:45.000Z (over 8 years ago)
- Last Synced: 2024-07-16T14:03:09.076Z (4 months ago)
- Topics: config, configuration-management, dynamic-configuration
- Language: Go
- Homepage:
- Size: 19.5 KB
- Stars: 12
- Watchers: 2
- Forks: 2
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
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
```go
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
```go
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
```go
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
```bash
$> redis-cli
127.0.0.1:6379> SET GOGAP:AppConfig:Server:AllowIPs 127.0.0.1,202.10.5.123
OK```
- Run example code
``` bash
$> go run example/*.go
```- Open new terminal session and change the config in redis
```bash
$> 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:
```go
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)
```