Skip to content

Watching and detecting changes in consul on the application level

License

Notifications You must be signed in to change notification settings

bubunyo/config-watcher

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

20 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

config-watcher

config-watcher is a simple project to watch external data stores and detect changes to keys and signal change in the values.

Supported Datastores

  • Consul
  • Redis [Todo]
  • Aerospike [Todo]
  • Postgres [Todo]

Usage

// Import relevant packages
import (
	"github.com/bubunyo/config-watcher/common"
	"github.com/bubunyo/config-watcher/consul"
)


// Setup Datastore configurations
config := &consul.Config{
    Address: "localhost:8500", // Consul address
    Config: common.Config{
        PollInterval: 1 * time.Second, // Polling Interval
        CloseTimeout: 5 * time.Second, // Close Timeout
    },
}

// Create consul watcher
watcher, err := consul.NewWatcher(config)
if err != nil ...

// Watch for changes by calling Wact(context, key) which returns a chanl of type []byte.
// Values are sent down the chanl only when changes are detected
// You can also watch for multiple keys on the same watcher
go func() {
    for byteSlice := range watcher.Watch(context.Background(), "a/b/c/d") {
        // use byteSlice	
    }
}()
go func() {
    for byteSlice := range watcher.Watch(context.Background(), "test_key") {
        // use byteSlice	
    }
}()

// or watch for the same key at multiple places, by calling watch with the same 
// key multiple times

// Finally, close the watcher when shutting down
err := watcher.Close()
if err != nil ...

Testing Consul Integration

Docker is a prerequisite to test consul integrations

  1. From the project folder, setup docker containers
docker-compose -f ./docker/docker-compose.yml up --detach
  1. Once your containers have started, you can import some sample values
curl --request PUT --data-binary "@./docker/sample1.json" http://127.0.0.1:8500/v1/kv/backend
curl --request PUT --data-binary "@./docker/sample2.json" http://127.0.0.1:8500/v1/kv/a/b/c/d
  1. You can verify if your keys have been imported into consul by visiting http://localhost:8500/ui/dc1/kv
  2. Run the test TestConsulWatcher, without t.Skip() to verify

Todo

  • Export Metrics

About

Watching and detecting changes in consul on the application level

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages