Subscribing to Consul Key-Value
I’m trying to use Consul’s Key-Value API to pass a JSON configuration to an app running in one of my containers. It works great, but the API does not provide any pub-sub functionality (just read/write/delete) for a key.
So far, I managed to artificially compensate by using the Watch feature to restart the container whenever the key changes, but that leaves me with the need to
- Externally configure Consul (either load a new configuration, or use the CLI, to specify a key to watch, and a shell script to run)
- Restart my app in mid-run – a good way to potentially lose data.
Is there any programatic way (other than polling every x seconds) to get notified of a key change in Consul, or should I just back away from this solution, and move to Redis?
One Solution collect form web for “Subscribing to Consul Key-Value”
After a quick look at consul’s sources, it seems that the “watch” is a simple periodic pull. You could implement something similar in your code to achieve this.
Periodic run of the watcher: https://github.com/hashicorp/consul/blob/master/watch/plan.go#L46
KeyWatch Handler: https://github.com/hashicorp/consul/blob/master/watch/funcs.go#L29