// Start app session start for consul func Start() { // Get a new client, with KV endpoints client, _ := consul.NewClient(getConsulMasterIP()) kv = client.KV() GetKV("mongodb_cluster1") }
//continue to register a service preferably ran in a go routine. func registerService(name string, port int, ttl int) { reportInterval := make(chan bool, 1) go func() { for { time.Sleep(time.Duration(ttl) / 2 * time.Second) reportInterval <- true } }() client, err := consul.NewClient(consul.DefaultConfig()) if nil != err { log.Error("Failed to get consul client") } agent := client.Agent() serviceRegister(name, port, ttl, agent) for { select { case <-reportInterval: //report registration { servicePassing(name, agent) } } } }