Created
May 31, 2014 14:05
-
-
Save fujiwara/4cdff1d718ecaa2b8294 to your computer and use it in GitHub Desktop.
consul-catalogを使って監視しているserviceのNode自体に変更があったことを検知する
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package main | |
import ( | |
"encoding/json" | |
"github.com/mizzy/consul-catalog" | |
"log" | |
"os" | |
"reflect" | |
"time" | |
) | |
func main() { | |
client, err := consulcatalog.NewClient(consulcatalog.DefaultConfig()) | |
if err != nil { | |
log.Fatalln(err) | |
} | |
var index uint64 | |
var nodes consulcatalog.Nodes | |
service := os.Args[1] | |
for { | |
log.Printf("Get blocking query service %s index %d ...", service, index) | |
meta, newNodes, err := client.Get("service", service, index) | |
if err != nil { | |
log.Printf("error: %s", err) | |
time.Sleep(5 * time.Second) | |
continue | |
} | |
log.Printf("new index: %v", meta.ModifyIndex) | |
index = meta.ModifyIndex | |
if reflect.DeepEqual(nodes, newNodes) { | |
log.Println("no Nodes changed.") | |
} else { | |
jsonBytes, _ := json.Marshal(newNodes) | |
log.Printf("Nodes changed. %s", jsonBytes) | |
nodes = newNodes | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment